flip_social_callback.c 95 KB

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