flip_social_callback.h 43 KB

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