flip_social_callback.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. #include <callback/flip_social_callback.h>
  2. /**
  3. * @brief Navigation callback to go back to the submenu Logged out.
  4. * @param context The context - unused
  5. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  6. */
  7. uint32_t flip_social_callback_to_submenu_logged_out(void* context) {
  8. UNUSED(context);
  9. return FlipSocialViewLoggedOutSubmenu;
  10. }
  11. /**
  12. * @brief Navigation callback to go back to the submenu Logged in.
  13. * @param context The context - unused
  14. * @return next view id (FlipSocialViewLoggedInSubmenu)
  15. */
  16. uint32_t flip_social_callback_to_submenu_logged_in(void* context) {
  17. UNUSED(context);
  18. flip_social_free_explore();
  19. flip_social_free_feed();
  20. flip_social_free_friends();
  21. flip_social_free_message_users();
  22. flip_social_free_messages();
  23. return FlipSocialViewLoggedInSubmenu;
  24. }
  25. /**
  26. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  27. * @param context The context - unused
  28. * @return next view id (FlipSocialViewLoggedOutLogin)
  29. */
  30. uint32_t flip_social_callback_to_login_logged_out(void* context) {
  31. UNUSED(context);
  32. flip_social_sent_login_request = false;
  33. flip_social_login_success = false;
  34. return FlipSocialViewLoggedOutLogin;
  35. }
  36. /**
  37. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  38. * @param context The context - unused
  39. * @return next view id (FlipSocialViewLoggedOutRegister)
  40. */
  41. uint32_t flip_social_callback_to_register_logged_out(void* context) {
  42. UNUSED(context);
  43. flip_social_sent_register_request = false;
  44. flip_social_register_success = false;
  45. return FlipSocialViewLoggedOutRegister;
  46. }
  47. /**
  48. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  49. * @param context The context - unused
  50. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  51. */
  52. uint32_t flip_social_callback_to_wifi_settings_logged_out(void* context) {
  53. UNUSED(context);
  54. return FlipSocialViewLoggedOutWifiSettings;
  55. }
  56. /**
  57. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  58. * @param context The context - unused
  59. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  60. */
  61. uint32_t flip_social_callback_to_wifi_settings_logged_in(void* context) {
  62. UNUSED(context);
  63. return FlipSocialViewLoggedInSettingsWifi;
  64. }
  65. /**
  66. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  67. * @param context The context - unused
  68. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  69. */
  70. uint32_t flip_social_callback_to_settings_logged_in(void* context) {
  71. UNUSED(context);
  72. return FlipSocialViewLoggedInSettings;
  73. }
  74. /**
  75. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  76. * @param context The context - unused
  77. * @return next view id (FlipSocialViewLoggedInCompose)
  78. */
  79. uint32_t flip_social_callback_to_compose_logged_in(void* context) {
  80. UNUSED(context);
  81. return FlipSocialViewLoggedInCompose;
  82. }
  83. /**
  84. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  85. * @param context The context - unused
  86. * @return next view id (FlipSocialViewLoggedInProfile)
  87. */
  88. uint32_t flip_social_callback_to_profile_logged_in(void* context) {
  89. UNUSED(context);
  90. return FlipSocialViewLoggedInProfile;
  91. }
  92. /**
  93. * @brief Navigation callback to bring the user back to the Explore submenu
  94. * @param context The context - unused
  95. * @return next view id (FlipSocialViewLoggedInExploreSubmenu)
  96. */
  97. uint32_t flip_social_callback_to_explore_logged_in(void* context) {
  98. UNUSED(context);
  99. flip_social_dialog_stop = true;
  100. last_explore_response = "";
  101. flip_social_dialog_shown = false;
  102. flip_social_explore->index = 0;
  103. action = ActionNone;
  104. return FlipSocialViewLoggedInExploreSubmenu;
  105. }
  106. /**
  107. * @brief Navigation callback to bring the user back to the Friends submenu
  108. * @param context The context - unused
  109. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  110. */
  111. uint32_t flip_social_callback_to_friends_logged_in(void* context) {
  112. UNUSED(context);
  113. flip_social_dialog_stop = true;
  114. last_explore_response = "";
  115. flip_social_dialog_shown = false;
  116. flip_social_friends->index = 0;
  117. action = ActionNone;
  118. return FlipSocialViewLoggedInFriendsSubmenu;
  119. }
  120. /**
  121. * @brief Navigation callback to bring the user back to the Messages submenu
  122. * @param context The context - unused
  123. * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
  124. */
  125. uint32_t flip_social_callback_to_messages_logged_in(void* context) {
  126. UNUSED(context);
  127. return FlipSocialViewLoggedInMessagesSubmenu;
  128. }
  129. /**
  130. * @brief Navigation callback to bring the user back to the User Choices screen
  131. * @param context The context - unused
  132. * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
  133. */
  134. uint32_t flip_social_callback_to_messages_user_choices(void* context) {
  135. UNUSED(context);
  136. return FlipSocialViewLoggedInMessagesUserChoices;
  137. }
  138. /**
  139. * @brief Navigation callback for exiting the application
  140. * @param context The context - unused
  141. * @return next view id (VIEW_NONE to exit the app)
  142. */
  143. uint32_t flip_social_callback_exit_app(void* context) {
  144. // Exit the application
  145. if(!context) {
  146. FURI_LOG_E(TAG, "Context is NULL");
  147. return VIEW_NONE;
  148. }
  149. UNUSED(context);
  150. return VIEW_NONE; // Return VIEW_NONE to exit the app
  151. }
  152. /**
  153. * @brief Handle ALL submenu item selections.
  154. * @param context The context - FlipSocialApp object.
  155. * @param index The FlipSocialSubmenuIndex item that was clicked.
  156. * @return void
  157. */
  158. void flip_social_callback_submenu_choices(void* context, uint32_t index) {
  159. FlipSocialApp* app = (FlipSocialApp*)context;
  160. if(!app) {
  161. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  162. return;
  163. }
  164. switch(index) {
  165. case FlipSocialSubmenuLoggedOutIndexLogin:
  166. flip_social_sent_login_request = false;
  167. flip_social_login_success = false;
  168. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  169. break;
  170. case FlipSocialSubmenuLoggedOutIndexRegister:
  171. flip_social_sent_register_request = false;
  172. flip_social_register_success = false;
  173. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  174. break;
  175. case FlipSocialSubmenuLoggedOutIndexAbout:
  176. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  177. break;
  178. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  179. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  180. break;
  181. case FlipSocialSubmenuLoggedInIndexProfile:
  182. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  183. break;
  184. case FlipSocialSubmenuLoggedInIndexMessages:
  185. if(flipper_http_process_response_async(
  186. flip_social_get_message_users, flip_social_parse_json_message_users)) {
  187. view_dispatcher_switch_to_view(
  188. app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  189. }
  190. break;
  191. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  192. if(flipper_http_process_response_async(
  193. flip_social_get_explore, flip_social_parse_json_message_user_choices)) {
  194. view_dispatcher_switch_to_view(
  195. app->view_dispatcher, FlipSocialViewLoggedInMessagesUserChoices);
  196. }
  197. break;
  198. case FlipSocialSubmenuLoggedInIndexFeed:
  199. if(flipper_http_process_response_async(flip_social_get_feed, flip_social_parse_json_feed)) {
  200. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  201. } else {
  202. // Set failure FlipSocialFeed object
  203. if(!flip_social_temp_feed()) {
  204. return;
  205. }
  206. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  207. }
  208. break;
  209. case FlipSocialSubmenuExploreIndex:
  210. if(flipper_http_process_response_async(
  211. flip_social_get_explore, flip_social_parse_json_explore)) {
  212. view_dispatcher_switch_to_view(
  213. app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  214. }
  215. break;
  216. case FlipSocialSubmenuLoggedInIndexCompose:
  217. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  218. break;
  219. case FlipSocialSubmenuLoggedInIndexSettings:
  220. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  221. break;
  222. case FlipSocialSubmenuLoggedInSignOutButton:
  223. app->is_logged_in = "false";
  224. save_settings(
  225. app->wifi_ssid_logged_out,
  226. app->wifi_password_logged_out,
  227. app->login_username_logged_out,
  228. app->login_username_logged_in,
  229. app->login_password_logged_out,
  230. app->change_password_logged_in,
  231. app->is_logged_in);
  232. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  233. break;
  234. case FlipSocialSubmenuComposeIndexAddPreSave:
  235. view_dispatcher_switch_to_view(
  236. app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  237. break;
  238. default:
  239. action = ActionNone;
  240. // Handle the pre-saved message selection (has a max of 25 items)
  241. if(index >= FlipSocialSubemnuComposeIndexStartIndex &&
  242. index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES) {
  243. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  244. snprintf(
  245. selected_message,
  246. MAX_MESSAGE_LENGTH,
  247. "%s",
  248. app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  249. if(!selected_message) {
  250. FURI_LOG_E(TAG, "Selected message is NULL");
  251. return;
  252. }
  253. view_dispatcher_switch_to_view(
  254. app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  255. }
  256. // Handle the explore selection
  257. else if(
  258. index >= FlipSocialSubmenuExploreIndexStartIndex &&
  259. index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS) {
  260. if(!flip_social_explore) {
  261. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  262. return;
  263. }
  264. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  265. view_dispatcher_switch_to_view(
  266. app->view_dispatcher, FlipSocialViewLoggedInExploreProccess);
  267. }
  268. // handle the friends selection
  269. else if(
  270. index >= FlipSocialSubmenuLoggedInIndexFriendsStart &&
  271. index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS) {
  272. if(!flip_social_friends) {
  273. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  274. return;
  275. }
  276. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  277. view_dispatcher_switch_to_view(
  278. app->view_dispatcher, FlipSocialViewLoggedInFriendsProcess);
  279. }
  280. // handle the messages selection
  281. else if(
  282. index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart &&
  283. index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS) {
  284. if(!flip_social_message_users) {
  285. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  286. return;
  287. }
  288. flip_social_message_users->index =
  289. index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  290. if(flipper_http_process_response_async(
  291. flip_social_get_messages_with_user, flip_social_parse_json_messages)) {
  292. view_dispatcher_switch_to_view(
  293. app->view_dispatcher, FlipSocialViewLoggedInMessagesProcess);
  294. }
  295. }
  296. // handle the messages user choices selection
  297. else if(
  298. index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart &&
  299. index <
  300. FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS) {
  301. if(!flip_social_explore) {
  302. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  303. return;
  304. }
  305. flip_social_explore->index =
  306. index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  307. view_dispatcher_switch_to_view(
  308. app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  309. } else {
  310. FURI_LOG_E(TAG, "Unknown submenu index");
  311. }
  312. break;
  313. }
  314. }
  315. /**
  316. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  317. * @param context The context - FlipSocialApp object.
  318. * @return void
  319. */
  320. void flip_social_logged_out_wifi_settings_ssid_updated(void* context) {
  321. FlipSocialApp* app = (FlipSocialApp*)context;
  322. if(!app) {
  323. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  324. return;
  325. }
  326. // Store the entered name
  327. strncpy(
  328. app->wifi_ssid_logged_out,
  329. app->wifi_ssid_logged_out_temp_buffer,
  330. app->wifi_ssid_logged_out_temp_buffer_size);
  331. // Store the entered name in the logged in name field
  332. strncpy(
  333. app->wifi_ssid_logged_in,
  334. app->wifi_ssid_logged_out_temp_buffer,
  335. app->wifi_ssid_logged_out_temp_buffer_size);
  336. strncpy(
  337. app->wifi_ssid_logged_in_temp_buffer,
  338. app->wifi_ssid_logged_out_temp_buffer,
  339. app->wifi_ssid_logged_out_temp_buffer_size);
  340. // Ensure null-termination
  341. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  342. // Update the name item text
  343. if(app->variable_item_logged_out_wifi_settings_ssid) {
  344. variable_item_set_current_value_text(
  345. app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  346. }
  347. // update the wifi settings
  348. if(!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out)) {
  349. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  350. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  351. }
  352. // Save the settings
  353. save_settings(
  354. app_instance->wifi_ssid_logged_out,
  355. app_instance->wifi_password_logged_out,
  356. app_instance->login_username_logged_out,
  357. app_instance->login_username_logged_in,
  358. app_instance->login_password_logged_out,
  359. app_instance->change_password_logged_in,
  360. app_instance->is_logged_in);
  361. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  362. }
  363. /**
  364. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  365. * @param context The context - FlipSocialApp object.
  366. * @return void
  367. */
  368. void flip_social_logged_out_wifi_settings_password_updated(void* context) {
  369. FlipSocialApp* app = (FlipSocialApp*)context;
  370. if(!app) {
  371. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  372. return;
  373. }
  374. // Store the entered WiFi password
  375. strncpy(
  376. app->wifi_password_logged_out,
  377. app->wifi_password_logged_out_temp_buffer,
  378. app->wifi_password_logged_out_temp_buffer_size);
  379. // Store the entered WiFi password in the logged in password field
  380. strncpy(
  381. app->wifi_password_logged_in,
  382. app->wifi_password_logged_out_temp_buffer,
  383. app->wifi_password_logged_out_temp_buffer_size);
  384. strncpy(
  385. app->wifi_password_logged_in_temp_buffer,
  386. app->wifi_password_logged_out_temp_buffer,
  387. app->wifi_password_logged_out_temp_buffer_size);
  388. // Ensure null-termination
  389. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  390. // Update the password item text
  391. if(app->variable_item_logged_out_wifi_settings_password) {
  392. variable_item_set_current_value_text(
  393. app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  394. }
  395. // update the wifi settings
  396. if(!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out)) {
  397. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  398. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  399. }
  400. // Save the settings
  401. save_settings(
  402. app_instance->wifi_ssid_logged_out,
  403. app_instance->wifi_password_logged_out,
  404. app_instance->login_username_logged_out,
  405. app_instance->login_username_logged_in,
  406. app_instance->login_password_logged_out,
  407. app_instance->change_password_logged_in,
  408. app_instance->is_logged_in);
  409. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  410. }
  411. /**
  412. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  413. * @param context The context - FlipSocialApp object.
  414. * @param index The index of the selected item.
  415. * @return void
  416. */
  417. void flip_social_text_input_logged_out_wifi_settings_item_selected(void* context, uint32_t index) {
  418. FlipSocialApp* app = (FlipSocialApp*)context;
  419. if(!app) {
  420. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  421. return;
  422. }
  423. switch(index) {
  424. case 0: // Input SSID
  425. view_dispatcher_switch_to_view(
  426. app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  427. break;
  428. case 1: // Input Password
  429. view_dispatcher_switch_to_view(
  430. app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  431. break;
  432. default:
  433. FURI_LOG_E(TAG, "Unknown configuration item index");
  434. break;
  435. }
  436. }
  437. /**
  438. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  439. * @param context The context - FlipSocialApp object.
  440. * @return void
  441. */
  442. void flip_social_logged_out_login_username_updated(void* context) {
  443. FlipSocialApp* app = (FlipSocialApp*)context;
  444. if(!app) {
  445. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  446. return;
  447. }
  448. // Store the entered name
  449. strncpy(
  450. app->login_username_logged_out,
  451. app->login_username_logged_out_temp_buffer,
  452. app->login_username_logged_out_temp_buffer_size);
  453. // Store the entered name in the logged in username field
  454. strncpy(
  455. app->login_username_logged_in,
  456. app->login_username_logged_out_temp_buffer,
  457. app->login_username_logged_out_temp_buffer_size);
  458. strncpy(
  459. app->login_username_logged_in_temp_buffer,
  460. app->login_username_logged_out_temp_buffer,
  461. app->login_username_logged_out_temp_buffer_size);
  462. // Ensure null-termination
  463. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  464. // Update the name item text
  465. if(app->variable_item_logged_out_login_username) {
  466. variable_item_set_current_value_text(
  467. app->variable_item_logged_out_login_username, app->login_username_logged_out);
  468. }
  469. // Save the settings
  470. save_settings(
  471. app_instance->wifi_ssid_logged_out,
  472. app_instance->wifi_password_logged_out,
  473. app_instance->login_username_logged_out,
  474. app_instance->login_username_logged_in,
  475. app_instance->login_password_logged_out,
  476. app_instance->change_password_logged_in,
  477. app_instance->is_logged_in);
  478. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  479. }
  480. /**
  481. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  482. * @param context The context - FlipSocialApp object.
  483. * @return void
  484. */
  485. void flip_social_logged_out_login_password_updated(void* context) {
  486. FlipSocialApp* app = (FlipSocialApp*)context;
  487. if(!app) {
  488. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  489. return;
  490. }
  491. // Store the entered password
  492. strncpy(
  493. app->login_password_logged_out,
  494. app->login_password_logged_out_temp_buffer,
  495. app->login_password_logged_out_temp_buffer_size);
  496. // Store the entered password in the change password field
  497. strncpy(
  498. app->change_password_logged_in,
  499. app->login_password_logged_out_temp_buffer,
  500. app->login_password_logged_out_temp_buffer_size);
  501. strncpy(
  502. app->change_password_logged_in_temp_buffer,
  503. app->login_password_logged_out_temp_buffer,
  504. app->login_password_logged_out_temp_buffer_size);
  505. // Ensure null-termination
  506. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  507. // Update the password item text
  508. if(app->variable_item_logged_out_login_password) {
  509. // dont show the password on the screen (version 0.2)
  510. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  511. }
  512. // Save the settings
  513. save_settings(
  514. app_instance->wifi_ssid_logged_out,
  515. app_instance->wifi_password_logged_out,
  516. app_instance->login_username_logged_out,
  517. app_instance->login_username_logged_in,
  518. app_instance->login_password_logged_out,
  519. app_instance->change_password_logged_in,
  520. app_instance->is_logged_in);
  521. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  522. }
  523. /**
  524. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  525. * @param context The context - FlipSocialApp object.
  526. * @param index The index of the selected item.
  527. * @return void
  528. */
  529. void flip_social_text_input_logged_out_login_item_selected(void* context, uint32_t index) {
  530. FlipSocialApp* app = (FlipSocialApp*)context;
  531. if(!app) {
  532. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  533. return;
  534. }
  535. switch(index) {
  536. case 0: // Input Username
  537. view_dispatcher_switch_to_view(
  538. app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  539. break;
  540. case 1: // Input Password
  541. view_dispatcher_switch_to_view(
  542. app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  543. break;
  544. case 2: // Login Button
  545. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessLogin);
  546. break;
  547. default:
  548. FURI_LOG_E(TAG, "Unknown configuration item index");
  549. break;
  550. }
  551. }
  552. /**
  553. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  554. * @param context The context - FlipSocialApp object.
  555. * @return void
  556. */
  557. void flip_social_logged_out_register_username_updated(void* context) {
  558. FlipSocialApp* app = (FlipSocialApp*)context;
  559. if(!app) {
  560. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  561. return;
  562. }
  563. // Store the entered name
  564. strncpy(
  565. app->register_username_logged_out,
  566. app->register_username_logged_out_temp_buffer,
  567. app->register_username_logged_out_temp_buffer_size);
  568. // Ensure null-termination
  569. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] =
  570. '\0';
  571. // Update the name item text
  572. if(app->variable_item_logged_out_register_username) {
  573. variable_item_set_current_value_text(
  574. app->variable_item_logged_out_register_username, app->register_username_logged_out);
  575. }
  576. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  577. }
  578. /**
  579. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  580. * @param context The context - FlipSocialApp object.
  581. * @return void
  582. */
  583. void flip_social_logged_out_register_password_updated(void* context) {
  584. FlipSocialApp* app = (FlipSocialApp*)context;
  585. if(!app) {
  586. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  587. return;
  588. }
  589. // Store the entered password
  590. strncpy(
  591. app->register_password_logged_out,
  592. app->register_password_logged_out_temp_buffer,
  593. app->register_password_logged_out_temp_buffer_size);
  594. // Ensure null-termination
  595. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] =
  596. '\0';
  597. // Update the password item text
  598. if(app->variable_item_logged_out_register_password) {
  599. variable_item_set_current_value_text(
  600. app->variable_item_logged_out_register_password, app->register_password_logged_out);
  601. }
  602. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  603. }
  604. /**
  605. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  606. * @param context The context - FlipSocialApp object.
  607. * @return void
  608. */
  609. void flip_social_logged_out_register_password_2_updated(void* context) {
  610. FlipSocialApp* app = (FlipSocialApp*)context;
  611. if(!app) {
  612. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  613. return;
  614. }
  615. // Store the entered password
  616. strncpy(
  617. app->register_password_2_logged_out,
  618. app->register_password_2_logged_out_temp_buffer,
  619. app->register_password_2_logged_out_temp_buffer_size);
  620. // Ensure null-termination
  621. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] =
  622. '\0';
  623. // Update the password item text
  624. if(app->variable_item_logged_out_register_password_2) {
  625. variable_item_set_current_value_text(
  626. app->variable_item_logged_out_register_password_2,
  627. app->register_password_2_logged_out);
  628. }
  629. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  630. }
  631. /**
  632. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  633. * @param context The context - FlipSocialApp object.
  634. * @param index The index of the selected item.
  635. * @return void
  636. */
  637. void flip_social_text_input_logged_out_register_item_selected(void* context, uint32_t index) {
  638. FlipSocialApp* app = (FlipSocialApp*)context;
  639. if(!app) {
  640. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  641. return;
  642. }
  643. switch(index) {
  644. case 0: // Input Username
  645. view_dispatcher_switch_to_view(
  646. app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  647. break;
  648. case 1: // Input Password
  649. view_dispatcher_switch_to_view(
  650. app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  651. break;
  652. case 2: // Input Password 2
  653. view_dispatcher_switch_to_view(
  654. app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  655. break;
  656. case 3: // Register button
  657. view_dispatcher_switch_to_view(
  658. app->view_dispatcher, FlipSocialViewLoggedOutProcessRegister);
  659. break;
  660. default:
  661. FURI_LOG_E(TAG, "Unknown configuration item index");
  662. break;
  663. }
  664. }
  665. /**
  666. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  667. * @param context The context - FlipSocialApp object.
  668. * @return void
  669. */
  670. void flip_social_logged_in_wifi_settings_ssid_updated(void* context) {
  671. FlipSocialApp* app = (FlipSocialApp*)context;
  672. if(!app) {
  673. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  674. return;
  675. }
  676. // Store the entered SSID
  677. strncpy(
  678. app->wifi_ssid_logged_in,
  679. app->wifi_ssid_logged_in_temp_buffer,
  680. app->wifi_ssid_logged_in_temp_buffer_size);
  681. // Store the entered SSID in the logged out SSID
  682. strncpy(
  683. app->wifi_ssid_logged_out,
  684. app->wifi_ssid_logged_in,
  685. app->wifi_ssid_logged_in_temp_buffer_size);
  686. strncpy(
  687. app->wifi_ssid_logged_out_temp_buffer,
  688. app->wifi_ssid_logged_in,
  689. app->wifi_ssid_logged_in_temp_buffer_size);
  690. // Ensure null-termination
  691. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  692. // Update the name item text
  693. if(app->variable_item_logged_in_wifi_settings_ssid) {
  694. variable_item_set_current_value_text(
  695. app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  696. }
  697. // Save the settings
  698. save_settings(
  699. app_instance->wifi_ssid_logged_in,
  700. app_instance->wifi_password_logged_in,
  701. app_instance->login_username_logged_out,
  702. app_instance->login_username_logged_in,
  703. app_instance->login_password_logged_out,
  704. app_instance->change_password_logged_in,
  705. app_instance->is_logged_in);
  706. // update the wifi settings
  707. if(strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0) {
  708. if(!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in)) {
  709. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  710. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  711. }
  712. }
  713. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  714. }
  715. /**
  716. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  717. * @param context The context - FlipSocialApp object.
  718. * @return void
  719. */
  720. void flip_social_logged_in_wifi_settings_password_updated(void* context) {
  721. FlipSocialApp* app = (FlipSocialApp*)context;
  722. if(!app) {
  723. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  724. return;
  725. }
  726. // Store the entered password
  727. strncpy(
  728. app->wifi_password_logged_in,
  729. app->wifi_password_logged_in_temp_buffer,
  730. app->wifi_password_logged_in_temp_buffer_size);
  731. // Store the entered password in the logged out password
  732. strncpy(
  733. app->login_password_logged_out,
  734. app->wifi_password_logged_in,
  735. app->wifi_password_logged_in_temp_buffer_size);
  736. strncpy(
  737. app->login_password_logged_out_temp_buffer,
  738. app->wifi_password_logged_in,
  739. app->wifi_password_logged_in_temp_buffer_size);
  740. // Ensure null-termination
  741. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  742. // Update the password item text
  743. if(app->variable_item_logged_in_wifi_settings_password) {
  744. // dont show the password on the screen (version 0.2)
  745. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  746. }
  747. // Save the settings
  748. save_settings(
  749. app_instance->wifi_ssid_logged_in,
  750. app_instance->wifi_password_logged_in,
  751. app_instance->login_username_logged_out,
  752. app_instance->login_username_logged_in,
  753. app_instance->login_password_logged_out,
  754. app_instance->change_password_logged_in,
  755. app_instance->is_logged_in);
  756. // update the wifi settings
  757. if(strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0) {
  758. if(!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in)) {
  759. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  760. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  761. }
  762. }
  763. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  764. }
  765. /**
  766. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  767. * @param context The context - FlipSocialApp object.
  768. * @param index The index of the selected item.
  769. * @return void
  770. */
  771. void flip_social_text_input_logged_in_wifi_settings_item_selected(void* context, uint32_t index) {
  772. FlipSocialApp* app = (FlipSocialApp*)context;
  773. if(!app) {
  774. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  775. return;
  776. }
  777. switch(index) {
  778. case 0: // Input SSID
  779. view_dispatcher_switch_to_view(
  780. app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  781. break;
  782. case 1: // Input Password
  783. view_dispatcher_switch_to_view(
  784. app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  785. break;
  786. default:
  787. FURI_LOG_E(TAG, "Unknown configuration item index");
  788. break;
  789. }
  790. }
  791. /**
  792. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  793. * @param context The context - FlipSocialApp object.
  794. * @return void
  795. */
  796. void flip_social_logged_in_compose_pre_save_updated(void* context) {
  797. FlipSocialApp* app = (FlipSocialApp*)context;
  798. if(!app) {
  799. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  800. return;
  801. }
  802. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  803. if(app->compose_pre_save_logged_in_temp_buffer_size == 0 ||
  804. app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES) {
  805. FURI_LOG_E(
  806. TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  807. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  808. return;
  809. }
  810. // Store the entered message
  811. strncpy(
  812. app->compose_pre_save_logged_in,
  813. app->compose_pre_save_logged_in_temp_buffer,
  814. app->compose_pre_save_logged_in_temp_buffer_size);
  815. // Ensure null-termination
  816. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  817. // add the item to the submenu
  818. submenu_reset(app->submenu_compose);
  819. // loop through the items and add them to the submenu
  820. app->pre_saved_messages.messages[app->pre_saved_messages.count] =
  821. app->compose_pre_save_logged_in;
  822. app->pre_saved_messages.count++;
  823. submenu_add_item(
  824. app->submenu_compose,
  825. "Add Pre-Save",
  826. FlipSocialSubmenuComposeIndexAddPreSave,
  827. flip_social_callback_submenu_choices,
  828. app);
  829. for(uint32_t i = 0; i < app->pre_saved_messages.count; i++) {
  830. submenu_add_item(
  831. app->submenu_compose,
  832. app->pre_saved_messages.messages[i],
  833. FlipSocialSubemnuComposeIndexStartIndex + i,
  834. flip_social_callback_submenu_choices,
  835. app);
  836. }
  837. // save playlist
  838. save_playlist(&app->pre_saved_messages);
  839. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  840. }
  841. /**
  842. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  843. * @param context The context - FlipSocialApp object.
  844. * @return void
  845. */
  846. void flip_social_logged_in_profile_change_password_updated(void* context) {
  847. FlipSocialApp* app = (FlipSocialApp*)context;
  848. if(!app) {
  849. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  850. return;
  851. }
  852. // Correct type: old_pass should be a pointer to a string (char *)
  853. const char* old_password = app->login_password_logged_out;
  854. // Store the entered message
  855. strncpy(
  856. app->change_password_logged_in,
  857. app->change_password_logged_in_temp_buffer,
  858. app->change_password_logged_in_temp_buffer_size);
  859. // store the entered password in the logged out password
  860. strncpy(
  861. app->login_password_logged_out,
  862. app->change_password_logged_in,
  863. app->login_password_logged_out_temp_buffer_size);
  864. strncpy(
  865. app->login_password_logged_out_temp_buffer,
  866. app->change_password_logged_in,
  867. app->login_password_logged_out_temp_buffer_size);
  868. // Ensure null-termination
  869. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  870. // Update the message item text
  871. if(app->variable_item_logged_in_profile_change_password) {
  872. // dont show the password on the screen (version 0.2)
  873. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  874. }
  875. // send post request to change password
  876. auth_headers_alloc();
  877. char payload[256];
  878. snprintf(
  879. payload,
  880. sizeof(payload),
  881. "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}",
  882. app->login_username_logged_out,
  883. old_password,
  884. app->change_password_logged_in);
  885. if(!flipper_http_post_request_with_headers(
  886. "https://www.flipsocial.net/api/user/change-password/", auth_headers, payload)) {
  887. FURI_LOG_E(TAG, "Failed to send post request to change password");
  888. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  889. return;
  890. }
  891. // Save the settings
  892. save_settings(
  893. app_instance->wifi_ssid_logged_out,
  894. app_instance->wifi_password_logged_out,
  895. app_instance->login_username_logged_out,
  896. app_instance->login_username_logged_in,
  897. app_instance->login_password_logged_out,
  898. app_instance->change_password_logged_in,
  899. app_instance->is_logged_in);
  900. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  901. }
  902. /**
  903. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  904. * @param context The context - FlipSocialApp object.
  905. * @param index The index of the selected item.
  906. * @return void
  907. */
  908. void flip_social_text_input_logged_in_profile_item_selected(void* context, uint32_t index) {
  909. FlipSocialApp* app = (FlipSocialApp*)context;
  910. if(!app) {
  911. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  912. return;
  913. }
  914. switch(index) {
  915. case 0: // Change Username
  916. // do nothing since username cannot be changed
  917. break;
  918. case 1: // Change Password
  919. view_dispatcher_switch_to_view(
  920. app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  921. break;
  922. case 2: // Friends
  923. // get friends then switch to the friends screen
  924. if(flip_social_get_friends()) // start the async friends request
  925. {
  926. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  927. }
  928. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  929. // Wait for the friends to be received
  930. furi_delay_ms(100);
  931. }
  932. furi_timer_stop(fhttp.get_timeout_timer);
  933. if(!flip_social_parse_json_friends()) // parse the JSON before switching to the friends (synchonous)
  934. {
  935. FURI_LOG_E(TAG, "Failed to parse the JSON friends...");
  936. return; // just return for now, no temporary friends yet
  937. // show a popup message saving wifi is disconnected
  938. }
  939. furi_timer_stop(fhttp.get_timeout_timer);
  940. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  941. break;
  942. default:
  943. FURI_LOG_E(TAG, "Unknown configuration item index");
  944. break;
  945. }
  946. }
  947. /**
  948. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  949. * @param context The context - FlipSocialApp object.
  950. * @param index The index of the selected item.
  951. * @return void
  952. */
  953. void flip_social_text_input_logged_in_settings_item_selected(void* context, uint32_t index) {
  954. FlipSocialApp* app = (FlipSocialApp*)context;
  955. if(!app) {
  956. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  957. return;
  958. }
  959. switch(index) {
  960. case 0: // About
  961. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  962. break;
  963. case 1: // Wifi
  964. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  965. break;
  966. default:
  967. break;
  968. }
  969. }
  970. /**
  971. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  972. * @param context The context - FlipSocialApp object.
  973. * @return void
  974. */
  975. void flip_social_logged_in_messages_user_choice_message_updated(void* context) {
  976. FlipSocialApp* app = (FlipSocialApp*)context;
  977. if(!app) {
  978. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  979. return;
  980. }
  981. // check if the message is empty
  982. if(app->message_user_choice_logged_in_temp_buffer_size == 0) {
  983. FURI_LOG_E(TAG, "Message is empty");
  984. view_dispatcher_switch_to_view(
  985. app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  986. return;
  987. }
  988. // Store the entered message
  989. strncpy(
  990. app->message_user_choice_logged_in,
  991. app->message_user_choice_logged_in_temp_buffer,
  992. app->message_user_choice_logged_in_temp_buffer_size);
  993. // Ensure null-termination
  994. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] =
  995. '\0';
  996. // send post request to send message
  997. auth_headers_alloc();
  998. char url[128];
  999. char payload[256];
  1000. snprintf(
  1001. url,
  1002. sizeof(url),
  1003. "https://www.flipsocial.net/api/messages/%s/post/",
  1004. app->login_username_logged_in);
  1005. snprintf(
  1006. payload,
  1007. sizeof(payload),
  1008. "{\"receiver\":\"%s\",\"content\":\"%s\"}",
  1009. flip_social_explore->usernames[flip_social_explore->index],
  1010. app->message_user_choice_logged_in);
  1011. if(flipper_http_post_request_with_headers(
  1012. url, auth_headers, payload)) // start the async request
  1013. {
  1014. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1015. fhttp.state = RECEIVING;
  1016. } else {
  1017. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1018. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1019. return;
  1020. }
  1021. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  1022. // Wait for the request to be received
  1023. furi_delay_ms(100);
  1024. }
  1025. furi_timer_stop(fhttp.get_timeout_timer);
  1026. // add user to the message list
  1027. strncpy(
  1028. flip_social_message_users->usernames[flip_social_message_users->count],
  1029. flip_social_explore->usernames[flip_social_explore->index],
  1030. strlen(flip_social_explore->usernames[flip_social_explore->index]));
  1031. flip_social_message_users->count++;
  1032. // redraw submenu
  1033. flip_social_update_messages_submenu();
  1034. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1035. }
  1036. /**
  1037. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1038. * @param context The context - FlipSocialApp object.
  1039. * @return void
  1040. */
  1041. void flip_social_logged_in_messages_new_message_updated(void* context) {
  1042. FlipSocialApp* app = (FlipSocialApp*)context;
  1043. if(!app) {
  1044. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1045. return;
  1046. }
  1047. // check if the message is empty
  1048. if(app->messages_new_message_logged_in_temp_buffer_size == 0) {
  1049. FURI_LOG_E(TAG, "Message is empty");
  1050. view_dispatcher_switch_to_view(
  1051. app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1052. return;
  1053. }
  1054. // Store the entered message
  1055. strncpy(
  1056. app->messages_new_message_logged_in,
  1057. app->messages_new_message_logged_in_temp_buffer,
  1058. app->messages_new_message_logged_in_temp_buffer_size);
  1059. // Ensure null-termination
  1060. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] =
  1061. '\0';
  1062. // send post request to send message
  1063. auth_headers_alloc();
  1064. char url[128];
  1065. char payload[256];
  1066. snprintf(
  1067. url,
  1068. sizeof(url),
  1069. "https://www.flipsocial.net/api/messages/%s/post/",
  1070. app->login_username_logged_in);
  1071. snprintf(
  1072. payload,
  1073. sizeof(payload),
  1074. "{\"receiver\":\"%s\",\"content\":\"%s\"}",
  1075. flip_social_message_users->usernames[flip_social_message_users->index],
  1076. app->messages_new_message_logged_in);
  1077. if(flipper_http_post_request_with_headers(
  1078. url, auth_headers, payload)) // start the async request
  1079. {
  1080. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1081. fhttp.state = RECEIVING;
  1082. } else {
  1083. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1084. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1085. return;
  1086. }
  1087. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  1088. // Wait for the request to be received
  1089. furi_delay_ms(100);
  1090. }
  1091. furi_timer_stop(fhttp.get_timeout_timer);
  1092. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1093. }