flip_social_callback.c 98 KB

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