callback.c 97 KB

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