flip_social_callback.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. char payload[256];
  877. snprintf(
  878. payload,
  879. sizeof(payload),
  880. "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}",
  881. app->login_username_logged_out,
  882. old_password,
  883. app->change_password_logged_in);
  884. char* headers = jsmn("Content-Type", "application/json");
  885. if(!flipper_http_post_request_with_headers(
  886. "https://www.flipsocial.net/api/user/change-password/", 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. free(headers);
  890. return;
  891. }
  892. free(headers);
  893. // Save the settings
  894. save_settings(
  895. app_instance->wifi_ssid_logged_out,
  896. app_instance->wifi_password_logged_out,
  897. app_instance->login_username_logged_out,
  898. app_instance->login_username_logged_in,
  899. app_instance->login_password_logged_out,
  900. app_instance->change_password_logged_in,
  901. app_instance->is_logged_in);
  902. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  903. }
  904. /**
  905. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  906. * @param context The context - FlipSocialApp object.
  907. * @param index The index of the selected item.
  908. * @return void
  909. */
  910. void flip_social_text_input_logged_in_profile_item_selected(void* context, uint32_t index) {
  911. FlipSocialApp* app = (FlipSocialApp*)context;
  912. if(!app) {
  913. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  914. return;
  915. }
  916. switch(index) {
  917. case 0: // Change Username
  918. // do nothing since username cannot be changed
  919. break;
  920. case 1: // Change Password
  921. view_dispatcher_switch_to_view(
  922. app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  923. break;
  924. case 2: // Friends
  925. // get friends then switch to the friends screen
  926. if(flip_social_get_friends()) // start the async friends request
  927. {
  928. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  929. }
  930. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  931. // Wait for the friends to be received
  932. furi_delay_ms(100);
  933. }
  934. furi_timer_stop(fhttp.get_timeout_timer);
  935. if(!flip_social_parse_json_friends()) // parse the JSON before switching to the friends (synchonous)
  936. {
  937. FURI_LOG_E(TAG, "Failed to parse the JSON friends...");
  938. return; // just return for now, no temporary friends yet
  939. // show a popup message saving wifi is disconnected
  940. }
  941. furi_timer_stop(fhttp.get_timeout_timer);
  942. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  943. break;
  944. default:
  945. FURI_LOG_E(TAG, "Unknown configuration item index");
  946. break;
  947. }
  948. }
  949. /**
  950. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  951. * @param context The context - FlipSocialApp object.
  952. * @param index The index of the selected item.
  953. * @return void
  954. */
  955. void flip_social_text_input_logged_in_settings_item_selected(void* context, uint32_t index) {
  956. FlipSocialApp* app = (FlipSocialApp*)context;
  957. if(!app) {
  958. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  959. return;
  960. }
  961. switch(index) {
  962. case 0: // About
  963. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  964. break;
  965. case 1: // Wifi
  966. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  967. break;
  968. default:
  969. break;
  970. }
  971. }
  972. /**
  973. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  974. * @param context The context - FlipSocialApp object.
  975. * @return void
  976. */
  977. void flip_social_logged_in_messages_user_choice_message_updated(void* context) {
  978. FlipSocialApp* app = (FlipSocialApp*)context;
  979. if(!app) {
  980. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  981. return;
  982. }
  983. // check if the message is empty
  984. if(app->message_user_choice_logged_in_temp_buffer_size == 0) {
  985. FURI_LOG_E(TAG, "Message is empty");
  986. view_dispatcher_switch_to_view(
  987. app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  988. return;
  989. }
  990. // Store the entered message
  991. strncpy(
  992. app->message_user_choice_logged_in,
  993. app->message_user_choice_logged_in_temp_buffer,
  994. app->message_user_choice_logged_in_temp_buffer_size);
  995. // Ensure null-termination
  996. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] =
  997. '\0';
  998. // send post request to send message
  999. char url[128];
  1000. char payload[256];
  1001. snprintf(
  1002. url,
  1003. sizeof(url),
  1004. "https://www.flipsocial.net/api/messages/%s/post/",
  1005. app->login_username_logged_in);
  1006. snprintf(
  1007. payload,
  1008. sizeof(payload),
  1009. "{\"receiver\":\"%s\",\"content\":\"%s\"}",
  1010. flip_social_explore->usernames[flip_social_explore->index],
  1011. app->message_user_choice_logged_in);
  1012. char* headers = jsmn("Content-Type", "application/json");
  1013. if(flipper_http_post_request_with_headers(url, headers, payload)) // start the async request
  1014. {
  1015. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1016. fhttp.state = RECEIVING;
  1017. free(headers);
  1018. } else {
  1019. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1020. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1021. free(headers);
  1022. return;
  1023. }
  1024. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  1025. // Wait for the request to be received
  1026. furi_delay_ms(100);
  1027. }
  1028. furi_timer_stop(fhttp.get_timeout_timer);
  1029. // add user to the message list
  1030. strncpy(
  1031. flip_social_message_users->usernames[flip_social_message_users->count],
  1032. flip_social_explore->usernames[flip_social_explore->index],
  1033. strlen(flip_social_explore->usernames[flip_social_explore->index]));
  1034. flip_social_message_users->count++;
  1035. // redraw submenu
  1036. flip_social_update_messages_submenu();
  1037. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1038. }
  1039. /**
  1040. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1041. * @param context The context - FlipSocialApp object.
  1042. * @return void
  1043. */
  1044. void flip_social_logged_in_messages_new_message_updated(void* context) {
  1045. FlipSocialApp* app = (FlipSocialApp*)context;
  1046. if(!app) {
  1047. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1048. return;
  1049. }
  1050. // check if the message is empty
  1051. if(app->messages_new_message_logged_in_temp_buffer_size == 0) {
  1052. FURI_LOG_E(TAG, "Message is empty");
  1053. view_dispatcher_switch_to_view(
  1054. app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1055. return;
  1056. }
  1057. // Store the entered message
  1058. strncpy(
  1059. app->messages_new_message_logged_in,
  1060. app->messages_new_message_logged_in_temp_buffer,
  1061. app->messages_new_message_logged_in_temp_buffer_size);
  1062. // Ensure null-termination
  1063. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] =
  1064. '\0';
  1065. // send post request to send message
  1066. char url[128];
  1067. char payload[256];
  1068. snprintf(
  1069. url,
  1070. sizeof(url),
  1071. "https://www.flipsocial.net/api/messages/%s/post/",
  1072. app->login_username_logged_in);
  1073. snprintf(
  1074. payload,
  1075. sizeof(payload),
  1076. "{\"receiver\":\"%s\",\"content\":\"%s\"}",
  1077. flip_social_message_users->usernames[flip_social_message_users->index],
  1078. app->messages_new_message_logged_in);
  1079. char* headers = jsmn("Content-Type", "application/json");
  1080. if(flipper_http_post_request_with_headers(url, headers, payload)) // start the async request
  1081. {
  1082. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1083. fhttp.state = RECEIVING;
  1084. free(headers);
  1085. } else {
  1086. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1087. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1088. free(headers);
  1089. return;
  1090. }
  1091. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  1092. // Wait for the request to be received
  1093. furi_delay_ms(100);
  1094. }
  1095. furi_timer_stop(fhttp.get_timeout_timer);
  1096. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1097. }