flip_social_callback.c 96 KB

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