flip_social_callback.c 96 KB

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