flip_social_callback.c 101 KB

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