flip_social_callback.c 99 KB

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