flip_social_callback.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. #include <callback/flip_social_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. void flip_social_request_error_draw(Canvas *canvas)
  12. {
  13. if (canvas == NULL)
  14. {
  15. FURI_LOG_E(TAG, "flip_social_request_error_draw - canvas is NULL");
  16. DEV_CRASH();
  17. return;
  18. }
  19. if (fhttp.last_response != NULL)
  20. {
  21. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  22. {
  23. canvas_clear(canvas);
  24. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  25. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  26. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  27. }
  28. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  29. {
  30. canvas_clear(canvas);
  31. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  32. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  33. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  34. }
  35. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  36. {
  37. canvas_clear(canvas);
  38. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  39. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  40. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  41. }
  42. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  43. {
  44. canvas_clear(canvas);
  45. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  46. }
  47. else
  48. {
  49. canvas_clear(canvas);
  50. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  51. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  52. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  53. }
  54. }
  55. else
  56. {
  57. canvas_clear(canvas);
  58. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  59. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  60. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  61. }
  62. }
  63. static bool flip_social_login_fetch(DataLoaderModel *model)
  64. {
  65. UNUSED(model);
  66. 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)
  67. {
  68. return false;
  69. }
  70. char buffer[256];
  71. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  72. auth_headers_alloc();
  73. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", auth_headers, buffer))
  74. {
  75. fhttp.state = RECEIVING;
  76. return true;
  77. }
  78. else
  79. {
  80. fhttp.state = ISSUE;
  81. return false;
  82. }
  83. }
  84. static char *flip_social_login_parse(DataLoaderModel *model)
  85. {
  86. UNUSED(model);
  87. // read response
  88. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  89. {
  90. app_instance->is_logged_in = "true";
  91. // set the logged_in_username and change_password_logged_in
  92. if (app_instance->login_username_logged_out)
  93. {
  94. strcpy(app_instance->login_username_logged_in, app_instance->login_username_logged_out);
  95. }
  96. if (app_instance->login_password_logged_out)
  97. {
  98. strcpy(app_instance->change_password_logged_in, app_instance->login_password_logged_out);
  99. }
  100. 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->is_logged_in);
  101. // send user to the logged in submenu
  102. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  103. return "Login successful!";
  104. }
  105. else if (strstr(fhttp.last_response, "User not found") != NULL)
  106. {
  107. return "Account not found...";
  108. }
  109. else
  110. {
  111. return "Failed to login...";
  112. }
  113. }
  114. static void flip_social_login_switch_to_view(FlipSocialApp *app)
  115. {
  116. flip_social_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 1, flip_social_callback_to_login_logged_out, FlipSocialViewLoader);
  117. }
  118. static bool flip_social_register_fetch(DataLoaderModel *model)
  119. {
  120. UNUSED(model);
  121. // check if the username and password are valid
  122. 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)
  123. {
  124. FURI_LOG_E(TAG, "Username or password is NULL");
  125. return false;
  126. }
  127. // check if both passwords match
  128. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  129. {
  130. FURI_LOG_E(TAG, "Passwords do not match");
  131. return false;
  132. }
  133. char buffer[128];
  134. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  135. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", buffer))
  136. {
  137. // Set the state to RECEIVING to ensure we continue to see the receiving message
  138. fhttp.state = RECEIVING;
  139. return true;
  140. }
  141. else
  142. {
  143. fhttp.state = ISSUE;
  144. return false;
  145. }
  146. }
  147. static char *flip_social_register_parse(DataLoaderModel *model)
  148. {
  149. UNUSED(model);
  150. // read response
  151. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  152. {
  153. // set the login credentials
  154. if (app_instance->login_username_logged_out)
  155. {
  156. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  157. }
  158. if (app_instance->login_password_logged_out)
  159. {
  160. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  161. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  162. }
  163. if (app_instance->login_username_logged_in)
  164. {
  165. app_instance->login_username_logged_in = app_instance->register_username_logged_out;
  166. }
  167. app_instance->is_logged_in = "true";
  168. // update header credentials
  169. auth_headers_alloc();
  170. // save the credentials
  171. 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->is_logged_in);
  172. // send user to the logged in submenu
  173. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  174. return "Registration successful!\nWelcome to FlipSocial!";
  175. }
  176. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  177. {
  178. return "Please enter your credentials.\nPress BACK to return.";
  179. }
  180. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  181. {
  182. return "Registration failed...\nUsername already exists.\nPress BACK to return.";
  183. }
  184. else
  185. {
  186. return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
  187. }
  188. }
  189. static void flip_social_register_switch_to_view(FlipSocialApp *app)
  190. {
  191. flip_social_generic_switch_to_view(app, "Registering...", flip_social_register_fetch, flip_social_register_parse, 1, flip_social_callback_to_register_logged_out, FlipSocialViewLoader);
  192. }
  193. /**
  194. * @brief Navigation callback to go back to the submenu Logged out.
  195. * @param context The context - unused
  196. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  197. */
  198. uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  199. {
  200. UNUSED(context);
  201. return FlipSocialViewLoggedOutSubmenu;
  202. }
  203. /**
  204. * @brief Navigation callback to go back to the submenu Logged in.
  205. * @param context The context - unused
  206. * @return next view id (FlipSocialViewLoggedInSubmenu)
  207. */
  208. uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  209. {
  210. UNUSED(context);
  211. flip_social_free_explore();
  212. flip_social_free_friends();
  213. flip_social_free_message_users();
  214. flip_social_free_messages();
  215. flip_feed_info_free();
  216. if (flip_feed_item)
  217. {
  218. free(flip_feed_item);
  219. flip_feed_item = NULL;
  220. }
  221. return FlipSocialViewLoggedInSubmenu;
  222. }
  223. /**
  224. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  225. * @param context The context - unused
  226. * @return next view id (FlipSocialViewLoggedOutLogin)
  227. */
  228. uint32_t flip_social_callback_to_login_logged_out(void *context)
  229. {
  230. UNUSED(context);
  231. flip_social_sent_login_request = false;
  232. flip_social_login_success = false;
  233. return FlipSocialViewLoggedOutLogin;
  234. }
  235. /**
  236. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  237. * @param context The context - unused
  238. * @return next view id (FlipSocialViewLoggedOutRegister)
  239. */
  240. uint32_t flip_social_callback_to_register_logged_out(void *context)
  241. {
  242. UNUSED(context);
  243. flip_social_sent_register_request = false;
  244. flip_social_register_success = false;
  245. return FlipSocialViewLoggedOutRegister;
  246. }
  247. /**
  248. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  249. * @param context The context - unused
  250. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  251. */
  252. uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  253. {
  254. UNUSED(context);
  255. return FlipSocialViewLoggedOutWifiSettings;
  256. }
  257. /**
  258. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  259. * @param context The context - unused
  260. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  261. */
  262. uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  263. {
  264. UNUSED(context);
  265. return FlipSocialViewLoggedInSettingsWifi;
  266. }
  267. /**
  268. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  269. * @param context The context - unused
  270. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  271. */
  272. uint32_t flip_social_callback_to_settings_logged_in(void *context)
  273. {
  274. UNUSED(context);
  275. return FlipSocialViewLoggedInSettings;
  276. }
  277. /**
  278. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  279. * @param context The context - unused
  280. * @return next view id (FlipSocialViewLoggedInCompose)
  281. */
  282. uint32_t flip_social_callback_to_compose_logged_in(void *context)
  283. {
  284. UNUSED(context);
  285. return FlipSocialViewLoggedInCompose;
  286. }
  287. /**
  288. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  289. * @param context The context - unused
  290. * @return next view id (FlipSocialViewLoggedInProfile)
  291. */
  292. uint32_t flip_social_callback_to_profile_logged_in(void *context)
  293. {
  294. UNUSED(context);
  295. return FlipSocialViewLoggedInProfile;
  296. }
  297. /**
  298. * @brief Navigation callback to bring the user back to the Explore submenu
  299. * @param context The context - unused
  300. * @return next view id (FlipSocialViewLoggedInExploreSubmenu)
  301. */
  302. uint32_t flip_social_callback_to_explore_logged_in(void *context)
  303. {
  304. UNUSED(context);
  305. flip_social_dialog_stop = false;
  306. last_explore_response = "";
  307. flip_social_dialog_shown = false;
  308. flip_social_explore->index = 0;
  309. action = ActionNone;
  310. return FlipSocialViewLoggedInExploreSubmenu;
  311. }
  312. /**
  313. * @brief Navigation callback to bring the user back to the Friends submenu
  314. * @param context The context - unused
  315. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  316. */
  317. uint32_t flip_social_callback_to_friends_logged_in(void *context)
  318. {
  319. UNUSED(context);
  320. flip_social_dialog_stop = false;
  321. last_explore_response = "";
  322. flip_social_dialog_shown = false;
  323. flip_social_friends->index = 0;
  324. action = ActionNone;
  325. return FlipSocialViewLoggedInFriendsSubmenu;
  326. }
  327. /**
  328. * @brief Navigation callback to bring the user back to the Messages submenu
  329. * @param context The context - unused
  330. * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
  331. */
  332. uint32_t flip_social_callback_to_messages_logged_in(void *context)
  333. {
  334. UNUSED(context);
  335. return FlipSocialViewLoggedInMessagesSubmenu;
  336. }
  337. /**
  338. * @brief Navigation callback to bring the user back to the User Choices screen
  339. * @param context The context - unused
  340. * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
  341. */
  342. uint32_t flip_social_callback_to_messages_user_choices(void *context)
  343. {
  344. UNUSED(context);
  345. return FlipSocialViewLoggedInMessagesUserChoices;
  346. }
  347. /**
  348. * @brief Navigation callback for exiting the application
  349. * @param context The context - unused
  350. * @return next view id (VIEW_NONE to exit the app)
  351. */
  352. uint32_t flip_social_callback_exit_app(void *context)
  353. {
  354. // Exit the application
  355. if (!context)
  356. {
  357. FURI_LOG_E(TAG, "Context is NULL");
  358. return VIEW_NONE;
  359. }
  360. UNUSED(context);
  361. return VIEW_NONE; // Return VIEW_NONE to exit the app
  362. }
  363. /**
  364. * @brief Handle ALL submenu item selections.
  365. * @param context The context - FlipSocialApp object.
  366. * @param index The FlipSocialSubmenuIndex item that was clicked.
  367. * @return void
  368. */
  369. void flip_social_callback_submenu_choices(void *context, uint32_t index)
  370. {
  371. FlipSocialApp *app = (FlipSocialApp *)context;
  372. if (!app)
  373. {
  374. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  375. return;
  376. }
  377. switch (index)
  378. {
  379. case FlipSocialSubmenuLoggedOutIndexLogin:
  380. flip_social_sent_login_request = false;
  381. flip_social_login_success = false;
  382. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  383. break;
  384. case FlipSocialSubmenuLoggedOutIndexRegister:
  385. flip_social_sent_register_request = false;
  386. flip_social_register_success = false;
  387. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  388. break;
  389. case FlipSocialSubmenuLoggedOutIndexAbout:
  390. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  391. break;
  392. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  393. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  394. break;
  395. case FlipSocialSubmenuLoggedInIndexProfile:
  396. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  397. break;
  398. case FlipSocialSubmenuLoggedInIndexMessages:
  399. flipper_http_loading_task(
  400. flip_social_get_message_users, // get the message users
  401. flip_social_parse_json_message_users, // parse the message users
  402. FlipSocialViewLoggedInMessagesSubmenu, // switch to the messages submenu if successful
  403. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  404. &app->view_dispatcher); // view dispatcher
  405. break;
  406. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  407. flipper_http_loading_task(
  408. flip_social_get_explore, // get the explore users
  409. flip_social_parse_json_message_user_choices, // parse the explore users
  410. FlipSocialViewLoggedInMessagesUserChoices, // switch to the user choices if successful
  411. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  412. &app->view_dispatcher); // view dispatcher
  413. break;
  414. case FlipSocialSubmenuLoggedInIndexFeed:
  415. if (fhttp.state == INACTIVE)
  416. {
  417. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  418. return;
  419. }
  420. if (!easy_flipper_set_loading(&app->loading, FlipSocialViewLoading, flip_social_callback_to_submenu_logged_in, &app->view_dispatcher))
  421. {
  422. FURI_LOG_E(TAG, "Failed to set loading screen");
  423. return; // already on the submenu
  424. }
  425. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoading);
  426. if (flip_social_get_feed()) // start the async request
  427. {
  428. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  429. fhttp.state = RECEIVING;
  430. }
  431. else
  432. {
  433. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  434. fhttp.state = ISSUE;
  435. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  436. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  437. loading_free(app->loading);
  438. return;
  439. }
  440. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  441. {
  442. // Wait for the request to be received
  443. furi_delay_ms(100);
  444. }
  445. furi_timer_stop(fhttp.get_timeout_timer);
  446. // load feed info
  447. flip_feed_info = flip_social_parse_json_feed();
  448. if (!flip_feed_info || flip_feed_info->count < 1)
  449. {
  450. FURI_LOG_E(TAG, "Failed to parse JSON feed");
  451. fhttp.state = ISSUE;
  452. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  453. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  454. loading_free(app->loading);
  455. return;
  456. }
  457. // load the current feed post
  458. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  459. {
  460. FURI_LOG_E(TAG, "Failed to load feed post");
  461. fhttp.state = ISSUE;
  462. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  463. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  464. loading_free(app->loading);
  465. return;
  466. }
  467. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  468. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  469. loading_free(app->loading);
  470. break;
  471. case FlipSocialSubmenuExploreIndex:
  472. flipper_http_loading_task(
  473. flip_social_get_explore, // get the explore users
  474. flip_social_parse_json_explore, // parse the explore users
  475. FlipSocialViewLoggedInExploreSubmenu, // switch to the explore submenu if successful
  476. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  477. &app->view_dispatcher); // view dispatcher
  478. break;
  479. case FlipSocialSubmenuLoggedInIndexCompose:
  480. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  481. break;
  482. case FlipSocialSubmenuLoggedInIndexSettings:
  483. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  484. break;
  485. case FlipSocialSubmenuLoggedInSignOutButton:
  486. app->is_logged_in = "false";
  487. 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->is_logged_in);
  488. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  489. break;
  490. case FlipSocialSubmenuComposeIndexAddPreSave:
  491. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  492. break;
  493. default:
  494. action = ActionNone;
  495. // Handle the pre-saved message selection (has a max of 25 items)
  496. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  497. {
  498. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  499. snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  500. if (!selected_message)
  501. {
  502. FURI_LOG_E(TAG, "Selected message is NULL");
  503. return;
  504. }
  505. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  506. }
  507. // Handle the explore selection
  508. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  509. {
  510. if (!flip_social_explore)
  511. {
  512. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  513. return;
  514. }
  515. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  516. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreProccess);
  517. }
  518. // handle the friends selection
  519. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  520. {
  521. if (!flip_social_friends)
  522. {
  523. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  524. return;
  525. }
  526. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  527. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsProcess);
  528. }
  529. // handle the messages selection
  530. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
  531. {
  532. if (!flip_social_message_users)
  533. {
  534. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  535. return;
  536. }
  537. flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  538. flipper_http_loading_task(
  539. flip_social_get_messages_with_user, // get the messages with the selected user
  540. flip_social_parse_json_messages, // parse the messages
  541. FlipSocialViewLoggedInMessagesProcess, // switch to the messages process if successful
  542. FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
  543. &app->view_dispatcher); // view dispatcher
  544. }
  545. // handle the messages user choices selection
  546. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
  547. {
  548. if (!flip_social_explore)
  549. {
  550. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  551. return;
  552. }
  553. flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  554. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  555. }
  556. else
  557. {
  558. FURI_LOG_E(TAG, "Unknown submenu index");
  559. }
  560. break;
  561. }
  562. }
  563. /**
  564. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  565. * @param context The context - FlipSocialApp object.
  566. * @return void
  567. */
  568. void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  569. {
  570. FlipSocialApp *app = (FlipSocialApp *)context;
  571. if (!app)
  572. {
  573. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  574. return;
  575. }
  576. // Store the entered name
  577. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  578. // Store the entered name in the logged in name field
  579. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  580. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  581. // Ensure null-termination
  582. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  583. // Update the name item text
  584. if (app->variable_item_logged_out_wifi_settings_ssid)
  585. {
  586. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  587. }
  588. // update the wifi settings
  589. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  590. {
  591. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  592. {
  593. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  594. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  595. }
  596. }
  597. // Save the settings
  598. 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->is_logged_in);
  599. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  600. }
  601. /**
  602. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  603. * @param context The context - FlipSocialApp object.
  604. * @return void
  605. */
  606. void flip_social_logged_out_wifi_settings_password_updated(void *context)
  607. {
  608. FlipSocialApp *app = (FlipSocialApp *)context;
  609. if (!app)
  610. {
  611. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  612. return;
  613. }
  614. // Store the entered WiFi password
  615. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  616. // Store the entered WiFi password in the logged in password field
  617. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  618. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  619. // Ensure null-termination
  620. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  621. // Update the password item text
  622. if (app->variable_item_logged_out_wifi_settings_password)
  623. {
  624. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  625. }
  626. // update the wifi settings
  627. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  628. {
  629. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  630. {
  631. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  632. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  633. }
  634. }
  635. // Save the settings
  636. 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->is_logged_in);
  637. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  638. }
  639. /**
  640. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  641. * @param context The context - FlipSocialApp object.
  642. * @param index The index of the selected item.
  643. * @return void
  644. */
  645. void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  646. {
  647. FlipSocialApp *app = (FlipSocialApp *)context;
  648. if (!app)
  649. {
  650. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  651. return;
  652. }
  653. switch (index)
  654. {
  655. case 0: // Input SSID
  656. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  657. break;
  658. case 1: // Input Password
  659. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  660. break;
  661. default:
  662. FURI_LOG_E(TAG, "Unknown configuration item index");
  663. break;
  664. }
  665. }
  666. /**
  667. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  668. * @param context The context - FlipSocialApp object.
  669. * @return void
  670. */
  671. void flip_social_logged_out_login_username_updated(void *context)
  672. {
  673. FlipSocialApp *app = (FlipSocialApp *)context;
  674. if (!app)
  675. {
  676. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  677. return;
  678. }
  679. // Store the entered name
  680. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  681. // Store the entered name in the logged in username field
  682. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  683. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  684. // Ensure null-termination
  685. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  686. // Update the name item text
  687. if (app->variable_item_logged_out_login_username)
  688. {
  689. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  690. }
  691. // Save the settings
  692. 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->is_logged_in);
  693. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  694. }
  695. /**
  696. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  697. * @param context The context - FlipSocialApp object.
  698. * @return void
  699. */
  700. void flip_social_logged_out_login_password_updated(void *context)
  701. {
  702. FlipSocialApp *app = (FlipSocialApp *)context;
  703. if (!app)
  704. {
  705. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  706. return;
  707. }
  708. // Store the entered password
  709. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  710. // Store the entered password in the change password field
  711. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  712. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  713. // Ensure null-termination
  714. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  715. // Update the password item text
  716. if (app->variable_item_logged_out_login_password)
  717. {
  718. // dont show the password on the screen (version 0.2)
  719. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  720. }
  721. // Save the settings
  722. 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->is_logged_in);
  723. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  724. }
  725. /**
  726. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  727. * @param context The context - FlipSocialApp object.
  728. * @param index The index of the selected item.
  729. * @return void
  730. */
  731. void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  732. {
  733. FlipSocialApp *app = (FlipSocialApp *)context;
  734. if (!app)
  735. {
  736. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  737. return;
  738. }
  739. switch (index)
  740. {
  741. case 0: // Input Username
  742. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  743. break;
  744. case 1: // Input Password
  745. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  746. break;
  747. case 2: // Login Button
  748. flip_social_login_switch_to_view(app);
  749. break;
  750. default:
  751. FURI_LOG_E(TAG, "Unknown configuration item index");
  752. break;
  753. }
  754. }
  755. /**
  756. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  757. * @param context The context - FlipSocialApp object.
  758. * @return void
  759. */
  760. void flip_social_logged_out_register_username_updated(void *context)
  761. {
  762. FlipSocialApp *app = (FlipSocialApp *)context;
  763. if (!app)
  764. {
  765. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  766. return;
  767. }
  768. // Store the entered name
  769. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  770. // Ensure null-termination
  771. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  772. // Update the name item text
  773. if (app->variable_item_logged_out_register_username)
  774. {
  775. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  776. }
  777. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  778. }
  779. /**
  780. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  781. * @param context The context - FlipSocialApp object.
  782. * @return void
  783. */
  784. void flip_social_logged_out_register_password_updated(void *context)
  785. {
  786. FlipSocialApp *app = (FlipSocialApp *)context;
  787. if (!app)
  788. {
  789. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  790. return;
  791. }
  792. // Store the entered password
  793. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  794. // Ensure null-termination
  795. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  796. // Update the password item text
  797. if (app->variable_item_logged_out_register_password)
  798. {
  799. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  800. }
  801. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  802. }
  803. /**
  804. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  805. * @param context The context - FlipSocialApp object.
  806. * @return void
  807. */
  808. void flip_social_logged_out_register_password_2_updated(void *context)
  809. {
  810. FlipSocialApp *app = (FlipSocialApp *)context;
  811. if (!app)
  812. {
  813. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  814. return;
  815. }
  816. // Store the entered password
  817. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  818. // Ensure null-termination
  819. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  820. // Update the password item text
  821. if (app->variable_item_logged_out_register_password_2)
  822. {
  823. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  824. }
  825. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  826. }
  827. /**
  828. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  829. * @param context The context - FlipSocialApp object.
  830. * @param index The index of the selected item.
  831. * @return void
  832. */
  833. void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  834. {
  835. FlipSocialApp *app = (FlipSocialApp *)context;
  836. if (!app)
  837. {
  838. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  839. return;
  840. }
  841. switch (index)
  842. {
  843. case 0: // Input Username
  844. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  845. break;
  846. case 1: // Input Password
  847. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  848. break;
  849. case 2: // Input Password 2
  850. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  851. break;
  852. case 3: // Register button
  853. flip_social_register_switch_to_view(app);
  854. break;
  855. default:
  856. FURI_LOG_E(TAG, "Unknown configuration item index");
  857. break;
  858. }
  859. }
  860. /**
  861. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  862. * @param context The context - FlipSocialApp object.
  863. * @return void
  864. */
  865. void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  866. {
  867. FlipSocialApp *app = (FlipSocialApp *)context;
  868. if (!app)
  869. {
  870. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  871. return;
  872. }
  873. // Store the entered SSID
  874. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  875. // Store the entered SSID in the logged out SSID
  876. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  877. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  878. // Ensure null-termination
  879. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  880. // Update the name item text
  881. if (app->variable_item_logged_in_wifi_settings_ssid)
  882. {
  883. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  884. }
  885. // Save the settings
  886. 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->is_logged_in);
  887. // update the wifi settings
  888. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  889. {
  890. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  891. {
  892. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  893. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  894. }
  895. }
  896. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  897. }
  898. /**
  899. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  900. * @param context The context - FlipSocialApp object.
  901. * @return void
  902. */
  903. void flip_social_logged_in_wifi_settings_password_updated(void *context)
  904. {
  905. FlipSocialApp *app = (FlipSocialApp *)context;
  906. if (!app)
  907. {
  908. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  909. return;
  910. }
  911. // Store the entered password
  912. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  913. // Store the entered password in the logged out password
  914. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  915. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  916. // Ensure null-termination
  917. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  918. // Update the password item text
  919. if (app->variable_item_logged_in_wifi_settings_password)
  920. {
  921. // dont show the password on the screen (version 0.2)
  922. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  923. }
  924. // Save the settings
  925. 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->is_logged_in);
  926. // update the wifi settings
  927. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  928. {
  929. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  930. {
  931. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  932. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  933. }
  934. }
  935. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  936. }
  937. /**
  938. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  939. * @param context The context - FlipSocialApp object.
  940. * @param index The index of the selected item.
  941. * @return void
  942. */
  943. void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  944. {
  945. FlipSocialApp *app = (FlipSocialApp *)context;
  946. if (!app)
  947. {
  948. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  949. return;
  950. }
  951. switch (index)
  952. {
  953. case 0: // Input SSID
  954. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  955. break;
  956. case 1: // Input Password
  957. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  958. break;
  959. default:
  960. FURI_LOG_E(TAG, "Unknown configuration item index");
  961. break;
  962. }
  963. }
  964. /**
  965. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  966. * @param context The context - FlipSocialApp object.
  967. * @return void
  968. */
  969. void flip_social_logged_in_compose_pre_save_updated(void *context)
  970. {
  971. FlipSocialApp *app = (FlipSocialApp *)context;
  972. if (!app)
  973. {
  974. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  975. return;
  976. }
  977. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  978. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  979. {
  980. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  981. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  982. return;
  983. }
  984. // Store the entered message
  985. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  986. // Ensure null-termination
  987. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  988. // add the item to the submenu
  989. submenu_reset(app->submenu_compose);
  990. // loop through the items and add them to the submenu
  991. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  992. app->pre_saved_messages.count++;
  993. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  994. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  995. {
  996. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  997. }
  998. // save playlist
  999. save_playlist(&app->pre_saved_messages);
  1000. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1001. }
  1002. /**
  1003. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1004. * @param context The context - FlipSocialApp object.
  1005. * @return void
  1006. */
  1007. void flip_social_logged_in_profile_change_password_updated(void *context)
  1008. {
  1009. FlipSocialApp *app = (FlipSocialApp *)context;
  1010. if (!app)
  1011. {
  1012. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1013. return;
  1014. }
  1015. // Correct type: old_pass should be a pointer to a string (char *)
  1016. const char *old_password = app->login_password_logged_out;
  1017. // Store the entered message
  1018. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1019. // store the entered password in the logged out password
  1020. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1021. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1022. // Ensure null-termination
  1023. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1024. // Update the message item text
  1025. if (app->variable_item_logged_in_profile_change_password)
  1026. {
  1027. // dont show the password on the screen (version 0.2)
  1028. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1029. }
  1030. // send post request to change password
  1031. auth_headers_alloc();
  1032. char payload[256];
  1033. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1034. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
  1035. {
  1036. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1037. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1038. return;
  1039. }
  1040. // Save the settings
  1041. 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->is_logged_in);
  1042. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  1043. }
  1044. /**
  1045. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1046. * @param context The context - FlipSocialApp object.
  1047. * @param index The index of the selected item.
  1048. * @return void
  1049. */
  1050. void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1051. {
  1052. FlipSocialApp *app = (FlipSocialApp *)context;
  1053. if (!app)
  1054. {
  1055. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1056. return;
  1057. }
  1058. switch (index)
  1059. {
  1060. case 0: // Change Username
  1061. // do nothing since username cannot be changed
  1062. break;
  1063. case 1: // Change Password
  1064. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1065. break;
  1066. case 2: // Friends
  1067. flipper_http_loading_task(flip_social_get_friends, flip_social_parse_json_friends, FlipSocialViewLoggedInFriendsSubmenu, FlipSocialViewLoggedInProfile, &app->view_dispatcher);
  1068. break;
  1069. default:
  1070. FURI_LOG_E(TAG, "Unknown configuration item index");
  1071. break;
  1072. }
  1073. }
  1074. /**
  1075. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  1076. * @param context The context - FlipSocialApp object.
  1077. * @param index The index of the selected item.
  1078. * @return void
  1079. */
  1080. void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  1081. {
  1082. FlipSocialApp *app = (FlipSocialApp *)context;
  1083. if (!app)
  1084. {
  1085. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1086. return;
  1087. }
  1088. switch (index)
  1089. {
  1090. case 0: // About
  1091. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  1092. break;
  1093. case 1: // Wifi
  1094. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1095. break;
  1096. default:
  1097. break;
  1098. }
  1099. }
  1100. /**
  1101. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  1102. * @param context The context - FlipSocialApp object.
  1103. * @return void
  1104. */
  1105. void flip_social_logged_in_messages_user_choice_message_updated(void *context)
  1106. {
  1107. FlipSocialApp *app = (FlipSocialApp *)context;
  1108. if (!app)
  1109. {
  1110. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1111. return;
  1112. }
  1113. // check if the message is empty
  1114. if (app->message_user_choice_logged_in_temp_buffer_size == 0)
  1115. {
  1116. FURI_LOG_E(TAG, "Message is empty");
  1117. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  1118. return;
  1119. }
  1120. // Store the entered message
  1121. strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
  1122. // Ensure null-termination
  1123. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
  1124. // send post request to send message
  1125. auth_headers_alloc();
  1126. char url[128];
  1127. char payload[256];
  1128. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1129. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
  1130. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1131. {
  1132. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1133. fhttp.state = RECEIVING;
  1134. }
  1135. else
  1136. {
  1137. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1138. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1139. fhttp.state = ISSUE;
  1140. return;
  1141. }
  1142. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1143. {
  1144. // Wait for the request to be received
  1145. furi_delay_ms(10);
  1146. }
  1147. furi_timer_stop(fhttp.get_timeout_timer);
  1148. // add user to the message list
  1149. strncpy(flip_social_message_users->usernames[flip_social_message_users->count], flip_social_explore->usernames[flip_social_explore->index], strlen(flip_social_explore->usernames[flip_social_explore->index]));
  1150. flip_social_message_users->count++;
  1151. // redraw submenu
  1152. flip_social_update_messages_submenu();
  1153. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1154. }
  1155. /**
  1156. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1157. * @param context The context - FlipSocialApp object.
  1158. * @return void
  1159. */
  1160. void flip_social_logged_in_messages_new_message_updated(void *context)
  1161. {
  1162. FlipSocialApp *app = (FlipSocialApp *)context;
  1163. if (!app)
  1164. {
  1165. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1166. return;
  1167. }
  1168. // check if the message is empty
  1169. if (app->messages_new_message_logged_in_temp_buffer_size == 0)
  1170. {
  1171. FURI_LOG_E(TAG, "Message is empty");
  1172. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1173. return;
  1174. }
  1175. // Store the entered message
  1176. strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
  1177. // Ensure null-termination
  1178. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
  1179. // send post request to send message
  1180. auth_headers_alloc();
  1181. char url[128];
  1182. char payload[256];
  1183. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1184. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
  1185. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1186. {
  1187. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1188. fhttp.state = RECEIVING;
  1189. }
  1190. else
  1191. {
  1192. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1193. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1194. fhttp.state = ISSUE;
  1195. return;
  1196. }
  1197. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1198. {
  1199. // Wait for the request to be received
  1200. furi_delay_ms(10);
  1201. }
  1202. furi_timer_stop(fhttp.get_timeout_timer);
  1203. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1204. }
  1205. static void flip_social_widget_set_text(char *message, Widget **widget)
  1206. {
  1207. if (widget == NULL)
  1208. {
  1209. FURI_LOG_E(TAG, "flip_social_set_widget_text - widget is NULL");
  1210. DEV_CRASH();
  1211. return;
  1212. }
  1213. if (message == NULL)
  1214. {
  1215. FURI_LOG_E(TAG, "flip_social_set_widget_text - message is NULL");
  1216. DEV_CRASH();
  1217. return;
  1218. }
  1219. widget_reset(*widget);
  1220. uint32_t message_length = strlen(message); // Length of the message
  1221. uint32_t i = 0; // Index tracker
  1222. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1223. char *formatted_message; // Buffer to hold the final formatted message
  1224. // Allocate buffer with double the message length plus one for safety
  1225. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1226. {
  1227. return;
  1228. }
  1229. while (i < message_length)
  1230. {
  1231. uint32_t max_line_length = 31; // Maximum characters per line
  1232. uint32_t remaining_length = message_length - i; // Remaining characters
  1233. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1234. // Check for newline character within the current segment
  1235. uint32_t newline_pos = i;
  1236. bool found_newline = false;
  1237. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1238. {
  1239. if (message[newline_pos] == '\n')
  1240. {
  1241. found_newline = true;
  1242. break;
  1243. }
  1244. }
  1245. if (found_newline)
  1246. {
  1247. // If newline found, set line_length up to the newline
  1248. line_length = newline_pos - i;
  1249. }
  1250. // Temporary buffer to hold the current line
  1251. char line[32];
  1252. strncpy(line, message + i, line_length);
  1253. line[line_length] = '\0';
  1254. // If newline was found, skip it for the next iteration
  1255. if (found_newline)
  1256. {
  1257. i += line_length + 1; // +1 to skip the '\n' character
  1258. }
  1259. else
  1260. {
  1261. // Check if the line ends in the middle of a word and adjust accordingly
  1262. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1263. {
  1264. // Find the last space within the current line to avoid breaking a word
  1265. char *last_space = strrchr(line, ' ');
  1266. if (last_space != NULL)
  1267. {
  1268. // Adjust the line_length to avoid cutting the word
  1269. line_length = last_space - line;
  1270. line[line_length] = '\0'; // Null-terminate at the space
  1271. }
  1272. }
  1273. // Move the index forward by the determined line_length
  1274. i += line_length;
  1275. // Skip any spaces at the beginning of the next line
  1276. while (i < message_length && message[i] == ' ')
  1277. {
  1278. i++;
  1279. }
  1280. }
  1281. // Manually copy the fixed line into the formatted_message buffer
  1282. for (uint32_t j = 0; j < line_length; j++)
  1283. {
  1284. formatted_message[formatted_index++] = line[j];
  1285. }
  1286. // Add a newline character for line spacing
  1287. formatted_message[formatted_index++] = '\n';
  1288. }
  1289. // Null-terminate the formatted_message
  1290. formatted_message[formatted_index] = '\0';
  1291. // Add the formatted message to the widget
  1292. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1293. }
  1294. void flip_social_loader_draw_callback(Canvas *canvas, void *model)
  1295. {
  1296. if (!canvas || !model)
  1297. {
  1298. FURI_LOG_E(TAG, "flip_social_loader_draw_callback - canvas or model is NULL");
  1299. return;
  1300. }
  1301. SerialState http_state = fhttp.state;
  1302. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1303. DataState data_state = data_loader_model->data_state;
  1304. char *title = data_loader_model->title;
  1305. canvas_set_font(canvas, FontSecondary);
  1306. if (http_state == INACTIVE)
  1307. {
  1308. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1309. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1310. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1311. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1312. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1313. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1314. return;
  1315. }
  1316. if (data_state == DataStateError || data_state == DataStateParseError)
  1317. {
  1318. flip_social_request_error_draw(canvas);
  1319. return;
  1320. }
  1321. canvas_draw_str(canvas, 0, 7, title);
  1322. canvas_draw_str(canvas, 0, 17, "Loading...");
  1323. if (data_state == DataStateInitial)
  1324. {
  1325. return;
  1326. }
  1327. if (http_state == SENDING)
  1328. {
  1329. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1330. return;
  1331. }
  1332. if (http_state == RECEIVING || data_state == DataStateRequested)
  1333. {
  1334. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1335. return;
  1336. }
  1337. if (http_state == IDLE && data_state == DataStateReceived)
  1338. {
  1339. canvas_draw_str(canvas, 0, 27, "Processing...");
  1340. return;
  1341. }
  1342. if (http_state == IDLE && data_state == DataStateParsed)
  1343. {
  1344. canvas_draw_str(canvas, 0, 27, "Processed...");
  1345. return;
  1346. }
  1347. }
  1348. static void flip_social_loader_process_callback(void *context)
  1349. {
  1350. if (context == NULL)
  1351. {
  1352. FURI_LOG_E(TAG, "flip_social_loader_process_callback - context is NULL");
  1353. DEV_CRASH();
  1354. return;
  1355. }
  1356. FlipSocialApp *app = (FlipSocialApp *)context;
  1357. View *view = app->view_loader;
  1358. DataState current_data_state;
  1359. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1360. if (current_data_state == DataStateInitial)
  1361. {
  1362. with_view_model(
  1363. view,
  1364. DataLoaderModel * model,
  1365. {
  1366. model->data_state = DataStateRequested;
  1367. DataLoaderFetch fetch = model->fetcher;
  1368. if (fetch == NULL)
  1369. {
  1370. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1371. model->data_state = DataStateError;
  1372. return;
  1373. }
  1374. // Clear any previous responses
  1375. strncpy(fhttp.last_response, "", 1);
  1376. bool request_status = fetch(model);
  1377. if (!request_status)
  1378. {
  1379. model->data_state = DataStateError;
  1380. }
  1381. },
  1382. true);
  1383. }
  1384. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1385. {
  1386. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1387. {
  1388. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1389. {
  1390. FURI_LOG_DEV(TAG, "PONG received.");
  1391. }
  1392. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1393. {
  1394. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1395. }
  1396. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1397. {
  1398. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1399. }
  1400. else if (strlen(fhttp.last_response) == 0)
  1401. {
  1402. // Still waiting on response
  1403. }
  1404. else
  1405. {
  1406. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1407. }
  1408. }
  1409. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1410. {
  1411. // continue waiting
  1412. }
  1413. else if (fhttp.state == INACTIVE)
  1414. {
  1415. // inactive. try again
  1416. }
  1417. else if (fhttp.state == ISSUE)
  1418. {
  1419. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1420. }
  1421. else
  1422. {
  1423. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1424. DEV_CRASH();
  1425. }
  1426. }
  1427. else if (current_data_state == DataStateReceived)
  1428. {
  1429. with_view_model(
  1430. view,
  1431. DataLoaderModel * model,
  1432. {
  1433. char *data_text;
  1434. if (model->parser == NULL)
  1435. {
  1436. data_text = NULL;
  1437. FURI_LOG_DEV(TAG, "Parser is NULL");
  1438. DEV_CRASH();
  1439. }
  1440. else
  1441. {
  1442. data_text = model->parser(model);
  1443. }
  1444. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1445. model->data_text = data_text;
  1446. if (data_text == NULL)
  1447. {
  1448. model->data_state = DataStateParseError;
  1449. }
  1450. else
  1451. {
  1452. model->data_state = DataStateParsed;
  1453. }
  1454. },
  1455. true);
  1456. }
  1457. else if (current_data_state == DataStateParsed)
  1458. {
  1459. with_view_model(
  1460. view,
  1461. DataLoaderModel * model,
  1462. {
  1463. if (++model->request_index < model->request_count)
  1464. {
  1465. model->data_state = DataStateInitial;
  1466. }
  1467. else
  1468. {
  1469. flip_social_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app_instance->widget_result);
  1470. if (model->data_text != NULL)
  1471. {
  1472. free(model->data_text);
  1473. model->data_text = NULL;
  1474. }
  1475. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  1476. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
  1477. }
  1478. },
  1479. true);
  1480. }
  1481. }
  1482. static void flip_social_loader_timer_callback(void *context)
  1483. {
  1484. if (context == NULL)
  1485. {
  1486. FURI_LOG_E(TAG, "flip_social_loader_timer_callback - context is NULL");
  1487. DEV_CRASH();
  1488. return;
  1489. }
  1490. FlipSocialApp *app = (FlipSocialApp *)context;
  1491. view_dispatcher_send_custom_event(app->view_dispatcher, FlipSocialCustomEventProcess);
  1492. }
  1493. static void flip_social_loader_on_enter(void *context)
  1494. {
  1495. if (context == NULL)
  1496. {
  1497. FURI_LOG_E(TAG, "flip_social_loader_on_enter - context is NULL");
  1498. DEV_CRASH();
  1499. return;
  1500. }
  1501. FlipSocialApp *app = (FlipSocialApp *)context;
  1502. View *view = app->view_loader;
  1503. with_view_model(
  1504. view,
  1505. DataLoaderModel * model,
  1506. {
  1507. view_set_previous_callback(view, model->back_callback);
  1508. if (model->timer == NULL)
  1509. {
  1510. model->timer = furi_timer_alloc(flip_social_loader_timer_callback, FuriTimerTypePeriodic, app);
  1511. }
  1512. furi_timer_start(model->timer, 250);
  1513. },
  1514. true);
  1515. }
  1516. static void flip_social_loader_on_exit(void *context)
  1517. {
  1518. if (context == NULL)
  1519. {
  1520. FURI_LOG_E(TAG, "flip_social_loader_on_exit - context is NULL");
  1521. DEV_CRASH();
  1522. return;
  1523. }
  1524. FlipSocialApp *app = (FlipSocialApp *)context;
  1525. View *view = app->view_loader;
  1526. with_view_model(
  1527. view,
  1528. DataLoaderModel * model,
  1529. {
  1530. if (model->timer)
  1531. {
  1532. furi_timer_stop(model->timer);
  1533. }
  1534. },
  1535. false);
  1536. }
  1537. void flip_social_loader_init(View *view)
  1538. {
  1539. if (view == NULL)
  1540. {
  1541. FURI_LOG_E(TAG, "flip_social_loader_init - view is NULL");
  1542. DEV_CRASH();
  1543. return;
  1544. }
  1545. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1546. view_set_enter_callback(view, flip_social_loader_on_enter);
  1547. view_set_exit_callback(view, flip_social_loader_on_exit);
  1548. }
  1549. void flip_social_loader_free_model(View *view)
  1550. {
  1551. if (view == NULL)
  1552. {
  1553. FURI_LOG_E(TAG, "flip_social_loader_free_model - view is NULL");
  1554. DEV_CRASH();
  1555. return;
  1556. }
  1557. with_view_model(
  1558. view,
  1559. DataLoaderModel * model,
  1560. {
  1561. if (model->timer)
  1562. {
  1563. furi_timer_free(model->timer);
  1564. model->timer = NULL;
  1565. }
  1566. if (model->parser_context)
  1567. {
  1568. free(model->parser_context);
  1569. model->parser_context = NULL;
  1570. }
  1571. },
  1572. false);
  1573. }
  1574. bool flip_social_custom_event_callback(void *context, uint32_t index)
  1575. {
  1576. if (context == NULL)
  1577. {
  1578. FURI_LOG_E(TAG, "flip_social_custom_event_callback - context is NULL");
  1579. DEV_CRASH();
  1580. return false;
  1581. }
  1582. switch (index)
  1583. {
  1584. case FlipSocialCustomEventProcess:
  1585. flip_social_loader_process_callback(context);
  1586. return true;
  1587. default:
  1588. FURI_LOG_DEV(TAG, "flip_social_custom_event_callback. Unknown index: %ld", index);
  1589. return false;
  1590. }
  1591. }
  1592. void flip_social_generic_switch_to_view(FlipSocialApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1593. {
  1594. if (app == NULL)
  1595. {
  1596. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - app is NULL");
  1597. DEV_CRASH();
  1598. return;
  1599. }
  1600. View *view = app->view_loader;
  1601. if (view == NULL)
  1602. {
  1603. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - view is NULL");
  1604. DEV_CRASH();
  1605. return;
  1606. }
  1607. with_view_model(
  1608. view,
  1609. DataLoaderModel * model,
  1610. {
  1611. model->title = title;
  1612. model->fetcher = fetcher;
  1613. model->parser = parser;
  1614. model->request_index = 0;
  1615. model->request_count = request_count;
  1616. model->back_callback = back;
  1617. model->data_state = DataStateInitial;
  1618. model->data_text = NULL;
  1619. },
  1620. true);
  1621. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1622. }