flip_social_callback.c 96 KB

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