callback.c 86 KB

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