flip_social_callback.c 44 KB

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