flip_social_callback.c 97 KB

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