flip_social_callback.c 80 KB

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