flip_social.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include "flip_social.h"
  2. FlipSocialModel *flip_social_friends = NULL; // Store the friends
  3. FlipSocialModel2 *flip_social_message_users = NULL; // Store the users that have sent messages to the logged in user
  4. FlipSocialModel *flip_social_explore = NULL; // Store the users to explore
  5. FlipSocialMessage *flip_social_messages = NULL; // Store the messages between the logged in user and the selected user
  6. FlipSocialFeedMini *flip_feed_info = NULL; // Store the feed info
  7. FlipSocialFeedItem *flip_feed_item = NULL; // Store a feed item
  8. FlipSocialApp *app_instance = NULL;
  9. bool flip_social_sent_login_request = false;
  10. bool flip_social_sent_register_request = false;
  11. bool flip_social_login_success = false;
  12. bool flip_social_register_success = false;
  13. bool flip_social_dialog_shown = false;
  14. bool flip_social_dialog_stop = false;
  15. bool flip_social_send_message = false;
  16. char *last_explore_response = NULL;
  17. char *selected_message = NULL;
  18. char auth_headers[256] = {0};
  19. void flip_social_loader_free_model(View *view);
  20. /**
  21. * @brief Function to free the resources used by FlipSocialApp.
  22. * @details Cleans up all allocated resources before exiting the application.
  23. * @param app The FlipSocialApp object to free.
  24. * @return void
  25. */
  26. void flip_social_app_free(FlipSocialApp *app)
  27. {
  28. if (!app)
  29. {
  30. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  31. return;
  32. }
  33. if (!app->view_dispatcher)
  34. {
  35. FURI_LOG_E(TAG, "ViewDispatcher is NULL");
  36. return;
  37. }
  38. // Free Submenu(s)
  39. if (app->submenu_logged_out)
  40. {
  41. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  42. submenu_free(app->submenu_logged_out);
  43. }
  44. if (app->submenu_logged_in)
  45. {
  46. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  47. submenu_free(app->submenu_logged_in);
  48. }
  49. if (app->submenu_compose)
  50. {
  51. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  52. submenu_free(app->submenu_compose);
  53. }
  54. if (app->submenu_explore)
  55. {
  56. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  57. submenu_free(app->submenu_explore);
  58. }
  59. if (app->submenu_friends)
  60. {
  61. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  62. submenu_free(app->submenu_friends);
  63. }
  64. if (app->submenu_messages)
  65. {
  66. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  67. submenu_free(app->submenu_messages);
  68. }
  69. if (app->submenu_messages_user_choices)
  70. {
  71. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesUserChoices);
  72. submenu_free(app->submenu_messages_user_choices);
  73. }
  74. // Free Variable Item List(s)
  75. if (app->variable_item_list_logged_out_wifi_settings)
  76. {
  77. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  78. variable_item_list_free(app->variable_item_list_logged_out_wifi_settings);
  79. }
  80. if (app->variable_item_list_logged_out_login)
  81. {
  82. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  83. variable_item_list_free(app->variable_item_list_logged_out_login);
  84. }
  85. if (app->variable_item_list_logged_out_register)
  86. {
  87. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  88. variable_item_list_free(app->variable_item_list_logged_out_register);
  89. }
  90. if (app->variable_item_list_logged_in_profile)
  91. {
  92. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  93. variable_item_list_free(app->variable_item_list_logged_in_profile);
  94. }
  95. if (app->variable_item_list_logged_in_settings)
  96. {
  97. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  98. variable_item_list_free(app->variable_item_list_logged_in_settings);
  99. }
  100. if (app->variable_item_list_logged_in_settings_wifi)
  101. {
  102. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  103. variable_item_list_free(app->variable_item_list_logged_in_settings_wifi);
  104. }
  105. // Free Text Input(s)
  106. if (app->text_input_logged_out_wifi_settings_ssid)
  107. {
  108. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  109. uart_text_input_free(app->text_input_logged_out_wifi_settings_ssid);
  110. }
  111. if (app->text_input_logged_out_wifi_settings_password)
  112. {
  113. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  114. uart_text_input_free(app->text_input_logged_out_wifi_settings_password);
  115. }
  116. if (app->text_input_logged_out_login_username)
  117. {
  118. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  119. uart_text_input_free(app->text_input_logged_out_login_username);
  120. }
  121. if (app->text_input_logged_out_login_password)
  122. {
  123. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  124. uart_text_input_free(app->text_input_logged_out_login_password);
  125. }
  126. if (app->text_input_logged_out_register_username)
  127. {
  128. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  129. uart_text_input_free(app->text_input_logged_out_register_username);
  130. }
  131. if (app->text_input_logged_out_register_password)
  132. {
  133. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  134. uart_text_input_free(app->text_input_logged_out_register_password);
  135. }
  136. if (app->text_input_logged_out_register_password_2)
  137. {
  138. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  139. uart_text_input_free(app->text_input_logged_out_register_password_2);
  140. }
  141. if (app->text_input_logged_in_change_password)
  142. {
  143. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  144. uart_text_input_free(app->text_input_logged_in_change_password);
  145. }
  146. if (app->text_input_logged_in_compose_pre_save_input)
  147. {
  148. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  149. uart_text_input_free(app->text_input_logged_in_compose_pre_save_input);
  150. }
  151. if (app->text_input_logged_in_wifi_settings_ssid)
  152. {
  153. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  154. uart_text_input_free(app->text_input_logged_in_wifi_settings_ssid);
  155. }
  156. if (app->text_input_logged_in_wifi_settings_password)
  157. {
  158. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  159. uart_text_input_free(app->text_input_logged_in_wifi_settings_password);
  160. }
  161. if (app->text_input_logged_in_messages_new_message)
  162. {
  163. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  164. uart_text_input_free(app->text_input_logged_in_messages_new_message);
  165. }
  166. if (app->text_input_logged_in_messages_new_message_user_choices)
  167. {
  168. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  169. uart_text_input_free(app->text_input_logged_in_messages_new_message_user_choices);
  170. }
  171. // Free Widget(s)
  172. if (app->widget_logged_out_about)
  173. {
  174. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  175. widget_free(app->widget_logged_out_about);
  176. }
  177. if (app->widget_logged_in_about)
  178. {
  179. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  180. widget_free(app->widget_logged_in_about);
  181. }
  182. if (app->widget_result)
  183. {
  184. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewWidgetResult);
  185. widget_free(app->widget_result);
  186. }
  187. // Free View(s)
  188. if (app->view_loader)
  189. {
  190. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoader);
  191. flip_social_loader_free_model(app->view_loader);
  192. view_free(app->view_loader);
  193. }
  194. if (app->view_process_login)
  195. {
  196. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessLogin);
  197. view_free(app->view_process_login);
  198. }
  199. if (app->view_process_register)
  200. {
  201. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedOutProcessRegister);
  202. view_free(app->view_process_register);
  203. }
  204. if (app->view_process_feed)
  205. {
  206. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  207. view_free(app->view_process_feed);
  208. }
  209. if (app->view_process_compose)
  210. {
  211. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  212. view_free(app->view_process_compose);
  213. }
  214. if (app->view_process_explore)
  215. {
  216. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInExploreProccess);
  217. view_free(app->view_process_explore);
  218. }
  219. if (app->view_process_friends)
  220. {
  221. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsProcess);
  222. view_free(app->view_process_friends);
  223. }
  224. if (app->view_process_messages)
  225. {
  226. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesProcess);
  227. view_free(app->view_process_messages);
  228. }
  229. if (app->view_dispatcher)
  230. view_dispatcher_free(app->view_dispatcher);
  231. // Free the app structure members
  232. if (app->wifi_ssid_logged_out)
  233. free(app->wifi_ssid_logged_out);
  234. if (app->wifi_ssid_logged_out_temp_buffer)
  235. free(app->wifi_ssid_logged_out_temp_buffer);
  236. if (app->wifi_password_logged_out)
  237. free(app->wifi_password_logged_out);
  238. if (app->wifi_password_logged_out_temp_buffer)
  239. free(app->wifi_password_logged_out_temp_buffer);
  240. if (app->login_username_logged_out)
  241. free(app->login_username_logged_out);
  242. if (app->login_username_logged_out_temp_buffer)
  243. free(app->login_username_logged_out_temp_buffer);
  244. if (app->login_password_logged_out)
  245. free(app->login_password_logged_out);
  246. if (app->login_password_logged_out_temp_buffer)
  247. free(app->login_password_logged_out_temp_buffer);
  248. if (app->register_username_logged_out)
  249. free(app->register_username_logged_out);
  250. if (app->register_username_logged_out_temp_buffer)
  251. free(app->register_username_logged_out_temp_buffer);
  252. if (app->register_password_logged_out)
  253. free(app->register_password_logged_out);
  254. if (app->register_password_logged_out_temp_buffer)
  255. free(app->register_password_logged_out_temp_buffer);
  256. if (app->register_password_2_logged_out)
  257. free(app->register_password_2_logged_out);
  258. if (app->register_password_2_logged_out_temp_buffer)
  259. free(app->register_password_2_logged_out_temp_buffer);
  260. if (app->change_password_logged_in)
  261. free(app->change_password_logged_in);
  262. if (app->change_password_logged_in_temp_buffer)
  263. free(app->change_password_logged_in_temp_buffer);
  264. if (app->compose_pre_save_logged_in)
  265. free(app->compose_pre_save_logged_in);
  266. if (app->compose_pre_save_logged_in_temp_buffer)
  267. free(app->compose_pre_save_logged_in_temp_buffer);
  268. if (app->wifi_ssid_logged_in)
  269. free(app->wifi_ssid_logged_in);
  270. if (app->wifi_ssid_logged_in_temp_buffer)
  271. free(app->wifi_ssid_logged_in_temp_buffer);
  272. if (app->wifi_password_logged_in)
  273. free(app->wifi_password_logged_in);
  274. if (app->wifi_password_logged_in_temp_buffer)
  275. free(app->wifi_password_logged_in_temp_buffer);
  276. if (app->is_logged_in)
  277. free(app->is_logged_in);
  278. if (app->login_username_logged_in)
  279. free(app->login_username_logged_in);
  280. if (app->login_username_logged_in_temp_buffer)
  281. free(app->login_username_logged_in_temp_buffer);
  282. if (app->messages_new_message_logged_in)
  283. free(app->messages_new_message_logged_in);
  284. if (app->messages_new_message_logged_in_temp_buffer)
  285. free(app->messages_new_message_logged_in_temp_buffer);
  286. if (app->message_user_choice_logged_in)
  287. free(app->message_user_choice_logged_in);
  288. if (app->message_user_choice_logged_in_temp_buffer)
  289. free(app->message_user_choice_logged_in_temp_buffer);
  290. if (last_explore_response)
  291. free(last_explore_response);
  292. if (selected_message)
  293. free(selected_message);
  294. if (app->input_event && app->input_event_queue)
  295. furi_pubsub_unsubscribe(app->input_event_queue, app->input_event);
  296. // DeInit UART
  297. flipper_http_deinit();
  298. // Free the app structure
  299. if (app_instance)
  300. free(app_instance);
  301. }
  302. void auth_headers_alloc(void)
  303. {
  304. if (!app_instance)
  305. {
  306. snprintf(auth_headers, sizeof(auth_headers), "{\"Content-Type\":\"application/json\"}");
  307. return;
  308. }
  309. if (app_instance->login_username_logged_out && app_instance->login_password_logged_out && strlen(app_instance->login_username_logged_out) > 0 && strlen(app_instance->login_password_logged_out) > 0)
  310. {
  311. snprintf(auth_headers, sizeof(auth_headers), "{\"Content-Type\":\"application/json\",\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  312. }
  313. else if (app_instance->login_username_logged_in && app_instance->change_password_logged_in && strlen(app_instance->login_username_logged_in) > 0 && strlen(app_instance->change_password_logged_in) > 0)
  314. {
  315. snprintf(auth_headers, sizeof(auth_headers), "{\"Content-Type\":\"application/json\",\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_in, app_instance->change_password_logged_in);
  316. }
  317. else
  318. {
  319. snprintf(auth_headers, sizeof(auth_headers), "{\"Content-Type\":\"application/json\"}");
  320. }
  321. }
  322. FlipSocialFeedMini *flip_feed_info_alloc(void)
  323. {
  324. FlipSocialFeedMini *feed_info = (FlipSocialFeedMini *)malloc(sizeof(FlipSocialFeedMini));
  325. if (!feed_info)
  326. {
  327. FURI_LOG_E(TAG, "Failed to allocate memory for feed_info");
  328. return NULL;
  329. }
  330. feed_info->count = 0;
  331. feed_info->index = 0;
  332. return feed_info;
  333. }
  334. void flip_feed_info_free(void)
  335. {
  336. if (!flip_feed_info)
  337. {
  338. return;
  339. }
  340. free(flip_feed_info);
  341. flip_feed_info = NULL;
  342. }