flip_social_callback.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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 FlipSocialViewSubmenu;
  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 FlipSocialViewSubmenu;
  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 (FlipSocialViewSubmenu)
  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 FlipSocialViewSubmenu;
  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 FlipSocialViewSubmenu;
  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 FlipSocialViewSubmenu;
  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 FlipSocialViewSubmenu;
  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, 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, FlipSocialViewSubmenu);
  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, FlipSocialViewSubmenu);
  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, FlipSocialViewSubmenu);
  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, updated_user_message(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, FlipSocialViewWidgetResult);
  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, updated_user_message(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, FlipSocialViewWidgetResult);
  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_text_input();
  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);
  503. submenu_add_item(app_instance->submenu, "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,
  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, FlipSocialViewSubmenu);
  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_view();
  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_view_alloc())
  594. {
  595. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  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_view();
  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_view_alloc())
  621. {
  622. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  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_view();
  676. if (feed_view_alloc())
  677. {
  678. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFeed);
  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);
  720. char *friends = get_json_value("friends", fhttp.last_response);
  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, 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, 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, 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, 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, true);
  804. if (!alloc_submenu(FlipSocialViewLoggedInMessagesSubmenu))
  805. {
  806. FURI_LOG_E(TAG, "Failed to allocate submenu");
  807. return;
  808. }
  809. flipper_http_loading_task(
  810. flip_social_get_message_users, // get the message users
  811. flip_social_parse_json_message_users, // parse the message users
  812. FlipSocialViewSubmenu, // switch to the messages submenu if successful
  813. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  814. &app->view_dispatcher); // view dispatcher
  815. break;
  816. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  817. // they need to search for the user to send a message
  818. free_text_input();
  819. if (!alloc_text_input(FlipSocialViewLoggedInMessageUsersInput))
  820. {
  821. FURI_LOG_E(TAG, "Failed to allocate text input");
  822. return;
  823. }
  824. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  825. break;
  826. case FlipSocialSubmenuLoggedInIndexFeed:
  827. free_all(true, true);
  828. if (!flip_social_load_initial_feed(true))
  829. {
  830. FURI_LOG_E(TAG, "Failed to load the initial feed");
  831. return;
  832. }
  833. break;
  834. case FlipSocialSubmenuExploreIndex:
  835. free_all(true, true);
  836. if (!alloc_text_input(FlipSocialViewLoggedInExploreInput))
  837. {
  838. FURI_LOG_E(TAG, "Failed to allocate text input");
  839. return;
  840. }
  841. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  842. break;
  843. case FlipSocialSubmenuLoggedInIndexCompose:
  844. free_all(true, true);
  845. if (!alloc_submenu(FlipSocialViewLoggedInCompose))
  846. {
  847. FURI_LOG_E(TAG, "Failed to allocate submenu");
  848. return;
  849. }
  850. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  851. break;
  852. case FlipSocialSubmenuLoggedInIndexSettings:
  853. free_all(true, true);
  854. if (!alloc_submenu(FlipSocialViewLoggedInSettings))
  855. {
  856. FURI_LOG_E(TAG, "Failed to allocate submenu");
  857. return;
  858. }
  859. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  860. break;
  861. case FlipSocialSubmenuLoggedInIndexAbout:
  862. free_all(true, false);
  863. if (!about_widget_alloc(true))
  864. {
  865. FURI_LOG_E(TAG, "Failed to allocate about widget");
  866. return;
  867. }
  868. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  869. break;
  870. case FlipSocialSubmenuLoggedInIndexWifiSettings:
  871. free_all(true, false);
  872. if (!alloc_variable_item_list(FlipSocialViewLoggedInSettingsWifi))
  873. {
  874. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  875. return;
  876. }
  877. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  878. break;
  879. case FlipSocialSubmenuLoggedInSignOutButton:
  880. free_all(true, true);
  881. app->is_logged_in = "false";
  882. 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);
  883. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  884. break;
  885. case FlipSocialSubmenuComposeIndexAddPreSave:
  886. free_text_input();
  887. if (!alloc_text_input(FlipSocialViewLoggedInComposeAddPreSaveInput))
  888. {
  889. FURI_LOG_E(TAG, "Failed to allocate text input");
  890. return;
  891. }
  892. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  893. break;
  894. default:
  895. // Handle the pre-saved message selection (has a max of 25 items)
  896. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  897. {
  898. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  899. snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  900. if (!selected_message)
  901. {
  902. FURI_LOG_E(TAG, "Selected message is NULL");
  903. return;
  904. }
  905. flip_social_free_compose_dialog();
  906. if (!app->dialog_compose)
  907. {
  908. if (!easy_flipper_set_dialog_ex(
  909. &app->dialog_compose,
  910. FlipSocialViewComposeDialog,
  911. "New Feed Post",
  912. 0,
  913. 0,
  914. updated_user_message(selected_message),
  915. 0,
  916. 10,
  917. "Delete",
  918. "Post",
  919. NULL,
  920. compose_dialog_callback,
  921. flip_social_callback_to_compose_logged_in,
  922. &app->view_dispatcher,
  923. app))
  924. {
  925. return;
  926. }
  927. }
  928. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewComposeDialog);
  929. }
  930. // Handle the explore selection
  931. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  932. {
  933. if (!flip_social_explore)
  934. {
  935. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  936. return;
  937. }
  938. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  939. // loading task to get the user info
  940. if (app->explore_user_bio)
  941. {
  942. free(app->explore_user_bio);
  943. app->explore_user_bio = NULL;
  944. }
  945. if (!easy_flipper_set_buffer(&app->explore_user_bio, MAX_MESSAGE_LENGTH))
  946. {
  947. return;
  948. }
  949. if (flipper_http_process_response_async(flip_social_get_user_info, flip_social_parse_user_info))
  950. {
  951. flip_social_free_explore_dialog();
  952. if (!app->dialog_explore)
  953. {
  954. if (!easy_flipper_set_dialog_ex(
  955. &app->dialog_explore,
  956. FlipSocialViewExploreDialog,
  957. flip_social_explore->usernames[flip_social_explore->index],
  958. 0,
  959. 0,
  960. updated_user_message(app->explore_user_bio),
  961. 0,
  962. 10,
  963. "Remove", // remove if user is a friend (future update)
  964. "Add", // add if user is not a friend (future update)
  965. NULL,
  966. explore_dialog_callback,
  967. flip_social_callback_to_explore_logged_in,
  968. &app->view_dispatcher,
  969. app))
  970. {
  971. return;
  972. }
  973. }
  974. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  975. }
  976. else
  977. {
  978. flip_social_free_explore_dialog();
  979. if (!app->dialog_explore)
  980. {
  981. if (!easy_flipper_set_dialog_ex(
  982. &app->dialog_explore,
  983. FlipSocialViewExploreDialog,
  984. flip_social_explore->usernames[flip_social_explore->index],
  985. 0,
  986. 0,
  987. "",
  988. 0,
  989. 10,
  990. "Remove", // remove if user is a friend (future update)
  991. "Add", // add if user is not a friend (future update)
  992. NULL,
  993. explore_dialog_callback,
  994. flip_social_callback_to_explore_logged_in,
  995. &app->view_dispatcher,
  996. app))
  997. {
  998. return;
  999. }
  1000. }
  1001. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  1002. }
  1003. }
  1004. // handle the friends selection
  1005. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  1006. {
  1007. if (!flip_social_friends)
  1008. {
  1009. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  1010. return;
  1011. }
  1012. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  1013. flip_social_free_friends_dialog();
  1014. if (!app->dialog_friends)
  1015. {
  1016. if (!easy_flipper_set_dialog_ex(
  1017. &app->dialog_friends,
  1018. FlipSocialViewFriendsDialog,
  1019. "Friend Options",
  1020. 0,
  1021. 0,
  1022. flip_social_friends->usernames[flip_social_friends->index],
  1023. 0,
  1024. 10,
  1025. "Remove",
  1026. "",
  1027. NULL,
  1028. friends_dialog_callback,
  1029. flip_social_callback_to_friends_logged_in,
  1030. &app->view_dispatcher,
  1031. app))
  1032. {
  1033. return;
  1034. }
  1035. }
  1036. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFriendsDialog);
  1037. }
  1038. // handle the messages selection
  1039. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
  1040. {
  1041. if (!flip_social_message_users)
  1042. {
  1043. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  1044. return;
  1045. }
  1046. flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  1047. flipper_http_loading_task(
  1048. flip_social_get_messages_with_user, // get the messages with the selected user
  1049. flip_social_parse_json_messages, // parse the messages
  1050. FlipSocialViewMessagesDialog, // switch to the messages process if successful
  1051. FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
  1052. &app->view_dispatcher // view dispatcher
  1053. );
  1054. }
  1055. // handle the messages user choices selection
  1056. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
  1057. {
  1058. if (!flip_social_explore)
  1059. {
  1060. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  1061. return;
  1062. }
  1063. flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  1064. free_text_input();
  1065. if (!alloc_text_input(FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput))
  1066. {
  1067. FURI_LOG_E(TAG, "Failed to allocate text input");
  1068. return;
  1069. } // switch to the text input view
  1070. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1071. }
  1072. else
  1073. {
  1074. FURI_LOG_E(TAG, "Unknown submenu index");
  1075. }
  1076. break;
  1077. }
  1078. }
  1079. /**
  1080. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  1081. * @param context The context - FlipSocialApp object.
  1082. * @return void
  1083. */
  1084. void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  1085. {
  1086. FlipSocialApp *app = (FlipSocialApp *)context;
  1087. if (!app)
  1088. {
  1089. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1090. return;
  1091. }
  1092. // Store the entered name
  1093. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1094. // Store the entered name in the logged in name field
  1095. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1096. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1097. // Ensure null-termination
  1098. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  1099. // Update the name item text
  1100. if (app->variable_item_logged_out_wifi_settings_ssid)
  1101. {
  1102. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  1103. }
  1104. // update the wifi settings
  1105. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1106. {
  1107. if (flipper_http_init(flipper_http_rx_callback, app_instance))
  1108. {
  1109. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1110. {
  1111. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1112. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1113. }
  1114. furi_delay_ms(500);
  1115. flipper_http_deinit();
  1116. }
  1117. }
  1118. // Save the settings
  1119. 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);
  1120. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1121. }
  1122. /**
  1123. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  1124. * @param context The context - FlipSocialApp object.
  1125. * @return void
  1126. */
  1127. void flip_social_logged_out_wifi_settings_password_updated(void *context)
  1128. {
  1129. FlipSocialApp *app = (FlipSocialApp *)context;
  1130. if (!app)
  1131. {
  1132. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1133. return;
  1134. }
  1135. // Store the entered WiFi password
  1136. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1137. // Store the entered WiFi password in the logged in password field
  1138. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1139. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1140. // Ensure null-termination
  1141. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  1142. // Update the password item text
  1143. if (app->variable_item_logged_out_wifi_settings_password)
  1144. {
  1145. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  1146. }
  1147. // update the wifi settings
  1148. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1149. {
  1150. if (flipper_http_init(flipper_http_rx_callback, app_instance))
  1151. {
  1152. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1153. {
  1154. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1155. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1156. }
  1157. furi_delay_ms(500);
  1158. flipper_http_deinit();
  1159. }
  1160. }
  1161. // Save the settings
  1162. 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);
  1163. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1164. }
  1165. /**
  1166. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  1167. * @param context The context - FlipSocialApp object.
  1168. * @param index The index of the selected item.
  1169. * @return void
  1170. */
  1171. void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  1172. {
  1173. FlipSocialApp *app = (FlipSocialApp *)context;
  1174. if (!app)
  1175. {
  1176. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1177. return;
  1178. }
  1179. switch (index)
  1180. {
  1181. case 0: // Input SSID
  1182. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  1183. free_text_input();
  1184. if (!alloc_text_input(FlipSocialViewLoggedOutWifiSettingsSSIDInput))
  1185. {
  1186. FURI_LOG_E(TAG, "Failed to allocate text input");
  1187. return;
  1188. }
  1189. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1190. break;
  1191. case 1: // Input Password
  1192. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  1193. free_text_input();
  1194. if (!alloc_text_input(FlipSocialViewLoggedOutWifiSettingsPasswordInput))
  1195. {
  1196. FURI_LOG_E(TAG, "Failed to allocate text input");
  1197. return;
  1198. }
  1199. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1200. break;
  1201. default:
  1202. FURI_LOG_E(TAG, "Unknown configuration item index");
  1203. break;
  1204. }
  1205. }
  1206. /**
  1207. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  1208. * @param context The context - FlipSocialApp object.
  1209. * @return void
  1210. */
  1211. void flip_social_logged_out_login_username_updated(void *context)
  1212. {
  1213. FlipSocialApp *app = (FlipSocialApp *)context;
  1214. if (!app)
  1215. {
  1216. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1217. return;
  1218. }
  1219. // Store the entered name
  1220. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1221. // Store the entered name in the logged in username field
  1222. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1223. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1224. // Ensure null-termination
  1225. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  1226. // Update the name item text
  1227. if (app->variable_item_logged_out_login_username)
  1228. {
  1229. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  1230. }
  1231. // Save the settings
  1232. 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);
  1233. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1234. }
  1235. /**
  1236. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  1237. * @param context The context - FlipSocialApp object.
  1238. * @return void
  1239. */
  1240. void flip_social_logged_out_login_password_updated(void *context)
  1241. {
  1242. FlipSocialApp *app = (FlipSocialApp *)context;
  1243. if (!app)
  1244. {
  1245. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1246. return;
  1247. }
  1248. // Store the entered password
  1249. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1250. // Store the entered password in the change password field
  1251. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1252. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1253. // Ensure null-termination
  1254. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  1255. // Update the password item text
  1256. if (app->variable_item_logged_out_login_password)
  1257. {
  1258. // dont show the password on the screen (version 0.2)
  1259. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  1260. }
  1261. // Save the settings
  1262. 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);
  1263. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1264. }
  1265. /**
  1266. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  1267. * @param context The context - FlipSocialApp object.
  1268. * @param index The index of the selected item.
  1269. * @return void
  1270. */
  1271. void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  1272. {
  1273. FlipSocialApp *app = (FlipSocialApp *)context;
  1274. if (!app)
  1275. {
  1276. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1277. return;
  1278. }
  1279. switch (index)
  1280. {
  1281. case 0: // Input Username
  1282. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  1283. free_all(false, true);
  1284. if (!alloc_text_input(FlipSocialViewLoggedOutLoginUsernameInput))
  1285. {
  1286. FURI_LOG_E(TAG, "Failed to allocate text input");
  1287. return;
  1288. }
  1289. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1290. break;
  1291. case 1: // Input Password
  1292. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  1293. free_all(false, true);
  1294. if (!alloc_text_input(FlipSocialViewLoggedOutLoginPasswordInput))
  1295. {
  1296. FURI_LOG_E(TAG, "Failed to allocate text input");
  1297. return;
  1298. }
  1299. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1300. break;
  1301. case 2: // Login Button
  1302. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  1303. {
  1304. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1305. return;
  1306. }
  1307. flip_social_login_switch_to_view(app);
  1308. break;
  1309. default:
  1310. FURI_LOG_E(TAG, "Unknown configuration item index");
  1311. break;
  1312. }
  1313. }
  1314. /**
  1315. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  1316. * @param context The context - FlipSocialApp object.
  1317. * @return void
  1318. */
  1319. void flip_social_logged_out_register_username_updated(void *context)
  1320. {
  1321. FlipSocialApp *app = (FlipSocialApp *)context;
  1322. if (!app)
  1323. {
  1324. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1325. return;
  1326. }
  1327. // Store the entered name
  1328. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  1329. // Ensure null-termination
  1330. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  1331. // Update the name item text
  1332. if (app->variable_item_logged_out_register_username)
  1333. {
  1334. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  1335. }
  1336. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1337. }
  1338. /**
  1339. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  1340. * @param context The context - FlipSocialApp object.
  1341. * @return void
  1342. */
  1343. void flip_social_logged_out_register_password_updated(void *context)
  1344. {
  1345. FlipSocialApp *app = (FlipSocialApp *)context;
  1346. if (!app)
  1347. {
  1348. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1349. return;
  1350. }
  1351. // Store the entered password
  1352. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  1353. // Ensure null-termination
  1354. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  1355. // Update the password item text
  1356. if (app->variable_item_logged_out_register_password)
  1357. {
  1358. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  1359. }
  1360. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1361. }
  1362. /**
  1363. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  1364. * @param context The context - FlipSocialApp object.
  1365. * @return void
  1366. */
  1367. void flip_social_logged_out_register_password_2_updated(void *context)
  1368. {
  1369. FlipSocialApp *app = (FlipSocialApp *)context;
  1370. if (!app)
  1371. {
  1372. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1373. return;
  1374. }
  1375. // Store the entered password
  1376. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  1377. // Ensure null-termination
  1378. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  1379. // Update the password item text
  1380. if (app->variable_item_logged_out_register_password_2)
  1381. {
  1382. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  1383. }
  1384. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1385. }
  1386. /**
  1387. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  1388. * @param context The context - FlipSocialApp object.
  1389. * @param index The index of the selected item.
  1390. * @return void
  1391. */
  1392. void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  1393. {
  1394. FlipSocialApp *app = (FlipSocialApp *)context;
  1395. if (!app)
  1396. {
  1397. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1398. return;
  1399. }
  1400. switch (index)
  1401. {
  1402. case 0: // Input Username
  1403. free_all(false, true);
  1404. if (!alloc_text_input(FlipSocialViewLoggedOutRegisterUsernameInput))
  1405. {
  1406. FURI_LOG_E(TAG, "Failed to allocate text input");
  1407. return;
  1408. }
  1409. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1410. break;
  1411. case 1: // Input Password
  1412. free_all(false, true);
  1413. if (!alloc_text_input(FlipSocialViewLoggedOutRegisterPasswordInput))
  1414. {
  1415. FURI_LOG_E(TAG, "Failed to allocate text input");
  1416. return;
  1417. }
  1418. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1419. break;
  1420. case 2: // Input Password 2
  1421. free_all(false, true);
  1422. if (!alloc_text_input(FlipSocialViewLoggedOutRegisterPassword2Input))
  1423. {
  1424. FURI_LOG_E(TAG, "Failed to allocate text input");
  1425. return;
  1426. }
  1427. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1428. break;
  1429. case 3: // Register button
  1430. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  1431. {
  1432. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1433. return;
  1434. }
  1435. flip_social_register_switch_to_view(app);
  1436. break;
  1437. default:
  1438. FURI_LOG_E(TAG, "Unknown configuration item index");
  1439. break;
  1440. }
  1441. }
  1442. /**
  1443. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1444. * @param context The context - FlipSocialApp object.
  1445. * @return void
  1446. */
  1447. void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1448. {
  1449. FlipSocialApp *app = (FlipSocialApp *)context;
  1450. if (!app)
  1451. {
  1452. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1453. return;
  1454. }
  1455. // Store the entered SSID
  1456. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1457. // Store the entered SSID in the logged out SSID
  1458. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1459. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1460. // Ensure null-termination
  1461. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1462. // Update the name item text
  1463. if (app->variable_item_logged_in_wifi_settings_ssid)
  1464. {
  1465. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1466. }
  1467. // Save the settings
  1468. 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);
  1469. // update the wifi settings
  1470. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1471. {
  1472. if (flipper_http_init(flipper_http_rx_callback, app_instance))
  1473. {
  1474. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1475. {
  1476. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1477. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1478. }
  1479. furi_delay_ms(500);
  1480. flipper_http_deinit();
  1481. }
  1482. }
  1483. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1484. }
  1485. /**
  1486. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1487. * @param context The context - FlipSocialApp object.
  1488. * @return void
  1489. */
  1490. void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1491. {
  1492. FlipSocialApp *app = (FlipSocialApp *)context;
  1493. if (!app)
  1494. {
  1495. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1496. return;
  1497. }
  1498. // Store the entered password
  1499. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1500. // Store the entered password in the logged out password
  1501. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1502. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1503. // Ensure null-termination
  1504. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1505. // Update the password item text
  1506. if (app->variable_item_logged_in_wifi_settings_password)
  1507. {
  1508. // dont show the password on the screen (version 0.2)
  1509. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1510. }
  1511. // Save the settings
  1512. 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);
  1513. // update the wifi settings
  1514. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1515. {
  1516. if (flipper_http_init(flipper_http_rx_callback, app_instance))
  1517. {
  1518. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1519. {
  1520. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1521. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1522. }
  1523. furi_delay_ms(500);
  1524. flipper_http_deinit();
  1525. }
  1526. }
  1527. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
  1528. }
  1529. /**
  1530. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1531. * @param context The context - FlipSocialApp object.
  1532. * @param index The index of the selected item.
  1533. * @return void
  1534. */
  1535. void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1536. {
  1537. FlipSocialApp *app = (FlipSocialApp *)context;
  1538. if (!app)
  1539. {
  1540. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1541. return;
  1542. }
  1543. switch (index)
  1544. {
  1545. case 0: // Input SSID
  1546. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1547. free_all(false, false);
  1548. if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsSSIDInput))
  1549. {
  1550. FURI_LOG_E(TAG, "Failed to allocate text input for SSID");
  1551. return;
  1552. }
  1553. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1554. break;
  1555. case 1: // Input Password
  1556. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1557. free_all(false, false);
  1558. if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsPasswordInput))
  1559. {
  1560. FURI_LOG_E(TAG, "Failed to allocate text input for Password");
  1561. return;
  1562. }
  1563. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1564. break;
  1565. default:
  1566. FURI_LOG_E(TAG, "Unknown configuration item index");
  1567. break;
  1568. }
  1569. }
  1570. /**
  1571. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1572. * @param context The context - FlipSocialApp object.
  1573. * @return void
  1574. */
  1575. void flip_social_logged_in_compose_pre_save_updated(void *context)
  1576. {
  1577. FlipSocialApp *app = (FlipSocialApp *)context;
  1578. if (!app)
  1579. {
  1580. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1581. return;
  1582. }
  1583. // Check if the message is empty or if adding the message would exceed MAX_PRE_SAVED_MESSAGES
  1584. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1585. {
  1586. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1587. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1588. return;
  1589. }
  1590. // Copy the entered message into the next available slot
  1591. strncpy(
  1592. app->pre_saved_messages.messages[app->pre_saved_messages.count],
  1593. app->compose_pre_save_logged_in_temp_buffer,
  1594. MAX_MESSAGE_LENGTH - 1);
  1595. // Ensure null-termination
  1596. app->pre_saved_messages.messages[app->pre_saved_messages.count][MAX_MESSAGE_LENGTH - 1] = '\0';
  1597. // Increment the count
  1598. app->pre_saved_messages.count++;
  1599. // Rebuild the submenu
  1600. submenu_reset(app->submenu);
  1601. submenu_add_item(
  1602. app->submenu,
  1603. "Add Pre-Save",
  1604. FlipSocialSubmenuComposeIndexAddPreSave,
  1605. flip_social_callback_submenu_choices,
  1606. app);
  1607. for (size_t i = 0; i < app->pre_saved_messages.count; i++)
  1608. {
  1609. submenu_add_item(
  1610. app->submenu,
  1611. app->pre_saved_messages.messages[i],
  1612. FlipSocialSubemnuComposeIndexStartIndex + i,
  1613. flip_social_callback_submenu_choices,
  1614. app);
  1615. }
  1616. // Save the updated playlist
  1617. save_playlist(&app->pre_saved_messages);
  1618. // Switch back to the compose view
  1619. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1620. }
  1621. /**
  1622. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1623. * @param context The context - FlipSocialApp object.
  1624. * @return void
  1625. */
  1626. void flip_social_logged_in_profile_change_password_updated(void *context)
  1627. {
  1628. FlipSocialApp *app = (FlipSocialApp *)context;
  1629. if (!app)
  1630. {
  1631. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1632. return;
  1633. }
  1634. // Correct type: old_pass should be a pointer to a string (char *)
  1635. const char *old_password = app->login_password_logged_out;
  1636. // Store the entered message
  1637. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1638. // store the entered password in the logged out password
  1639. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1640. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1641. // Ensure null-termination
  1642. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1643. // Update the message item text
  1644. if (app->variable_item_logged_in_profile_change_password)
  1645. {
  1646. // dont show the password on the screen (version 0.2)
  1647. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1648. }
  1649. // send post request to change password
  1650. if (flipper_http_init(flipper_http_rx_callback, app))
  1651. {
  1652. auth_headers_alloc();
  1653. char payload[256];
  1654. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1655. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
  1656. {
  1657. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1658. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1659. }
  1660. flipper_http_deinit();
  1661. }
  1662. // Save the settings
  1663. 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);
  1664. // instead of going to a view, just show a success message
  1665. easy_flipper_dialog("Success", "Password updated successfully\n\n\nPress BACK to return :D");
  1666. }
  1667. void flip_social_logged_in_profile_change_bio_updated(void *context)
  1668. {
  1669. FlipSocialApp *app = (FlipSocialApp *)context;
  1670. if (!app)
  1671. {
  1672. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1673. return;
  1674. }
  1675. // Store the entered message
  1676. strncpy(app->change_bio_logged_in, app->change_bio_logged_in_temp_buffer, app->change_bio_logged_in_temp_buffer_size);
  1677. // Ensure null-termination
  1678. app->change_bio_logged_in[app->change_bio_logged_in_temp_buffer_size - 1] = '\0';
  1679. // Update the message item text
  1680. if (app->variable_item_logged_in_profile_change_bio)
  1681. {
  1682. variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_bio, app->change_bio_logged_in);
  1683. }
  1684. // send post request to change bio
  1685. if (flipper_http_init(flipper_http_rx_callback, app))
  1686. {
  1687. auth_headers_alloc();
  1688. char payload[256];
  1689. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
  1690. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-bio/", auth_headers, payload))
  1691. {
  1692. FURI_LOG_E(TAG, "Failed to send post request to change bio");
  1693. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1694. }
  1695. furi_delay_ms(500);
  1696. flipper_http_deinit();
  1697. }
  1698. // Save the settings
  1699. 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);
  1700. // instead of going to a view, just show a success message
  1701. easy_flipper_dialog("Success", "Bio updated successfully\n\n\nPress BACK to return :D");
  1702. }
  1703. /**
  1704. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1705. * @param context The context - FlipSocialApp object.
  1706. * @param index The index of the selected item.
  1707. * @return void
  1708. */
  1709. void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1710. {
  1711. FlipSocialApp *app = (FlipSocialApp *)context;
  1712. if (!app)
  1713. {
  1714. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1715. return;
  1716. }
  1717. switch (index)
  1718. {
  1719. case 0: // Change Username
  1720. // do nothing since username cannot be changed
  1721. break;
  1722. case 1: // Change Password
  1723. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1724. free_text_input();
  1725. if (!alloc_text_input(FlipSocialViewLoggedInChangePasswordInput))
  1726. {
  1727. FURI_LOG_E(TAG, "Failed to allocate text input for change password");
  1728. return;
  1729. }
  1730. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1731. break;
  1732. case 2: // Change Bio
  1733. // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangeBioInput);
  1734. free_text_input();
  1735. if (!alloc_text_input(FlipSocialViewLoggedInChangeBioInput))
  1736. {
  1737. FURI_LOG_E(TAG, "Failed to allocate text input for change bio");
  1738. return;
  1739. }
  1740. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1741. break;
  1742. case 3: // Friends
  1743. free_all(false, true);
  1744. if (!alloc_submenu(FlipSocialViewLoggedInFriendsSubmenu))
  1745. {
  1746. FURI_LOG_E(TAG, "Failed to allocate submenu for friends");
  1747. return;
  1748. }
  1749. if (!flipper_http_init(flipper_http_rx_callback, app))
  1750. {
  1751. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1752. return;
  1753. }
  1754. flipper_http_loading_task(
  1755. flip_social_get_friends,
  1756. flip_social_parse_json_friends,
  1757. FlipSocialViewSubmenu,
  1758. FlipSocialViewVariableItemList,
  1759. &app->view_dispatcher);
  1760. break;
  1761. default:
  1762. FURI_LOG_E(TAG, "Unknown configuration item index");
  1763. break;
  1764. }
  1765. }
  1766. /**
  1767. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  1768. * @param context The context - FlipSocialApp object.
  1769. * @return void
  1770. */
  1771. void flip_social_logged_in_messages_user_choice_message_updated(void *context)
  1772. {
  1773. FlipSocialApp *app = (FlipSocialApp *)context;
  1774. if (!app)
  1775. {
  1776. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1777. return;
  1778. }
  1779. // check if the message is empty
  1780. if (app->message_user_choice_logged_in_temp_buffer_size == 0)
  1781. {
  1782. FURI_LOG_E(TAG, "Message is empty");
  1783. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
  1784. return;
  1785. }
  1786. // Store the entered message
  1787. strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
  1788. // Ensure null-termination
  1789. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
  1790. // send post request to send message
  1791. if (!flipper_http_init(flipper_http_rx_callback, app))
  1792. {
  1793. FURI_LOG_E(TAG, "Failed to initialize HTTP");
  1794. return;
  1795. }
  1796. auth_headers_alloc();
  1797. char url[128];
  1798. char payload[256];
  1799. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1800. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
  1801. if (!flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1802. {
  1803. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1804. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1805. }
  1806. furi_delay_ms(1000);
  1807. flipper_http_deinit();
  1808. // add user to the top of the list if not already there
  1809. for (int i = 0; i < flip_social_message_users->count; i++)
  1810. {
  1811. if (strcmp(flip_social_message_users->usernames[i], flip_social_explore->usernames[flip_social_explore->index]) == 0)
  1812. {
  1813. // remove the user from the list
  1814. for (int j = i; j < flip_social_message_users->count - 1; j++)
  1815. {
  1816. strncpy(flip_social_message_users->usernames[j], flip_social_message_users->usernames[j + 1], strlen(flip_social_message_users->usernames[j + 1]));
  1817. }
  1818. flip_social_message_users->count--;
  1819. break;
  1820. }
  1821. }
  1822. // add the user to the top of the list
  1823. for (int i = flip_social_message_users->count; i > 0; i--)
  1824. {
  1825. strncpy(flip_social_message_users->usernames[i], flip_social_message_users->usernames[i - 1], strlen(flip_social_message_users->usernames[i - 1]));
  1826. }
  1827. strncpy(flip_social_message_users->usernames[0], flip_social_explore->usernames[flip_social_explore->index], strlen(flip_social_explore->usernames[flip_social_explore->index]));
  1828. flip_social_message_users->count++;
  1829. // redraw submenu
  1830. flip_social_update_messages_submenu();
  1831. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1832. }
  1833. /**
  1834. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1835. * @param context The context - FlipSocialApp object.
  1836. * @return void
  1837. */
  1838. void flip_social_logged_in_messages_new_message_updated(void *context)
  1839. {
  1840. FlipSocialApp *app = (FlipSocialApp *)context;
  1841. if (!app)
  1842. {
  1843. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1844. return;
  1845. }
  1846. // check if the message is empty
  1847. if (app->messages_new_message_logged_in_temp_buffer_size == 0)
  1848. {
  1849. FURI_LOG_E(TAG, "Message is empty");
  1850. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1851. return;
  1852. }
  1853. // Store the entered message
  1854. strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
  1855. // Ensure null-termination
  1856. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
  1857. // send post request to send message
  1858. if (!flipper_http_init(flipper_http_rx_callback, app))
  1859. {
  1860. FURI_LOG_E(TAG, "Failed to initialize HTTP");
  1861. return;
  1862. }
  1863. auth_headers_alloc();
  1864. char url[128];
  1865. char payload[256];
  1866. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1867. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
  1868. if (!flipper_http_post_request_with_headers(url, auth_headers, payload))
  1869. {
  1870. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1871. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1872. }
  1873. furi_delay_ms(1000);
  1874. flipper_http_deinit();
  1875. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
  1876. }
  1877. void flip_social_logged_in_explore_updated(void *context)
  1878. {
  1879. FlipSocialApp *app = (FlipSocialApp *)context;
  1880. if (!app)
  1881. {
  1882. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1883. return;
  1884. }
  1885. // check if the message is empty
  1886. if (app->explore_logged_in_temp_buffer_size == 0)
  1887. {
  1888. FURI_LOG_E(TAG, "Message is empty");
  1889. strncpy(app->explore_logged_in, "a", 2);
  1890. }
  1891. else
  1892. {
  1893. // Store the entered message
  1894. strncpy(app->explore_logged_in, app->explore_logged_in_temp_buffer, app->explore_logged_in_temp_buffer_size);
  1895. }
  1896. // Ensure null-termination
  1897. app->explore_logged_in[app->explore_logged_in_temp_buffer_size - 1] = '\0';
  1898. free_submenu();
  1899. if (!alloc_submenu(FlipSocialViewLoggedInExploreSubmenu))
  1900. {
  1901. FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
  1902. return;
  1903. }
  1904. flipper_http_loading_task(
  1905. flip_social_get_explore, // get the explore users
  1906. flip_social_parse_json_explore, // parse the explore users
  1907. FlipSocialViewSubmenu, // switch to the explore submenu if successful
  1908. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  1909. &app->view_dispatcher); // view dispatcher
  1910. }
  1911. void flip_social_logged_in_message_users_updated(void *context)
  1912. {
  1913. FlipSocialApp *app = (FlipSocialApp *)context;
  1914. if (!app)
  1915. {
  1916. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1917. return;
  1918. }
  1919. // check if the message is empty
  1920. if (!app->message_users_logged_in_temp_buffer || app->message_users_logged_in_temp_buffer_size == 0)
  1921. {
  1922. FURI_LOG_E(TAG, "Message is empty");
  1923. strncpy(app->message_users_logged_in, "a", 2);
  1924. }
  1925. else
  1926. {
  1927. // Store the entered message
  1928. strncpy(app->message_users_logged_in, app->message_users_logged_in_temp_buffer, app->message_users_logged_in_temp_buffer_size);
  1929. }
  1930. // Ensure null-termination
  1931. app->message_users_logged_in[app->message_users_logged_in_temp_buffer_size - 1] = '\0';
  1932. free_submenu();
  1933. if (!alloc_submenu(FlipSocialViewLoggedInExploreSubmenu))
  1934. {
  1935. FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
  1936. return;
  1937. }
  1938. // get users
  1939. flipper_http_loading_task(
  1940. flip_social_get_explore_2, // get the explore users
  1941. flip_social_parse_json_message_user_choices, // parse the explore users
  1942. FlipSocialViewSubmenu, // switch to the explore submenu if successful
  1943. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  1944. &app->view_dispatcher); // view dispatcher
  1945. }
  1946. static void flip_social_widget_set_text(char *message, Widget **widget)
  1947. {
  1948. if (widget == NULL)
  1949. {
  1950. FURI_LOG_E(TAG, "flip_social_set_widget_text - widget is NULL");
  1951. DEV_CRASH();
  1952. return;
  1953. }
  1954. if (message == NULL)
  1955. {
  1956. FURI_LOG_E(TAG, "flip_social_set_widget_text - message is NULL");
  1957. DEV_CRASH();
  1958. return;
  1959. }
  1960. widget_reset(*widget);
  1961. uint32_t message_length = strlen(message); // Length of the message
  1962. uint32_t i = 0; // Index tracker
  1963. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1964. char *formatted_message; // Buffer to hold the final formatted message
  1965. // Allocate buffer with double the message length plus one for safety
  1966. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1967. {
  1968. return;
  1969. }
  1970. while (i < message_length)
  1971. {
  1972. uint32_t max_line_length = 31; // Maximum characters per line
  1973. uint32_t remaining_length = message_length - i; // Remaining characters
  1974. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1975. // Check for newline character within the current segment
  1976. uint32_t newline_pos = i;
  1977. bool found_newline = false;
  1978. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1979. {
  1980. if (message[newline_pos] == '\n')
  1981. {
  1982. found_newline = true;
  1983. break;
  1984. }
  1985. }
  1986. if (found_newline)
  1987. {
  1988. // If newline found, set line_length up to the newline
  1989. line_length = newline_pos - i;
  1990. }
  1991. // Temporary buffer to hold the current line
  1992. char line[32];
  1993. strncpy(line, message + i, line_length);
  1994. line[line_length] = '\0';
  1995. // If newline was found, skip it for the next iteration
  1996. if (found_newline)
  1997. {
  1998. i += line_length + 1; // +1 to skip the '\n' character
  1999. }
  2000. else
  2001. {
  2002. // Check if the line ends in the middle of a word and adjust accordingly
  2003. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  2004. {
  2005. // Find the last space within the current line to avoid breaking a word
  2006. char *last_space = strrchr(line, ' ');
  2007. if (last_space != NULL)
  2008. {
  2009. // Adjust the line_length to avoid cutting the word
  2010. line_length = last_space - line;
  2011. line[line_length] = '\0'; // Null-terminate at the space
  2012. }
  2013. }
  2014. // Move the index forward by the determined line_length
  2015. i += line_length;
  2016. // Skip any spaces at the beginning of the next line
  2017. while (i < message_length && message[i] == ' ')
  2018. {
  2019. i++;
  2020. }
  2021. }
  2022. // Manually copy the fixed line into the formatted_message buffer
  2023. for (uint32_t j = 0; j < line_length; j++)
  2024. {
  2025. formatted_message[formatted_index++] = line[j];
  2026. }
  2027. // Add a newline character for line spacing
  2028. formatted_message[formatted_index++] = '\n';
  2029. }
  2030. // Null-terminate the formatted_message
  2031. formatted_message[formatted_index] = '\0';
  2032. // Add the formatted message to the widget
  2033. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  2034. }
  2035. void flip_social_loader_draw_callback(Canvas *canvas, void *model)
  2036. {
  2037. if (!canvas || !model)
  2038. {
  2039. FURI_LOG_E(TAG, "flip_social_loader_draw_callback - canvas or model is NULL");
  2040. return;
  2041. }
  2042. SerialState http_state = fhttp.state;
  2043. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  2044. DataState data_state = data_loader_model->data_state;
  2045. char *title = data_loader_model->title;
  2046. canvas_set_font(canvas, FontSecondary);
  2047. if (http_state == INACTIVE)
  2048. {
  2049. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  2050. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  2051. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  2052. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  2053. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  2054. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  2055. return;
  2056. }
  2057. if (data_state == DataStateError || data_state == DataStateParseError)
  2058. {
  2059. flip_social_request_error_draw(canvas);
  2060. return;
  2061. }
  2062. canvas_draw_str(canvas, 0, 7, title);
  2063. canvas_draw_str(canvas, 0, 17, "Loading...");
  2064. if (data_state == DataStateInitial)
  2065. {
  2066. return;
  2067. }
  2068. if (http_state == SENDING)
  2069. {
  2070. canvas_draw_str(canvas, 0, 27, "Fetching...");
  2071. return;
  2072. }
  2073. if (http_state == RECEIVING || data_state == DataStateRequested)
  2074. {
  2075. canvas_draw_str(canvas, 0, 27, "Receiving...");
  2076. return;
  2077. }
  2078. if (http_state == IDLE && data_state == DataStateReceived)
  2079. {
  2080. canvas_draw_str(canvas, 0, 27, "Processing...");
  2081. return;
  2082. }
  2083. if (http_state == IDLE && data_state == DataStateParsed)
  2084. {
  2085. canvas_draw_str(canvas, 0, 27, "Processed...");
  2086. return;
  2087. }
  2088. }
  2089. static void flip_social_loader_process_callback(void *context)
  2090. {
  2091. if (context == NULL)
  2092. {
  2093. FURI_LOG_E(TAG, "flip_social_loader_process_callback - context is NULL");
  2094. DEV_CRASH();
  2095. return;
  2096. }
  2097. FlipSocialApp *app = (FlipSocialApp *)context;
  2098. View *view = app->view_loader;
  2099. DataState current_data_state;
  2100. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  2101. if (current_data_state == DataStateInitial)
  2102. {
  2103. with_view_model(
  2104. view,
  2105. DataLoaderModel * model,
  2106. {
  2107. model->data_state = DataStateRequested;
  2108. DataLoaderFetch fetch = model->fetcher;
  2109. if (fetch == NULL)
  2110. {
  2111. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  2112. model->data_state = DataStateError;
  2113. return;
  2114. }
  2115. // Clear any previous responses
  2116. strncpy(fhttp.last_response, "", 1);
  2117. bool request_status = fetch(model);
  2118. if (!request_status)
  2119. {
  2120. model->data_state = DataStateError;
  2121. }
  2122. },
  2123. true);
  2124. }
  2125. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  2126. {
  2127. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  2128. {
  2129. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  2130. {
  2131. FURI_LOG_DEV(TAG, "PONG received.");
  2132. }
  2133. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  2134. {
  2135. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  2136. }
  2137. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  2138. {
  2139. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  2140. }
  2141. else if (strlen(fhttp.last_response) == 0)
  2142. {
  2143. // Still waiting on response
  2144. }
  2145. else
  2146. {
  2147. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  2148. }
  2149. }
  2150. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  2151. {
  2152. // continue waiting
  2153. }
  2154. else if (fhttp.state == INACTIVE)
  2155. {
  2156. // inactive. try again
  2157. }
  2158. else if (fhttp.state == ISSUE)
  2159. {
  2160. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  2161. }
  2162. else
  2163. {
  2164. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  2165. DEV_CRASH();
  2166. }
  2167. }
  2168. else if (current_data_state == DataStateReceived)
  2169. {
  2170. with_view_model(
  2171. view,
  2172. DataLoaderModel * model,
  2173. {
  2174. char *data_text;
  2175. if (model->parser == NULL)
  2176. {
  2177. data_text = NULL;
  2178. FURI_LOG_DEV(TAG, "Parser is NULL");
  2179. DEV_CRASH();
  2180. }
  2181. else
  2182. {
  2183. data_text = model->parser(model);
  2184. }
  2185. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  2186. model->data_text = data_text;
  2187. if (data_text == NULL)
  2188. {
  2189. model->data_state = DataStateParseError;
  2190. }
  2191. else
  2192. {
  2193. model->data_state = DataStateParsed;
  2194. }
  2195. },
  2196. true);
  2197. }
  2198. else if (current_data_state == DataStateParsed)
  2199. {
  2200. with_view_model(
  2201. view,
  2202. DataLoaderModel * model,
  2203. {
  2204. if (++model->request_index < model->request_count)
  2205. {
  2206. model->data_state = DataStateInitial;
  2207. }
  2208. else
  2209. {
  2210. flip_social_widget_set_text(model->data_text != NULL ? model->data_text : "", &app_instance->widget_result);
  2211. if (model->data_text != NULL)
  2212. {
  2213. free(model->data_text);
  2214. model->data_text = NULL;
  2215. }
  2216. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  2217. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
  2218. }
  2219. },
  2220. true);
  2221. }
  2222. }
  2223. static void flip_social_loader_timer_callback(void *context)
  2224. {
  2225. if (context == NULL)
  2226. {
  2227. FURI_LOG_E(TAG, "flip_social_loader_timer_callback - context is NULL");
  2228. DEV_CRASH();
  2229. return;
  2230. }
  2231. FlipSocialApp *app = (FlipSocialApp *)context;
  2232. view_dispatcher_send_custom_event(app->view_dispatcher, FlipSocialCustomEventProcess);
  2233. }
  2234. static void flip_social_loader_on_enter(void *context)
  2235. {
  2236. if (context == NULL)
  2237. {
  2238. FURI_LOG_E(TAG, "flip_social_loader_on_enter - context is NULL");
  2239. DEV_CRASH();
  2240. return;
  2241. }
  2242. FlipSocialApp *app = (FlipSocialApp *)context;
  2243. View *view = app->view_loader;
  2244. with_view_model(
  2245. view,
  2246. DataLoaderModel * model,
  2247. {
  2248. view_set_previous_callback(view, model->back_callback);
  2249. if (model->timer == NULL)
  2250. {
  2251. model->timer = furi_timer_alloc(flip_social_loader_timer_callback, FuriTimerTypePeriodic, app);
  2252. }
  2253. furi_timer_start(model->timer, 250);
  2254. },
  2255. true);
  2256. }
  2257. static void flip_social_loader_on_exit(void *context)
  2258. {
  2259. if (context == NULL)
  2260. {
  2261. FURI_LOG_E(TAG, "flip_social_loader_on_exit - context is NULL");
  2262. DEV_CRASH();
  2263. return;
  2264. }
  2265. FlipSocialApp *app = (FlipSocialApp *)context;
  2266. View *view = app->view_loader;
  2267. with_view_model(
  2268. view,
  2269. DataLoaderModel * model,
  2270. {
  2271. if (model->timer)
  2272. {
  2273. furi_timer_stop(model->timer);
  2274. }
  2275. },
  2276. false);
  2277. }
  2278. void flip_social_loader_init(View *view)
  2279. {
  2280. if (view == NULL)
  2281. {
  2282. FURI_LOG_E(TAG, "flip_social_loader_init - view is NULL");
  2283. DEV_CRASH();
  2284. return;
  2285. }
  2286. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  2287. view_set_enter_callback(view, flip_social_loader_on_enter);
  2288. view_set_exit_callback(view, flip_social_loader_on_exit);
  2289. }
  2290. void flip_social_loader_free_model(View *view)
  2291. {
  2292. if (view == NULL)
  2293. {
  2294. FURI_LOG_E(TAG, "flip_social_loader_free_model - view is NULL");
  2295. DEV_CRASH();
  2296. return;
  2297. }
  2298. with_view_model(
  2299. view,
  2300. DataLoaderModel * model,
  2301. {
  2302. if (model->timer)
  2303. {
  2304. furi_timer_free(model->timer);
  2305. model->timer = NULL;
  2306. }
  2307. if (model->parser_context)
  2308. {
  2309. free(model->parser_context);
  2310. model->parser_context = NULL;
  2311. }
  2312. },
  2313. false);
  2314. }
  2315. bool flip_social_custom_event_callback(void *context, uint32_t index)
  2316. {
  2317. if (context == NULL)
  2318. {
  2319. FURI_LOG_E(TAG, "flip_social_custom_event_callback - context is NULL");
  2320. DEV_CRASH();
  2321. return false;
  2322. }
  2323. switch (index)
  2324. {
  2325. case FlipSocialCustomEventProcess:
  2326. flip_social_loader_process_callback(context);
  2327. return true;
  2328. default:
  2329. FURI_LOG_DEV(TAG, "flip_social_custom_event_callback. Unknown index: %ld", index);
  2330. return false;
  2331. }
  2332. }
  2333. 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)
  2334. {
  2335. if (app == NULL)
  2336. {
  2337. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - app is NULL");
  2338. DEV_CRASH();
  2339. return;
  2340. }
  2341. View *view = app->view_loader;
  2342. if (view == NULL)
  2343. {
  2344. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - view is NULL");
  2345. DEV_CRASH();
  2346. return;
  2347. }
  2348. with_view_model(
  2349. view,
  2350. DataLoaderModel * model,
  2351. {
  2352. model->title = title;
  2353. model->fetcher = fetcher;
  2354. model->parser = parser;
  2355. model->request_index = 0;
  2356. model->request_count = request_count;
  2357. model->back_callback = back;
  2358. model->data_state = DataStateInitial;
  2359. model->data_text = NULL;
  2360. },
  2361. true);
  2362. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2363. }
  2364. static bool flip_social_get_home_notification()
  2365. {
  2366. if (!app_instance)
  2367. {
  2368. FURI_LOG_E(TAG, "app_instance is NULL");
  2369. return false;
  2370. }
  2371. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  2372. {
  2373. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  2374. return false;
  2375. }
  2376. // Create the directory for saving settings
  2377. char directory_path[256];
  2378. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data");
  2379. // Create the directory
  2380. Storage *storage = furi_record_open(RECORD_STORAGE);
  2381. storage_common_mkdir(storage, directory_path);
  2382. furi_record_close(RECORD_STORAGE);
  2383. auth_headers_alloc();
  2384. snprintf(
  2385. fhttp.file_path,
  2386. sizeof(fhttp.file_path),
  2387. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data/notification.json");
  2388. fhttp.save_received_data = true;
  2389. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/flip-social-notifications/", auth_headers);
  2390. }
  2391. static bool flip_social_parse_home_notification()
  2392. {
  2393. FuriString *notification = flipper_http_load_from_file(fhttp.file_path);
  2394. if (notification == NULL)
  2395. {
  2396. FURI_LOG_E(TAG, "Failed to load notification from file");
  2397. flipper_http_deinit();
  2398. return false;
  2399. }
  2400. flipper_http_deinit();
  2401. // check if announcement and analytics key exists
  2402. FuriString *announcement_json = get_json_value_furi("announcement", notification);
  2403. FuriString *analytics_json = get_json_value_furi("analytics", notification);
  2404. if (announcement_json == NULL || analytics_json == NULL)
  2405. {
  2406. FURI_LOG_E(TAG, "Failed to get announcement or analytics from notification");
  2407. if (announcement_json)
  2408. {
  2409. furi_string_free(announcement_json);
  2410. }
  2411. if (analytics_json)
  2412. {
  2413. furi_string_free(analytics_json);
  2414. }
  2415. return false;
  2416. }
  2417. FuriString *announcement_value = get_json_value_furi("content", announcement_json);
  2418. FuriString *announcement_time = get_json_value_furi("date_created", announcement_json);
  2419. FuriString *analytics_value = get_json_value_furi("count", analytics_json);
  2420. FuriString *analytics_time = get_json_value_furi("time", analytics_json);
  2421. if (!announcement_value || !announcement_time || !analytics_value || !analytics_time)
  2422. {
  2423. FURI_LOG_E(TAG, "Failed to get announcement or analytics value from notification");
  2424. if (announcement_value)
  2425. {
  2426. furi_string_free(announcement_value);
  2427. }
  2428. if (announcement_time)
  2429. {
  2430. furi_string_free(announcement_time);
  2431. }
  2432. if (analytics_value)
  2433. {
  2434. furi_string_free(analytics_value);
  2435. }
  2436. if (analytics_time)
  2437. {
  2438. furi_string_free(analytics_time);
  2439. }
  2440. return false;
  2441. }
  2442. // load previous announcement and analytics times to see if there is a new announcement or analytics
  2443. char past_analytics_time[32];
  2444. char past_announcement_time[32];
  2445. if (load_char("analytics_time", past_analytics_time, sizeof(past_analytics_time)) && load_char("announcement_time", past_announcement_time, sizeof(past_announcement_time)))
  2446. {
  2447. // check if the announcement or analytics time has changed
  2448. if (strcmp(furi_string_get_cstr(announcement_time), past_announcement_time) == 0 && strcmp(furi_string_get_cstr(analytics_time), past_analytics_time) == 0)
  2449. {
  2450. FURI_LOG_D(TAG, "No new announcement or analytics");
  2451. furi_string_free(announcement_value);
  2452. furi_string_free(announcement_time);
  2453. furi_string_free(analytics_value);
  2454. furi_string_free(analytics_time);
  2455. furi_string_free(announcement_json);
  2456. furi_string_free(analytics_json);
  2457. furi_string_free(notification);
  2458. return true;
  2459. }
  2460. }
  2461. // save the new announcement and analytics time
  2462. save_char("analytics_time", furi_string_get_cstr(analytics_time));
  2463. save_char("announcement_time", furi_string_get_cstr(announcement_time));
  2464. // show the announcement and analytics
  2465. char analytics_text[128];
  2466. // if previous analytics posts is not empty, then subtract the current posts from the previous psots and add it to analytics_text
  2467. if (atoi(furi_string_get_cstr(analytics_value)) > 0)
  2468. {
  2469. char past_analytics_value[32];
  2470. if (load_char("analytics_value", past_analytics_value, sizeof(past_analytics_value)))
  2471. {
  2472. int past_posts = atoi(past_analytics_value);
  2473. int current_posts = atoi(furi_string_get_cstr(analytics_value));
  2474. int new_posts = current_posts - past_posts;
  2475. snprintf(analytics_text, sizeof(analytics_text), "%s feed posts\n%d new posts", furi_string_get_cstr(analytics_value), new_posts);
  2476. }
  2477. else
  2478. {
  2479. snprintf(analytics_text, sizeof(analytics_text), "%s feed posts", furi_string_get_cstr(analytics_value));
  2480. }
  2481. save_char("analytics_value", furi_string_get_cstr(analytics_value));
  2482. }
  2483. else
  2484. {
  2485. snprintf(analytics_text, sizeof(analytics_text), "%s feed posts", furi_string_get_cstr(analytics_value));
  2486. }
  2487. easy_flipper_dialog("Announcement", (char *)furi_string_get_cstr(announcement_value));
  2488. easy_flipper_dialog("Analytics", analytics_text);
  2489. furi_string_free(announcement_value);
  2490. furi_string_free(announcement_time);
  2491. furi_string_free(analytics_value);
  2492. furi_string_free(analytics_time);
  2493. furi_string_free(announcement_json);
  2494. furi_string_free(analytics_json);
  2495. furi_string_free(notification);
  2496. return true;
  2497. }
  2498. // home notification
  2499. bool flip_social_home_notification()
  2500. {
  2501. return flipper_http_process_response_async(flip_social_get_home_notification, flip_social_parse_home_notification);
  2502. }