callback.c 98 KB

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