flip_social_callback.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. {
  11. UNUSED(context);
  12. return FlipSocialViewLoggedOutSubmenu;
  13. }
  14. /**
  15. * @brief Navigation callback to go back to the submenu Logged in.
  16. * @param context The context - unused
  17. * @return next view id (FlipSocialViewLoggedInSubmenu)
  18. */
  19. static uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  20. {
  21. UNUSED(context);
  22. // flip_social_get_feed(); // start the feed request
  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. static uint32_t flip_social_callback_to_login_logged_out(void *context)
  31. {
  32. UNUSED(context);
  33. flip_social_sent_login_request = false;
  34. flip_social_login_success = false;
  35. return FlipSocialViewLoggedOutLogin;
  36. }
  37. /**
  38. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  39. * @param context The context - unused
  40. * @return next view id (FlipSocialViewLoggedOutRegister)
  41. */
  42. static uint32_t flip_social_callback_to_register_logged_out(void *context)
  43. {
  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. {
  56. UNUSED(context);
  57. return FlipSocialViewLoggedOutWifiSettings;
  58. }
  59. /**
  60. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  61. * @param context The context - unused
  62. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  63. */
  64. static uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  65. {
  66. UNUSED(context);
  67. return FlipSocialViewLoggedInSettingsWifi;
  68. }
  69. /**
  70. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  71. * @param context The context - unused
  72. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  73. */
  74. static uint32_t flip_social_callback_to_settings_logged_in(void *context)
  75. {
  76. UNUSED(context);
  77. return FlipSocialViewLoggedInSettings;
  78. }
  79. /**
  80. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  81. * @param context The context - unused
  82. * @return next view id (FlipSocialViewLoggedInCompose)
  83. */
  84. static uint32_t flip_social_callback_to_compose_logged_in(void *context)
  85. {
  86. UNUSED(context);
  87. return FlipSocialViewLoggedInCompose;
  88. }
  89. /**
  90. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  91. * @param context The context - unused
  92. * @return next view id (FlipSocialViewLoggedInProfile)
  93. */
  94. static uint32_t flip_social_callback_to_profile_logged_in(void *context)
  95. {
  96. UNUSED(context);
  97. return FlipSocialViewLoggedInProfile;
  98. }
  99. /**
  100. * @brief Navigation callback to bring the user back to the Explore submenu
  101. * @param context The context - unused
  102. * @return next view id (FlipSocialViewLoggedInExploreSubmenu)
  103. */
  104. static uint32_t flip_social_callback_to_explore_logged_in(void *context)
  105. {
  106. UNUSED(context);
  107. flip_social_dialog_stop = true;
  108. last_explore_response = "";
  109. flip_social_dialog_shown = false;
  110. flip_social_explore.index = 0;
  111. action = ActionNone;
  112. return FlipSocialViewLoggedInExploreSubmenu;
  113. }
  114. /**
  115. * @brief Navigation callback to bring the user back to the Friends submenu
  116. * @param context The context - unused
  117. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  118. */
  119. static uint32_t flip_social_callback_to_friends_logged_in(void *context)
  120. {
  121. UNUSED(context);
  122. flip_social_dialog_stop = true;
  123. last_explore_response = "";
  124. flip_social_dialog_shown = false;
  125. flip_social_friends.index = 0;
  126. action = ActionNone;
  127. return FlipSocialViewLoggedInFriendsSubmenu;
  128. }
  129. /**
  130. * @brief Navigation callback for exiting the application
  131. * @param context The context - unused
  132. * @return next view id (VIEW_NONE to exit the app)
  133. */
  134. static uint32_t flip_social_callback_exit_app(void *context)
  135. {
  136. // Exit the application
  137. if (!context)
  138. {
  139. FURI_LOG_E(TAG, "Context is NULL");
  140. return VIEW_NONE;
  141. }
  142. UNUSED(context);
  143. return VIEW_NONE; // Return VIEW_NONE to exit the app
  144. }
  145. /**
  146. * @brief Handle ALL submenu item selections.
  147. * @param context The context - FlipSocialApp object.
  148. * @param index The FlipSocialSubmenuIndex item that was clicked.
  149. * @return void
  150. */
  151. static void flip_social_callback_submenu_choices(void *context, uint32_t index)
  152. {
  153. FlipSocialApp *app = (FlipSocialApp *)context;
  154. if (!app)
  155. {
  156. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  157. return;
  158. }
  159. switch (index)
  160. {
  161. case FlipSocialSubmenuLoggedOutIndexLogin:
  162. flip_social_sent_login_request = false;
  163. flip_social_login_success = false;
  164. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  165. break;
  166. case FlipSocialSubmenuLoggedOutIndexRegister:
  167. flip_social_sent_register_request = false;
  168. flip_social_register_success = false;
  169. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  170. break;
  171. case FlipSocialSubmenuLoggedOutIndexAbout:
  172. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  173. break;
  174. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  175. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  176. break;
  177. case FlipSocialSubmenuLoggedInIndexProfile:
  178. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  179. break;
  180. case FlipSocialSubmenuLoggedInIndexFeed:
  181. if (flip_social_get_feed()) // start the async feed request
  182. {
  183. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  184. }
  185. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  186. {
  187. // Wait for the feed to be received
  188. furi_delay_ms(100);
  189. }
  190. furi_timer_stop(fhttp.get_timeout_timer);
  191. if (!flip_social_parse_json_feed()) // parse the JSON before switching to the feed (synchonous)
  192. {
  193. FURI_LOG_E(TAG, "Failed to parse the JSON feed. Using the temporary feed.");
  194. }
  195. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  196. break;
  197. case FlipSocialSubmenuExploreIndex:
  198. // process explore page the same way as the feed page, then switch to the explore page
  199. if (flip_social_get_explore()) // start the async explore request
  200. {
  201. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  202. }
  203. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  204. {
  205. // Wait for the explore to be received
  206. furi_delay_ms(100);
  207. }
  208. furi_timer_stop(fhttp.get_timeout_timer);
  209. if (!flip_social_parse_json_explore()) // parse the JSON before switching to the explore (synchonous)
  210. {
  211. FURI_LOG_E(TAG, "Failed to parse the JSON explore...");
  212. return; // just return for now, no temporary explore yet
  213. // show a popup message saving wifi is disconnected
  214. }
  215. furi_timer_stop(fhttp.get_timeout_timer);
  216. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  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(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  227. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  228. break;
  229. case FlipSocialSubmenuComposeIndexAddPreSave:
  230. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  231. break;
  232. default:
  233. // Handle the pre-saved message selection (has a max of 25 items)
  234. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  235. {
  236. flip_social_feed.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  237. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  238. }
  239. // Handle the explore selection
  240. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  241. {
  242. flip_social_explore.index = index - FlipSocialSubmenuExploreIndexStartIndex;
  243. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreProccess);
  244. }
  245. // handle the friends selection
  246. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  247. {
  248. flip_social_friends.index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  249. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsProcess);
  250. }
  251. else
  252. {
  253. FURI_LOG_E(TAG, "Unknown submenu index");
  254. }
  255. break;
  256. }
  257. }
  258. /**
  259. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  260. * @param context The context - FlipSocialApp object.
  261. * @return void
  262. */
  263. static void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  264. {
  265. FlipSocialApp *app = (FlipSocialApp *)context;
  266. if (!app)
  267. {
  268. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  269. return;
  270. }
  271. // Store the entered name
  272. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  273. // Store the entered name in the logged in name field
  274. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  275. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  276. // Ensure null-termination
  277. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  278. // Update the name item text
  279. if (app->variable_item_logged_out_wifi_settings_ssid)
  280. {
  281. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  282. }
  283. // update the wifi settings
  284. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  285. {
  286. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  287. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  288. }
  289. // Save the settings
  290. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  291. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  292. }
  293. /**
  294. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  295. * @param context The context - FlipSocialApp object.
  296. * @return void
  297. */
  298. static void flip_social_logged_out_wifi_settings_password_updated(void *context)
  299. {
  300. FlipSocialApp *app = (FlipSocialApp *)context;
  301. if (!app)
  302. {
  303. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  304. return;
  305. }
  306. // Store the entered WiFi password
  307. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  308. // Store the entered WiFi password in the logged in password field
  309. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  310. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  311. // Ensure null-termination
  312. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  313. // Update the password item text
  314. if (app->variable_item_logged_out_wifi_settings_password)
  315. {
  316. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  317. }
  318. // update the wifi settings
  319. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  320. {
  321. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  322. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  323. }
  324. // Save the settings
  325. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  326. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  327. }
  328. /**
  329. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  330. * @param context The context - FlipSocialApp object.
  331. * @param index The index of the selected item.
  332. * @return void
  333. */
  334. static void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  335. {
  336. FlipSocialApp *app = (FlipSocialApp *)context;
  337. if (!app)
  338. {
  339. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  340. return;
  341. }
  342. switch (index)
  343. {
  344. case 0: // Input SSID
  345. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  346. break;
  347. case 1: // Input Password
  348. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  349. break;
  350. default:
  351. FURI_LOG_E(TAG, "Unknown configuration item index");
  352. break;
  353. }
  354. }
  355. /**
  356. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  357. * @param context The context - FlipSocialApp object.
  358. * @return void
  359. */
  360. static void flip_social_logged_out_login_username_updated(void *context)
  361. {
  362. FlipSocialApp *app = (FlipSocialApp *)context;
  363. if (!app)
  364. {
  365. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  366. return;
  367. }
  368. // Store the entered name
  369. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  370. // Store the entered name in the logged in username field
  371. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  372. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  373. // Ensure null-termination
  374. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  375. // Update the name item text
  376. if (app->variable_item_logged_out_login_username)
  377. {
  378. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  379. }
  380. // Save the settings
  381. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  382. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  383. }
  384. /**
  385. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  386. * @param context The context - FlipSocialApp object.
  387. * @return void
  388. */
  389. static void flip_social_logged_out_login_password_updated(void *context)
  390. {
  391. FlipSocialApp *app = (FlipSocialApp *)context;
  392. if (!app)
  393. {
  394. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  395. return;
  396. }
  397. // Store the entered password
  398. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  399. // Store the entered password in the change password field
  400. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  401. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  402. // Ensure null-termination
  403. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  404. // Update the password item text
  405. if (app->variable_item_logged_out_login_password)
  406. {
  407. // dont show the password on the screen (version 0.2)
  408. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  409. }
  410. // Save the settings
  411. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  412. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  413. }
  414. /**
  415. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  416. * @param context The context - FlipSocialApp object.
  417. * @param index The index of the selected item.
  418. * @return void
  419. */
  420. static void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  421. {
  422. FlipSocialApp *app = (FlipSocialApp *)context;
  423. if (!app)
  424. {
  425. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  426. return;
  427. }
  428. switch (index)
  429. {
  430. case 0: // Input Username
  431. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  432. break;
  433. case 1: // Input Password
  434. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  435. break;
  436. case 2: // Login Button
  437. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessLogin);
  438. break;
  439. default:
  440. FURI_LOG_E(TAG, "Unknown configuration item index");
  441. break;
  442. }
  443. }
  444. /**
  445. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  446. * @param context The context - FlipSocialApp object.
  447. * @return void
  448. */
  449. static void flip_social_logged_out_register_username_updated(void *context)
  450. {
  451. FlipSocialApp *app = (FlipSocialApp *)context;
  452. if (!app)
  453. {
  454. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  455. return;
  456. }
  457. // Store the entered name
  458. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  459. // Ensure null-termination
  460. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  461. // Update the name item text
  462. if (app->variable_item_logged_out_register_username)
  463. {
  464. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  465. }
  466. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  467. }
  468. /**
  469. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  470. * @param context The context - FlipSocialApp object.
  471. * @return void
  472. */
  473. static void flip_social_logged_out_register_password_updated(void *context)
  474. {
  475. FlipSocialApp *app = (FlipSocialApp *)context;
  476. if (!app)
  477. {
  478. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  479. return;
  480. }
  481. // Store the entered password
  482. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  483. // Ensure null-termination
  484. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  485. // Update the password item text
  486. if (app->variable_item_logged_out_register_password)
  487. {
  488. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  489. }
  490. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  491. }
  492. /**
  493. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  494. * @param context The context - FlipSocialApp object.
  495. * @return void
  496. */
  497. static void flip_social_logged_out_register_password_2_updated(void *context)
  498. {
  499. FlipSocialApp *app = (FlipSocialApp *)context;
  500. if (!app)
  501. {
  502. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  503. return;
  504. }
  505. // Store the entered password
  506. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  507. // Ensure null-termination
  508. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  509. // Update the password item text
  510. if (app->variable_item_logged_out_register_password_2)
  511. {
  512. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  513. }
  514. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  515. }
  516. /**
  517. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  518. * @param context The context - FlipSocialApp object.
  519. * @param index The index of the selected item.
  520. * @return void
  521. */
  522. static void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  523. {
  524. FlipSocialApp *app = (FlipSocialApp *)context;
  525. if (!app)
  526. {
  527. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  528. return;
  529. }
  530. switch (index)
  531. {
  532. case 0: // Input Username
  533. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  534. break;
  535. case 1: // Input Password
  536. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  537. break;
  538. case 2: // Input Password 2
  539. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  540. break;
  541. case 3: // Register button
  542. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessRegister);
  543. break;
  544. default:
  545. FURI_LOG_E(TAG, "Unknown configuration item index");
  546. break;
  547. }
  548. }
  549. /**
  550. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  551. * @param context The context - FlipSocialApp object.
  552. * @return void
  553. */
  554. static void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  555. {
  556. FlipSocialApp *app = (FlipSocialApp *)context;
  557. if (!app)
  558. {
  559. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  560. return;
  561. }
  562. // Store the entered SSID
  563. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  564. // Store the entered SSID in the logged out SSID
  565. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  566. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  567. // Ensure null-termination
  568. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  569. // Update the name item text
  570. if (app->variable_item_logged_in_wifi_settings_ssid)
  571. {
  572. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  573. }
  574. // Save the settings
  575. save_settings(app_instance->wifi_ssid_logged_in, app_instance->wifi_password_logged_in, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  576. // update the wifi settings
  577. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  578. {
  579. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  580. {
  581. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  582. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  583. }
  584. }
  585. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  586. }
  587. /**
  588. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  589. * @param context The context - FlipSocialApp object.
  590. * @return void
  591. */
  592. static void flip_social_logged_in_wifi_settings_password_updated(void *context)
  593. {
  594. FlipSocialApp *app = (FlipSocialApp *)context;
  595. if (!app)
  596. {
  597. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  598. return;
  599. }
  600. // Store the entered password
  601. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  602. // Store the entered password in the logged out password
  603. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  604. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  605. // Ensure null-termination
  606. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  607. // Update the password item text
  608. if (app->variable_item_logged_in_wifi_settings_password)
  609. {
  610. // dont show the password on the screen (version 0.2)
  611. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  612. }
  613. // Save the settings
  614. save_settings(app_instance->wifi_ssid_logged_in, app_instance->wifi_password_logged_in, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  615. // update the wifi settings
  616. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  617. {
  618. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  619. {
  620. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  621. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  622. }
  623. }
  624. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  625. }
  626. /**
  627. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  628. * @param context The context - FlipSocialApp object.
  629. * @param index The index of the selected item.
  630. * @return void
  631. */
  632. static void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  633. {
  634. FlipSocialApp *app = (FlipSocialApp *)context;
  635. if (!app)
  636. {
  637. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  638. return;
  639. }
  640. switch (index)
  641. {
  642. case 0: // Input SSID
  643. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  644. break;
  645. case 1: // Input Password
  646. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  647. break;
  648. default:
  649. FURI_LOG_E(TAG, "Unknown configuration item index");
  650. break;
  651. }
  652. }
  653. /**
  654. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  655. * @param context The context - FlipSocialApp object.
  656. * @return void
  657. */
  658. static void flip_social_logged_in_compose_pre_save_updated(void *context)
  659. {
  660. FlipSocialApp *app = (FlipSocialApp *)context;
  661. if (!app)
  662. {
  663. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  664. return;
  665. }
  666. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  667. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  668. {
  669. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  670. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  671. return;
  672. }
  673. // Store the entered message
  674. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  675. // Ensure null-termination
  676. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  677. // add the item to the submenu
  678. submenu_reset(app->submenu_compose);
  679. // loop through the items and add them to the submenu
  680. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  681. app->pre_saved_messages.count++;
  682. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  683. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  684. {
  685. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  686. }
  687. // save playlist
  688. save_playlist(&app->pre_saved_messages);
  689. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  690. }
  691. /**
  692. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  693. * @param context The context - FlipSocialApp object.
  694. * @return void
  695. */
  696. static void flip_social_logged_in_profile_change_password_updated(void *context)
  697. {
  698. FlipSocialApp *app = (FlipSocialApp *)context;
  699. if (!app)
  700. {
  701. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  702. return;
  703. }
  704. // Correct type: old_pass should be a pointer to a string (char *)
  705. const char *old_password = app->login_password_logged_out;
  706. // Store the entered message
  707. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  708. // store the entered password in the logged out password
  709. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  710. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  711. // Ensure null-termination
  712. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  713. // Update the message item text
  714. if (app->variable_item_logged_in_profile_change_password)
  715. {
  716. // dont show the password on the screen (version 0.2)
  717. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  718. }
  719. // send post request to change password
  720. char payload[256];
  721. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  722. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", "Content-Type: application/json", payload))
  723. {
  724. FURI_LOG_E(TAG, "Failed to send post request to change password");
  725. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  726. return;
  727. }
  728. // Save the settings
  729. save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
  730. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  731. }
  732. /**
  733. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  734. * @param context The context - FlipSocialApp object.
  735. * @param index The index of the selected item.
  736. * @return void
  737. */
  738. static void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  739. {
  740. FlipSocialApp *app = (FlipSocialApp *)context;
  741. if (!app)
  742. {
  743. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  744. return;
  745. }
  746. switch (index)
  747. {
  748. case 0: // Change Username
  749. // do nothing since username cannot be changed
  750. break;
  751. case 1: // Change Password
  752. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  753. break;
  754. case 2: // Friends
  755. // get friends then switch to the friends screen
  756. if (flip_social_get_friends()) // start the async friends request
  757. {
  758. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  759. }
  760. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  761. {
  762. // Wait for the friends to be received
  763. furi_delay_ms(100);
  764. }
  765. furi_timer_stop(fhttp.get_timeout_timer);
  766. if (!flip_social_parse_json_friends()) // parse the JSON before switching to the friends (synchonous)
  767. {
  768. FURI_LOG_E(TAG, "Failed to parse the JSON friends...");
  769. return; // just return for now, no temporary friends yet
  770. // show a popup message saving wifi is disconnected
  771. }
  772. furi_timer_stop(fhttp.get_timeout_timer);
  773. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  774. break;
  775. default:
  776. FURI_LOG_E(TAG, "Unknown configuration item index");
  777. break;
  778. }
  779. }
  780. /**
  781. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  782. * @param context The context - FlipSocialApp object.
  783. * @param index The index of the selected item.
  784. * @return void
  785. */
  786. static void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  787. {
  788. FlipSocialApp *app = (FlipSocialApp *)context;
  789. if (!app)
  790. {
  791. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  792. return;
  793. }
  794. switch (index)
  795. {
  796. case 0: // About
  797. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  798. break;
  799. case 1: // Wifi
  800. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  801. break;
  802. default:
  803. break;
  804. }
  805. }
  806. #endif // FLIP_SOCIAL_CALLBACK_H