flip_social_callback.c 72 KB

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