callback.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  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. }