flip_social_callback.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  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. static bool about_widget_alloc(bool is_logged_in)
  12. {
  13. if (!is_logged_in)
  14. {
  15. if (!app_instance->widget_logged_out_about)
  16. {
  17. return easy_flipper_set_widget(&app_instance->widget_logged_out_about, FlipSocialViewLoggedOutAbout, "Welcome to FlipSocial\n---\nThe social media app for\nFlipper Zero, created by\nJBlanked: www.flipsocial.net\n---\nPress BACK to return.", flip_social_callback_to_submenu_logged_out, &app_instance->view_dispatcher);
  18. }
  19. }
  20. else
  21. {
  22. if (!app_instance->widget_logged_in_about)
  23. {
  24. return easy_flipper_set_widget(&app_instance->widget_logged_in_about, FlipSocialViewLoggedInSettingsAbout, "Welcome to FlipSocial\n---\nThe social media app for\nFlipper Zero, created by\nJBlanked: www.flipsocial.net\n---\nPress BACK to return.", flip_social_callback_to_settings_logged_in, &app_instance->view_dispatcher);
  25. }
  26. }
  27. return true;
  28. }
  29. static void free_about_widget(bool is_logged_in)
  30. {
  31. if (is_logged_in && app_instance->widget_logged_in_about)
  32. {
  33. widget_free(app_instance->widget_logged_in_about);
  34. app_instance->widget_logged_in_about = NULL;
  35. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  36. }
  37. if (!is_logged_in && app_instance->widget_logged_out_about)
  38. {
  39. widget_free(app_instance->widget_logged_out_about);
  40. app_instance->widget_logged_out_about = NULL;
  41. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedOutAbout);
  42. }
  43. }
  44. static bool pre_saved_messages_alloc(void)
  45. {
  46. if (!app_instance)
  47. {
  48. return false;
  49. }
  50. if (!app_instance->submenu_compose)
  51. {
  52. if (!easy_flipper_set_submenu(&app_instance->submenu_compose, FlipSocialViewLoggedInCompose, "Create A Post", flip_social_callback_to_submenu_logged_in, &app_instance->view_dispatcher))
  53. {
  54. return false;
  55. }
  56. submenu_reset(app_instance->submenu_compose);
  57. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app_instance);
  58. // load the playlist
  59. if (load_playlist(&app_instance->pre_saved_messages))
  60. {
  61. // Update the playlist submenu
  62. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  63. {
  64. if (app_instance->pre_saved_messages.messages[i])
  65. {
  66. submenu_add_item(app_instance->submenu_compose, app_instance->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  67. }
  68. }
  69. }
  70. }
  71. return true;
  72. }
  73. static void free_pre_saved_messages(void)
  74. {
  75. if (app_instance->submenu_compose)
  76. {
  77. submenu_free(app_instance->submenu_compose);
  78. app_instance->submenu_compose = NULL;
  79. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
  80. }
  81. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  82. {
  83. if (app_instance->pre_saved_messages.messages[i])
  84. {
  85. free(app_instance->pre_saved_messages.messages[i]);
  86. app_instance->pre_saved_messages.messages[i] = NULL;
  87. }
  88. }
  89. }
  90. static void flip_social_free_friends(void)
  91. {
  92. if (!flip_social_friends)
  93. {
  94. return;
  95. }
  96. free(flip_social_friends);
  97. flip_social_friends = NULL;
  98. if (app_instance->submenu_friends)
  99. {
  100. submenu_free(app_instance->submenu_friends);
  101. app_instance->submenu_friends = NULL;
  102. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  103. }
  104. }
  105. void flip_social_request_error_draw(Canvas *canvas)
  106. {
  107. if (canvas == NULL)
  108. {
  109. FURI_LOG_E(TAG, "flip_social_request_error_draw - canvas is NULL");
  110. DEV_CRASH();
  111. return;
  112. }
  113. if (fhttp.last_response != NULL)
  114. {
  115. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  116. {
  117. canvas_clear(canvas);
  118. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  119. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  120. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  121. }
  122. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  123. {
  124. canvas_clear(canvas);
  125. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  126. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  127. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  128. }
  129. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  130. {
  131. canvas_clear(canvas);
  132. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  133. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  134. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  135. }
  136. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  137. {
  138. canvas_clear(canvas);
  139. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  140. }
  141. else
  142. {
  143. canvas_clear(canvas);
  144. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  145. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  146. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  147. }
  148. }
  149. else
  150. {
  151. canvas_clear(canvas);
  152. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  153. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  154. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  155. }
  156. }
  157. static bool flip_social_login_fetch(DataLoaderModel *model)
  158. {
  159. UNUSED(model);
  160. 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)
  161. {
  162. return false;
  163. }
  164. char buffer[256];
  165. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  166. auth_headers_alloc();
  167. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", auth_headers, buffer))
  168. {
  169. fhttp.state = RECEIVING;
  170. return true;
  171. }
  172. else
  173. {
  174. fhttp.state = ISSUE;
  175. return false;
  176. }
  177. }
  178. static char *flip_social_login_parse(DataLoaderModel *model)
  179. {
  180. UNUSED(model);
  181. // read response
  182. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  183. {
  184. app_instance->is_logged_in = "true";
  185. // set the logged_in_username and change_password_logged_in
  186. if (app_instance->login_username_logged_out)
  187. {
  188. strcpy(app_instance->login_username_logged_in, app_instance->login_username_logged_out);
  189. }
  190. if (app_instance->login_password_logged_out)
  191. {
  192. strcpy(app_instance->change_password_logged_in, app_instance->login_password_logged_out);
  193. }
  194. 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);
  195. // send user to the logged in submenu
  196. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  197. return "Login successful!";
  198. }
  199. else if (strstr(fhttp.last_response, "User not found") != NULL)
  200. {
  201. return "Account not found...";
  202. }
  203. else
  204. {
  205. return "Failed to login...";
  206. }
  207. }
  208. static void flip_social_login_switch_to_view(FlipSocialApp *app)
  209. {
  210. 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);
  211. }
  212. static bool flip_social_register_fetch(DataLoaderModel *model)
  213. {
  214. UNUSED(model);
  215. // check if the username and password are valid
  216. 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)
  217. {
  218. FURI_LOG_E(TAG, "Username or password is NULL");
  219. return false;
  220. }
  221. // check if both passwords match
  222. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  223. {
  224. FURI_LOG_E(TAG, "Passwords do not match");
  225. return false;
  226. }
  227. char buffer[128];
  228. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  229. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", buffer))
  230. {
  231. // Set the state to RECEIVING to ensure we continue to see the receiving message
  232. fhttp.state = RECEIVING;
  233. return true;
  234. }
  235. else
  236. {
  237. fhttp.state = ISSUE;
  238. return false;
  239. }
  240. }
  241. static char *flip_social_register_parse(DataLoaderModel *model)
  242. {
  243. UNUSED(model);
  244. // read response
  245. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  246. {
  247. // set the login credentials
  248. if (app_instance->login_username_logged_out)
  249. {
  250. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  251. }
  252. if (app_instance->login_password_logged_out)
  253. {
  254. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  255. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  256. }
  257. if (app_instance->login_username_logged_in)
  258. {
  259. app_instance->login_username_logged_in = app_instance->register_username_logged_out;
  260. }
  261. app_instance->is_logged_in = "true";
  262. // update header credentials
  263. auth_headers_alloc();
  264. // save the credentials
  265. 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);
  266. // send user to the logged in submenu
  267. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  268. return "Registration successful!\nWelcome to FlipSocial!";
  269. }
  270. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  271. {
  272. return "Please enter your credentials.\nPress BACK to return.";
  273. }
  274. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  275. {
  276. return "Registration failed...\nUsername already exists.\nPress BACK to return.";
  277. }
  278. else
  279. {
  280. return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
  281. }
  282. }
  283. static void flip_social_register_switch_to_view(FlipSocialApp *app)
  284. {
  285. 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);
  286. }
  287. /**
  288. * @brief Navigation callback to go back to the submenu Logged out.
  289. * @param context The context - unused
  290. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  291. */
  292. uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  293. {
  294. UNUSED(context);
  295. return FlipSocialViewLoggedOutSubmenu;
  296. }
  297. static void flip_social_free_explore_dialog()
  298. {
  299. if (app_instance->dialog_explore)
  300. {
  301. dialog_ex_free(app_instance->dialog_explore);
  302. app_instance->dialog_explore = NULL;
  303. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewExploreDialog);
  304. }
  305. }
  306. static void flip_social_free_friends_dialog()
  307. {
  308. if (app_instance->dialog_friends)
  309. {
  310. dialog_ex_free(app_instance->dialog_friends);
  311. app_instance->dialog_friends = NULL;
  312. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewFriendsDialog);
  313. }
  314. }
  315. static void flip_social_free_messages_dialog()
  316. {
  317. if (app_instance->dialog_messages)
  318. {
  319. dialog_ex_free(app_instance->dialog_messages);
  320. app_instance->dialog_messages = NULL;
  321. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewMessagesDialog);
  322. return;
  323. }
  324. }
  325. static void flip_social_free_compose_dialog()
  326. {
  327. if (app_instance->dialog_compose)
  328. {
  329. dialog_ex_free(app_instance->dialog_compose);
  330. app_instance->dialog_compose = NULL;
  331. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewComposeDialog);
  332. }
  333. }
  334. static void flip_social_free_feed_dialog()
  335. {
  336. if (app_instance->dialog_feed)
  337. {
  338. dialog_ex_free(app_instance->dialog_feed);
  339. app_instance->dialog_feed = NULL;
  340. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewFeedDialog);
  341. }
  342. }
  343. /**
  344. * @brief Navigation callback to go back to the submenu Logged in.
  345. * @param context The context - unused
  346. * @return next view id (FlipSocialViewLoggedInSubmenu)
  347. */
  348. uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  349. {
  350. UNUSED(context);
  351. free_about_widget(false);
  352. //
  353. flip_social_free_friends();
  354. flip_social_free_message_users();
  355. flip_feed_info_free();
  356. if (flip_feed_item)
  357. {
  358. free(flip_feed_item);
  359. flip_feed_item = NULL;
  360. }
  361. // free the about widget if it exists
  362. free_about_widget(true);
  363. flip_social_free_explore_dialog();
  364. flip_social_free_friends_dialog();
  365. flip_social_free_messages_dialog();
  366. flip_social_free_compose_dialog();
  367. return FlipSocialViewLoggedInSubmenu;
  368. }
  369. /**
  370. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  371. * @param context The context - unused
  372. * @return next view id (FlipSocialViewLoggedOutLogin)
  373. */
  374. uint32_t flip_social_callback_to_login_logged_out(void *context)
  375. {
  376. UNUSED(context);
  377. flip_social_sent_login_request = false;
  378. flip_social_login_success = false;
  379. return FlipSocialViewLoggedOutLogin;
  380. }
  381. /**
  382. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  383. * @param context The context - unused
  384. * @return next view id (FlipSocialViewLoggedOutRegister)
  385. */
  386. uint32_t flip_social_callback_to_register_logged_out(void *context)
  387. {
  388. UNUSED(context);
  389. flip_social_sent_register_request = false;
  390. flip_social_register_success = false;
  391. return FlipSocialViewLoggedOutRegister;
  392. }
  393. /**
  394. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  395. * @param context The context - unused
  396. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  397. */
  398. uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  399. {
  400. UNUSED(context);
  401. return FlipSocialViewLoggedOutWifiSettings;
  402. }
  403. /**
  404. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  405. * @param context The context - unused
  406. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  407. */
  408. uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  409. {
  410. UNUSED(context);
  411. return FlipSocialViewLoggedInSettingsWifi;
  412. }
  413. /**
  414. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  415. * @param context The context - unused
  416. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  417. */
  418. uint32_t flip_social_callback_to_settings_logged_in(void *context)
  419. {
  420. UNUSED(context);
  421. return FlipSocialViewLoggedInSettings;
  422. }
  423. /**
  424. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  425. * @param context The context - unused
  426. * @return next view id (FlipSocialViewLoggedInCompose)
  427. */
  428. uint32_t flip_social_callback_to_compose_logged_in(void *context)
  429. {
  430. UNUSED(context);
  431. return FlipSocialViewLoggedInCompose;
  432. }
  433. /**
  434. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  435. * @param context The context - unused
  436. * @return next view id (FlipSocialViewLoggedInProfile)
  437. */
  438. uint32_t flip_social_callback_to_profile_logged_in(void *context)
  439. {
  440. UNUSED(context);
  441. return FlipSocialViewLoggedInProfile;
  442. }
  443. /**
  444. * @brief Navigation callback to bring the user back to the Explore submenu
  445. * @param context The context - unused
  446. * @return next view id (FlipSocialViewLoggedInExploreSubmenu)
  447. */
  448. uint32_t flip_social_callback_to_explore_logged_in(void *context)
  449. {
  450. UNUSED(context);
  451. flip_social_dialog_stop = false;
  452. last_explore_response = "";
  453. flip_social_dialog_shown = false;
  454. if (flip_social_explore)
  455. {
  456. flip_social_explore->index = 0;
  457. }
  458. return FlipSocialViewLoggedInExploreSubmenu;
  459. }
  460. /**
  461. * @brief Navigation callback to bring the user back to the Friends submenu
  462. * @param context The context - unused
  463. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  464. */
  465. uint32_t flip_social_callback_to_friends_logged_in(void *context)
  466. {
  467. UNUSED(context);
  468. flip_social_dialog_stop = false;
  469. last_explore_response = "";
  470. flip_social_dialog_shown = false;
  471. flip_social_friends->index = 0;
  472. return FlipSocialViewLoggedInFriendsSubmenu;
  473. }
  474. /**
  475. * @brief Navigation callback to bring the user back to the Messages submenu
  476. * @param context The context - unused
  477. * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
  478. */
  479. uint32_t flip_social_callback_to_messages_logged_in(void *context)
  480. {
  481. UNUSED(context);
  482. return FlipSocialViewLoggedInMessagesSubmenu;
  483. }
  484. /**
  485. * @brief Navigation callback to bring the user back to the User Choices screen
  486. * @param context The context - unused
  487. * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
  488. */
  489. uint32_t flip_social_callback_to_messages_user_choices(void *context)
  490. {
  491. UNUSED(context);
  492. return FlipSocialViewLoggedInMessagesUserChoices;
  493. }
  494. static void free_flip_social_group()
  495. {
  496. flip_social_free_messages();
  497. flip_social_free_explore();
  498. flip_social_free_feed_dialog();
  499. }
  500. /**
  501. * @brief Navigation callback for exiting the application
  502. * @param context The context - unused
  503. * @return next view id (VIEW_NONE to exit the app)
  504. */
  505. uint32_t flip_social_callback_exit_app(void *context)
  506. {
  507. // Exit the application
  508. UNUSED(context);
  509. free_flip_social_group();
  510. free_pre_saved_messages();
  511. return VIEW_NONE; // Return VIEW_NONE to exit the app
  512. }
  513. static void explore_dialog_callback(DialogExResult result, void *context)
  514. {
  515. furi_assert(context);
  516. FlipSocialApp *app = (FlipSocialApp *)context;
  517. if (result == DialogExResultLeft) // Remove
  518. {
  519. // remove friend
  520. char remove_payload[128];
  521. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  522. auth_headers_alloc();
  523. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
  524. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  525. flip_social_free_explore_dialog();
  526. }
  527. else if (result == DialogExResultRight)
  528. {
  529. // add friend
  530. char add_payload[128];
  531. snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  532. auth_headers_alloc();
  533. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/add-friend/", auth_headers, add_payload);
  534. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  535. flip_social_free_explore_dialog();
  536. }
  537. }
  538. static void friends_dialog_callback(DialogExResult result, void *context)
  539. {
  540. furi_assert(context);
  541. FlipSocialApp *app = (FlipSocialApp *)context;
  542. if (result == DialogExResultLeft) // Remove
  543. {
  544. // remove friend
  545. char remove_payload[128];
  546. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
  547. auth_headers_alloc();
  548. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
  549. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  550. flip_social_free_friends_dialog();
  551. }
  552. }
  553. static void messages_dialog_callback(DialogExResult result, void *context)
  554. {
  555. furi_assert(context);
  556. FlipSocialApp *app = (FlipSocialApp *)context;
  557. if (result == DialogExResultLeft) // Previous message
  558. {
  559. if (flip_social_messages->index > 0)
  560. {
  561. flip_social_messages->index--;
  562. dialog_ex_reset(app->dialog_messages);
  563. dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
  564. dialog_ex_set_text(app->dialog_messages, flip_social_messages->messages[flip_social_messages->index], 0, 10, AlignLeft, AlignTop);
  565. if (flip_social_messages->index != 0)
  566. {
  567. dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
  568. }
  569. dialog_ex_set_right_button_text(app->dialog_messages, "Next");
  570. dialog_ex_set_center_button_text(app->dialog_messages, "Create");
  571. // switch view, free dialog, re-alloc dialog, switch back to dialog
  572. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  573. flip_social_free_messages_dialog();
  574. messages_dialog_alloc(false);
  575. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
  576. }
  577. }
  578. else if (result == DialogExResultRight) // Next message
  579. {
  580. if (flip_social_messages->index < flip_social_messages->count - 1)
  581. {
  582. flip_social_messages->index++;
  583. dialog_ex_reset(app->dialog_messages);
  584. dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
  585. dialog_ex_set_text(app->dialog_messages, flip_social_messages->messages[flip_social_messages->index], 0, 10, AlignLeft, AlignTop);
  586. dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
  587. if (flip_social_messages->index != flip_social_messages->count - 1)
  588. {
  589. dialog_ex_set_right_button_text(app->dialog_messages, "Next");
  590. }
  591. dialog_ex_set_center_button_text(app->dialog_messages, "Create");
  592. // switch view, free dialog, re-alloc dialog, switch back to dialog
  593. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  594. flip_social_free_messages_dialog();
  595. messages_dialog_alloc(false);
  596. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
  597. }
  598. }
  599. else if (result == DialogExResultCenter) // new message
  600. {
  601. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  602. }
  603. }
  604. bool messages_dialog_alloc(bool free_first)
  605. {
  606. if (free_first)
  607. {
  608. flip_social_free_messages_dialog();
  609. }
  610. if (!app_instance->dialog_messages)
  611. {
  612. if (!easy_flipper_set_dialog_ex(
  613. &app_instance->dialog_messages,
  614. FlipSocialViewMessagesDialog,
  615. flip_social_messages->usernames[flip_social_messages->index],
  616. 0,
  617. 0,
  618. flip_social_messages->messages[flip_social_messages->index],
  619. 0,
  620. 10,
  621. flip_social_messages->index != 0 ? "Prev" : NULL,
  622. flip_social_messages->index != flip_social_messages->count - 1 ? "Next" : NULL,
  623. "Create",
  624. messages_dialog_callback,
  625. flip_social_callback_to_messages_logged_in,
  626. &app_instance->view_dispatcher,
  627. app_instance))
  628. {
  629. return false;
  630. }
  631. return true;
  632. }
  633. return false;
  634. }
  635. static void compose_dialog_callback(DialogExResult result, void *context)
  636. {
  637. furi_assert(context);
  638. FlipSocialApp *app = (FlipSocialApp *)context;
  639. if (result == DialogExResultLeft) // Delete
  640. {
  641. // delete message
  642. app->pre_saved_messages.messages[app_instance->pre_saved_messages.index] = NULL;
  643. for (uint32_t i = app->pre_saved_messages.index; i < app->pre_saved_messages.count - 1; i++)
  644. {
  645. app->pre_saved_messages.messages[i] = app->pre_saved_messages.messages[i + 1];
  646. }
  647. app->pre_saved_messages.count--;
  648. // add the item to the submenu
  649. submenu_reset(app_instance->submenu_compose);
  650. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  651. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  652. {
  653. submenu_add_item(app_instance->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  654. }
  655. // save playlist
  656. save_playlist(&app_instance->pre_saved_messages);
  657. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  658. flip_social_free_compose_dialog();
  659. }
  660. else if (result == DialogExResultRight) // Post
  661. {
  662. // post the message
  663. // send selected_message
  664. if (selected_message && app_instance->login_username_logged_in)
  665. {
  666. if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
  667. {
  668. FURI_LOG_E(TAG, "Message is too long");
  669. return;
  670. }
  671. // Send the selected_message
  672. char command[256];
  673. snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
  674. app_instance->login_username_logged_in, selected_message);
  675. if (!flipper_http_post_request_with_headers(
  676. "https://www.flipsocial.net/api/feed/post/",
  677. auth_headers,
  678. command))
  679. {
  680. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  681. fhttp.state = ISSUE;
  682. return;
  683. }
  684. fhttp.state = RECEIVING;
  685. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  686. }
  687. else
  688. {
  689. FURI_LOG_E(TAG, "Message or username is NULL");
  690. return;
  691. }
  692. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  693. {
  694. furi_delay_ms(100);
  695. }
  696. if (flip_social_load_initial_feed())
  697. {
  698. flip_social_free_compose_dialog();
  699. }
  700. FURI_LOG_E(TAG, "Failed to load initial feed");
  701. return;
  702. }
  703. }
  704. static void feed_dialog_callback(DialogExResult result, void *context)
  705. {
  706. furi_assert(context);
  707. FlipSocialApp *app = (FlipSocialApp *)context;
  708. if (result == DialogExResultLeft) // Previous message
  709. {
  710. if (flip_feed_info->index > 0)
  711. {
  712. flip_feed_info->index--;
  713. }
  714. // switch view, free dialog, re-alloc dialog, switch back to dialog
  715. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  716. // load feed item
  717. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  718. {
  719. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  720. fhttp.state = ISSUE;
  721. return;
  722. }
  723. if (feed_dialog_alloc())
  724. {
  725. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
  726. }
  727. else
  728. {
  729. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  730. fhttp.state = ISSUE;
  731. return;
  732. }
  733. }
  734. else if (result == DialogExResultRight) // Next message
  735. {
  736. if (flip_feed_info->index < flip_feed_info->count - 1)
  737. {
  738. flip_feed_info->index++;
  739. }
  740. // switch view, free dialog, re-alloc dialog, switch back to dialog
  741. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  742. // load feed item
  743. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  744. {
  745. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  746. fhttp.state = ISSUE;
  747. return;
  748. }
  749. if (feed_dialog_alloc())
  750. {
  751. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
  752. }
  753. else
  754. {
  755. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  756. fhttp.state = ISSUE;
  757. return;
  758. }
  759. }
  760. else if (result == DialogExResultCenter) // Flip/Unflip
  761. {
  762. // Moved to above the is_flipped check
  763. if (!flip_feed_item->is_flipped)
  764. {
  765. // increase the flip count
  766. flip_feed_item->flips++;
  767. }
  768. else
  769. {
  770. // decrease the flip count
  771. flip_feed_item->flips--;
  772. }
  773. // change the flip status
  774. flip_feed_item->is_flipped = !flip_feed_item->is_flipped;
  775. // send post request to flip the message
  776. if (app_instance->login_username_logged_in == NULL)
  777. {
  778. FURI_LOG_E(TAG, "Username is NULL");
  779. return;
  780. }
  781. char payload[256];
  782. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%u\"}", app_instance->login_username_logged_in, flip_feed_item->id);
  783. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", auth_headers, payload))
  784. {
  785. // save feed item
  786. char new_save[256];
  787. snprintf(new_save, sizeof(new_save), "{\"id\":%u,\"username\":\"%s\",\"message\":\"%s\",\"flip_count\":%u,\"flipped\":%s}",
  788. flip_feed_item->id, flip_feed_item->username, flip_feed_item->message, flip_feed_item->flips, flip_feed_item->is_flipped ? "true" : "false");
  789. if (!flip_social_save_post((char *)flip_feed_item->id, new_save))
  790. {
  791. FURI_LOG_E(TAG, "Failed to save the feed post");
  792. fhttp.state = ISSUE;
  793. return;
  794. }
  795. }
  796. // switch view, free dialog, re-alloc dialog, switch back to dialog
  797. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  798. if (feed_dialog_alloc())
  799. {
  800. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
  801. }
  802. else
  803. {
  804. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  805. fhttp.state = ISSUE;
  806. return;
  807. }
  808. }
  809. }
  810. static char *updated_user_message(const char *user_message)
  811. {
  812. if (user_message == NULL)
  813. {
  814. FURI_LOG_E(TAG, "User message is NULL.");
  815. return NULL;
  816. }
  817. size_t msg_length = strlen(user_message);
  818. size_t start = 0;
  819. int line_num = 0;
  820. // Allocate memory for the updated message
  821. char *updated_message = malloc(MAX_MESSAGE_LENGTH + 10);
  822. if (updated_message == NULL)
  823. {
  824. FURI_LOG_E(TAG, "Failed to allocate memory for updated_message.");
  825. return NULL;
  826. }
  827. size_t current_pos = 0; // Tracks the current position in updated_message
  828. updated_message[0] = '\0'; // Initialize as empty string
  829. while (start < msg_length && line_num < 4)
  830. {
  831. size_t remaining = msg_length - start;
  832. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  833. // Adjust length to the last space if the line exceeds MAX_LINE_LENGTH
  834. if (remaining > MAX_LINE_LENGTH)
  835. {
  836. size_t last_space = len;
  837. while (last_space > 0 && user_message[start + last_space - 1] != ' ')
  838. {
  839. last_space--;
  840. }
  841. if (last_space > 0)
  842. {
  843. len = last_space; // Adjust len to the position of the last space
  844. }
  845. }
  846. // Check if the new line fits in the updated_message buffer
  847. if (current_pos + len + 1 >= (MAX_MESSAGE_LENGTH + 10))
  848. {
  849. FURI_LOG_E(TAG, "Updated message exceeds maximum length.");
  850. // break and return what we have so far
  851. break;
  852. }
  853. // Copy the line and append a newline character
  854. memcpy(updated_message + current_pos, user_message + start, len);
  855. current_pos += len;
  856. updated_message[current_pos++] = '\n'; // Append newline
  857. // Update the start position for the next line
  858. start += len;
  859. // Skip any spaces to avoid leading spaces on the next line
  860. while (start < msg_length && user_message[start] == ' ')
  861. {
  862. start++;
  863. }
  864. // Increment the line number
  865. line_num++;
  866. }
  867. // Null-terminate the final string
  868. if (current_pos < (MAX_MESSAGE_LENGTH + 10))
  869. {
  870. updated_message[current_pos] = '\0';
  871. }
  872. else
  873. {
  874. FURI_LOG_E(TAG, "Buffer overflow while null-terminating.");
  875. free(updated_message);
  876. return NULL;
  877. }
  878. return updated_message;
  879. }
  880. bool feed_dialog_alloc()
  881. {
  882. if (!flip_feed_item)
  883. {
  884. FURI_LOG_E(TAG, "Feed item is NULL");
  885. return false;
  886. }
  887. flip_social_free_feed_dialog();
  888. if (!app_instance->dialog_feed)
  889. {
  890. char updated_message[MAX_MESSAGE_LENGTH + 10];
  891. snprintf(updated_message, MAX_MESSAGE_LENGTH + 10, "%s (%u %s)", flip_feed_item->message, flip_feed_item->flips, flip_feed_item->flips == 1 ? "flip" : "flips");
  892. char *real_message = updated_user_message(updated_message);
  893. if (!real_message)
  894. {
  895. FURI_LOG_E(TAG, "Failed to update the user message");
  896. return false;
  897. }
  898. if (!easy_flipper_set_dialog_ex(
  899. &app_instance->dialog_feed,
  900. FlipSocialViewFeedDialog,
  901. flip_feed_item->username,
  902. 0,
  903. 0,
  904. updated_message,
  905. 0,
  906. 10,
  907. flip_feed_info->index != 0 ? "Prev" : NULL,
  908. flip_feed_info->index != flip_feed_info->count - 1 ? "Next" : NULL,
  909. flip_feed_item->is_flipped ? "Unflip" : "Flip",
  910. feed_dialog_callback,
  911. flip_social_callback_to_submenu_logged_in,
  912. &app_instance->view_dispatcher,
  913. app_instance))
  914. {
  915. free(real_message);
  916. return false;
  917. }
  918. free(real_message);
  919. return true;
  920. }
  921. return false;
  922. }
  923. /**
  924. * @brief Handle ALL submenu item selections.
  925. * @param context The context - FlipSocialApp object.
  926. * @param index The FlipSocialSubmenuIndex item that was clicked.
  927. * @return void
  928. */
  929. void flip_social_callback_submenu_choices(void *context, uint32_t index)
  930. {
  931. FlipSocialApp *app = (FlipSocialApp *)context;
  932. if (!app)
  933. {
  934. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  935. return;
  936. }
  937. switch (index)
  938. {
  939. case FlipSocialSubmenuLoggedOutIndexLogin:
  940. flip_social_sent_login_request = false;
  941. flip_social_login_success = false;
  942. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  943. break;
  944. case FlipSocialSubmenuLoggedOutIndexRegister:
  945. flip_social_sent_register_request = false;
  946. flip_social_register_success = false;
  947. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  948. break;
  949. case FlipSocialSubmenuLoggedOutIndexAbout:
  950. if (!about_widget_alloc(false))
  951. {
  952. return;
  953. }
  954. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  955. break;
  956. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  957. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  958. break;
  959. case FlipSocialSubmenuLoggedInIndexProfile:
  960. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  961. break;
  962. case FlipSocialSubmenuLoggedInIndexMessages:
  963. free_flip_social_group();
  964. flipper_http_loading_task(
  965. flip_social_get_message_users, // get the message users
  966. flip_social_parse_json_message_users, // parse the message users
  967. FlipSocialViewLoggedInMessagesSubmenu, // switch to the messages submenu if successful
  968. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  969. &app->view_dispatcher); // view dispatcher
  970. break;
  971. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  972. flipper_http_loading_task(
  973. flip_social_get_explore, // get the explore users
  974. flip_social_parse_json_message_user_choices, // parse the explore users
  975. FlipSocialViewLoggedInMessagesUserChoices, // switch to the user choices if successful
  976. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  977. &app->view_dispatcher); // view dispatcher
  978. break;
  979. case FlipSocialSubmenuLoggedInIndexFeed:
  980. free_flip_social_group();
  981. if (!flip_social_load_initial_feed())
  982. {
  983. FURI_LOG_E(TAG, "Failed to load the initial feed");
  984. return;
  985. }
  986. free_pre_saved_messages();
  987. break;
  988. case FlipSocialSubmenuExploreIndex:
  989. free_flip_social_group();
  990. flipper_http_loading_task(
  991. flip_social_get_explore, // get the explore users
  992. flip_social_parse_json_explore, // parse the explore users
  993. FlipSocialViewLoggedInExploreSubmenu, // switch to the explore submenu if successful
  994. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  995. &app->view_dispatcher); // view dispatcher
  996. break;
  997. case FlipSocialSubmenuLoggedInIndexCompose:
  998. free_pre_saved_messages();
  999. if (!pre_saved_messages_alloc())
  1000. {
  1001. FURI_LOG_E(TAG, "Failed to allocate pre-saved messages");
  1002. return;
  1003. }
  1004. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1005. break;
  1006. case FlipSocialSubmenuLoggedInIndexSettings:
  1007. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  1008. break;
  1009. case FlipSocialSubmenuLoggedInSignOutButton:
  1010. free_flip_social_group();
  1011. app->is_logged_in = "false";
  1012. 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);
  1013. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  1014. break;
  1015. case FlipSocialSubmenuComposeIndexAddPreSave:
  1016. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  1017. break;
  1018. default:
  1019. // Handle the pre-saved message selection (has a max of 25 items)
  1020. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  1021. {
  1022. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  1023. snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  1024. if (!selected_message)
  1025. {
  1026. FURI_LOG_E(TAG, "Selected message is NULL");
  1027. return;
  1028. }
  1029. flip_social_free_compose_dialog();
  1030. if (!app->dialog_compose)
  1031. {
  1032. if (!easy_flipper_set_dialog_ex(
  1033. &app->dialog_compose,
  1034. FlipSocialViewComposeDialog,
  1035. "New Feed Post",
  1036. 0,
  1037. 0,
  1038. selected_message,
  1039. 0,
  1040. 10,
  1041. "Delete",
  1042. "Post",
  1043. NULL,
  1044. compose_dialog_callback,
  1045. flip_social_callback_to_compose_logged_in,
  1046. &app->view_dispatcher,
  1047. app))
  1048. {
  1049. return;
  1050. }
  1051. }
  1052. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewComposeDialog);
  1053. }
  1054. // Handle the explore selection
  1055. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  1056. {
  1057. if (!flip_social_explore)
  1058. {
  1059. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  1060. return;
  1061. }
  1062. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  1063. flip_social_free_explore_dialog();
  1064. if (!app->dialog_explore)
  1065. {
  1066. if (!easy_flipper_set_dialog_ex(
  1067. &app->dialog_explore,
  1068. FlipSocialViewExploreDialog,
  1069. "User Options",
  1070. 0,
  1071. 0,
  1072. flip_social_explore->usernames[flip_social_explore->index],
  1073. 0,
  1074. 10,
  1075. "Remove",
  1076. "Add",
  1077. NULL,
  1078. explore_dialog_callback,
  1079. flip_social_callback_to_explore_logged_in,
  1080. &app->view_dispatcher,
  1081. app))
  1082. {
  1083. return;
  1084. }
  1085. }
  1086. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  1087. }
  1088. // handle the friends selection
  1089. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  1090. {
  1091. if (!flip_social_friends)
  1092. {
  1093. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  1094. return;
  1095. }
  1096. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  1097. flip_social_free_friends_dialog();
  1098. if (!app->dialog_friends)
  1099. {
  1100. if (!easy_flipper_set_dialog_ex(
  1101. &app->dialog_friends,
  1102. FlipSocialViewFriendsDialog,
  1103. "Friend Options",
  1104. 0,
  1105. 0,
  1106. flip_social_friends->usernames[flip_social_friends->index],
  1107. 0,
  1108. 10,
  1109. "Remove",
  1110. "",
  1111. NULL,
  1112. friends_dialog_callback,
  1113. flip_social_callback_to_friends_logged_in,
  1114. &app->view_dispatcher,
  1115. app))
  1116. {
  1117. return;
  1118. }
  1119. }
  1120. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFriendsDialog);
  1121. }
  1122. // handle the messages selection
  1123. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
  1124. {
  1125. if (!flip_social_message_users)
  1126. {
  1127. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  1128. return;
  1129. }
  1130. flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  1131. flipper_http_loading_task(
  1132. flip_social_get_messages_with_user, // get the messages with the selected user
  1133. flip_social_parse_json_messages, // parse the messages
  1134. FlipSocialViewMessagesDialog, // switch to the messages process if successful
  1135. FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
  1136. &app->view_dispatcher // view dispatcher
  1137. );
  1138. }
  1139. // handle the messages user choices selection
  1140. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
  1141. {
  1142. if (!flip_social_explore)
  1143. {
  1144. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  1145. return;
  1146. }
  1147. flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  1148. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  1149. }
  1150. else
  1151. {
  1152. FURI_LOG_E(TAG, "Unknown submenu index");
  1153. }
  1154. break;
  1155. }
  1156. }
  1157. /**
  1158. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  1159. * @param context The context - FlipSocialApp object.
  1160. * @return void
  1161. */
  1162. void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  1163. {
  1164. FlipSocialApp *app = (FlipSocialApp *)context;
  1165. if (!app)
  1166. {
  1167. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1168. return;
  1169. }
  1170. // Store the entered name
  1171. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1172. // Store the entered name in the logged in name field
  1173. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1174. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1175. // Ensure null-termination
  1176. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  1177. // Update the name item text
  1178. if (app->variable_item_logged_out_wifi_settings_ssid)
  1179. {
  1180. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  1181. }
  1182. // update the wifi settings
  1183. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1184. {
  1185. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1186. {
  1187. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1188. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1189. }
  1190. }
  1191. // Save the settings
  1192. 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);
  1193. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  1194. }
  1195. /**
  1196. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  1197. * @param context The context - FlipSocialApp object.
  1198. * @return void
  1199. */
  1200. void flip_social_logged_out_wifi_settings_password_updated(void *context)
  1201. {
  1202. FlipSocialApp *app = (FlipSocialApp *)context;
  1203. if (!app)
  1204. {
  1205. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1206. return;
  1207. }
  1208. // Store the entered WiFi password
  1209. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1210. // Store the entered WiFi password in the logged in password field
  1211. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1212. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1213. // Ensure null-termination
  1214. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  1215. // Update the password item text
  1216. if (app->variable_item_logged_out_wifi_settings_password)
  1217. {
  1218. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  1219. }
  1220. // update the wifi settings
  1221. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1222. {
  1223. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1224. {
  1225. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1226. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1227. }
  1228. }
  1229. // Save the settings
  1230. 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);
  1231. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  1232. }
  1233. /**
  1234. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  1235. * @param context The context - FlipSocialApp object.
  1236. * @param index The index of the selected item.
  1237. * @return void
  1238. */
  1239. void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  1240. {
  1241. FlipSocialApp *app = (FlipSocialApp *)context;
  1242. if (!app)
  1243. {
  1244. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1245. return;
  1246. }
  1247. switch (index)
  1248. {
  1249. case 0: // Input SSID
  1250. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  1251. break;
  1252. case 1: // Input Password
  1253. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  1254. break;
  1255. default:
  1256. FURI_LOG_E(TAG, "Unknown configuration item index");
  1257. break;
  1258. }
  1259. }
  1260. /**
  1261. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  1262. * @param context The context - FlipSocialApp object.
  1263. * @return void
  1264. */
  1265. void flip_social_logged_out_login_username_updated(void *context)
  1266. {
  1267. FlipSocialApp *app = (FlipSocialApp *)context;
  1268. if (!app)
  1269. {
  1270. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1271. return;
  1272. }
  1273. // Store the entered name
  1274. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1275. // Store the entered name in the logged in username field
  1276. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1277. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1278. // Ensure null-termination
  1279. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  1280. // Update the name item text
  1281. if (app->variable_item_logged_out_login_username)
  1282. {
  1283. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  1284. }
  1285. // Save the settings
  1286. 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);
  1287. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  1288. }
  1289. /**
  1290. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  1291. * @param context The context - FlipSocialApp object.
  1292. * @return void
  1293. */
  1294. void flip_social_logged_out_login_password_updated(void *context)
  1295. {
  1296. FlipSocialApp *app = (FlipSocialApp *)context;
  1297. if (!app)
  1298. {
  1299. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1300. return;
  1301. }
  1302. // Store the entered password
  1303. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1304. // Store the entered password in the change password field
  1305. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1306. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1307. // Ensure null-termination
  1308. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  1309. // Update the password item text
  1310. if (app->variable_item_logged_out_login_password)
  1311. {
  1312. // dont show the password on the screen (version 0.2)
  1313. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  1314. }
  1315. // Save the settings
  1316. 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);
  1317. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  1318. }
  1319. /**
  1320. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  1321. * @param context The context - FlipSocialApp object.
  1322. * @param index The index of the selected item.
  1323. * @return void
  1324. */
  1325. void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  1326. {
  1327. FlipSocialApp *app = (FlipSocialApp *)context;
  1328. if (!app)
  1329. {
  1330. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1331. return;
  1332. }
  1333. switch (index)
  1334. {
  1335. case 0: // Input Username
  1336. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  1337. break;
  1338. case 1: // Input Password
  1339. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  1340. break;
  1341. case 2: // Login Button
  1342. flip_social_login_switch_to_view(app);
  1343. break;
  1344. default:
  1345. FURI_LOG_E(TAG, "Unknown configuration item index");
  1346. break;
  1347. }
  1348. }
  1349. /**
  1350. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  1351. * @param context The context - FlipSocialApp object.
  1352. * @return void
  1353. */
  1354. void flip_social_logged_out_register_username_updated(void *context)
  1355. {
  1356. FlipSocialApp *app = (FlipSocialApp *)context;
  1357. if (!app)
  1358. {
  1359. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1360. return;
  1361. }
  1362. // Store the entered name
  1363. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  1364. // Ensure null-termination
  1365. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  1366. // Update the name item text
  1367. if (app->variable_item_logged_out_register_username)
  1368. {
  1369. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  1370. }
  1371. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1372. }
  1373. /**
  1374. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  1375. * @param context The context - FlipSocialApp object.
  1376. * @return void
  1377. */
  1378. void flip_social_logged_out_register_password_updated(void *context)
  1379. {
  1380. FlipSocialApp *app = (FlipSocialApp *)context;
  1381. if (!app)
  1382. {
  1383. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1384. return;
  1385. }
  1386. // Store the entered password
  1387. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  1388. // Ensure null-termination
  1389. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  1390. // Update the password item text
  1391. if (app->variable_item_logged_out_register_password)
  1392. {
  1393. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  1394. }
  1395. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1396. }
  1397. /**
  1398. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  1399. * @param context The context - FlipSocialApp object.
  1400. * @return void
  1401. */
  1402. void flip_social_logged_out_register_password_2_updated(void *context)
  1403. {
  1404. FlipSocialApp *app = (FlipSocialApp *)context;
  1405. if (!app)
  1406. {
  1407. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1408. return;
  1409. }
  1410. // Store the entered password
  1411. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  1412. // Ensure null-termination
  1413. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  1414. // Update the password item text
  1415. if (app->variable_item_logged_out_register_password_2)
  1416. {
  1417. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  1418. }
  1419. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1420. }
  1421. /**
  1422. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  1423. * @param context The context - FlipSocialApp object.
  1424. * @param index The index of the selected item.
  1425. * @return void
  1426. */
  1427. void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  1428. {
  1429. FlipSocialApp *app = (FlipSocialApp *)context;
  1430. if (!app)
  1431. {
  1432. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1433. return;
  1434. }
  1435. switch (index)
  1436. {
  1437. case 0: // Input Username
  1438. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  1439. break;
  1440. case 1: // Input Password
  1441. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  1442. break;
  1443. case 2: // Input Password 2
  1444. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  1445. break;
  1446. case 3: // Register button
  1447. flip_social_register_switch_to_view(app);
  1448. break;
  1449. default:
  1450. FURI_LOG_E(TAG, "Unknown configuration item index");
  1451. break;
  1452. }
  1453. }
  1454. /**
  1455. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1456. * @param context The context - FlipSocialApp object.
  1457. * @return void
  1458. */
  1459. void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1460. {
  1461. FlipSocialApp *app = (FlipSocialApp *)context;
  1462. if (!app)
  1463. {
  1464. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1465. return;
  1466. }
  1467. // Store the entered SSID
  1468. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1469. // Store the entered SSID in the logged out SSID
  1470. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1471. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1472. // Ensure null-termination
  1473. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1474. // Update the name item text
  1475. if (app->variable_item_logged_in_wifi_settings_ssid)
  1476. {
  1477. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1478. }
  1479. // Save the settings
  1480. 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);
  1481. // update the wifi settings
  1482. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1483. {
  1484. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1485. {
  1486. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1487. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1488. }
  1489. }
  1490. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1491. }
  1492. /**
  1493. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1494. * @param context The context - FlipSocialApp object.
  1495. * @return void
  1496. */
  1497. void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1498. {
  1499. FlipSocialApp *app = (FlipSocialApp *)context;
  1500. if (!app)
  1501. {
  1502. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1503. return;
  1504. }
  1505. // Store the entered password
  1506. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1507. // Store the entered password in the logged out password
  1508. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1509. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1510. // Ensure null-termination
  1511. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1512. // Update the password item text
  1513. if (app->variable_item_logged_in_wifi_settings_password)
  1514. {
  1515. // dont show the password on the screen (version 0.2)
  1516. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1517. }
  1518. // Save the settings
  1519. 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);
  1520. // update the wifi settings
  1521. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1522. {
  1523. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1524. {
  1525. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1526. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1527. }
  1528. }
  1529. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1530. }
  1531. /**
  1532. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1533. * @param context The context - FlipSocialApp object.
  1534. * @param index The index of the selected item.
  1535. * @return void
  1536. */
  1537. void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1538. {
  1539. FlipSocialApp *app = (FlipSocialApp *)context;
  1540. if (!app)
  1541. {
  1542. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1543. return;
  1544. }
  1545. switch (index)
  1546. {
  1547. case 0: // Input SSID
  1548. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1549. break;
  1550. case 1: // Input Password
  1551. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1552. break;
  1553. default:
  1554. FURI_LOG_E(TAG, "Unknown configuration item index");
  1555. break;
  1556. }
  1557. }
  1558. /**
  1559. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1560. * @param context The context - FlipSocialApp object.
  1561. * @return void
  1562. */
  1563. void flip_social_logged_in_compose_pre_save_updated(void *context)
  1564. {
  1565. FlipSocialApp *app = (FlipSocialApp *)context;
  1566. if (!app)
  1567. {
  1568. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1569. return;
  1570. }
  1571. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  1572. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1573. {
  1574. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1575. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1576. return;
  1577. }
  1578. // Store the entered message
  1579. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  1580. // Ensure null-termination
  1581. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  1582. // add the item to the submenu
  1583. submenu_reset(app->submenu_compose);
  1584. // loop through the items and add them to the submenu
  1585. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  1586. app->pre_saved_messages.count++;
  1587. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  1588. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  1589. {
  1590. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  1591. }
  1592. // save playlist
  1593. save_playlist(&app->pre_saved_messages);
  1594. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1595. }
  1596. /**
  1597. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1598. * @param context The context - FlipSocialApp object.
  1599. * @return void
  1600. */
  1601. void flip_social_logged_in_profile_change_password_updated(void *context)
  1602. {
  1603. FlipSocialApp *app = (FlipSocialApp *)context;
  1604. if (!app)
  1605. {
  1606. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1607. return;
  1608. }
  1609. // Correct type: old_pass should be a pointer to a string (char *)
  1610. const char *old_password = app->login_password_logged_out;
  1611. // Store the entered message
  1612. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1613. // store the entered password in the logged out password
  1614. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1615. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1616. // Ensure null-termination
  1617. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1618. // Update the message item text
  1619. if (app->variable_item_logged_in_profile_change_password)
  1620. {
  1621. // dont show the password on the screen (version 0.2)
  1622. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1623. }
  1624. // send post request to change password
  1625. auth_headers_alloc();
  1626. char payload[256];
  1627. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1628. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
  1629. {
  1630. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1631. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1632. return;
  1633. }
  1634. // Save the settings
  1635. 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);
  1636. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  1637. }
  1638. /**
  1639. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1640. * @param context The context - FlipSocialApp object.
  1641. * @param index The index of the selected item.
  1642. * @return void
  1643. */
  1644. void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1645. {
  1646. FlipSocialApp *app = (FlipSocialApp *)context;
  1647. if (!app)
  1648. {
  1649. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1650. return;
  1651. }
  1652. switch (index)
  1653. {
  1654. case 0: // Change Username
  1655. // do nothing since username cannot be changed
  1656. break;
  1657. case 1: // Change Password
  1658. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1659. break;
  1660. case 2: // Friends
  1661. flip_social_free_friends();
  1662. free_flip_social_group();
  1663. if (!app->submenu_friends)
  1664. {
  1665. if (!easy_flipper_set_submenu(&app->submenu_friends, FlipSocialViewLoggedInFriendsSubmenu, "Friends", flip_social_callback_to_profile_logged_in, &app->view_dispatcher))
  1666. {
  1667. FURI_LOG_DEV(TAG, "Failed to set submenu for friends");
  1668. return;
  1669. }
  1670. }
  1671. flipper_http_loading_task(flip_social_get_friends, flip_social_parse_json_friends, FlipSocialViewLoggedInFriendsSubmenu, FlipSocialViewLoggedInProfile, &app->view_dispatcher);
  1672. break;
  1673. default:
  1674. FURI_LOG_E(TAG, "Unknown configuration item index");
  1675. break;
  1676. }
  1677. }
  1678. /**
  1679. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  1680. * @param context The context - FlipSocialApp object.
  1681. * @param index The index of the selected item.
  1682. * @return void
  1683. */
  1684. void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  1685. {
  1686. FlipSocialApp *app = (FlipSocialApp *)context;
  1687. if (!app)
  1688. {
  1689. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1690. return;
  1691. }
  1692. switch (index)
  1693. {
  1694. case 0: // About
  1695. if (!about_widget_alloc(true))
  1696. {
  1697. return;
  1698. }
  1699. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  1700. break;
  1701. case 1: // Wifi
  1702. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1703. break;
  1704. default:
  1705. break;
  1706. }
  1707. }
  1708. /**
  1709. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  1710. * @param context The context - FlipSocialApp object.
  1711. * @return void
  1712. */
  1713. void flip_social_logged_in_messages_user_choice_message_updated(void *context)
  1714. {
  1715. FlipSocialApp *app = (FlipSocialApp *)context;
  1716. if (!app)
  1717. {
  1718. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1719. return;
  1720. }
  1721. // check if the message is empty
  1722. if (app->message_user_choice_logged_in_temp_buffer_size == 0)
  1723. {
  1724. FURI_LOG_E(TAG, "Message is empty");
  1725. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  1726. return;
  1727. }
  1728. // Store the entered message
  1729. strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
  1730. // Ensure null-termination
  1731. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
  1732. // send post request to send message
  1733. auth_headers_alloc();
  1734. char url[128];
  1735. char payload[256];
  1736. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1737. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
  1738. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1739. {
  1740. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1741. fhttp.state = RECEIVING;
  1742. }
  1743. else
  1744. {
  1745. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1746. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1747. fhttp.state = ISSUE;
  1748. return;
  1749. }
  1750. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1751. {
  1752. // Wait for the request to be received
  1753. furi_delay_ms(10);
  1754. }
  1755. furi_timer_stop(fhttp.get_timeout_timer);
  1756. // add user to the message list
  1757. 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]));
  1758. flip_social_message_users->count++;
  1759. // redraw submenu
  1760. flip_social_update_messages_submenu();
  1761. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1762. }
  1763. /**
  1764. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1765. * @param context The context - FlipSocialApp object.
  1766. * @return void
  1767. */
  1768. void flip_social_logged_in_messages_new_message_updated(void *context)
  1769. {
  1770. FlipSocialApp *app = (FlipSocialApp *)context;
  1771. if (!app)
  1772. {
  1773. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1774. return;
  1775. }
  1776. // check if the message is empty
  1777. if (app->messages_new_message_logged_in_temp_buffer_size == 0)
  1778. {
  1779. FURI_LOG_E(TAG, "Message is empty");
  1780. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1781. return;
  1782. }
  1783. // Store the entered message
  1784. strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
  1785. // Ensure null-termination
  1786. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
  1787. // send post request to send message
  1788. auth_headers_alloc();
  1789. char url[128];
  1790. char payload[256];
  1791. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1792. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
  1793. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1794. {
  1795. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1796. fhttp.state = RECEIVING;
  1797. }
  1798. else
  1799. {
  1800. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1801. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1802. fhttp.state = ISSUE;
  1803. return;
  1804. }
  1805. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1806. {
  1807. // Wait for the request to be received
  1808. furi_delay_ms(10);
  1809. }
  1810. furi_timer_stop(fhttp.get_timeout_timer);
  1811. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1812. }
  1813. static void flip_social_widget_set_text(char *message, Widget **widget)
  1814. {
  1815. if (widget == NULL)
  1816. {
  1817. FURI_LOG_E(TAG, "flip_social_set_widget_text - widget is NULL");
  1818. DEV_CRASH();
  1819. return;
  1820. }
  1821. if (message == NULL)
  1822. {
  1823. FURI_LOG_E(TAG, "flip_social_set_widget_text - message is NULL");
  1824. DEV_CRASH();
  1825. return;
  1826. }
  1827. widget_reset(*widget);
  1828. uint32_t message_length = strlen(message); // Length of the message
  1829. uint32_t i = 0; // Index tracker
  1830. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1831. char *formatted_message; // Buffer to hold the final formatted message
  1832. // Allocate buffer with double the message length plus one for safety
  1833. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1834. {
  1835. return;
  1836. }
  1837. while (i < message_length)
  1838. {
  1839. uint32_t max_line_length = 31; // Maximum characters per line
  1840. uint32_t remaining_length = message_length - i; // Remaining characters
  1841. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1842. // Check for newline character within the current segment
  1843. uint32_t newline_pos = i;
  1844. bool found_newline = false;
  1845. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1846. {
  1847. if (message[newline_pos] == '\n')
  1848. {
  1849. found_newline = true;
  1850. break;
  1851. }
  1852. }
  1853. if (found_newline)
  1854. {
  1855. // If newline found, set line_length up to the newline
  1856. line_length = newline_pos - i;
  1857. }
  1858. // Temporary buffer to hold the current line
  1859. char line[32];
  1860. strncpy(line, message + i, line_length);
  1861. line[line_length] = '\0';
  1862. // If newline was found, skip it for the next iteration
  1863. if (found_newline)
  1864. {
  1865. i += line_length + 1; // +1 to skip the '\n' character
  1866. }
  1867. else
  1868. {
  1869. // Check if the line ends in the middle of a word and adjust accordingly
  1870. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1871. {
  1872. // Find the last space within the current line to avoid breaking a word
  1873. char *last_space = strrchr(line, ' ');
  1874. if (last_space != NULL)
  1875. {
  1876. // Adjust the line_length to avoid cutting the word
  1877. line_length = last_space - line;
  1878. line[line_length] = '\0'; // Null-terminate at the space
  1879. }
  1880. }
  1881. // Move the index forward by the determined line_length
  1882. i += line_length;
  1883. // Skip any spaces at the beginning of the next line
  1884. while (i < message_length && message[i] == ' ')
  1885. {
  1886. i++;
  1887. }
  1888. }
  1889. // Manually copy the fixed line into the formatted_message buffer
  1890. for (uint32_t j = 0; j < line_length; j++)
  1891. {
  1892. formatted_message[formatted_index++] = line[j];
  1893. }
  1894. // Add a newline character for line spacing
  1895. formatted_message[formatted_index++] = '\n';
  1896. }
  1897. // Null-terminate the formatted_message
  1898. formatted_message[formatted_index] = '\0';
  1899. // Add the formatted message to the widget
  1900. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1901. }
  1902. void flip_social_loader_draw_callback(Canvas *canvas, void *model)
  1903. {
  1904. if (!canvas || !model)
  1905. {
  1906. FURI_LOG_E(TAG, "flip_social_loader_draw_callback - canvas or model is NULL");
  1907. return;
  1908. }
  1909. SerialState http_state = fhttp.state;
  1910. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1911. DataState data_state = data_loader_model->data_state;
  1912. char *title = data_loader_model->title;
  1913. canvas_set_font(canvas, FontSecondary);
  1914. if (http_state == INACTIVE)
  1915. {
  1916. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1917. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1918. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1919. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1920. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1921. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1922. return;
  1923. }
  1924. if (data_state == DataStateError || data_state == DataStateParseError)
  1925. {
  1926. flip_social_request_error_draw(canvas);
  1927. return;
  1928. }
  1929. canvas_draw_str(canvas, 0, 7, title);
  1930. canvas_draw_str(canvas, 0, 17, "Loading...");
  1931. if (data_state == DataStateInitial)
  1932. {
  1933. return;
  1934. }
  1935. if (http_state == SENDING)
  1936. {
  1937. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1938. return;
  1939. }
  1940. if (http_state == RECEIVING || data_state == DataStateRequested)
  1941. {
  1942. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1943. return;
  1944. }
  1945. if (http_state == IDLE && data_state == DataStateReceived)
  1946. {
  1947. canvas_draw_str(canvas, 0, 27, "Processing...");
  1948. return;
  1949. }
  1950. if (http_state == IDLE && data_state == DataStateParsed)
  1951. {
  1952. canvas_draw_str(canvas, 0, 27, "Processed...");
  1953. return;
  1954. }
  1955. }
  1956. static void flip_social_loader_process_callback(void *context)
  1957. {
  1958. if (context == NULL)
  1959. {
  1960. FURI_LOG_E(TAG, "flip_social_loader_process_callback - context is NULL");
  1961. DEV_CRASH();
  1962. return;
  1963. }
  1964. FlipSocialApp *app = (FlipSocialApp *)context;
  1965. View *view = app->view_loader;
  1966. DataState current_data_state;
  1967. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1968. if (current_data_state == DataStateInitial)
  1969. {
  1970. with_view_model(
  1971. view,
  1972. DataLoaderModel * model,
  1973. {
  1974. model->data_state = DataStateRequested;
  1975. DataLoaderFetch fetch = model->fetcher;
  1976. if (fetch == NULL)
  1977. {
  1978. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1979. model->data_state = DataStateError;
  1980. return;
  1981. }
  1982. // Clear any previous responses
  1983. strncpy(fhttp.last_response, "", 1);
  1984. bool request_status = fetch(model);
  1985. if (!request_status)
  1986. {
  1987. model->data_state = DataStateError;
  1988. }
  1989. },
  1990. true);
  1991. }
  1992. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1993. {
  1994. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1995. {
  1996. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1997. {
  1998. FURI_LOG_DEV(TAG, "PONG received.");
  1999. }
  2000. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  2001. {
  2002. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  2003. }
  2004. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  2005. {
  2006. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  2007. }
  2008. else if (strlen(fhttp.last_response) == 0)
  2009. {
  2010. // Still waiting on response
  2011. }
  2012. else
  2013. {
  2014. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  2015. }
  2016. }
  2017. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  2018. {
  2019. // continue waiting
  2020. }
  2021. else if (fhttp.state == INACTIVE)
  2022. {
  2023. // inactive. try again
  2024. }
  2025. else if (fhttp.state == ISSUE)
  2026. {
  2027. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  2028. }
  2029. else
  2030. {
  2031. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  2032. DEV_CRASH();
  2033. }
  2034. }
  2035. else if (current_data_state == DataStateReceived)
  2036. {
  2037. with_view_model(
  2038. view,
  2039. DataLoaderModel * model,
  2040. {
  2041. char *data_text;
  2042. if (model->parser == NULL)
  2043. {
  2044. data_text = NULL;
  2045. FURI_LOG_DEV(TAG, "Parser is NULL");
  2046. DEV_CRASH();
  2047. }
  2048. else
  2049. {
  2050. data_text = model->parser(model);
  2051. }
  2052. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  2053. model->data_text = data_text;
  2054. if (data_text == NULL)
  2055. {
  2056. model->data_state = DataStateParseError;
  2057. }
  2058. else
  2059. {
  2060. model->data_state = DataStateParsed;
  2061. }
  2062. },
  2063. true);
  2064. }
  2065. else if (current_data_state == DataStateParsed)
  2066. {
  2067. with_view_model(
  2068. view,
  2069. DataLoaderModel * model,
  2070. {
  2071. if (++model->request_index < model->request_count)
  2072. {
  2073. model->data_state = DataStateInitial;
  2074. }
  2075. else
  2076. {
  2077. flip_social_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app_instance->widget_result);
  2078. if (model->data_text != NULL)
  2079. {
  2080. free(model->data_text);
  2081. model->data_text = NULL;
  2082. }
  2083. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  2084. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
  2085. }
  2086. },
  2087. true);
  2088. }
  2089. }
  2090. static void flip_social_loader_timer_callback(void *context)
  2091. {
  2092. if (context == NULL)
  2093. {
  2094. FURI_LOG_E(TAG, "flip_social_loader_timer_callback - context is NULL");
  2095. DEV_CRASH();
  2096. return;
  2097. }
  2098. FlipSocialApp *app = (FlipSocialApp *)context;
  2099. view_dispatcher_send_custom_event(app->view_dispatcher, FlipSocialCustomEventProcess);
  2100. }
  2101. static void flip_social_loader_on_enter(void *context)
  2102. {
  2103. if (context == NULL)
  2104. {
  2105. FURI_LOG_E(TAG, "flip_social_loader_on_enter - context is NULL");
  2106. DEV_CRASH();
  2107. return;
  2108. }
  2109. FlipSocialApp *app = (FlipSocialApp *)context;
  2110. View *view = app->view_loader;
  2111. with_view_model(
  2112. view,
  2113. DataLoaderModel * model,
  2114. {
  2115. view_set_previous_callback(view, model->back_callback);
  2116. if (model->timer == NULL)
  2117. {
  2118. model->timer = furi_timer_alloc(flip_social_loader_timer_callback, FuriTimerTypePeriodic, app);
  2119. }
  2120. furi_timer_start(model->timer, 250);
  2121. },
  2122. true);
  2123. }
  2124. static void flip_social_loader_on_exit(void *context)
  2125. {
  2126. if (context == NULL)
  2127. {
  2128. FURI_LOG_E(TAG, "flip_social_loader_on_exit - context is NULL");
  2129. DEV_CRASH();
  2130. return;
  2131. }
  2132. FlipSocialApp *app = (FlipSocialApp *)context;
  2133. View *view = app->view_loader;
  2134. with_view_model(
  2135. view,
  2136. DataLoaderModel * model,
  2137. {
  2138. if (model->timer)
  2139. {
  2140. furi_timer_stop(model->timer);
  2141. }
  2142. },
  2143. false);
  2144. }
  2145. void flip_social_loader_init(View *view)
  2146. {
  2147. if (view == NULL)
  2148. {
  2149. FURI_LOG_E(TAG, "flip_social_loader_init - view is NULL");
  2150. DEV_CRASH();
  2151. return;
  2152. }
  2153. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  2154. view_set_enter_callback(view, flip_social_loader_on_enter);
  2155. view_set_exit_callback(view, flip_social_loader_on_exit);
  2156. }
  2157. void flip_social_loader_free_model(View *view)
  2158. {
  2159. if (view == NULL)
  2160. {
  2161. FURI_LOG_E(TAG, "flip_social_loader_free_model - view is NULL");
  2162. DEV_CRASH();
  2163. return;
  2164. }
  2165. with_view_model(
  2166. view,
  2167. DataLoaderModel * model,
  2168. {
  2169. if (model->timer)
  2170. {
  2171. furi_timer_free(model->timer);
  2172. model->timer = NULL;
  2173. }
  2174. if (model->parser_context)
  2175. {
  2176. free(model->parser_context);
  2177. model->parser_context = NULL;
  2178. }
  2179. },
  2180. false);
  2181. }
  2182. bool flip_social_custom_event_callback(void *context, uint32_t index)
  2183. {
  2184. if (context == NULL)
  2185. {
  2186. FURI_LOG_E(TAG, "flip_social_custom_event_callback - context is NULL");
  2187. DEV_CRASH();
  2188. return false;
  2189. }
  2190. switch (index)
  2191. {
  2192. case FlipSocialCustomEventProcess:
  2193. flip_social_loader_process_callback(context);
  2194. return true;
  2195. default:
  2196. FURI_LOG_DEV(TAG, "flip_social_custom_event_callback. Unknown index: %ld", index);
  2197. return false;
  2198. }
  2199. }
  2200. 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)
  2201. {
  2202. if (app == NULL)
  2203. {
  2204. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - app is NULL");
  2205. DEV_CRASH();
  2206. return;
  2207. }
  2208. View *view = app->view_loader;
  2209. if (view == NULL)
  2210. {
  2211. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - view is NULL");
  2212. DEV_CRASH();
  2213. return;
  2214. }
  2215. with_view_model(
  2216. view,
  2217. DataLoaderModel * model,
  2218. {
  2219. model->title = title;
  2220. model->fetcher = fetcher;
  2221. model->parser = parser;
  2222. model->request_index = 0;
  2223. model->request_count = request_count;
  2224. model->back_callback = back;
  2225. model->data_state = DataStateInitial;
  2226. model->data_text = NULL;
  2227. },
  2228. true);
  2229. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2230. }