flip_social_callback.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. #include <callback/flip_social_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. static bool about_widget_alloc(bool is_logged_in)
  12. {
  13. if (!is_logged_in)
  14. {
  15. if (!app_instance->widget_logged_out_about)
  16. {
  17. return easy_flipper_set_widget(&app_instance->widget_logged_out_about, FlipSocialViewLoggedOutAbout, "Welcome to FlipSocial\n---\nThe social media app for\nFlipper Zero, created by\nJBlanked: www.flipsocial.net\n---\nPress BACK to return.", flip_social_callback_to_submenu_logged_out, &app_instance->view_dispatcher);
  18. }
  19. }
  20. else
  21. {
  22. if (!app_instance->widget_logged_in_about)
  23. {
  24. return easy_flipper_set_widget(&app_instance->widget_logged_in_about, FlipSocialViewLoggedInSettingsAbout, "Welcome to FlipSocial\n---\nThe social media app for\nFlipper Zero, created by\nJBlanked: www.flipsocial.net\n---\nPress BACK to return.", flip_social_callback_to_settings_logged_in, &app_instance->view_dispatcher);
  25. }
  26. }
  27. return true;
  28. }
  29. static void free_about_widget(bool is_logged_in)
  30. {
  31. if (is_logged_in && app_instance->widget_logged_in_about)
  32. {
  33. widget_free(app_instance->widget_logged_in_about);
  34. app_instance->widget_logged_in_about = NULL;
  35. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  36. }
  37. if (!is_logged_in && app_instance->widget_logged_out_about)
  38. {
  39. widget_free(app_instance->widget_logged_out_about);
  40. app_instance->widget_logged_out_about = NULL;
  41. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedOutAbout);
  42. }
  43. }
  44. static bool pre_saved_messages_alloc(void)
  45. {
  46. if (!app_instance)
  47. {
  48. return false;
  49. }
  50. if (!app_instance->submenu_compose)
  51. {
  52. if (!easy_flipper_set_submenu(&app_instance->submenu_compose, FlipSocialViewLoggedInCompose, "Create A Post", flip_social_callback_to_submenu_logged_in, &app_instance->view_dispatcher))
  53. {
  54. return false;
  55. }
  56. submenu_reset(app_instance->submenu_compose);
  57. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app_instance);
  58. // load the playlist
  59. if (load_playlist(&app_instance->pre_saved_messages))
  60. {
  61. // Update the playlist submenu
  62. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  63. {
  64. if (app_instance->pre_saved_messages.messages[i])
  65. {
  66. submenu_add_item(app_instance->submenu_compose, app_instance->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  67. }
  68. }
  69. }
  70. }
  71. return true;
  72. }
  73. static void free_pre_saved_messages(void)
  74. {
  75. if (app_instance->submenu_compose)
  76. {
  77. submenu_free(app_instance->submenu_compose);
  78. app_instance->submenu_compose = NULL;
  79. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
  80. }
  81. for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
  82. {
  83. if (app_instance->pre_saved_messages.messages[i])
  84. {
  85. free(app_instance->pre_saved_messages.messages[i]);
  86. app_instance->pre_saved_messages.messages[i] = NULL;
  87. }
  88. }
  89. }
  90. static void flip_social_free_friends(void)
  91. {
  92. if (!flip_social_friends)
  93. {
  94. return;
  95. }
  96. free(flip_social_friends);
  97. flip_social_friends = NULL;
  98. if (app_instance->submenu_friends)
  99. {
  100. submenu_free(app_instance->submenu_friends);
  101. app_instance->submenu_friends = NULL;
  102. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  103. }
  104. }
  105. void flip_social_request_error_draw(Canvas *canvas)
  106. {
  107. if (canvas == NULL)
  108. {
  109. FURI_LOG_E(TAG, "flip_social_request_error_draw - canvas is NULL");
  110. DEV_CRASH();
  111. return;
  112. }
  113. if (fhttp.last_response != NULL)
  114. {
  115. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  116. {
  117. canvas_clear(canvas);
  118. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  119. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  120. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  121. }
  122. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  123. {
  124. canvas_clear(canvas);
  125. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  126. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  127. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  128. }
  129. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  130. {
  131. canvas_clear(canvas);
  132. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  133. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  134. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  135. }
  136. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  137. {
  138. canvas_clear(canvas);
  139. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  140. }
  141. else
  142. {
  143. canvas_clear(canvas);
  144. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  145. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  146. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  147. }
  148. }
  149. else
  150. {
  151. canvas_clear(canvas);
  152. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  153. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  154. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  155. }
  156. }
  157. static bool flip_social_login_fetch(DataLoaderModel *model)
  158. {
  159. UNUSED(model);
  160. 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)
  161. {
  162. return false;
  163. }
  164. char buffer[256];
  165. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  166. auth_headers_alloc();
  167. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", auth_headers, buffer))
  168. {
  169. fhttp.state = RECEIVING;
  170. return true;
  171. }
  172. else
  173. {
  174. fhttp.state = ISSUE;
  175. return false;
  176. }
  177. }
  178. static char *flip_social_login_parse(DataLoaderModel *model)
  179. {
  180. UNUSED(model);
  181. // read response
  182. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  183. {
  184. app_instance->is_logged_in = "true";
  185. // set the logged_in_username and change_password_logged_in
  186. if (app_instance->login_username_logged_out)
  187. {
  188. strcpy(app_instance->login_username_logged_in, app_instance->login_username_logged_out);
  189. }
  190. if (app_instance->login_password_logged_out)
  191. {
  192. strcpy(app_instance->change_password_logged_in, app_instance->login_password_logged_out);
  193. }
  194. 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);
  195. // send user to the logged in submenu
  196. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  197. return "Login successful!";
  198. }
  199. else if (strstr(fhttp.last_response, "User not found") != NULL)
  200. {
  201. return "Account not found...";
  202. }
  203. else
  204. {
  205. return "Failed to login...";
  206. }
  207. }
  208. static void flip_social_login_switch_to_view(FlipSocialApp *app)
  209. {
  210. flip_social_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 1, flip_social_callback_to_login_logged_out, FlipSocialViewLoader);
  211. }
  212. static bool flip_social_register_fetch(DataLoaderModel *model)
  213. {
  214. UNUSED(model);
  215. // check if the username and password are valid
  216. if (!app_instance->register_username_logged_out || !app_instance->register_password_logged_out || strlen(app_instance->register_username_logged_out) == 0 || strlen(app_instance->register_password_logged_out) == 0)
  217. {
  218. FURI_LOG_E(TAG, "Username or password is NULL");
  219. return false;
  220. }
  221. // check if both passwords match
  222. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  223. {
  224. FURI_LOG_E(TAG, "Passwords do not match");
  225. return false;
  226. }
  227. char buffer[128];
  228. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  229. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", buffer))
  230. {
  231. // Set the state to RECEIVING to ensure we continue to see the receiving message
  232. fhttp.state = RECEIVING;
  233. return true;
  234. }
  235. else
  236. {
  237. fhttp.state = ISSUE;
  238. return false;
  239. }
  240. }
  241. static char *flip_social_register_parse(DataLoaderModel *model)
  242. {
  243. UNUSED(model);
  244. // read response
  245. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  246. {
  247. // set the login credentials
  248. if (app_instance->login_username_logged_out)
  249. {
  250. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  251. }
  252. if (app_instance->login_password_logged_out)
  253. {
  254. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  255. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  256. }
  257. if (app_instance->login_username_logged_in)
  258. {
  259. app_instance->login_username_logged_in = app_instance->register_username_logged_out;
  260. }
  261. app_instance->is_logged_in = "true";
  262. // update header credentials
  263. auth_headers_alloc();
  264. // save the credentials
  265. 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);
  266. // send user to the logged in submenu
  267. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  268. return "Registration successful!\nWelcome to FlipSocial!";
  269. }
  270. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  271. {
  272. return "Please enter your credentials.\nPress BACK to return.";
  273. }
  274. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  275. {
  276. return "Registration failed...\nUsername already exists.\nPress BACK to return.";
  277. }
  278. else
  279. {
  280. return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
  281. }
  282. }
  283. static void flip_social_register_switch_to_view(FlipSocialApp *app)
  284. {
  285. flip_social_generic_switch_to_view(app, "Registering...", flip_social_register_fetch, flip_social_register_parse, 1, flip_social_callback_to_register_logged_out, FlipSocialViewLoader);
  286. }
  287. /**
  288. * @brief Navigation callback to go back to the submenu Logged out.
  289. * @param context The context - unused
  290. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  291. */
  292. uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  293. {
  294. UNUSED(context);
  295. return FlipSocialViewLoggedOutSubmenu;
  296. }
  297. static void flip_social_free_explore_dialog()
  298. {
  299. if (app_instance->dialog_explore)
  300. {
  301. dialog_ex_free(app_instance->dialog_explore);
  302. app_instance->dialog_explore = NULL;
  303. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewExploreDialog);
  304. }
  305. }
  306. static void flip_social_free_friends_dialog()
  307. {
  308. if (app_instance->dialog_friends)
  309. {
  310. dialog_ex_free(app_instance->dialog_friends);
  311. app_instance->dialog_friends = NULL;
  312. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewFriendsDialog);
  313. }
  314. }
  315. /**
  316. * @brief Navigation callback to go back to the submenu Logged in.
  317. * @param context The context - unused
  318. * @return next view id (FlipSocialViewLoggedInSubmenu)
  319. */
  320. uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  321. {
  322. UNUSED(context);
  323. free_about_widget(false);
  324. //
  325. flip_social_free_friends();
  326. flip_social_free_message_users();
  327. flip_feed_info_free();
  328. if (flip_feed_item)
  329. {
  330. free(flip_feed_item);
  331. flip_feed_item = NULL;
  332. }
  333. // free the about widget if it exists
  334. free_about_widget(true);
  335. flip_social_free_explore_dialog();
  336. flip_social_free_friends_dialog();
  337. return FlipSocialViewLoggedInSubmenu;
  338. }
  339. /**
  340. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  341. * @param context The context - unused
  342. * @return next view id (FlipSocialViewLoggedOutLogin)
  343. */
  344. uint32_t flip_social_callback_to_login_logged_out(void *context)
  345. {
  346. UNUSED(context);
  347. flip_social_sent_login_request = false;
  348. flip_social_login_success = false;
  349. return FlipSocialViewLoggedOutLogin;
  350. }
  351. /**
  352. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  353. * @param context The context - unused
  354. * @return next view id (FlipSocialViewLoggedOutRegister)
  355. */
  356. uint32_t flip_social_callback_to_register_logged_out(void *context)
  357. {
  358. UNUSED(context);
  359. flip_social_sent_register_request = false;
  360. flip_social_register_success = false;
  361. return FlipSocialViewLoggedOutRegister;
  362. }
  363. /**
  364. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  365. * @param context The context - unused
  366. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  367. */
  368. uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  369. {
  370. UNUSED(context);
  371. return FlipSocialViewLoggedOutWifiSettings;
  372. }
  373. /**
  374. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  375. * @param context The context - unused
  376. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  377. */
  378. uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  379. {
  380. UNUSED(context);
  381. return FlipSocialViewLoggedInSettingsWifi;
  382. }
  383. /**
  384. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  385. * @param context The context - unused
  386. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  387. */
  388. uint32_t flip_social_callback_to_settings_logged_in(void *context)
  389. {
  390. UNUSED(context);
  391. return FlipSocialViewLoggedInSettings;
  392. }
  393. /**
  394. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  395. * @param context The context - unused
  396. * @return next view id (FlipSocialViewLoggedInCompose)
  397. */
  398. uint32_t flip_social_callback_to_compose_logged_in(void *context)
  399. {
  400. UNUSED(context);
  401. return FlipSocialViewLoggedInCompose;
  402. }
  403. /**
  404. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  405. * @param context The context - unused
  406. * @return next view id (FlipSocialViewLoggedInProfile)
  407. */
  408. uint32_t flip_social_callback_to_profile_logged_in(void *context)
  409. {
  410. UNUSED(context);
  411. return FlipSocialViewLoggedInProfile;
  412. }
  413. /**
  414. * @brief Navigation callback to bring the user back to the Explore submenu
  415. * @param context The context - unused
  416. * @return next view id (FlipSocialViewLoggedInExploreSubmenu)
  417. */
  418. uint32_t flip_social_callback_to_explore_logged_in(void *context)
  419. {
  420. UNUSED(context);
  421. flip_social_dialog_stop = false;
  422. last_explore_response = "";
  423. flip_social_dialog_shown = false;
  424. if (flip_social_explore)
  425. {
  426. flip_social_explore->index = 0;
  427. }
  428. action = ActionNone;
  429. return FlipSocialViewLoggedInExploreSubmenu;
  430. }
  431. /**
  432. * @brief Navigation callback to bring the user back to the Friends submenu
  433. * @param context The context - unused
  434. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  435. */
  436. uint32_t flip_social_callback_to_friends_logged_in(void *context)
  437. {
  438. UNUSED(context);
  439. flip_social_dialog_stop = false;
  440. last_explore_response = "";
  441. flip_social_dialog_shown = false;
  442. flip_social_friends->index = 0;
  443. action = ActionNone;
  444. return FlipSocialViewLoggedInFriendsSubmenu;
  445. }
  446. /**
  447. * @brief Navigation callback to bring the user back to the Messages submenu
  448. * @param context The context - unused
  449. * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
  450. */
  451. uint32_t flip_social_callback_to_messages_logged_in(void *context)
  452. {
  453. UNUSED(context);
  454. return FlipSocialViewLoggedInMessagesSubmenu;
  455. }
  456. /**
  457. * @brief Navigation callback to bring the user back to the User Choices screen
  458. * @param context The context - unused
  459. * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
  460. */
  461. uint32_t flip_social_callback_to_messages_user_choices(void *context)
  462. {
  463. UNUSED(context);
  464. return FlipSocialViewLoggedInMessagesUserChoices;
  465. }
  466. static void free_flip_social_group()
  467. {
  468. flip_social_free_messages();
  469. flip_social_free_explore();
  470. }
  471. /**
  472. * @brief Navigation callback for exiting the application
  473. * @param context The context - unused
  474. * @return next view id (VIEW_NONE to exit the app)
  475. */
  476. uint32_t flip_social_callback_exit_app(void *context)
  477. {
  478. // Exit the application
  479. UNUSED(context);
  480. free_flip_social_group();
  481. free_pre_saved_messages();
  482. return VIEW_NONE; // Return VIEW_NONE to exit the app
  483. }
  484. static void explore_dialog_callback(DialogExResult result, void *context)
  485. {
  486. furi_assert(context);
  487. FlipSocialApp *app = (FlipSocialApp *)context;
  488. if (result == DialogExResultLeft) // Remove
  489. {
  490. // remove friend
  491. char remove_payload[128];
  492. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  493. auth_headers_alloc();
  494. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
  495. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  496. flip_social_free_explore_dialog();
  497. }
  498. else if (result == DialogExResultRight)
  499. {
  500. // add friend
  501. char add_payload[128];
  502. snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  503. auth_headers_alloc();
  504. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/add-friend/", auth_headers, add_payload);
  505. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  506. flip_social_free_explore_dialog();
  507. }
  508. }
  509. static void friends_dialog_callback(DialogExResult result, void *context)
  510. {
  511. furi_assert(context);
  512. FlipSocialApp *app = (FlipSocialApp *)context;
  513. if (result == DialogExResultLeft) // Remove
  514. {
  515. // remove friend
  516. char remove_payload[128];
  517. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
  518. auth_headers_alloc();
  519. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
  520. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  521. flip_social_free_friends_dialog();
  522. }
  523. }
  524. /**
  525. * @brief Handle ALL submenu item selections.
  526. * @param context The context - FlipSocialApp object.
  527. * @param index The FlipSocialSubmenuIndex item that was clicked.
  528. * @return void
  529. */
  530. void flip_social_callback_submenu_choices(void *context, uint32_t index)
  531. {
  532. FlipSocialApp *app = (FlipSocialApp *)context;
  533. if (!app)
  534. {
  535. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  536. return;
  537. }
  538. switch (index)
  539. {
  540. case FlipSocialSubmenuLoggedOutIndexLogin:
  541. flip_social_sent_login_request = false;
  542. flip_social_login_success = false;
  543. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  544. break;
  545. case FlipSocialSubmenuLoggedOutIndexRegister:
  546. flip_social_sent_register_request = false;
  547. flip_social_register_success = false;
  548. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  549. break;
  550. case FlipSocialSubmenuLoggedOutIndexAbout:
  551. if (!about_widget_alloc(false))
  552. {
  553. return;
  554. }
  555. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  556. break;
  557. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  558. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  559. break;
  560. case FlipSocialSubmenuLoggedInIndexProfile:
  561. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  562. break;
  563. case FlipSocialSubmenuLoggedInIndexMessages:
  564. free_flip_social_group();
  565. flipper_http_loading_task(
  566. flip_social_get_message_users, // get the message users
  567. flip_social_parse_json_message_users, // parse the message users
  568. FlipSocialViewLoggedInMessagesSubmenu, // switch to the messages submenu if successful
  569. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  570. &app->view_dispatcher); // view dispatcher
  571. break;
  572. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  573. flipper_http_loading_task(
  574. flip_social_get_explore, // get the explore users
  575. flip_social_parse_json_message_user_choices, // parse the explore users
  576. FlipSocialViewLoggedInMessagesUserChoices, // switch to the user choices if successful
  577. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  578. &app->view_dispatcher); // view dispatcher
  579. break;
  580. case FlipSocialSubmenuLoggedInIndexFeed:
  581. free_flip_social_group();
  582. // use derek's switch_to_view instead
  583. if (fhttp.state == INACTIVE)
  584. {
  585. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  586. return;
  587. }
  588. if (!easy_flipper_set_loading(&app->loading, FlipSocialViewLoading, flip_social_callback_to_submenu_logged_in, &app->view_dispatcher))
  589. {
  590. FURI_LOG_E(TAG, "Failed to set loading screen");
  591. return; // already on the submenu
  592. }
  593. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoading);
  594. if (flip_social_get_feed()) // start the async request
  595. {
  596. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  597. fhttp.state = RECEIVING;
  598. }
  599. else
  600. {
  601. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  602. fhttp.state = ISSUE;
  603. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  604. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  605. loading_free(app->loading);
  606. return;
  607. }
  608. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  609. {
  610. // Wait for the request to be received
  611. furi_delay_ms(100);
  612. }
  613. furi_timer_stop(fhttp.get_timeout_timer);
  614. // load feed info
  615. flip_feed_info = flip_social_parse_json_feed();
  616. if (!flip_feed_info || flip_feed_info->count < 1)
  617. {
  618. FURI_LOG_E(TAG, "Failed to parse JSON feed");
  619. fhttp.state = ISSUE;
  620. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  621. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  622. loading_free(app->loading);
  623. return;
  624. }
  625. // load the current feed post
  626. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  627. {
  628. FURI_LOG_E(TAG, "Failed to load feed post");
  629. fhttp.state = ISSUE;
  630. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  631. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  632. loading_free(app->loading);
  633. return;
  634. }
  635. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  636. view_dispatcher_remove_view(app->view_dispatcher, FlipSocialViewLoading);
  637. loading_free(app->loading);
  638. free_pre_saved_messages();
  639. break;
  640. case FlipSocialSubmenuExploreIndex:
  641. free_flip_social_group();
  642. flipper_http_loading_task(
  643. flip_social_get_explore, // get the explore users
  644. flip_social_parse_json_explore, // parse the explore users
  645. FlipSocialViewLoggedInExploreSubmenu, // switch to the explore submenu if successful
  646. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  647. &app->view_dispatcher); // view dispatcher
  648. break;
  649. case FlipSocialSubmenuLoggedInIndexCompose:
  650. free_pre_saved_messages();
  651. if (!pre_saved_messages_alloc())
  652. {
  653. FURI_LOG_E(TAG, "Failed to allocate pre-saved messages");
  654. return;
  655. }
  656. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  657. break;
  658. case FlipSocialSubmenuLoggedInIndexSettings:
  659. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  660. break;
  661. case FlipSocialSubmenuLoggedInSignOutButton:
  662. free_flip_social_group();
  663. app->is_logged_in = "false";
  664. save_settings(app->wifi_ssid_logged_out, app->wifi_password_logged_out, app->login_username_logged_out, app->login_username_logged_in, app->login_password_logged_out, app->change_password_logged_in, app->is_logged_in);
  665. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  666. break;
  667. case FlipSocialSubmenuComposeIndexAddPreSave:
  668. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  669. break;
  670. default:
  671. action = ActionNone;
  672. // Handle the pre-saved message selection (has a max of 25 items)
  673. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  674. {
  675. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  676. snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  677. if (!selected_message)
  678. {
  679. FURI_LOG_E(TAG, "Selected message is NULL");
  680. return;
  681. }
  682. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProcessCompose);
  683. }
  684. // Handle the explore selection
  685. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  686. {
  687. if (!flip_social_explore)
  688. {
  689. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  690. return;
  691. }
  692. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  693. flip_social_free_explore_dialog();
  694. if (!app->dialog_explore)
  695. {
  696. if (!easy_flipper_set_dialog_ex(
  697. &app->dialog_explore,
  698. FlipSocialViewExploreDialog,
  699. "User Options",
  700. 0,
  701. 0,
  702. flip_social_explore->usernames[flip_social_explore->index],
  703. 0,
  704. 10,
  705. "Remove",
  706. "Add",
  707. NULL,
  708. explore_dialog_callback,
  709. flip_social_callback_to_explore_logged_in,
  710. &app->view_dispatcher,
  711. app))
  712. {
  713. return;
  714. }
  715. }
  716. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  717. }
  718. // handle the friends selection
  719. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  720. {
  721. if (!flip_social_friends)
  722. {
  723. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  724. return;
  725. }
  726. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  727. flip_social_free_friends_dialog();
  728. if (!app->dialog_friends)
  729. {
  730. if (!easy_flipper_set_dialog_ex(
  731. &app->dialog_friends,
  732. FlipSocialViewFriendsDialog,
  733. "User Options",
  734. 0,
  735. 0,
  736. flip_social_friends->usernames[flip_social_friends->index],
  737. 0,
  738. 10,
  739. "Remove",
  740. "",
  741. NULL,
  742. friends_dialog_callback,
  743. flip_social_callback_to_friends_logged_in,
  744. &app->view_dispatcher,
  745. app))
  746. {
  747. return;
  748. }
  749. }
  750. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFriendsDialog);
  751. }
  752. // handle the messages selection
  753. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
  754. {
  755. if (!flip_social_message_users)
  756. {
  757. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  758. return;
  759. }
  760. flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  761. flipper_http_loading_task(
  762. flip_social_get_messages_with_user, // get the messages with the selected user
  763. flip_social_parse_json_messages, // parse the messages
  764. FlipSocialViewLoggedInMessagesProcess, // switch to the messages process if successful
  765. FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
  766. &app->view_dispatcher); // view dispatcher
  767. }
  768. // handle the messages user choices selection
  769. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
  770. {
  771. if (!flip_social_explore)
  772. {
  773. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  774. return;
  775. }
  776. flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  777. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  778. }
  779. else
  780. {
  781. FURI_LOG_E(TAG, "Unknown submenu index");
  782. }
  783. break;
  784. }
  785. }
  786. /**
  787. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  788. * @param context The context - FlipSocialApp object.
  789. * @return void
  790. */
  791. void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  792. {
  793. FlipSocialApp *app = (FlipSocialApp *)context;
  794. if (!app)
  795. {
  796. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  797. return;
  798. }
  799. // Store the entered name
  800. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  801. // Store the entered name in the logged in name field
  802. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  803. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  804. // Ensure null-termination
  805. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  806. // Update the name item text
  807. if (app->variable_item_logged_out_wifi_settings_ssid)
  808. {
  809. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  810. }
  811. // update the wifi settings
  812. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  813. {
  814. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  815. {
  816. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  817. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  818. }
  819. }
  820. // Save the settings
  821. 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);
  822. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  823. }
  824. /**
  825. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  826. * @param context The context - FlipSocialApp object.
  827. * @return void
  828. */
  829. void flip_social_logged_out_wifi_settings_password_updated(void *context)
  830. {
  831. FlipSocialApp *app = (FlipSocialApp *)context;
  832. if (!app)
  833. {
  834. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  835. return;
  836. }
  837. // Store the entered WiFi password
  838. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  839. // Store the entered WiFi password in the logged in password field
  840. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  841. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  842. // Ensure null-termination
  843. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  844. // Update the password item text
  845. if (app->variable_item_logged_out_wifi_settings_password)
  846. {
  847. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  848. }
  849. // update the wifi settings
  850. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  851. {
  852. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  853. {
  854. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  855. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  856. }
  857. }
  858. // Save the settings
  859. 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);
  860. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  861. }
  862. /**
  863. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  864. * @param context The context - FlipSocialApp object.
  865. * @param index The index of the selected item.
  866. * @return void
  867. */
  868. void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  869. {
  870. FlipSocialApp *app = (FlipSocialApp *)context;
  871. if (!app)
  872. {
  873. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  874. return;
  875. }
  876. switch (index)
  877. {
  878. case 0: // Input SSID
  879. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  880. break;
  881. case 1: // Input Password
  882. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  883. break;
  884. default:
  885. FURI_LOG_E(TAG, "Unknown configuration item index");
  886. break;
  887. }
  888. }
  889. /**
  890. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  891. * @param context The context - FlipSocialApp object.
  892. * @return void
  893. */
  894. void flip_social_logged_out_login_username_updated(void *context)
  895. {
  896. FlipSocialApp *app = (FlipSocialApp *)context;
  897. if (!app)
  898. {
  899. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  900. return;
  901. }
  902. // Store the entered name
  903. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  904. // Store the entered name in the logged in username field
  905. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  906. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  907. // Ensure null-termination
  908. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  909. // Update the name item text
  910. if (app->variable_item_logged_out_login_username)
  911. {
  912. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  913. }
  914. // Save the settings
  915. 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);
  916. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  917. }
  918. /**
  919. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  920. * @param context The context - FlipSocialApp object.
  921. * @return void
  922. */
  923. void flip_social_logged_out_login_password_updated(void *context)
  924. {
  925. FlipSocialApp *app = (FlipSocialApp *)context;
  926. if (!app)
  927. {
  928. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  929. return;
  930. }
  931. // Store the entered password
  932. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  933. // Store the entered password in the change password field
  934. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  935. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  936. // Ensure null-termination
  937. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  938. // Update the password item text
  939. if (app->variable_item_logged_out_login_password)
  940. {
  941. // dont show the password on the screen (version 0.2)
  942. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  943. }
  944. // Save the settings
  945. 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);
  946. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  947. }
  948. /**
  949. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  950. * @param context The context - FlipSocialApp object.
  951. * @param index The index of the selected item.
  952. * @return void
  953. */
  954. void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  955. {
  956. FlipSocialApp *app = (FlipSocialApp *)context;
  957. if (!app)
  958. {
  959. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  960. return;
  961. }
  962. switch (index)
  963. {
  964. case 0: // Input Username
  965. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  966. break;
  967. case 1: // Input Password
  968. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  969. break;
  970. case 2: // Login Button
  971. flip_social_login_switch_to_view(app);
  972. break;
  973. default:
  974. FURI_LOG_E(TAG, "Unknown configuration item index");
  975. break;
  976. }
  977. }
  978. /**
  979. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  980. * @param context The context - FlipSocialApp object.
  981. * @return void
  982. */
  983. void flip_social_logged_out_register_username_updated(void *context)
  984. {
  985. FlipSocialApp *app = (FlipSocialApp *)context;
  986. if (!app)
  987. {
  988. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  989. return;
  990. }
  991. // Store the entered name
  992. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  993. // Ensure null-termination
  994. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  995. // Update the name item text
  996. if (app->variable_item_logged_out_register_username)
  997. {
  998. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  999. }
  1000. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1001. }
  1002. /**
  1003. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  1004. * @param context The context - FlipSocialApp object.
  1005. * @return void
  1006. */
  1007. void flip_social_logged_out_register_password_updated(void *context)
  1008. {
  1009. FlipSocialApp *app = (FlipSocialApp *)context;
  1010. if (!app)
  1011. {
  1012. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1013. return;
  1014. }
  1015. // Store the entered password
  1016. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  1017. // Ensure null-termination
  1018. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  1019. // Update the password item text
  1020. if (app->variable_item_logged_out_register_password)
  1021. {
  1022. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  1023. }
  1024. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1025. }
  1026. /**
  1027. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  1028. * @param context The context - FlipSocialApp object.
  1029. * @return void
  1030. */
  1031. void flip_social_logged_out_register_password_2_updated(void *context)
  1032. {
  1033. FlipSocialApp *app = (FlipSocialApp *)context;
  1034. if (!app)
  1035. {
  1036. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1037. return;
  1038. }
  1039. // Store the entered password
  1040. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  1041. // Ensure null-termination
  1042. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  1043. // Update the password item text
  1044. if (app->variable_item_logged_out_register_password_2)
  1045. {
  1046. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  1047. }
  1048. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1049. }
  1050. /**
  1051. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  1052. * @param context The context - FlipSocialApp object.
  1053. * @param index The index of the selected item.
  1054. * @return void
  1055. */
  1056. void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  1057. {
  1058. FlipSocialApp *app = (FlipSocialApp *)context;
  1059. if (!app)
  1060. {
  1061. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1062. return;
  1063. }
  1064. switch (index)
  1065. {
  1066. case 0: // Input Username
  1067. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  1068. break;
  1069. case 1: // Input Password
  1070. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  1071. break;
  1072. case 2: // Input Password 2
  1073. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  1074. break;
  1075. case 3: // Register button
  1076. flip_social_register_switch_to_view(app);
  1077. break;
  1078. default:
  1079. FURI_LOG_E(TAG, "Unknown configuration item index");
  1080. break;
  1081. }
  1082. }
  1083. /**
  1084. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1085. * @param context The context - FlipSocialApp object.
  1086. * @return void
  1087. */
  1088. void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1089. {
  1090. FlipSocialApp *app = (FlipSocialApp *)context;
  1091. if (!app)
  1092. {
  1093. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1094. return;
  1095. }
  1096. // Store the entered SSID
  1097. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1098. // Store the entered SSID in the logged out SSID
  1099. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1100. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1101. // Ensure null-termination
  1102. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1103. // Update the name item text
  1104. if (app->variable_item_logged_in_wifi_settings_ssid)
  1105. {
  1106. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1107. }
  1108. // Save the settings
  1109. 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);
  1110. // update the wifi settings
  1111. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1112. {
  1113. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1114. {
  1115. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1116. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1117. }
  1118. }
  1119. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1120. }
  1121. /**
  1122. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1123. * @param context The context - FlipSocialApp object.
  1124. * @return void
  1125. */
  1126. void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1127. {
  1128. FlipSocialApp *app = (FlipSocialApp *)context;
  1129. if (!app)
  1130. {
  1131. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1132. return;
  1133. }
  1134. // Store the entered password
  1135. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1136. // Store the entered password in the logged out password
  1137. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1138. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1139. // Ensure null-termination
  1140. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1141. // Update the password item text
  1142. if (app->variable_item_logged_in_wifi_settings_password)
  1143. {
  1144. // dont show the password on the screen (version 0.2)
  1145. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1146. }
  1147. // Save the settings
  1148. 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);
  1149. // update the wifi settings
  1150. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1151. {
  1152. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1153. {
  1154. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1155. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1156. }
  1157. }
  1158. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1159. }
  1160. /**
  1161. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1162. * @param context The context - FlipSocialApp object.
  1163. * @param index The index of the selected item.
  1164. * @return void
  1165. */
  1166. void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1167. {
  1168. FlipSocialApp *app = (FlipSocialApp *)context;
  1169. if (!app)
  1170. {
  1171. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1172. return;
  1173. }
  1174. switch (index)
  1175. {
  1176. case 0: // Input SSID
  1177. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1178. break;
  1179. case 1: // Input Password
  1180. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1181. break;
  1182. default:
  1183. FURI_LOG_E(TAG, "Unknown configuration item index");
  1184. break;
  1185. }
  1186. }
  1187. /**
  1188. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1189. * @param context The context - FlipSocialApp object.
  1190. * @return void
  1191. */
  1192. void flip_social_logged_in_compose_pre_save_updated(void *context)
  1193. {
  1194. FlipSocialApp *app = (FlipSocialApp *)context;
  1195. if (!app)
  1196. {
  1197. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1198. return;
  1199. }
  1200. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  1201. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1202. {
  1203. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1204. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1205. return;
  1206. }
  1207. // Store the entered message
  1208. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  1209. // Ensure null-termination
  1210. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  1211. // add the item to the submenu
  1212. submenu_reset(app->submenu_compose);
  1213. // loop through the items and add them to the submenu
  1214. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  1215. app->pre_saved_messages.count++;
  1216. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  1217. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  1218. {
  1219. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  1220. }
  1221. // save playlist
  1222. save_playlist(&app->pre_saved_messages);
  1223. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1224. }
  1225. /**
  1226. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1227. * @param context The context - FlipSocialApp object.
  1228. * @return void
  1229. */
  1230. void flip_social_logged_in_profile_change_password_updated(void *context)
  1231. {
  1232. FlipSocialApp *app = (FlipSocialApp *)context;
  1233. if (!app)
  1234. {
  1235. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1236. return;
  1237. }
  1238. // Correct type: old_pass should be a pointer to a string (char *)
  1239. const char *old_password = app->login_password_logged_out;
  1240. // Store the entered message
  1241. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1242. // store the entered password in the logged out password
  1243. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1244. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1245. // Ensure null-termination
  1246. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1247. // Update the message item text
  1248. if (app->variable_item_logged_in_profile_change_password)
  1249. {
  1250. // dont show the password on the screen (version 0.2)
  1251. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1252. }
  1253. // send post request to change password
  1254. auth_headers_alloc();
  1255. char payload[256];
  1256. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1257. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
  1258. {
  1259. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1260. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1261. return;
  1262. }
  1263. // Save the settings
  1264. 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);
  1265. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  1266. }
  1267. /**
  1268. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1269. * @param context The context - FlipSocialApp object.
  1270. * @param index The index of the selected item.
  1271. * @return void
  1272. */
  1273. void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1274. {
  1275. FlipSocialApp *app = (FlipSocialApp *)context;
  1276. if (!app)
  1277. {
  1278. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1279. return;
  1280. }
  1281. switch (index)
  1282. {
  1283. case 0: // Change Username
  1284. // do nothing since username cannot be changed
  1285. break;
  1286. case 1: // Change Password
  1287. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1288. break;
  1289. case 2: // Friends
  1290. flip_social_free_friends();
  1291. free_flip_social_group();
  1292. if (!app->submenu_friends)
  1293. {
  1294. if (!easy_flipper_set_submenu(&app->submenu_friends, FlipSocialViewLoggedInFriendsSubmenu, "Friends", flip_social_callback_to_profile_logged_in, &app->view_dispatcher))
  1295. {
  1296. FURI_LOG_DEV(TAG, "Failed to set submenu for friends");
  1297. return;
  1298. }
  1299. }
  1300. flipper_http_loading_task(flip_social_get_friends, flip_social_parse_json_friends, FlipSocialViewLoggedInFriendsSubmenu, FlipSocialViewLoggedInProfile, &app->view_dispatcher);
  1301. break;
  1302. default:
  1303. FURI_LOG_E(TAG, "Unknown configuration item index");
  1304. break;
  1305. }
  1306. }
  1307. /**
  1308. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  1309. * @param context The context - FlipSocialApp object.
  1310. * @param index The index of the selected item.
  1311. * @return void
  1312. */
  1313. void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  1314. {
  1315. FlipSocialApp *app = (FlipSocialApp *)context;
  1316. if (!app)
  1317. {
  1318. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1319. return;
  1320. }
  1321. switch (index)
  1322. {
  1323. case 0: // About
  1324. if (!about_widget_alloc(true))
  1325. {
  1326. return;
  1327. }
  1328. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  1329. break;
  1330. case 1: // Wifi
  1331. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1332. break;
  1333. default:
  1334. break;
  1335. }
  1336. }
  1337. /**
  1338. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  1339. * @param context The context - FlipSocialApp object.
  1340. * @return void
  1341. */
  1342. void flip_social_logged_in_messages_user_choice_message_updated(void *context)
  1343. {
  1344. FlipSocialApp *app = (FlipSocialApp *)context;
  1345. if (!app)
  1346. {
  1347. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1348. return;
  1349. }
  1350. // check if the message is empty
  1351. if (app->message_user_choice_logged_in_temp_buffer_size == 0)
  1352. {
  1353. FURI_LOG_E(TAG, "Message is empty");
  1354. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  1355. return;
  1356. }
  1357. // Store the entered message
  1358. strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
  1359. // Ensure null-termination
  1360. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
  1361. // send post request to send message
  1362. auth_headers_alloc();
  1363. char url[128];
  1364. char payload[256];
  1365. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1366. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
  1367. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1368. {
  1369. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1370. fhttp.state = RECEIVING;
  1371. }
  1372. else
  1373. {
  1374. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1375. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1376. fhttp.state = ISSUE;
  1377. return;
  1378. }
  1379. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1380. {
  1381. // Wait for the request to be received
  1382. furi_delay_ms(10);
  1383. }
  1384. furi_timer_stop(fhttp.get_timeout_timer);
  1385. // add user to the message list
  1386. strncpy(flip_social_message_users->usernames[flip_social_message_users->count], flip_social_explore->usernames[flip_social_explore->index], strlen(flip_social_explore->usernames[flip_social_explore->index]));
  1387. flip_social_message_users->count++;
  1388. // redraw submenu
  1389. flip_social_update_messages_submenu();
  1390. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1391. }
  1392. /**
  1393. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1394. * @param context The context - FlipSocialApp object.
  1395. * @return void
  1396. */
  1397. void flip_social_logged_in_messages_new_message_updated(void *context)
  1398. {
  1399. FlipSocialApp *app = (FlipSocialApp *)context;
  1400. if (!app)
  1401. {
  1402. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1403. return;
  1404. }
  1405. // check if the message is empty
  1406. if (app->messages_new_message_logged_in_temp_buffer_size == 0)
  1407. {
  1408. FURI_LOG_E(TAG, "Message is empty");
  1409. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1410. return;
  1411. }
  1412. // Store the entered message
  1413. strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
  1414. // Ensure null-termination
  1415. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
  1416. // send post request to send message
  1417. auth_headers_alloc();
  1418. char url[128];
  1419. char payload[256];
  1420. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1421. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
  1422. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1423. {
  1424. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1425. fhttp.state = RECEIVING;
  1426. }
  1427. else
  1428. {
  1429. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1430. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1431. fhttp.state = ISSUE;
  1432. return;
  1433. }
  1434. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1435. {
  1436. // Wait for the request to be received
  1437. furi_delay_ms(10);
  1438. }
  1439. furi_timer_stop(fhttp.get_timeout_timer);
  1440. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1441. }
  1442. static void flip_social_widget_set_text(char *message, Widget **widget)
  1443. {
  1444. if (widget == NULL)
  1445. {
  1446. FURI_LOG_E(TAG, "flip_social_set_widget_text - widget is NULL");
  1447. DEV_CRASH();
  1448. return;
  1449. }
  1450. if (message == NULL)
  1451. {
  1452. FURI_LOG_E(TAG, "flip_social_set_widget_text - message is NULL");
  1453. DEV_CRASH();
  1454. return;
  1455. }
  1456. widget_reset(*widget);
  1457. uint32_t message_length = strlen(message); // Length of the message
  1458. uint32_t i = 0; // Index tracker
  1459. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1460. char *formatted_message; // Buffer to hold the final formatted message
  1461. // Allocate buffer with double the message length plus one for safety
  1462. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1463. {
  1464. return;
  1465. }
  1466. while (i < message_length)
  1467. {
  1468. uint32_t max_line_length = 31; // Maximum characters per line
  1469. uint32_t remaining_length = message_length - i; // Remaining characters
  1470. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1471. // Check for newline character within the current segment
  1472. uint32_t newline_pos = i;
  1473. bool found_newline = false;
  1474. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1475. {
  1476. if (message[newline_pos] == '\n')
  1477. {
  1478. found_newline = true;
  1479. break;
  1480. }
  1481. }
  1482. if (found_newline)
  1483. {
  1484. // If newline found, set line_length up to the newline
  1485. line_length = newline_pos - i;
  1486. }
  1487. // Temporary buffer to hold the current line
  1488. char line[32];
  1489. strncpy(line, message + i, line_length);
  1490. line[line_length] = '\0';
  1491. // If newline was found, skip it for the next iteration
  1492. if (found_newline)
  1493. {
  1494. i += line_length + 1; // +1 to skip the '\n' character
  1495. }
  1496. else
  1497. {
  1498. // Check if the line ends in the middle of a word and adjust accordingly
  1499. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1500. {
  1501. // Find the last space within the current line to avoid breaking a word
  1502. char *last_space = strrchr(line, ' ');
  1503. if (last_space != NULL)
  1504. {
  1505. // Adjust the line_length to avoid cutting the word
  1506. line_length = last_space - line;
  1507. line[line_length] = '\0'; // Null-terminate at the space
  1508. }
  1509. }
  1510. // Move the index forward by the determined line_length
  1511. i += line_length;
  1512. // Skip any spaces at the beginning of the next line
  1513. while (i < message_length && message[i] == ' ')
  1514. {
  1515. i++;
  1516. }
  1517. }
  1518. // Manually copy the fixed line into the formatted_message buffer
  1519. for (uint32_t j = 0; j < line_length; j++)
  1520. {
  1521. formatted_message[formatted_index++] = line[j];
  1522. }
  1523. // Add a newline character for line spacing
  1524. formatted_message[formatted_index++] = '\n';
  1525. }
  1526. // Null-terminate the formatted_message
  1527. formatted_message[formatted_index] = '\0';
  1528. // Add the formatted message to the widget
  1529. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1530. }
  1531. void flip_social_loader_draw_callback(Canvas *canvas, void *model)
  1532. {
  1533. if (!canvas || !model)
  1534. {
  1535. FURI_LOG_E(TAG, "flip_social_loader_draw_callback - canvas or model is NULL");
  1536. return;
  1537. }
  1538. SerialState http_state = fhttp.state;
  1539. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1540. DataState data_state = data_loader_model->data_state;
  1541. char *title = data_loader_model->title;
  1542. canvas_set_font(canvas, FontSecondary);
  1543. if (http_state == INACTIVE)
  1544. {
  1545. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1546. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1547. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1548. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1549. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1550. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1551. return;
  1552. }
  1553. if (data_state == DataStateError || data_state == DataStateParseError)
  1554. {
  1555. flip_social_request_error_draw(canvas);
  1556. return;
  1557. }
  1558. canvas_draw_str(canvas, 0, 7, title);
  1559. canvas_draw_str(canvas, 0, 17, "Loading...");
  1560. if (data_state == DataStateInitial)
  1561. {
  1562. return;
  1563. }
  1564. if (http_state == SENDING)
  1565. {
  1566. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1567. return;
  1568. }
  1569. if (http_state == RECEIVING || data_state == DataStateRequested)
  1570. {
  1571. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1572. return;
  1573. }
  1574. if (http_state == IDLE && data_state == DataStateReceived)
  1575. {
  1576. canvas_draw_str(canvas, 0, 27, "Processing...");
  1577. return;
  1578. }
  1579. if (http_state == IDLE && data_state == DataStateParsed)
  1580. {
  1581. canvas_draw_str(canvas, 0, 27, "Processed...");
  1582. return;
  1583. }
  1584. }
  1585. static void flip_social_loader_process_callback(void *context)
  1586. {
  1587. if (context == NULL)
  1588. {
  1589. FURI_LOG_E(TAG, "flip_social_loader_process_callback - context is NULL");
  1590. DEV_CRASH();
  1591. return;
  1592. }
  1593. FlipSocialApp *app = (FlipSocialApp *)context;
  1594. View *view = app->view_loader;
  1595. DataState current_data_state;
  1596. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1597. if (current_data_state == DataStateInitial)
  1598. {
  1599. with_view_model(
  1600. view,
  1601. DataLoaderModel * model,
  1602. {
  1603. model->data_state = DataStateRequested;
  1604. DataLoaderFetch fetch = model->fetcher;
  1605. if (fetch == NULL)
  1606. {
  1607. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1608. model->data_state = DataStateError;
  1609. return;
  1610. }
  1611. // Clear any previous responses
  1612. strncpy(fhttp.last_response, "", 1);
  1613. bool request_status = fetch(model);
  1614. if (!request_status)
  1615. {
  1616. model->data_state = DataStateError;
  1617. }
  1618. },
  1619. true);
  1620. }
  1621. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1622. {
  1623. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1624. {
  1625. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1626. {
  1627. FURI_LOG_DEV(TAG, "PONG received.");
  1628. }
  1629. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1630. {
  1631. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1632. }
  1633. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1634. {
  1635. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1636. }
  1637. else if (strlen(fhttp.last_response) == 0)
  1638. {
  1639. // Still waiting on response
  1640. }
  1641. else
  1642. {
  1643. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1644. }
  1645. }
  1646. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1647. {
  1648. // continue waiting
  1649. }
  1650. else if (fhttp.state == INACTIVE)
  1651. {
  1652. // inactive. try again
  1653. }
  1654. else if (fhttp.state == ISSUE)
  1655. {
  1656. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1657. }
  1658. else
  1659. {
  1660. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1661. DEV_CRASH();
  1662. }
  1663. }
  1664. else if (current_data_state == DataStateReceived)
  1665. {
  1666. with_view_model(
  1667. view,
  1668. DataLoaderModel * model,
  1669. {
  1670. char *data_text;
  1671. if (model->parser == NULL)
  1672. {
  1673. data_text = NULL;
  1674. FURI_LOG_DEV(TAG, "Parser is NULL");
  1675. DEV_CRASH();
  1676. }
  1677. else
  1678. {
  1679. data_text = model->parser(model);
  1680. }
  1681. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1682. model->data_text = data_text;
  1683. if (data_text == NULL)
  1684. {
  1685. model->data_state = DataStateParseError;
  1686. }
  1687. else
  1688. {
  1689. model->data_state = DataStateParsed;
  1690. }
  1691. },
  1692. true);
  1693. }
  1694. else if (current_data_state == DataStateParsed)
  1695. {
  1696. with_view_model(
  1697. view,
  1698. DataLoaderModel * model,
  1699. {
  1700. if (++model->request_index < model->request_count)
  1701. {
  1702. model->data_state = DataStateInitial;
  1703. }
  1704. else
  1705. {
  1706. flip_social_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app_instance->widget_result);
  1707. if (model->data_text != NULL)
  1708. {
  1709. free(model->data_text);
  1710. model->data_text = NULL;
  1711. }
  1712. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  1713. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
  1714. }
  1715. },
  1716. true);
  1717. }
  1718. }
  1719. static void flip_social_loader_timer_callback(void *context)
  1720. {
  1721. if (context == NULL)
  1722. {
  1723. FURI_LOG_E(TAG, "flip_social_loader_timer_callback - context is NULL");
  1724. DEV_CRASH();
  1725. return;
  1726. }
  1727. FlipSocialApp *app = (FlipSocialApp *)context;
  1728. view_dispatcher_send_custom_event(app->view_dispatcher, FlipSocialCustomEventProcess);
  1729. }
  1730. static void flip_social_loader_on_enter(void *context)
  1731. {
  1732. if (context == NULL)
  1733. {
  1734. FURI_LOG_E(TAG, "flip_social_loader_on_enter - context is NULL");
  1735. DEV_CRASH();
  1736. return;
  1737. }
  1738. FlipSocialApp *app = (FlipSocialApp *)context;
  1739. View *view = app->view_loader;
  1740. with_view_model(
  1741. view,
  1742. DataLoaderModel * model,
  1743. {
  1744. view_set_previous_callback(view, model->back_callback);
  1745. if (model->timer == NULL)
  1746. {
  1747. model->timer = furi_timer_alloc(flip_social_loader_timer_callback, FuriTimerTypePeriodic, app);
  1748. }
  1749. furi_timer_start(model->timer, 250);
  1750. },
  1751. true);
  1752. }
  1753. static void flip_social_loader_on_exit(void *context)
  1754. {
  1755. if (context == NULL)
  1756. {
  1757. FURI_LOG_E(TAG, "flip_social_loader_on_exit - context is NULL");
  1758. DEV_CRASH();
  1759. return;
  1760. }
  1761. FlipSocialApp *app = (FlipSocialApp *)context;
  1762. View *view = app->view_loader;
  1763. with_view_model(
  1764. view,
  1765. DataLoaderModel * model,
  1766. {
  1767. if (model->timer)
  1768. {
  1769. furi_timer_stop(model->timer);
  1770. }
  1771. },
  1772. false);
  1773. }
  1774. void flip_social_loader_init(View *view)
  1775. {
  1776. if (view == NULL)
  1777. {
  1778. FURI_LOG_E(TAG, "flip_social_loader_init - view is NULL");
  1779. DEV_CRASH();
  1780. return;
  1781. }
  1782. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1783. view_set_enter_callback(view, flip_social_loader_on_enter);
  1784. view_set_exit_callback(view, flip_social_loader_on_exit);
  1785. }
  1786. void flip_social_loader_free_model(View *view)
  1787. {
  1788. if (view == NULL)
  1789. {
  1790. FURI_LOG_E(TAG, "flip_social_loader_free_model - view is NULL");
  1791. DEV_CRASH();
  1792. return;
  1793. }
  1794. with_view_model(
  1795. view,
  1796. DataLoaderModel * model,
  1797. {
  1798. if (model->timer)
  1799. {
  1800. furi_timer_free(model->timer);
  1801. model->timer = NULL;
  1802. }
  1803. if (model->parser_context)
  1804. {
  1805. free(model->parser_context);
  1806. model->parser_context = NULL;
  1807. }
  1808. },
  1809. false);
  1810. }
  1811. bool flip_social_custom_event_callback(void *context, uint32_t index)
  1812. {
  1813. if (context == NULL)
  1814. {
  1815. FURI_LOG_E(TAG, "flip_social_custom_event_callback - context is NULL");
  1816. DEV_CRASH();
  1817. return false;
  1818. }
  1819. switch (index)
  1820. {
  1821. case FlipSocialCustomEventProcess:
  1822. flip_social_loader_process_callback(context);
  1823. return true;
  1824. default:
  1825. FURI_LOG_DEV(TAG, "flip_social_custom_event_callback. Unknown index: %ld", index);
  1826. return false;
  1827. }
  1828. }
  1829. void flip_social_generic_switch_to_view(FlipSocialApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1830. {
  1831. if (app == NULL)
  1832. {
  1833. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - app is NULL");
  1834. DEV_CRASH();
  1835. return;
  1836. }
  1837. View *view = app->view_loader;
  1838. if (view == NULL)
  1839. {
  1840. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - view is NULL");
  1841. DEV_CRASH();
  1842. return;
  1843. }
  1844. with_view_model(
  1845. view,
  1846. DataLoaderModel * model,
  1847. {
  1848. model->title = title;
  1849. model->fetcher = fetcher;
  1850. model->parser = parser;
  1851. model->request_index = 0;
  1852. model->request_count = request_count;
  1853. model->back_callback = back;
  1854. model->data_state = DataStateInitial;
  1855. model->data_text = NULL;
  1856. },
  1857. true);
  1858. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1859. }