flip_social_callback.c 100 KB

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