flip_social_callback.c 80 KB

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