callback.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. #include <callback/callback.h>
  2. #include <callback/loader.h>
  3. #include <messages/flip_social_messages.h>
  4. #include <friends/flip_social_friends.h>
  5. #include <explore/flip_social_explore.h>
  6. #include <feed/flip_social_feed.h>
  7. #include <flip_storage/flip_social_storage.h>
  8. #include <free/free.h>
  9. #include <alloc/alloc.h>
  10. static bool flip_social_login_fetch(DataLoaderModel *model)
  11. {
  12. UNUSED(model);
  13. if (!app_instance)
  14. {
  15. FURI_LOG_E(TAG, "app_instance is NULL");
  16. return false;
  17. }
  18. if (!model->fhttp)
  19. {
  20. FURI_LOG_E(TAG, "fhttp is NULL");
  21. return false;
  22. }
  23. if (!app_instance->login_username_logged_out || !app_instance->login_password_logged_out || strlen(app_instance->login_username_logged_out) == 0 || strlen(app_instance->login_password_logged_out) == 0)
  24. {
  25. return false;
  26. }
  27. char buffer[256];
  28. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  29. auth_headers_alloc();
  30. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/login/", auth_headers, buffer);
  31. }
  32. static char *flip_social_login_parse(DataLoaderModel *model)
  33. {
  34. UNUSED(model);
  35. if (!app_instance)
  36. {
  37. FURI_LOG_E(TAG, "app_instance is NULL");
  38. return "Failed to login...";
  39. }
  40. if (!model->fhttp->last_response)
  41. {
  42. return "Failed to login...";
  43. }
  44. // read response
  45. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  46. {
  47. app_instance->is_logged_in = "true";
  48. // set the logged_in_username and change_password_logged_in
  49. if (app_instance->login_username_logged_out)
  50. {
  51. strcpy(app_instance->login_username_logged_in, app_instance->login_username_logged_out);
  52. }
  53. if (app_instance->login_password_logged_out)
  54. {
  55. strcpy(app_instance->change_password_logged_in, app_instance->login_password_logged_out);
  56. }
  57. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  58. // send user to the logged in submenu
  59. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  60. return "Login successful!";
  61. }
  62. else if (strstr(model->fhttp->last_response, "User not found") != NULL)
  63. {
  64. return "Account not found...";
  65. }
  66. else
  67. {
  68. return "Failed to login...";
  69. }
  70. }
  71. static void flip_social_login_switch_to_view(FlipSocialApp *app)
  72. {
  73. if (!loader_view_alloc(app))
  74. {
  75. FURI_LOG_E(TAG, "Failed to allocate view loader");
  76. return;
  77. }
  78. loader_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 1, flip_social_callback_to_login_logged_out, FlipSocialViewLoader);
  79. }
  80. static bool flip_social_register_fetch(DataLoaderModel *model)
  81. {
  82. if (!app_instance)
  83. {
  84. FURI_LOG_E(TAG, "app_instance is NULL");
  85. return "Failed to login...";
  86. }
  87. if (!model->fhttp->last_response)
  88. {
  89. return "Failed to login...";
  90. }
  91. // check if the username and password are valid
  92. if (!app_instance->register_username_logged_out || !app_instance->register_password_logged_out || strlen(app_instance->register_username_logged_out) == 0 || strlen(app_instance->register_password_logged_out) == 0)
  93. {
  94. FURI_LOG_E(TAG, "Username or password is NULL");
  95. return false;
  96. }
  97. // check if both passwords match
  98. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  99. {
  100. FURI_LOG_E(TAG, "Passwords do not match");
  101. return false;
  102. }
  103. char buffer[128];
  104. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  105. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/register/", "{\"Content-Type\":\"application/json\"}", buffer);
  106. }
  107. static char *flip_social_register_parse(DataLoaderModel *model)
  108. {
  109. if (!app_instance)
  110. {
  111. FURI_LOG_E(TAG, "app_instance is NULL");
  112. return "Failed to login...";
  113. }
  114. if (!model->fhttp->last_response)
  115. {
  116. return "Failed to login...";
  117. }
  118. // read response
  119. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  120. {
  121. // set the login credentials
  122. if (app_instance->login_username_logged_out)
  123. {
  124. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  125. }
  126. if (app_instance->login_password_logged_out)
  127. {
  128. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  129. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  130. }
  131. if (app_instance->login_username_logged_in)
  132. {
  133. app_instance->login_username_logged_in = app_instance->register_username_logged_out;
  134. }
  135. app_instance->is_logged_in = "true";
  136. // update header credentials
  137. auth_headers_alloc();
  138. // save the credentials
  139. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  140. // send user to the logged in submenu
  141. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  142. return "Registration successful!\nWelcome to FlipSocial!";
  143. }
  144. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  145. {
  146. return "Please enter your credentials.\nPress BACK to return.";
  147. }
  148. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  149. {
  150. return "Registration failed...\nUsername already exists.\nPress BACK to return.";
  151. }
  152. else
  153. {
  154. return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
  155. }
  156. }
  157. static void flip_social_register_switch_to_view(FlipSocialApp *app)
  158. {
  159. if (!loader_view_alloc(app))
  160. {
  161. FURI_LOG_E(TAG, "Failed to allocate view loader");
  162. return;
  163. }
  164. loader_switch_to_view(app, "Registering...", flip_social_register_fetch, flip_social_register_parse, 1, flip_social_callback_to_register_logged_out, FlipSocialViewLoader);
  165. }
  166. /**
  167. * @brief Navigation callback to go back to the submenu Logged out.
  168. * @param context The context - unused
  169. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  170. */
  171. uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  172. {
  173. UNUSED(context);
  174. return FlipSocialViewLoggedOutSubmenu;
  175. }
  176. /**
  177. * @brief Navigation callback to go back to the submenu Logged in.
  178. * @param context The context - unused
  179. * @return next view id (FlipSocialViewLoggedInSubmenu)
  180. */
  181. uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  182. {
  183. UNUSED(context);
  184. free_about_widget(false);
  185. //
  186. free_friends();
  187. free_message_users();
  188. flip_feed_info_free();
  189. if (flip_feed_item)
  190. {
  191. free(flip_feed_item);
  192. flip_feed_item = NULL;
  193. }
  194. // free the about widget if it exists
  195. free_about_widget(true);
  196. free_explore_dialog();
  197. free_friends_dialog();
  198. free_messages_dialog();
  199. free_compose_dialog();
  200. return FlipSocialViewLoggedInSubmenu;
  201. }
  202. /**
  203. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  204. * @param context The context - unused
  205. * @return next view id (FlipSocialViewLoggedOutLogin)
  206. */
  207. uint32_t flip_social_callback_to_login_logged_out(void *context)
  208. {
  209. UNUSED(context);
  210. flip_social_sent_login_request = false;
  211. flip_social_login_success = false;
  212. return FlipSocialViewVariableItemList;
  213. }
  214. /**
  215. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  216. * @param context The context - unused
  217. * @return next view id (FlipSocialViewLoggedOutRegister)
  218. */
  219. uint32_t flip_social_callback_to_register_logged_out(void *context)
  220. {
  221. UNUSED(context);
  222. flip_social_sent_register_request = false;
  223. flip_social_register_success = false;
  224. return FlipSocialViewVariableItemList;
  225. }
  226. /**
  227. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  228. * @param context The context - unused
  229. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  230. */
  231. uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  232. {
  233. UNUSED(context);
  234. return FlipSocialViewVariableItemList;
  235. }
  236. /**
  237. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  238. * @param context The context - unused
  239. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  240. */
  241. uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  242. {
  243. UNUSED(context);
  244. return FlipSocialViewVariableItemList;
  245. }
  246. /**
  247. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  248. * @param context The context - unused
  249. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  250. */
  251. uint32_t flip_social_callback_to_settings_logged_in(void *context)
  252. {
  253. UNUSED(context);
  254. return FlipSocialViewSubmenu;
  255. }
  256. /**
  257. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  258. * @param context The context - unused
  259. * @return next view id (FlipSocialViewLoggedInCompose)
  260. */
  261. uint32_t flip_social_callback_to_compose_logged_in(void *context)
  262. {
  263. UNUSED(context);
  264. return FlipSocialViewSubmenu;
  265. }
  266. /**
  267. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  268. * @param context The context - unused
  269. * @return next view id (FlipSocialViewLoggedInProfile)
  270. */
  271. uint32_t flip_social_callback_to_profile_logged_in(void *context)
  272. {
  273. UNUSED(context);
  274. return FlipSocialViewVariableItemList;
  275. }
  276. /**
  277. * @brief Navigation callback to bring the user back to the Explore submenu
  278. * @param context The context - unused
  279. * @return next view id (FlipSocialViewSubmenu)
  280. */
  281. uint32_t flip_social_callback_to_explore_logged_in(void *context)
  282. {
  283. UNUSED(context);
  284. flip_social_dialog_stop = false;
  285. flip_social_dialog_shown = false;
  286. if (flip_social_explore)
  287. {
  288. flip_social_explore->index = 0;
  289. }
  290. return FlipSocialViewSubmenu;
  291. }
  292. /**
  293. * @brief Navigation callback to bring the user back to the Friends submenu
  294. * @param context The context - unused
  295. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  296. */
  297. uint32_t flip_social_callback_to_friends_logged_in(void *context)
  298. {
  299. UNUSED(context);
  300. flip_social_dialog_stop = false;
  301. flip_social_dialog_shown = false;
  302. flip_social_friends->index = 0;
  303. return FlipSocialViewSubmenu;
  304. }
  305. /**
  306. * @brief Navigation callback to bring the user back to the Messages submenu
  307. * @param context The context - unused
  308. * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
  309. */
  310. uint32_t flip_social_callback_to_messages_logged_in(void *context)
  311. {
  312. UNUSED(context);
  313. return FlipSocialViewSubmenu;
  314. }
  315. /**
  316. * @brief Navigation callback to bring the user back to the User Choices screen
  317. * @param context The context - unused
  318. * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
  319. */
  320. uint32_t flip_social_callback_to_messages_user_choices(void *context)
  321. {
  322. UNUSED(context);
  323. return FlipSocialViewSubmenu;
  324. }
  325. /**
  326. * @brief Navigation callback for exiting the application
  327. * @param context The context - unused
  328. * @return next view id (VIEW_NONE to exit the app)
  329. */
  330. uint32_t flip_social_callback_exit_app(void *context)
  331. {
  332. // Exit the application
  333. free_all(true, true, context);
  334. return VIEW_NONE;
  335. }
  336. void explore_dialog_callback(DialogExResult result, void *context)
  337. {
  338. furi_assert(context);
  339. FlipSocialApp *app = (FlipSocialApp *)context;
  340. if (result == DialogExResultLeft) // Remove
  341. {
  342. FlipperHTTP *fhttp = flipper_http_alloc();
  343. if (fhttp)
  344. {
  345. // remove friend
  346. char remove_payload[128];
  347. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  348. auth_headers_alloc();
  349. flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/remove-friend/", auth_headers, remove_payload);
  350. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  351. free_explore_dialog();
  352. furi_delay_ms(1000);
  353. flipper_http_free(fhttp);
  354. }
  355. }
  356. else if (result == DialogExResultRight)
  357. {
  358. FlipperHTTP *fhttp = flipper_http_alloc();
  359. if (fhttp)
  360. {
  361. // add friend
  362. char add_payload[128];
  363. snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  364. auth_headers_alloc();
  365. flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/add-friend/", auth_headers, add_payload);
  366. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  367. free_explore_dialog();
  368. furi_delay_ms(1000);
  369. flipper_http_free(fhttp);
  370. }
  371. }
  372. }
  373. static void friends_dialog_callback(DialogExResult result, void *context)
  374. {
  375. furi_assert(context);
  376. FlipSocialApp *app = (FlipSocialApp *)context;
  377. if (result == DialogExResultLeft) // Remove
  378. {
  379. FlipperHTTP *fhttp = flipper_http_alloc();
  380. if (fhttp)
  381. {
  382. // remove friend
  383. char remove_payload[128];
  384. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
  385. auth_headers_alloc();
  386. flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/remove-friend/", auth_headers, remove_payload);
  387. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  388. free_friends_dialog();
  389. furi_delay_ms(1000);
  390. flipper_http_free(fhttp);
  391. }
  392. }
  393. }
  394. void messages_dialog_callback(DialogExResult result, void *context)
  395. {
  396. furi_assert(context);
  397. FlipSocialApp *app = (FlipSocialApp *)context;
  398. if (result == DialogExResultLeft) // Previous message
  399. {
  400. if (flip_social_messages->index > 0)
  401. {
  402. flip_social_messages->index--;
  403. dialog_ex_reset(app->dialog_messages);
  404. dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
  405. dialog_ex_set_text(app->dialog_messages, updated_user_message(flip_social_messages->messages[flip_social_messages->index]), 0, 10, AlignLeft, AlignTop);
  406. if (flip_social_messages->index != 0)
  407. {
  408. dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
  409. }
  410. dialog_ex_set_right_button_text(app->dialog_messages, "Next");
  411. dialog_ex_set_center_button_text(app->dialog_messages, "Create");
  412. // switch view, free dialog, re-alloc dialog, switch back to dialog
  413. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
  414. free_messages_dialog();
  415. messages_dialog_alloc(false);
  416. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
  417. }
  418. }
  419. else if (result == DialogExResultRight) // Next message
  420. {
  421. if (flip_social_messages->index < flip_social_messages->count - 1)
  422. {
  423. flip_social_messages->index++;
  424. dialog_ex_reset(app->dialog_messages);
  425. dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
  426. dialog_ex_set_text(app->dialog_messages, updated_user_message(flip_social_messages->messages[flip_social_messages->index]), 0, 10, AlignLeft, AlignTop);
  427. dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
  428. if (flip_social_messages->index != flip_social_messages->count - 1)
  429. {
  430. dialog_ex_set_right_button_text(app->dialog_messages, "Next");
  431. }
  432. dialog_ex_set_center_button_text(app->dialog_messages, "Create");
  433. // switch view, free dialog, re-alloc dialog, switch back to dialog
  434. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
  435. free_messages_dialog();
  436. messages_dialog_alloc(false);
  437. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
  438. }
  439. }
  440. else if (result == DialogExResultCenter) // new message
  441. {
  442. free_text_input();
  443. if (!alloc_text_input(FlipSocialViewLoggedInMessagesNewMessageInput))
  444. {
  445. FURI_LOG_E(TAG, "Failed to allocate text input");
  446. return;
  447. }
  448. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  449. }
  450. }
  451. static void compose_dialog_callback(DialogExResult result, void *context)
  452. {
  453. furi_assert(context);
  454. FlipSocialApp *app = (FlipSocialApp *)context;
  455. if (result == DialogExResultLeft) // Delete
  456. {
  457. // Ensure index is within bounds
  458. if (app_instance->pre_saved_messages.index >= app_instance->pre_saved_messages.count)
  459. {
  460. FURI_LOG_E(TAG, "Invalid index for deletion: %zu", app_instance->pre_saved_messages.index);
  461. return;
  462. }
  463. // Shift messages to remove the selected message
  464. for (size_t i = app_instance->pre_saved_messages.index; i < app_instance->pre_saved_messages.count - 1; i++)
  465. {
  466. strncpy(app_instance->pre_saved_messages.messages[i],
  467. app_instance->pre_saved_messages.messages[i + 1],
  468. MAX_MESSAGE_LENGTH);
  469. }
  470. // Clear the last message after shifting
  471. memset(app_instance->pre_saved_messages.messages[app_instance->pre_saved_messages.count - 1], 0, MAX_MESSAGE_LENGTH);
  472. app_instance->pre_saved_messages.count--;
  473. // Reset and rebuild the submenu
  474. submenu_reset(app_instance->submenu);
  475. submenu_add_item(app_instance->submenu, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  476. for (size_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  477. {
  478. submenu_add_item(app_instance->submenu,
  479. app_instance->pre_saved_messages.messages[i],
  480. FlipSocialSubemnuComposeIndexStartIndex + i,
  481. flip_social_callback_submenu_choices,
  482. app);
  483. }
  484. // Save the updated playlist
  485. save_playlist(&app_instance->pre_saved_messages);
  486. // Switch back to the compose view
  487. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  488. // Free the dialog resources
  489. free_compose_dialog();
  490. }
  491. else if (result == DialogExResultRight) // Post
  492. {
  493. // post the message
  494. // send selected_message
  495. FlipperHTTP *fhttp = flipper_http_alloc();
  496. if (!fhttp)
  497. {
  498. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  499. return;
  500. }
  501. if (selected_message && app_instance->login_username_logged_in)
  502. {
  503. if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
  504. {
  505. FURI_LOG_E(TAG, "Message is too long");
  506. flipper_http_free(fhttp);
  507. return;
  508. }
  509. // Send the selected_message
  510. char command[256];
  511. snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
  512. app_instance->login_username_logged_in, selected_message);
  513. if (!flipper_http_request(
  514. fhttp,
  515. POST,
  516. "https://www.jblanked.com/flipper/api/feed/post/",
  517. auth_headers,
  518. command))
  519. {
  520. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  521. flipper_http_free(fhttp);
  522. return;
  523. }
  524. fhttp->state = RECEIVING;
  525. furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
  526. }
  527. else
  528. {
  529. FURI_LOG_E(TAG, "Message or username is NULL");
  530. flipper_http_free(fhttp);
  531. return;
  532. }
  533. while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
  534. {
  535. furi_delay_ms(100);
  536. }
  537. if (flip_social_load_initial_feed(false, 1))
  538. {
  539. free_compose_dialog();
  540. }
  541. else
  542. {
  543. FURI_LOG_E(TAG, "Failed to load the initial feed");
  544. flipper_http_free(fhttp);
  545. }
  546. }
  547. }
  548. bool flip_social_get_user_info(FlipperHTTP *fhttp)
  549. {
  550. if (!fhttp)
  551. {
  552. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  553. return false;
  554. }
  555. char url[256];
  556. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/users/%s/extended/", flip_social_explore->usernames[flip_social_explore->index]);
  557. if (!flipper_http_request(fhttp, GET, url, auth_headers, NULL))
  558. {
  559. FURI_LOG_E(TAG, "Failed to send HTTP request for user info");
  560. fhttp->state = ISSUE;
  561. return false;
  562. }
  563. fhttp->state = RECEIVING;
  564. return true;
  565. }
  566. bool flip_social_parse_user_info(FlipperHTTP *fhttp)
  567. {
  568. if (!app_instance)
  569. {
  570. FURI_LOG_E(TAG, "App instance is NULL");
  571. return false;
  572. }
  573. if (!fhttp)
  574. {
  575. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  576. return false;
  577. }
  578. if (fhttp->last_response == NULL)
  579. {
  580. FURI_LOG_E(TAG, "Response is NULL");
  581. return false;
  582. }
  583. if (!app_instance->explore_user_bio)
  584. {
  585. FURI_LOG_E(TAG, "App instance is NULL");
  586. return false;
  587. }
  588. char *bio = get_json_value("bio", fhttp->last_response);
  589. char *friends = get_json_value("friends", fhttp->last_response);
  590. if (bio && friends)
  591. {
  592. if (strlen(bio) != 0)
  593. {
  594. snprintf(app_instance->explore_user_bio, MAX_MESSAGE_LENGTH, "%s (%s friends)", bio, friends);
  595. }
  596. else
  597. {
  598. snprintf(app_instance->explore_user_bio, MAX_MESSAGE_LENGTH, "%s friends", friends);
  599. }
  600. free(bio);
  601. free(friends);
  602. return true;
  603. }
  604. return false;
  605. }
  606. /**
  607. * @brief Handle ALL submenu item selections.
  608. * @param context The context - FlipSocialApp object.
  609. * @param index The FlipSocialSubmenuIndex item that was clicked.
  610. * @return void
  611. */
  612. void flip_social_callback_submenu_choices(void *context, uint32_t index)
  613. {
  614. FlipSocialApp *app = (FlipSocialApp *)context;
  615. if (!app)
  616. {
  617. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  618. return;
  619. }
  620. switch (index)
  621. {
  622. case FlipSocialSubmenuLoggedOutIndexLogin:
  623. flip_social_sent_login_request = false;
  624. flip_social_login_success = false;
  625. free_all(true, true, context);
  626. if (!alloc_variable_item_list(FlipSocialViewLoggedOutLogin))
  627. {
  628. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  629. return;
  630. }
  631. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  632. break;
  633. case FlipSocialSubmenuLoggedOutIndexRegister:
  634. flip_social_sent_register_request = false;
  635. flip_social_register_success = false;
  636. free_all(true, true, context);
  637. if (!alloc_variable_item_list(FlipSocialViewLoggedOutRegister))
  638. {
  639. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  640. return;
  641. }
  642. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  643. break;
  644. case FlipSocialSubmenuLoggedOutIndexAbout:
  645. if (!about_widget_alloc(false))
  646. {
  647. return;
  648. }
  649. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  650. break;
  651. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  652. free_all(false, false, app);
  653. if (!alloc_variable_item_list(FlipSocialViewLoggedOutWifiSettings))
  654. {
  655. FURI_LOG_E(TAG, "Failed to allocate text input");
  656. return;
  657. }
  658. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  659. break;
  660. case FlipSocialSubmenuLoggedInIndexProfile:
  661. free_all(true, true, context);
  662. if (!alloc_variable_item_list(FlipSocialViewLoggedInProfile))
  663. {
  664. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  665. return;
  666. }
  667. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  668. break;
  669. case FlipSocialSubmenuLoggedInIndexMessages:
  670. free_all(true, true, context);
  671. if (!alloc_submenu(FlipSocialViewLoggedInMessagesSubmenu))
  672. {
  673. FURI_LOG_E(TAG, "Failed to allocate submenu");
  674. return;
  675. }
  676. // flipper_http_loading_task(
  677. // flip_social_get_message_users, // get the message users
  678. // flip_social_parse_json_message_users, // parse the message users
  679. // FlipSocialViewSubmenu, // switch to the messages submenu if successful
  680. // FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  681. // &app->view_dispatcher); // view dispatcher
  682. break;
  683. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  684. // they need to search for the user to send a message
  685. free_text_input();
  686. if (!alloc_text_input(FlipSocialViewLoggedInMessageUsersInput))
  687. {
  688. FURI_LOG_E(TAG, "Failed to allocate text input");
  689. return;
  690. }
  691. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  692. break;
  693. case FlipSocialSubmenuLoggedInIndexFeed:
  694. free_all(true, true, context);
  695. FlipperHTTP *fhttp = flipper_http_alloc();
  696. if (!fhttp)
  697. {
  698. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  699. return;
  700. }
  701. if (!flip_social_load_initial_feed(fhttp, 1))
  702. {
  703. FURI_LOG_E(TAG, "Failed to load the initial feed");
  704. return;
  705. }
  706. flipper_http_free(fhttp);
  707. break;
  708. case FlipSocialSubmenuExploreIndex:
  709. free_all(true, true, context);
  710. if (!alloc_text_input(FlipSocialViewLoggedInExploreInput))
  711. {
  712. FURI_LOG_E(TAG, "Failed to allocate text input");
  713. return;
  714. }
  715. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  716. break;
  717. case FlipSocialSubmenuLoggedInIndexCompose:
  718. free_all(true, true, context);
  719. if (!alloc_submenu(FlipSocialViewLoggedInCompose))
  720. {
  721. FURI_LOG_E(TAG, "Failed to allocate submenu");
  722. return;
  723. }
  724. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  725. break;
  726. case FlipSocialSubmenuLoggedInIndexSettings:
  727. free_all(true, true, context);
  728. if (!alloc_submenu(FlipSocialViewLoggedInSettings))
  729. {
  730. FURI_LOG_E(TAG, "Failed to allocate submenu");
  731. return;
  732. }
  733. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  734. break;
  735. case FlipSocialSubmenuLoggedInIndexAbout:
  736. free_all(true, false, context);
  737. if (!about_widget_alloc(true))
  738. {
  739. FURI_LOG_E(TAG, "Failed to allocate about widget");
  740. return;
  741. }
  742. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  743. break;
  744. case FlipSocialSubmenuLoggedInIndexWifiSettings:
  745. free_all(true, false, context);
  746. if (!alloc_variable_item_list(FlipSocialViewLoggedInSettingsWifi))
  747. {
  748. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  749. return;
  750. }
  751. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  752. break;
  753. case FlipSocialSubmenuLoggedInIndexUserSettings:
  754. free_all(true, false, context);
  755. if (!alloc_variable_item_list(FlipSocialViewLoggedInSettingsUser))
  756. {
  757. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  758. return;
  759. }
  760. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  761. break;
  762. case FlipSocialSubmenuLoggedInSignOutButton:
  763. free_all(true, true, context);
  764. app->is_logged_in = "false";
  765. save_settings(app->wifi_ssid_logged_out, app->wifi_password_logged_out, app->login_username_logged_out, app->login_username_logged_in, app->login_password_logged_out, app->change_password_logged_in, app->change_bio_logged_in, app->is_logged_in);
  766. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  767. break;
  768. case FlipSocialSubmenuComposeIndexAddPreSave:
  769. free_text_input();
  770. if (!alloc_text_input(FlipSocialViewLoggedInComposeAddPreSaveInput))
  771. {
  772. FURI_LOG_E(TAG, "Failed to allocate text input");
  773. return;
  774. }
  775. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  776. break;
  777. default:
  778. // Handle the pre-saved message selection (has a max of 25 items)
  779. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  780. {
  781. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  782. snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  783. if (!selected_message)
  784. {
  785. FURI_LOG_E(TAG, "Selected message is NULL");
  786. return;
  787. }
  788. free_compose_dialog();
  789. if (!app->dialog_compose)
  790. {
  791. if (!easy_flipper_set_dialog_ex(
  792. &app->dialog_compose,
  793. FlipSocialViewComposeDialog,
  794. "New Feed Post",
  795. 0,
  796. 0,
  797. updated_user_message(selected_message),
  798. 0,
  799. 10,
  800. "Delete",
  801. "Post",
  802. NULL,
  803. compose_dialog_callback,
  804. flip_social_callback_to_compose_logged_in,
  805. &app->view_dispatcher,
  806. app))
  807. {
  808. return;
  809. }
  810. }
  811. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewComposeDialog);
  812. }
  813. // Handle the explore selection
  814. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  815. {
  816. if (!flip_social_explore)
  817. {
  818. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  819. return;
  820. }
  821. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  822. // loading task to get the user info
  823. if (app->explore_user_bio)
  824. {
  825. free(app->explore_user_bio);
  826. app->explore_user_bio = NULL;
  827. }
  828. if (!easy_flipper_set_buffer(&app->explore_user_bio, MAX_MESSAGE_LENGTH))
  829. {
  830. return;
  831. }
  832. // if (flipper_http_process_response_async(flip_social_get_user_info, flip_social_parse_user_info))
  833. // {
  834. // free_explore_dialog();
  835. // if (!app->dialog_explore)
  836. // {
  837. // if (!easy_flipper_set_dialog_ex(
  838. // &app->dialog_explore,
  839. // FlipSocialViewExploreDialog,
  840. // flip_social_explore->usernames[flip_social_explore->index],
  841. // 0,
  842. // 0,
  843. // updated_user_message(app->explore_user_bio),
  844. // 0,
  845. // 10,
  846. // "Remove", // remove if user is a friend (future update)
  847. // "Add", // add if user is not a friend (future update)
  848. // NULL,
  849. // explore_dialog_callback,
  850. // flip_social_callback_to_explore_logged_in,
  851. // &app->view_dispatcher,
  852. // app))
  853. // {
  854. // return;
  855. // }
  856. // }
  857. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  858. // }
  859. // else
  860. // {
  861. // free_explore_dialog();
  862. // if (!app->dialog_explore)
  863. // {
  864. // if (!easy_flipper_set_dialog_ex(
  865. // &app->dialog_explore,
  866. // FlipSocialViewExploreDialog,
  867. // flip_social_explore->usernames[flip_social_explore->index],
  868. // 0,
  869. // 0,
  870. // "",
  871. // 0,
  872. // 10,
  873. // "Remove", // remove if user is a friend (future update)
  874. // "Add", // add if user is not a friend (future update)
  875. // NULL,
  876. // explore_dialog_callback,
  877. // flip_social_callback_to_explore_logged_in,
  878. // &app->view_dispatcher,
  879. // app))
  880. // {
  881. // return;
  882. // }
  883. // }
  884. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  885. // }
  886. }
  887. // handle the friends selection
  888. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  889. {
  890. if (!flip_social_friends)
  891. {
  892. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  893. return;
  894. }
  895. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  896. free_friends_dialog();
  897. if (!app->dialog_friends)
  898. {
  899. if (!easy_flipper_set_dialog_ex(
  900. &app->dialog_friends,
  901. FlipSocialViewFriendsDialog,
  902. "Friend Options",
  903. 0,
  904. 0,
  905. flip_social_friends->usernames[flip_social_friends->index],
  906. 0,
  907. 10,
  908. "Remove",
  909. "",
  910. NULL,
  911. friends_dialog_callback,
  912. flip_social_callback_to_friends_logged_in,
  913. &app->view_dispatcher,
  914. app))
  915. {
  916. return;
  917. }
  918. }
  919. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFriendsDialog);
  920. }
  921. // handle the messages selection
  922. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
  923. {
  924. if (!flip_social_message_users)
  925. {
  926. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  927. return;
  928. }
  929. flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  930. // flipper_http_loading_task(
  931. // flip_social_get_messages_with_user, // get the messages with the selected user
  932. // flip_social_parse_json_messages, // parse the messages
  933. // FlipSocialViewMessagesDialog, // switch to the messages process if successful
  934. // FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
  935. // &app->view_dispatcher // view dispatcher
  936. // );
  937. }
  938. // handle the messages user choices selection
  939. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
  940. {
  941. if (!flip_social_explore)
  942. {
  943. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  944. return;
  945. }
  946. flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  947. free_text_input();
  948. if (!alloc_text_input(FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput))
  949. {
  950. FURI_LOG_E(TAG, "Failed to allocate text input");
  951. return;
  952. } // switch to the text input view
  953. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  954. }
  955. else
  956. {
  957. FURI_LOG_E(TAG, "Unknown submenu index");
  958. }
  959. break;
  960. }
  961. }
  962. /**
  963. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  964. * @param context The context - FlipSocialApp object.
  965. * @return void
  966. */
  967. void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  968. {
  969. FlipSocialApp *app = (FlipSocialApp *)context;
  970. if (!app)
  971. {
  972. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  973. return;
  974. }
  975. // Store the entered name
  976. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  977. // Store the entered name in the logged in name field
  978. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  979. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  980. // Ensure null-termination
  981. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  982. // Update the name item text
  983. if (app->variable_item_logged_out_wifi_settings_ssid)
  984. {
  985. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  986. }
  987. // update the wifi settings
  988. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  989. {
  990. FlipperHTTP *fhttp = flipper_http_alloc();
  991. if (fhttp)
  992. {
  993. if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  994. {
  995. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  996. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  997. }
  998. furi_delay_ms(500);
  999. flipper_http_free(fhttp);
  1000. }
  1001. }
  1002. // Save the settings
  1003. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1004. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1005. }
  1006. /**
  1007. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  1008. * @param context The context - FlipSocialApp object.
  1009. * @return void
  1010. */
  1011. void flip_social_logged_out_wifi_settings_password_updated(void *context)
  1012. {
  1013. FlipSocialApp *app = (FlipSocialApp *)context;
  1014. if (!app)
  1015. {
  1016. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1017. return;
  1018. }
  1019. // Store the entered WiFi password
  1020. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1021. // Store the entered WiFi password in the logged in password field
  1022. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1023. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1024. // Ensure null-termination
  1025. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  1026. // Update the password item text
  1027. if (app->variable_item_logged_out_wifi_settings_password)
  1028. {
  1029. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  1030. }
  1031. // update the wifi settings
  1032. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1033. {
  1034. FlipperHTTP *fhttp = flipper_http_alloc();
  1035. if (fhttp)
  1036. {
  1037. if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1038. {
  1039. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1040. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1041. }
  1042. furi_delay_ms(500);
  1043. flipper_http_free(fhttp);
  1044. }
  1045. }
  1046. // Save the settings
  1047. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1048. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1049. }
  1050. /**
  1051. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  1052. * @param context The context - FlipSocialApp object.
  1053. * @param index The index of the selected item.
  1054. * @return void
  1055. */
  1056. void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  1057. {
  1058. FlipSocialApp *app = (FlipSocialApp *)context;
  1059. if (!app)
  1060. {
  1061. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1062. return;
  1063. }
  1064. switch (index)
  1065. {
  1066. case 0: // Input SSID
  1067. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  1068. free_text_input();
  1069. if (!alloc_text_input(FlipSocialViewLoggedOutWifiSettingsSSIDInput))
  1070. {
  1071. FURI_LOG_E(TAG, "Failed to allocate text input");
  1072. return;
  1073. }
  1074. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1075. break;
  1076. case 1: // Input Password
  1077. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  1078. free_text_input();
  1079. if (!alloc_text_input(FlipSocialViewLoggedOutWifiSettingsPasswordInput))
  1080. {
  1081. FURI_LOG_E(TAG, "Failed to allocate text input");
  1082. return;
  1083. }
  1084. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1085. break;
  1086. default:
  1087. FURI_LOG_E(TAG, "Unknown configuration item index");
  1088. break;
  1089. }
  1090. }
  1091. /**
  1092. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  1093. * @param context The context - FlipSocialApp object.
  1094. * @return void
  1095. */
  1096. void flip_social_logged_out_login_username_updated(void *context)
  1097. {
  1098. FlipSocialApp *app = (FlipSocialApp *)context;
  1099. if (!app)
  1100. {
  1101. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1102. return;
  1103. }
  1104. // Store the entered name
  1105. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1106. // Store the entered name in the logged in username field
  1107. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1108. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1109. // Ensure null-termination
  1110. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  1111. // Update the name item text
  1112. if (app->variable_item_logged_out_login_username)
  1113. {
  1114. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  1115. }
  1116. // Save the settings
  1117. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1118. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1119. }
  1120. /**
  1121. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  1122. * @param context The context - FlipSocialApp object.
  1123. * @return void
  1124. */
  1125. void flip_social_logged_out_login_password_updated(void *context)
  1126. {
  1127. FlipSocialApp *app = (FlipSocialApp *)context;
  1128. if (!app)
  1129. {
  1130. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1131. return;
  1132. }
  1133. // Store the entered password
  1134. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1135. // Store the entered password in the change password field
  1136. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1137. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1138. // Ensure null-termination
  1139. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  1140. // Update the password item text
  1141. if (app->variable_item_logged_out_login_password)
  1142. {
  1143. // dont show the password on the screen (version 0.2)
  1144. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  1145. }
  1146. // Save the settings
  1147. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1148. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1149. }
  1150. /**
  1151. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  1152. * @param context The context - FlipSocialApp object.
  1153. * @param index The index of the selected item.
  1154. * @return void
  1155. */
  1156. void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  1157. {
  1158. FlipSocialApp *app = (FlipSocialApp *)context;
  1159. if (!app)
  1160. {
  1161. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1162. return;
  1163. }
  1164. switch (index)
  1165. {
  1166. case 0: // Input Username
  1167. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  1168. free_all(false, true, app);
  1169. if (!alloc_text_input(FlipSocialViewLoggedOutLoginUsernameInput))
  1170. {
  1171. FURI_LOG_E(TAG, "Failed to allocate text input");
  1172. return;
  1173. }
  1174. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1175. break;
  1176. case 1: // Input Password
  1177. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  1178. free_all(false, true, app);
  1179. if (!alloc_text_input(FlipSocialViewLoggedOutLoginPasswordInput))
  1180. {
  1181. FURI_LOG_E(TAG, "Failed to allocate text input");
  1182. return;
  1183. }
  1184. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1185. break;
  1186. case 2: // Login Button
  1187. // if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  1188. // {
  1189. // FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1190. // return;
  1191. // }
  1192. flip_social_login_switch_to_view(app);
  1193. break;
  1194. default:
  1195. FURI_LOG_E(TAG, "Unknown configuration item index");
  1196. break;
  1197. }
  1198. }
  1199. /**
  1200. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  1201. * @param context The context - FlipSocialApp object.
  1202. * @return void
  1203. */
  1204. void flip_social_logged_out_register_username_updated(void *context)
  1205. {
  1206. FlipSocialApp *app = (FlipSocialApp *)context;
  1207. if (!app)
  1208. {
  1209. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1210. return;
  1211. }
  1212. // Store the entered name
  1213. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  1214. // Ensure null-termination
  1215. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  1216. // Update the name item text
  1217. if (app->variable_item_logged_out_register_username)
  1218. {
  1219. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  1220. }
  1221. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1222. }
  1223. /**
  1224. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  1225. * @param context The context - FlipSocialApp object.
  1226. * @return void
  1227. */
  1228. void flip_social_logged_out_register_password_updated(void *context)
  1229. {
  1230. FlipSocialApp *app = (FlipSocialApp *)context;
  1231. if (!app)
  1232. {
  1233. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1234. return;
  1235. }
  1236. // Store the entered password
  1237. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  1238. // Ensure null-termination
  1239. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  1240. // Update the password item text
  1241. if (app->variable_item_logged_out_register_password)
  1242. {
  1243. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  1244. }
  1245. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1246. }
  1247. /**
  1248. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  1249. * @param context The context - FlipSocialApp object.
  1250. * @return void
  1251. */
  1252. void flip_social_logged_out_register_password_2_updated(void *context)
  1253. {
  1254. FlipSocialApp *app = (FlipSocialApp *)context;
  1255. if (!app)
  1256. {
  1257. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1258. return;
  1259. }
  1260. // Store the entered password
  1261. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  1262. // Ensure null-termination
  1263. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  1264. // Update the password item text
  1265. if (app->variable_item_logged_out_register_password_2)
  1266. {
  1267. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  1268. }
  1269. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1270. }
  1271. /**
  1272. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  1273. * @param context The context - FlipSocialApp object.
  1274. * @param index The index of the selected item.
  1275. * @return void
  1276. */
  1277. void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  1278. {
  1279. FlipSocialApp *app = (FlipSocialApp *)context;
  1280. if (!app)
  1281. {
  1282. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1283. return;
  1284. }
  1285. switch (index)
  1286. {
  1287. case 0: // Input Username
  1288. free_all(false, true, app);
  1289. if (!alloc_text_input(FlipSocialViewLoggedOutRegisterUsernameInput))
  1290. {
  1291. FURI_LOG_E(TAG, "Failed to allocate text input");
  1292. return;
  1293. }
  1294. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1295. break;
  1296. case 1: // Input Password
  1297. free_all(false, true, app);
  1298. if (!alloc_text_input(FlipSocialViewLoggedOutRegisterPasswordInput))
  1299. {
  1300. FURI_LOG_E(TAG, "Failed to allocate text input");
  1301. return;
  1302. }
  1303. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1304. break;
  1305. case 2: // Input Password 2
  1306. free_all(false, true, app);
  1307. if (!alloc_text_input(FlipSocialViewLoggedOutRegisterPassword2Input))
  1308. {
  1309. FURI_LOG_E(TAG, "Failed to allocate text input");
  1310. return;
  1311. }
  1312. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1313. break;
  1314. case 3: // Register button
  1315. // if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  1316. // {
  1317. // FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1318. // return;
  1319. // }
  1320. flip_social_register_switch_to_view(app);
  1321. break;
  1322. default:
  1323. FURI_LOG_E(TAG, "Unknown configuration item index");
  1324. break;
  1325. }
  1326. }
  1327. /**
  1328. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1329. * @param context The context - FlipSocialApp object.
  1330. * @return void
  1331. */
  1332. void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1333. {
  1334. FlipSocialApp *app = (FlipSocialApp *)context;
  1335. if (!app)
  1336. {
  1337. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1338. return;
  1339. }
  1340. // Store the entered SSID
  1341. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1342. // Store the entered SSID in the logged out SSID
  1343. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1344. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1345. // Ensure null-termination
  1346. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1347. // Update the name item text
  1348. if (app->variable_item_logged_in_wifi_settings_ssid)
  1349. {
  1350. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1351. }
  1352. // Save the settings
  1353. save_settings(app_instance->wifi_ssid_logged_in, app_instance->wifi_password_logged_in, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1354. // update the wifi settings
  1355. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1356. {
  1357. FlipperHTTP *fhttp = flipper_http_alloc();
  1358. if (fhttp)
  1359. {
  1360. if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1361. {
  1362. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1363. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1364. }
  1365. furi_delay_ms(500);
  1366. flipper_http_free(fhttp);
  1367. }
  1368. }
  1369. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1370. }
  1371. /**
  1372. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1373. * @param context The context - FlipSocialApp object.
  1374. * @return void
  1375. */
  1376. void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1377. {
  1378. FlipSocialApp *app = (FlipSocialApp *)context;
  1379. if (!app)
  1380. {
  1381. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1382. return;
  1383. }
  1384. // Store the entered password
  1385. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1386. // Store the entered password in the logged out password
  1387. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1388. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1389. // Ensure null-termination
  1390. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1391. // Update the password item text
  1392. if (app->variable_item_logged_in_wifi_settings_password)
  1393. {
  1394. // dont show the password on the screen (version 0.2)
  1395. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1396. }
  1397. // Save the settings
  1398. save_settings(app_instance->wifi_ssid_logged_in, app_instance->wifi_password_logged_in, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1399. // update the wifi settings
  1400. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1401. {
  1402. FlipperHTTP *fhttp = flipper_http_alloc();
  1403. if (fhttp)
  1404. {
  1405. if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1406. {
  1407. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1408. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1409. }
  1410. furi_delay_ms(500);
  1411. flipper_http_free(fhttp);
  1412. }
  1413. }
  1414. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1415. }
  1416. /**
  1417. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1418. * @param context The context - FlipSocialApp object.
  1419. * @param index The index of the selected item.
  1420. * @return void
  1421. */
  1422. void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1423. {
  1424. FlipSocialApp *app = (FlipSocialApp *)context;
  1425. if (!app)
  1426. {
  1427. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1428. return;
  1429. }
  1430. switch (index)
  1431. {
  1432. case 0: // Input SSID
  1433. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1434. free_all(false, false, app);
  1435. if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsSSIDInput))
  1436. {
  1437. FURI_LOG_E(TAG, "Failed to allocate text input for SSID");
  1438. return;
  1439. }
  1440. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1441. break;
  1442. case 1: // Input Password
  1443. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1444. free_all(false, false, app);
  1445. if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsPasswordInput))
  1446. {
  1447. FURI_LOG_E(TAG, "Failed to allocate text input for Password");
  1448. return;
  1449. }
  1450. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1451. break;
  1452. default:
  1453. FURI_LOG_E(TAG, "Unknown configuration item index");
  1454. break;
  1455. }
  1456. }
  1457. void flip_social_logged_in_user_settings_item_selected(void *context, uint32_t index)
  1458. {
  1459. FlipSocialApp *app = (FlipSocialApp *)context;
  1460. if (!app)
  1461. {
  1462. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1463. return;
  1464. }
  1465. // Switch to the appropriate view
  1466. switch (index)
  1467. {
  1468. case 0: // Feed Type
  1469. break;
  1470. case 1: // Notifications
  1471. break;
  1472. }
  1473. }
  1474. /**
  1475. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1476. * @param context The context - FlipSocialApp object.
  1477. * @return void
  1478. */
  1479. void flip_social_logged_in_compose_pre_save_updated(void *context)
  1480. {
  1481. FlipSocialApp *app = (FlipSocialApp *)context;
  1482. if (!app)
  1483. {
  1484. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1485. return;
  1486. }
  1487. // Check if the message is empty or if adding the message would exceed MAX_PRE_SAVED_MESSAGES
  1488. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1489. {
  1490. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1491. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1492. return;
  1493. }
  1494. // Copy the entered message into the next available slot
  1495. strncpy(
  1496. app->pre_saved_messages.messages[app->pre_saved_messages.count],
  1497. app->compose_pre_save_logged_in_temp_buffer,
  1498. MAX_MESSAGE_LENGTH - 1);
  1499. // Ensure null-termination
  1500. app->pre_saved_messages.messages[app->pre_saved_messages.count][MAX_MESSAGE_LENGTH - 1] = '\0';
  1501. // Increment the count
  1502. app->pre_saved_messages.count++;
  1503. // Rebuild the submenu
  1504. submenu_reset(app->submenu);
  1505. submenu_add_item(
  1506. app->submenu,
  1507. "Add Pre-Save",
  1508. FlipSocialSubmenuComposeIndexAddPreSave,
  1509. flip_social_callback_submenu_choices,
  1510. app);
  1511. for (size_t i = 0; i < app->pre_saved_messages.count; i++)
  1512. {
  1513. submenu_add_item(
  1514. app->submenu,
  1515. app->pre_saved_messages.messages[i],
  1516. FlipSocialSubemnuComposeIndexStartIndex + i,
  1517. flip_social_callback_submenu_choices,
  1518. app);
  1519. }
  1520. // Save the updated playlist
  1521. save_playlist(&app->pre_saved_messages);
  1522. // Switch back to the compose view
  1523. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1524. }
  1525. /**
  1526. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1527. * @param context The context - FlipSocialApp object.
  1528. * @return void
  1529. */
  1530. void flip_social_logged_in_profile_change_password_updated(void *context)
  1531. {
  1532. FlipSocialApp *app = (FlipSocialApp *)context;
  1533. if (!app)
  1534. {
  1535. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1536. return;
  1537. }
  1538. // Correct type: old_pass should be a pointer to a string (char *)
  1539. const char *old_password = app->login_password_logged_out;
  1540. // Store the entered message
  1541. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1542. // store the entered password in the logged out password
  1543. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1544. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1545. // Ensure null-termination
  1546. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1547. // Update the message item text
  1548. if (app->variable_item_logged_in_profile_change_password)
  1549. {
  1550. // dont show the password on the screen (version 0.2)
  1551. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1552. }
  1553. // send post request to change password
  1554. FlipperHTTP *fhttp = flipper_http_alloc();
  1555. if (fhttp)
  1556. {
  1557. auth_headers_alloc();
  1558. char payload[256];
  1559. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1560. if (!flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/change-password/", auth_headers, payload))
  1561. {
  1562. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1563. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1564. }
  1565. flipper_http_free(fhttp);
  1566. }
  1567. // Save the settings
  1568. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
  1569. // instead of going to a view, just show a success message
  1570. easy_flipper_dialog("Success", "Password updated successfully\n\n\nPress BACK to return :D");
  1571. }
  1572. void flip_social_logged_in_profile_change_bio_updated(void *context)
  1573. {
  1574. FlipSocialApp *app = (FlipSocialApp *)context;
  1575. if (!app)
  1576. {
  1577. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1578. return;
  1579. }
  1580. // Store the entered message
  1581. strncpy(app->change_bio_logged_in, app->change_bio_logged_in_temp_buffer, app->change_bio_logged_in_temp_buffer_size);
  1582. // Ensure null-termination
  1583. app->change_bio_logged_in[app->change_bio_logged_in_temp_buffer_size - 1] = '\0';
  1584. // Update the message item text
  1585. if (app->variable_item_logged_in_profile_change_bio)
  1586. {
  1587. variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_bio, app->change_bio_logged_in);
  1588. }
  1589. // send post request to change bio
  1590. FlipperHTTP *fhttp = flipper_http_alloc();
  1591. if (fhttp)
  1592. {
  1593. auth_headers_alloc();
  1594. char payload[256];
  1595. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
  1596. if (!flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/change-bio/", auth_headers, payload))
  1597. {
  1598. FURI_LOG_E(TAG, "Failed to send post request to change bio");
  1599. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1600. }
  1601. furi_delay_ms(500);
  1602. flipper_http_free(fhttp);
  1603. }
  1604. // Save the settings
  1605. save_settings(app->wifi_ssid_logged_out, app->wifi_password_logged_out, app->login_username_logged_out, app->login_username_logged_in, app->login_password_logged_out, app->change_password_logged_in, app->change_bio_logged_in, app->is_logged_in);
  1606. // instead of going to a view, just show a success message
  1607. easy_flipper_dialog("Success", "Bio updated successfully\n\n\nPress BACK to return :D");
  1608. }
  1609. /**
  1610. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1611. * @param context The context - FlipSocialApp object.
  1612. * @param index The index of the selected item.
  1613. * @return void
  1614. */
  1615. void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1616. {
  1617. FlipSocialApp *app = (FlipSocialApp *)context;
  1618. if (!app)
  1619. {
  1620. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1621. return;
  1622. }
  1623. switch (index)
  1624. {
  1625. case 0: // Change Username
  1626. // do nothing since username cannot be changed
  1627. break;
  1628. case 1: // Change Password
  1629. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1630. free_text_input();
  1631. if (!alloc_text_input(FlipSocialViewLoggedInChangePasswordInput))
  1632. {
  1633. FURI_LOG_E(TAG, "Failed to allocate text input for change password");
  1634. return;
  1635. }
  1636. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1637. break;
  1638. case 2: // Change Bio
  1639. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangeBioInput);
  1640. free_text_input();
  1641. if (!alloc_text_input(FlipSocialViewLoggedInChangeBioInput))
  1642. {
  1643. FURI_LOG_E(TAG, "Failed to allocate text input for change bio");
  1644. return;
  1645. }
  1646. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1647. break;
  1648. case 3: // Friends
  1649. free_all(false, true, app);
  1650. if (!alloc_submenu(FlipSocialViewLoggedInFriendsSubmenu))
  1651. {
  1652. FURI_LOG_E(TAG, "Failed to allocate submenu for friends");
  1653. return;
  1654. }
  1655. // if (!flipper_http_init(flipper_http_rx_callback, app))
  1656. // {
  1657. // FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1658. // return;
  1659. // }
  1660. // flipper_http_loading_task(
  1661. // flip_social_get_friends,
  1662. // flip_social_parse_json_friends,
  1663. // FlipSocialViewSubmenu,
  1664. // FlipSocialViewVariableItemList,
  1665. // &app->view_dispatcher);
  1666. break;
  1667. default:
  1668. FURI_LOG_E(TAG, "Unknown configuration item index");
  1669. break;
  1670. }
  1671. }
  1672. /**
  1673. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  1674. * @param context The context - FlipSocialApp object.
  1675. * @return void
  1676. */
  1677. void flip_social_logged_in_messages_user_choice_message_updated(void *context)
  1678. {
  1679. FlipSocialApp *app = (FlipSocialApp *)context;
  1680. if (!app)
  1681. {
  1682. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1683. return;
  1684. }
  1685. // check if the message is empty
  1686. if (app->message_user_choice_logged_in_temp_buffer_size == 0)
  1687. {
  1688. FURI_LOG_E(TAG, "Message is empty");
  1689. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1690. return;
  1691. }
  1692. // Store the entered message
  1693. strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
  1694. // Ensure null-termination
  1695. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
  1696. // send post request to send message
  1697. FlipperHTTP *fhttp = flipper_http_alloc();
  1698. if (!fhttp)
  1699. {
  1700. FURI_LOG_E(TAG, "Failed to initialize HTTP");
  1701. return;
  1702. }
  1703. auth_headers_alloc();
  1704. char url[128];
  1705. char payload[256];
  1706. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/messages/%s/post/", app->login_username_logged_in);
  1707. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
  1708. if (!flipper_http_request(fhttp, POST, url, auth_headers, payload)) // start the async request
  1709. {
  1710. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1711. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1712. }
  1713. furi_delay_ms(1000);
  1714. flipper_http_free(fhttp);
  1715. // add user to the top of the list if not already there
  1716. for (int i = 0; i < flip_social_message_users->count; i++)
  1717. {
  1718. if (strcmp(flip_social_message_users->usernames[i], flip_social_explore->usernames[flip_social_explore->index]) == 0)
  1719. {
  1720. // remove the user from the list
  1721. for (int j = i; j < flip_social_message_users->count - 1; j++)
  1722. {
  1723. strncpy(flip_social_message_users->usernames[j], flip_social_message_users->usernames[j + 1], strlen(flip_social_message_users->usernames[j + 1]));
  1724. }
  1725. flip_social_message_users->count--;
  1726. break;
  1727. }
  1728. }
  1729. // add the user to the top of the list
  1730. for (int i = flip_social_message_users->count; i > 0; i--)
  1731. {
  1732. strncpy(flip_social_message_users->usernames[i], flip_social_message_users->usernames[i - 1], strlen(flip_social_message_users->usernames[i - 1]));
  1733. }
  1734. strncpy(flip_social_message_users->usernames[0], flip_social_explore->usernames[flip_social_explore->index], strlen(flip_social_explore->usernames[flip_social_explore->index]));
  1735. flip_social_message_users->count++;
  1736. // redraw submenu
  1737. flip_social_update_messages_submenu();
  1738. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1739. }
  1740. /**
  1741. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1742. * @param context The context - FlipSocialApp object.
  1743. * @return void
  1744. */
  1745. void flip_social_logged_in_messages_new_message_updated(void *context)
  1746. {
  1747. FlipSocialApp *app = (FlipSocialApp *)context;
  1748. if (!app)
  1749. {
  1750. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1751. return;
  1752. }
  1753. // check if the message is empty
  1754. if (app->messages_new_message_logged_in_temp_buffer_size == 0)
  1755. {
  1756. FURI_LOG_E(TAG, "Message is empty");
  1757. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1758. return;
  1759. }
  1760. // Store the entered message
  1761. strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
  1762. // Ensure null-termination
  1763. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
  1764. FlipperHTTP *fhttp = flipper_http_alloc();
  1765. if (!fhttp)
  1766. {
  1767. FURI_LOG_E(TAG, "Failed to initialize HTTP");
  1768. return;
  1769. }
  1770. bool send_message_to_user()
  1771. {
  1772. // send post request to send message
  1773. auth_headers_alloc();
  1774. char url[128];
  1775. char payload[256];
  1776. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/messages/%s/post/", app->login_username_logged_in);
  1777. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
  1778. if (!flipper_http_request(fhttp, POST, url, auth_headers, payload))
  1779. {
  1780. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1781. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1782. easy_flipper_dialog("Error", "Failed to send message\n\n\nPress BACK to return :D");
  1783. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1784. return false;
  1785. }
  1786. fhttp->state = RECEIVING;
  1787. return true;
  1788. }
  1789. bool parse_message_to_user()
  1790. {
  1791. while (fhttp->state != IDLE)
  1792. {
  1793. furi_delay_ms(10);
  1794. }
  1795. return true;
  1796. }
  1797. // well, we got a freeze here, so let's use the loading task to switch views and force refresh
  1798. flipper_http_loading_task(
  1799. fhttp,
  1800. send_message_to_user,
  1801. parse_message_to_user,
  1802. FlipSocialViewSubmenu,
  1803. FlipSocialViewLoggedInMessagesNewMessageInput,
  1804. &app->view_dispatcher);
  1805. flipper_http_free(fhttp);
  1806. }
  1807. void flip_social_logged_in_explore_updated(void *context)
  1808. {
  1809. FlipSocialApp *app = (FlipSocialApp *)context;
  1810. if (!app)
  1811. {
  1812. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1813. return;
  1814. }
  1815. // check if the message is empty
  1816. if (app->explore_logged_in_temp_buffer_size == 0)
  1817. {
  1818. FURI_LOG_E(TAG, "Message is empty");
  1819. strncpy(app->explore_logged_in, "a", 2);
  1820. }
  1821. else
  1822. {
  1823. // Store the entered message
  1824. strncpy(app->explore_logged_in, app->explore_logged_in_temp_buffer, app->explore_logged_in_temp_buffer_size);
  1825. }
  1826. // Ensure null-termination
  1827. app->explore_logged_in[app->explore_logged_in_temp_buffer_size - 1] = '\0';
  1828. free_submenu();
  1829. if (!alloc_submenu(FlipSocialViewLoggedInExploreSubmenu))
  1830. {
  1831. FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
  1832. return;
  1833. }
  1834. // flipper_http_loading_task(
  1835. // flip_social_get_explore, // get the explore users
  1836. // flip_social_parse_json_explore, // parse the explore users
  1837. // FlipSocialViewSubmenu, // switch to the explore submenu if successful
  1838. // FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  1839. // &app->view_dispatcher); // view dispatcher
  1840. }
  1841. void flip_social_logged_in_message_users_updated(void *context)
  1842. {
  1843. FlipSocialApp *app = (FlipSocialApp *)context;
  1844. if (!app)
  1845. {
  1846. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1847. return;
  1848. }
  1849. // check if the message is empty
  1850. if (!app->message_users_logged_in_temp_buffer || app->message_users_logged_in_temp_buffer_size == 0)
  1851. {
  1852. FURI_LOG_E(TAG, "Message is empty");
  1853. strncpy(app->message_users_logged_in, "a", 2);
  1854. }
  1855. else
  1856. {
  1857. // Store the entered message
  1858. strncpy(app->message_users_logged_in, app->message_users_logged_in_temp_buffer, app->message_users_logged_in_temp_buffer_size);
  1859. }
  1860. // Ensure null-termination
  1861. app->message_users_logged_in[app->message_users_logged_in_temp_buffer_size - 1] = '\0';
  1862. free_submenu();
  1863. if (!alloc_submenu(FlipSocialViewLoggedInExploreSubmenu))
  1864. {
  1865. FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
  1866. return;
  1867. }
  1868. // get users
  1869. // flipper_http_loading_task(
  1870. // flip_social_get_explore_2, // get the explore users
  1871. // flip_social_parse_json_message_user_choices, // parse the explore users
  1872. // FlipSocialViewSubmenu, // switch to the explore submenu if successful
  1873. // FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  1874. // &app->view_dispatcher); // view dispatcher
  1875. }
  1876. bool flip_social_get_home_notification(FlipperHTTP *fhttp)
  1877. {
  1878. if (!app_instance)
  1879. {
  1880. FURI_LOG_E(TAG, "app_instance is NULL");
  1881. return false;
  1882. }
  1883. if (!fhttp)
  1884. {
  1885. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1886. return false;
  1887. }
  1888. // Create the directory for saving settings
  1889. char directory_path[256];
  1890. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data");
  1891. // Create the directory
  1892. Storage *storage = furi_record_open(RECORD_STORAGE);
  1893. storage_common_mkdir(storage, directory_path);
  1894. furi_record_close(RECORD_STORAGE);
  1895. auth_headers_alloc();
  1896. snprintf(
  1897. fhttp->file_path,
  1898. sizeof(fhttp->file_path),
  1899. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data/notification.json");
  1900. fhttp->save_received_data = true;
  1901. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/flip-social-notifications/", auth_headers, NULL);
  1902. }
  1903. bool flip_social_parse_home_notification(FlipperHTTP *fhttp)
  1904. {
  1905. if (!app_instance)
  1906. {
  1907. FURI_LOG_E(TAG, "app_instance is NULL");
  1908. return false;
  1909. }
  1910. if (!fhttp)
  1911. {
  1912. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1913. return false;
  1914. }
  1915. // Load the notification from the file
  1916. FuriString *notification = flipper_http_load_from_file(fhttp->file_path);
  1917. if (notification == NULL)
  1918. {
  1919. FURI_LOG_E(TAG, "Failed to load notification from file");
  1920. return false;
  1921. }
  1922. // Check if announcement and analytics key exists
  1923. FuriString *announcement_json = get_json_value_furi("announcement", notification);
  1924. FuriString *analytics_json = get_json_value_furi("analytics", notification);
  1925. if (announcement_json == NULL || analytics_json == NULL)
  1926. {
  1927. FURI_LOG_E(TAG, "Failed to get announcement or analytics from notification");
  1928. if (announcement_json)
  1929. {
  1930. furi_string_free(announcement_json);
  1931. }
  1932. if (analytics_json)
  1933. {
  1934. furi_string_free(analytics_json);
  1935. }
  1936. furi_string_free(notification);
  1937. return false;
  1938. }
  1939. // Extract values from JSON
  1940. FuriString *announcement_value = get_json_value_furi("content", announcement_json);
  1941. FuriString *announcement_time = get_json_value_furi("date_created", announcement_json);
  1942. FuriString *analytics_value = get_json_value_furi("count", analytics_json);
  1943. FuriString *analytics_time = get_json_value_furi("time", analytics_json);
  1944. if (!announcement_value || !announcement_time || !analytics_value || !analytics_time)
  1945. {
  1946. FURI_LOG_E(TAG, "Failed to get announcement or analytics value from notification");
  1947. if (announcement_value)
  1948. {
  1949. furi_string_free(announcement_value);
  1950. }
  1951. if (announcement_time)
  1952. {
  1953. furi_string_free(announcement_time);
  1954. }
  1955. if (analytics_value)
  1956. {
  1957. furi_string_free(analytics_value);
  1958. }
  1959. if (analytics_time)
  1960. {
  1961. furi_string_free(analytics_time);
  1962. }
  1963. furi_string_free(announcement_json);
  1964. furi_string_free(analytics_json);
  1965. furi_string_free(notification);
  1966. return false;
  1967. }
  1968. // Load previous announcement and analytics times
  1969. char past_analytics_time[32] = {0};
  1970. char past_announcement_time[32] = {0};
  1971. bool analytics_time_loaded = load_char("analytics_time", past_analytics_time, sizeof(past_analytics_time));
  1972. bool announcement_time_loaded = load_char("announcement_time", past_announcement_time, sizeof(past_announcement_time));
  1973. bool new_announcement = false;
  1974. bool new_analytics = false;
  1975. // Check for new announcement
  1976. if (!announcement_time_loaded || strcmp(furi_string_get_cstr(announcement_time), past_announcement_time) != 0)
  1977. {
  1978. new_announcement = true;
  1979. }
  1980. // Check for new analytics
  1981. if (!analytics_time_loaded || strcmp(furi_string_get_cstr(analytics_time), past_analytics_time) != 0)
  1982. {
  1983. new_analytics = true;
  1984. }
  1985. // If no new announcement and no new analytics, exit early
  1986. if (!new_announcement && !new_analytics)
  1987. {
  1988. FURI_LOG_D(TAG, "No new announcement or analytics");
  1989. furi_string_free(announcement_value);
  1990. furi_string_free(announcement_time);
  1991. furi_string_free(analytics_value);
  1992. furi_string_free(analytics_time);
  1993. furi_string_free(announcement_json);
  1994. furi_string_free(analytics_json);
  1995. furi_string_free(notification);
  1996. return true;
  1997. }
  1998. // Save the new announcement and analytics times if they are new
  1999. if (new_announcement)
  2000. {
  2001. save_char("announcement_time", furi_string_get_cstr(announcement_time));
  2002. }
  2003. if (new_analytics)
  2004. {
  2005. save_char("analytics_time", furi_string_get_cstr(analytics_time));
  2006. }
  2007. // Prepare and show dialogs based on what is new
  2008. if (new_announcement)
  2009. {
  2010. easy_flipper_dialog("Announcement", (char *)furi_string_get_cstr(announcement_value));
  2011. }
  2012. if (new_analytics)
  2013. {
  2014. char analytics_text[128] = {0};
  2015. // Determine the new posts count
  2016. if (atoi(furi_string_get_cstr(analytics_value)) > 0)
  2017. {
  2018. char past_analytics_value[32] = {0};
  2019. int new_posts = 0;
  2020. if (load_char("analytics_value", past_analytics_value, sizeof(past_analytics_value)))
  2021. {
  2022. int past_posts = atoi(past_analytics_value);
  2023. int current_posts = atoi(furi_string_get_cstr(analytics_value));
  2024. new_posts = current_posts - past_posts;
  2025. snprintf(analytics_text, sizeof(analytics_text), "%d new posts", new_posts);
  2026. }
  2027. else
  2028. {
  2029. snprintf(analytics_text, sizeof(analytics_text), "%s feed posts", furi_string_get_cstr(analytics_value));
  2030. }
  2031. save_char("analytics_value", furi_string_get_cstr(analytics_value));
  2032. }
  2033. else
  2034. {
  2035. snprintf(analytics_text, sizeof(analytics_text), "%s feed posts", furi_string_get_cstr(analytics_value));
  2036. }
  2037. easy_flipper_dialog("Notifications", analytics_text);
  2038. }
  2039. // Free allocated resources
  2040. furi_string_free(announcement_value);
  2041. furi_string_free(announcement_time);
  2042. furi_string_free(analytics_value);
  2043. furi_string_free(analytics_time);
  2044. furi_string_free(announcement_json);
  2045. furi_string_free(analytics_json);
  2046. furi_string_free(notification);
  2047. return true;
  2048. }
  2049. // home notification
  2050. bool flip_social_home_notification()
  2051. {
  2052. // return flipper_http_process_response_async(flip_social_get_home_notification, flip_social_parse_home_notification);
  2053. return true; // for now
  2054. }