callback.c 99 KB

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