flip_social_callback.c 100 KB

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