flip_social_callback.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  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. 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. char buffer[256];
  165. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
  166. auth_headers_alloc();
  167. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", auth_headers, buffer))
  168. {
  169. fhttp.state = RECEIVING;
  170. return true;
  171. }
  172. else
  173. {
  174. fhttp.state = ISSUE;
  175. return false;
  176. }
  177. }
  178. static char *flip_social_login_parse(DataLoaderModel *model)
  179. {
  180. UNUSED(model);
  181. // read response
  182. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  183. {
  184. app_instance->is_logged_in = "true";
  185. // set the logged_in_username and change_password_logged_in
  186. if (app_instance->login_username_logged_out)
  187. {
  188. strcpy(app_instance->login_username_logged_in, app_instance->login_username_logged_out);
  189. }
  190. if (app_instance->login_password_logged_out)
  191. {
  192. strcpy(app_instance->change_password_logged_in, app_instance->login_password_logged_out);
  193. }
  194. 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->is_logged_in);
  195. // send user to the logged in submenu
  196. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  197. return "Login successful!";
  198. }
  199. else if (strstr(fhttp.last_response, "User not found") != NULL)
  200. {
  201. return "Account not found...";
  202. }
  203. else
  204. {
  205. return "Failed to login...";
  206. }
  207. }
  208. static void flip_social_login_switch_to_view(FlipSocialApp *app)
  209. {
  210. 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);
  211. }
  212. static bool flip_social_register_fetch(DataLoaderModel *model)
  213. {
  214. UNUSED(model);
  215. // check if the username and password are valid
  216. 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)
  217. {
  218. FURI_LOG_E(TAG, "Username or password is NULL");
  219. return false;
  220. }
  221. // check if both passwords match
  222. if (strcmp(app_instance->register_password_logged_out, app_instance->register_password_2_logged_out) != 0)
  223. {
  224. FURI_LOG_E(TAG, "Passwords do not match");
  225. return false;
  226. }
  227. char buffer[128];
  228. snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
  229. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", buffer))
  230. {
  231. // Set the state to RECEIVING to ensure we continue to see the receiving message
  232. fhttp.state = RECEIVING;
  233. return true;
  234. }
  235. else
  236. {
  237. fhttp.state = ISSUE;
  238. return false;
  239. }
  240. }
  241. static char *flip_social_register_parse(DataLoaderModel *model)
  242. {
  243. UNUSED(model);
  244. // read response
  245. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  246. {
  247. // set the login credentials
  248. if (app_instance->login_username_logged_out)
  249. {
  250. app_instance->login_username_logged_out = app_instance->register_username_logged_out;
  251. }
  252. if (app_instance->login_password_logged_out)
  253. {
  254. app_instance->login_password_logged_out = app_instance->register_password_logged_out;
  255. app_instance->change_password_logged_in = app_instance->register_password_logged_out;
  256. }
  257. if (app_instance->login_username_logged_in)
  258. {
  259. app_instance->login_username_logged_in = app_instance->register_username_logged_out;
  260. }
  261. app_instance->is_logged_in = "true";
  262. // update header credentials
  263. auth_headers_alloc();
  264. // save the credentials
  265. 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->is_logged_in);
  266. // send user to the logged in submenu
  267. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  268. return "Registration successful!\nWelcome to FlipSocial!";
  269. }
  270. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  271. {
  272. return "Please enter your credentials.\nPress BACK to return.";
  273. }
  274. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  275. {
  276. return "Registration failed...\nUsername already exists.\nPress BACK to return.";
  277. }
  278. else
  279. {
  280. return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
  281. }
  282. }
  283. static void flip_social_register_switch_to_view(FlipSocialApp *app)
  284. {
  285. 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);
  286. }
  287. /**
  288. * @brief Navigation callback to go back to the submenu Logged out.
  289. * @param context The context - unused
  290. * @return next view id (FlipSocialViewLoggedOutSubmenu)
  291. */
  292. uint32_t flip_social_callback_to_submenu_logged_out(void *context)
  293. {
  294. UNUSED(context);
  295. return FlipSocialViewLoggedOutSubmenu;
  296. }
  297. static void flip_social_free_explore_dialog()
  298. {
  299. if (app_instance->dialog_explore)
  300. {
  301. dialog_ex_free(app_instance->dialog_explore);
  302. app_instance->dialog_explore = NULL;
  303. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewExploreDialog);
  304. }
  305. }
  306. static void flip_social_free_friends_dialog()
  307. {
  308. if (app_instance->dialog_friends)
  309. {
  310. dialog_ex_free(app_instance->dialog_friends);
  311. app_instance->dialog_friends = NULL;
  312. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewFriendsDialog);
  313. }
  314. }
  315. static void flip_social_free_messages_dialog()
  316. {
  317. if (app_instance->dialog_messages)
  318. {
  319. dialog_ex_free(app_instance->dialog_messages);
  320. app_instance->dialog_messages = NULL;
  321. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewMessagesDialog);
  322. return;
  323. }
  324. }
  325. static void flip_social_free_compose_dialog()
  326. {
  327. if (app_instance->dialog_compose)
  328. {
  329. dialog_ex_free(app_instance->dialog_compose);
  330. app_instance->dialog_compose = NULL;
  331. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewComposeDialog);
  332. }
  333. }
  334. static void flip_social_free_feed_dialog()
  335. {
  336. if (app_instance->dialog_feed)
  337. {
  338. dialog_ex_free(app_instance->dialog_feed);
  339. app_instance->dialog_feed = NULL;
  340. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewFeedDialog);
  341. }
  342. }
  343. /**
  344. * @brief Navigation callback to go back to the submenu Logged in.
  345. * @param context The context - unused
  346. * @return next view id (FlipSocialViewLoggedInSubmenu)
  347. */
  348. uint32_t flip_social_callback_to_submenu_logged_in(void *context)
  349. {
  350. UNUSED(context);
  351. free_about_widget(false);
  352. //
  353. flip_social_free_friends();
  354. flip_social_free_message_users();
  355. flip_feed_info_free();
  356. if (flip_feed_item)
  357. {
  358. free(flip_feed_item);
  359. flip_feed_item = NULL;
  360. }
  361. // free the about widget if it exists
  362. free_about_widget(true);
  363. flip_social_free_explore_dialog();
  364. flip_social_free_friends_dialog();
  365. flip_social_free_messages_dialog();
  366. flip_social_free_compose_dialog();
  367. return FlipSocialViewLoggedInSubmenu;
  368. }
  369. /**
  370. * @brief Navigation callback to bring the user back to the (Logged out) Login screen
  371. * @param context The context - unused
  372. * @return next view id (FlipSocialViewLoggedOutLogin)
  373. */
  374. uint32_t flip_social_callback_to_login_logged_out(void *context)
  375. {
  376. UNUSED(context);
  377. flip_social_sent_login_request = false;
  378. flip_social_login_success = false;
  379. return FlipSocialViewLoggedOutLogin;
  380. }
  381. /**
  382. * @brief Navigation callback to bring the user back to the (Logged out) Register screen
  383. * @param context The context - unused
  384. * @return next view id (FlipSocialViewLoggedOutRegister)
  385. */
  386. uint32_t flip_social_callback_to_register_logged_out(void *context)
  387. {
  388. UNUSED(context);
  389. flip_social_sent_register_request = false;
  390. flip_social_register_success = false;
  391. return FlipSocialViewLoggedOutRegister;
  392. }
  393. /**
  394. * @brief Navigation callback to bring the user back to the (Logged out) Wifi Settings screen
  395. * @param context The context - unused
  396. * @return next view id (FlipSocialViewLoggedOutWifiSettings)
  397. */
  398. uint32_t flip_social_callback_to_wifi_settings_logged_out(void *context)
  399. {
  400. UNUSED(context);
  401. return FlipSocialViewLoggedOutWifiSettings;
  402. }
  403. /**
  404. * @brief Navigation callback to bring the user back to the (Logged in) Wifi Settings screen
  405. * @param context The context - unused
  406. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  407. */
  408. uint32_t flip_social_callback_to_wifi_settings_logged_in(void *context)
  409. {
  410. UNUSED(context);
  411. return FlipSocialViewLoggedInSettingsWifi;
  412. }
  413. /**
  414. * @brief Navigation callback to bring the user back to the (Logged in) Settings screen
  415. * @param context The context - unused
  416. * @return next view id (FlipSocialViewLoggedInSettingsWifi)
  417. */
  418. uint32_t flip_social_callback_to_settings_logged_in(void *context)
  419. {
  420. UNUSED(context);
  421. return FlipSocialViewLoggedInSettings;
  422. }
  423. /**
  424. * @brief Navigation callback to bring the user back to the (Logged in) Compose screen
  425. * @param context The context - unused
  426. * @return next view id (FlipSocialViewLoggedInCompose)
  427. */
  428. uint32_t flip_social_callback_to_compose_logged_in(void *context)
  429. {
  430. UNUSED(context);
  431. return FlipSocialViewLoggedInCompose;
  432. }
  433. /**
  434. * @brief Navigation callback to bring the user back to the (Logged in) Profile screen
  435. * @param context The context - unused
  436. * @return next view id (FlipSocialViewLoggedInProfile)
  437. */
  438. uint32_t flip_social_callback_to_profile_logged_in(void *context)
  439. {
  440. UNUSED(context);
  441. return FlipSocialViewLoggedInProfile;
  442. }
  443. /**
  444. * @brief Navigation callback to bring the user back to the Explore submenu
  445. * @param context The context - unused
  446. * @return next view id (FlipSocialViewLoggedInExploreSubmenu)
  447. */
  448. uint32_t flip_social_callback_to_explore_logged_in(void *context)
  449. {
  450. UNUSED(context);
  451. flip_social_dialog_stop = false;
  452. last_explore_response = "";
  453. flip_social_dialog_shown = false;
  454. if (flip_social_explore)
  455. {
  456. flip_social_explore->index = 0;
  457. }
  458. return FlipSocialViewLoggedInExploreSubmenu;
  459. }
  460. /**
  461. * @brief Navigation callback to bring the user back to the Friends submenu
  462. * @param context The context - unused
  463. * @return next view id (FlipSocialViewLoggedInFriendsSubmenu)
  464. */
  465. uint32_t flip_social_callback_to_friends_logged_in(void *context)
  466. {
  467. UNUSED(context);
  468. flip_social_dialog_stop = false;
  469. last_explore_response = "";
  470. flip_social_dialog_shown = false;
  471. flip_social_friends->index = 0;
  472. return FlipSocialViewLoggedInFriendsSubmenu;
  473. }
  474. /**
  475. * @brief Navigation callback to bring the user back to the Messages submenu
  476. * @param context The context - unused
  477. * @return next view id (FlipSocialViewLoggedInMessagesSubmenu)
  478. */
  479. uint32_t flip_social_callback_to_messages_logged_in(void *context)
  480. {
  481. UNUSED(context);
  482. return FlipSocialViewLoggedInMessagesSubmenu;
  483. }
  484. /**
  485. * @brief Navigation callback to bring the user back to the User Choices screen
  486. * @param context The context - unused
  487. * @return next view id (FlipSocialViewLoggedInMessagesUserChoices)
  488. */
  489. uint32_t flip_social_callback_to_messages_user_choices(void *context)
  490. {
  491. UNUSED(context);
  492. return FlipSocialViewLoggedInMessagesUserChoices;
  493. }
  494. static void free_flip_social_group()
  495. {
  496. flip_social_free_messages();
  497. flip_social_free_explore();
  498. flip_social_free_feed_dialog();
  499. }
  500. /**
  501. * @brief Navigation callback for exiting the application
  502. * @param context The context - unused
  503. * @return next view id (VIEW_NONE to exit the app)
  504. */
  505. uint32_t flip_social_callback_exit_app(void *context)
  506. {
  507. // Exit the application
  508. UNUSED(context);
  509. free_flip_social_group();
  510. free_pre_saved_messages();
  511. return VIEW_NONE; // Return VIEW_NONE to exit the app
  512. }
  513. static void explore_dialog_callback(DialogExResult result, void *context)
  514. {
  515. furi_assert(context);
  516. FlipSocialApp *app = (FlipSocialApp *)context;
  517. if (result == DialogExResultLeft) // Remove
  518. {
  519. // remove friend
  520. char remove_payload[128];
  521. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  522. auth_headers_alloc();
  523. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
  524. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  525. flip_social_free_explore_dialog();
  526. }
  527. else if (result == DialogExResultRight)
  528. {
  529. // add friend
  530. char add_payload[128];
  531. snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
  532. auth_headers_alloc();
  533. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/add-friend/", auth_headers, add_payload);
  534. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  535. flip_social_free_explore_dialog();
  536. }
  537. }
  538. static void friends_dialog_callback(DialogExResult result, void *context)
  539. {
  540. furi_assert(context);
  541. FlipSocialApp *app = (FlipSocialApp *)context;
  542. if (result == DialogExResultLeft) // Remove
  543. {
  544. // remove friend
  545. char remove_payload[128];
  546. snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
  547. auth_headers_alloc();
  548. flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
  549. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
  550. flip_social_free_friends_dialog();
  551. }
  552. }
  553. static void messages_dialog_callback(DialogExResult result, void *context)
  554. {
  555. furi_assert(context);
  556. FlipSocialApp *app = (FlipSocialApp *)context;
  557. if (result == DialogExResultLeft) // Previous message
  558. {
  559. if (flip_social_messages->index > 0)
  560. {
  561. flip_social_messages->index--;
  562. dialog_ex_reset(app->dialog_messages);
  563. dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
  564. dialog_ex_set_text(app->dialog_messages, flip_social_messages->messages[flip_social_messages->index], 0, 10, AlignLeft, AlignTop);
  565. if (flip_social_messages->index != 0)
  566. {
  567. dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
  568. }
  569. dialog_ex_set_right_button_text(app->dialog_messages, "Next");
  570. dialog_ex_set_center_button_text(app->dialog_messages, "Create");
  571. // switch view, free dialog, re-alloc dialog, switch back to dialog
  572. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  573. flip_social_free_messages_dialog();
  574. messages_dialog_alloc(false);
  575. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
  576. }
  577. }
  578. else if (result == DialogExResultRight) // Next message
  579. {
  580. if (flip_social_messages->index < flip_social_messages->count - 1)
  581. {
  582. flip_social_messages->index++;
  583. dialog_ex_reset(app->dialog_messages);
  584. dialog_ex_set_header(app->dialog_messages, flip_social_messages->usernames[flip_social_messages->index], 0, 0, AlignLeft, AlignTop);
  585. dialog_ex_set_text(app->dialog_messages, flip_social_messages->messages[flip_social_messages->index], 0, 10, AlignLeft, AlignTop);
  586. dialog_ex_set_left_button_text(app->dialog_messages, "Prev");
  587. if (flip_social_messages->index != flip_social_messages->count - 1)
  588. {
  589. dialog_ex_set_right_button_text(app->dialog_messages, "Next");
  590. }
  591. dialog_ex_set_center_button_text(app->dialog_messages, "Create");
  592. // switch view, free dialog, re-alloc dialog, switch back to dialog
  593. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  594. flip_social_free_messages_dialog();
  595. messages_dialog_alloc(false);
  596. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewMessagesDialog);
  597. }
  598. }
  599. else if (result == DialogExResultCenter) // new message
  600. {
  601. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  602. }
  603. }
  604. bool messages_dialog_alloc(bool free_first)
  605. {
  606. if (free_first)
  607. {
  608. flip_social_free_messages_dialog();
  609. }
  610. if (!app_instance->dialog_messages)
  611. {
  612. if (!easy_flipper_set_dialog_ex(
  613. &app_instance->dialog_messages,
  614. FlipSocialViewMessagesDialog,
  615. flip_social_messages->usernames[flip_social_messages->index],
  616. 0,
  617. 0,
  618. flip_social_messages->messages[flip_social_messages->index],
  619. 0,
  620. 10,
  621. flip_social_messages->index != 0 ? "Prev" : NULL,
  622. flip_social_messages->index != flip_social_messages->count - 1 ? "Next" : NULL,
  623. "Create",
  624. messages_dialog_callback,
  625. flip_social_callback_to_messages_logged_in,
  626. &app_instance->view_dispatcher,
  627. app_instance))
  628. {
  629. return false;
  630. }
  631. return true;
  632. }
  633. return false;
  634. }
  635. static void compose_dialog_callback(DialogExResult result, void *context)
  636. {
  637. furi_assert(context);
  638. FlipSocialApp *app = (FlipSocialApp *)context;
  639. if (result == DialogExResultLeft) // Delete
  640. {
  641. // delete message
  642. app->pre_saved_messages.messages[app_instance->pre_saved_messages.index] = NULL;
  643. for (uint32_t i = app->pre_saved_messages.index; i < app->pre_saved_messages.count - 1; i++)
  644. {
  645. app->pre_saved_messages.messages[i] = app->pre_saved_messages.messages[i + 1];
  646. }
  647. app->pre_saved_messages.count--;
  648. // add the item to the submenu
  649. submenu_reset(app_instance->submenu_compose);
  650. submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  651. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  652. {
  653. submenu_add_item(app_instance->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  654. }
  655. // save playlist
  656. save_playlist(&app_instance->pre_saved_messages);
  657. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  658. flip_social_free_compose_dialog();
  659. }
  660. else if (result == DialogExResultRight) // Post
  661. {
  662. // post the message
  663. // send selected_message
  664. if (selected_message && app_instance->login_username_logged_in)
  665. {
  666. if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
  667. {
  668. FURI_LOG_E(TAG, "Message is too long");
  669. return;
  670. }
  671. // Send the selected_message
  672. char command[256];
  673. snprintf(command, sizeof(command), "{\"username\":\"%s\",\"content\":\"%s\"}",
  674. app_instance->login_username_logged_in, selected_message);
  675. if (!flipper_http_post_request_with_headers(
  676. "https://www.flipsocial.net/api/feed/post/",
  677. auth_headers,
  678. command))
  679. {
  680. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  681. fhttp.state = ISSUE;
  682. return;
  683. }
  684. fhttp.state = RECEIVING;
  685. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  686. }
  687. else
  688. {
  689. FURI_LOG_E(TAG, "Message or username is NULL");
  690. return;
  691. }
  692. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  693. {
  694. furi_delay_ms(100);
  695. }
  696. if (flip_social_load_initial_feed())
  697. {
  698. flip_social_free_compose_dialog();
  699. }
  700. FURI_LOG_E(TAG, "Failed to load initial feed");
  701. return;
  702. }
  703. }
  704. static void feed_dialog_callback(DialogExResult result, void *context)
  705. {
  706. furi_assert(context);
  707. FlipSocialApp *app = (FlipSocialApp *)context;
  708. if (result == DialogExResultLeft) // Previous message
  709. {
  710. if (flip_feed_info->index > 0)
  711. {
  712. flip_feed_info->index--;
  713. }
  714. // switch view, free dialog, re-alloc dialog, switch back to dialog
  715. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  716. // load feed item
  717. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  718. {
  719. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  720. fhttp.state = ISSUE;
  721. return;
  722. }
  723. if (feed_dialog_alloc())
  724. {
  725. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
  726. }
  727. else
  728. {
  729. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  730. fhttp.state = ISSUE;
  731. return;
  732. }
  733. }
  734. else if (result == DialogExResultRight) // Next message
  735. {
  736. if (flip_feed_info->index < flip_feed_info->count - 1)
  737. {
  738. flip_feed_info->index++;
  739. }
  740. // switch view, free dialog, re-alloc dialog, switch back to dialog
  741. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  742. // load feed item
  743. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  744. {
  745. FURI_LOG_E(TAG, "Failed to load nexy feed post");
  746. fhttp.state = ISSUE;
  747. return;
  748. }
  749. if (feed_dialog_alloc())
  750. {
  751. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
  752. }
  753. else
  754. {
  755. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  756. fhttp.state = ISSUE;
  757. return;
  758. }
  759. }
  760. else if (result == DialogExResultCenter) // Flip/Unflip
  761. {
  762. // Moved to above the is_flipped check
  763. if (!flip_feed_item->is_flipped)
  764. {
  765. // increase the flip count
  766. flip_feed_item->flips++;
  767. }
  768. else
  769. {
  770. // decrease the flip count
  771. flip_feed_item->flips--;
  772. }
  773. // change the flip status
  774. flip_feed_item->is_flipped = !flip_feed_item->is_flipped;
  775. // send post request to flip the message
  776. if (app_instance->login_username_logged_in == NULL)
  777. {
  778. FURI_LOG_E(TAG, "Username is NULL");
  779. return;
  780. }
  781. char payload[256];
  782. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%u\"}", app_instance->login_username_logged_in, flip_feed_item->id);
  783. if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", auth_headers, payload))
  784. {
  785. // save feed item
  786. char new_save[256];
  787. snprintf(new_save, sizeof(new_save), "{\"id\":%u,\"username\":\"%s\",\"message\":\"%s\",\"flip_count\":%u,\"flipped\":%s}",
  788. flip_feed_item->id, flip_feed_item->username, flip_feed_item->message, flip_feed_item->flips, flip_feed_item->is_flipped ? "true" : "false");
  789. if (!flip_social_save_post((char *)flip_feed_item->id, new_save))
  790. {
  791. FURI_LOG_E(TAG, "Failed to save the feed post");
  792. fhttp.state = ISSUE;
  793. return;
  794. }
  795. }
  796. // switch view, free dialog, re-alloc dialog, switch back to dialog
  797. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  798. if (feed_dialog_alloc())
  799. {
  800. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
  801. }
  802. else
  803. {
  804. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  805. fhttp.state = ISSUE;
  806. return;
  807. }
  808. }
  809. }
  810. bool feed_dialog_alloc()
  811. {
  812. if (!flip_feed_item)
  813. {
  814. FURI_LOG_E(TAG, "Feed item is NULL");
  815. return false;
  816. }
  817. flip_social_free_feed_dialog();
  818. if (!app_instance->dialog_feed)
  819. {
  820. if (!easy_flipper_set_dialog_ex(
  821. &app_instance->dialog_feed,
  822. FlipSocialViewFeedDialog,
  823. flip_feed_item->username,
  824. 0,
  825. 0,
  826. flip_feed_item->message,
  827. 0,
  828. 10,
  829. flip_feed_info->index != 0 ? "Prev" : NULL,
  830. flip_feed_info->index != flip_feed_info->count - 1 ? "Next" : NULL,
  831. flip_feed_item->is_flipped ? "Unflip" : "Flip",
  832. feed_dialog_callback,
  833. flip_social_callback_to_submenu_logged_in,
  834. &app_instance->view_dispatcher,
  835. app_instance))
  836. {
  837. return false;
  838. }
  839. return true;
  840. }
  841. return false;
  842. }
  843. /**
  844. * @brief Handle ALL submenu item selections.
  845. * @param context The context - FlipSocialApp object.
  846. * @param index The FlipSocialSubmenuIndex item that was clicked.
  847. * @return void
  848. */
  849. void flip_social_callback_submenu_choices(void *context, uint32_t index)
  850. {
  851. FlipSocialApp *app = (FlipSocialApp *)context;
  852. if (!app)
  853. {
  854. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  855. return;
  856. }
  857. switch (index)
  858. {
  859. case FlipSocialSubmenuLoggedOutIndexLogin:
  860. flip_social_sent_login_request = false;
  861. flip_social_login_success = false;
  862. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  863. break;
  864. case FlipSocialSubmenuLoggedOutIndexRegister:
  865. flip_social_sent_register_request = false;
  866. flip_social_register_success = false;
  867. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  868. break;
  869. case FlipSocialSubmenuLoggedOutIndexAbout:
  870. if (!about_widget_alloc(false))
  871. {
  872. return;
  873. }
  874. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutAbout);
  875. break;
  876. case FlipSocialSubmenuLoggedOutIndexWifiSettings:
  877. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  878. break;
  879. case FlipSocialSubmenuLoggedInIndexProfile:
  880. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  881. break;
  882. case FlipSocialSubmenuLoggedInIndexMessages:
  883. free_flip_social_group();
  884. flipper_http_loading_task(
  885. flip_social_get_message_users, // get the message users
  886. flip_social_parse_json_message_users, // parse the message users
  887. FlipSocialViewLoggedInMessagesSubmenu, // switch to the messages submenu if successful
  888. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  889. &app->view_dispatcher); // view dispatcher
  890. break;
  891. case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
  892. flipper_http_loading_task(
  893. flip_social_get_explore, // get the explore users
  894. flip_social_parse_json_message_user_choices, // parse the explore users
  895. FlipSocialViewLoggedInMessagesUserChoices, // switch to the user choices if successful
  896. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  897. &app->view_dispatcher); // view dispatcher
  898. break;
  899. case FlipSocialSubmenuLoggedInIndexFeed:
  900. free_flip_social_group();
  901. if (!flip_social_load_initial_feed())
  902. {
  903. FURI_LOG_E(TAG, "Failed to load the initial feed");
  904. return;
  905. }
  906. free_pre_saved_messages();
  907. break;
  908. case FlipSocialSubmenuExploreIndex:
  909. free_flip_social_group();
  910. flipper_http_loading_task(
  911. flip_social_get_explore, // get the explore users
  912. flip_social_parse_json_explore, // parse the explore users
  913. FlipSocialViewLoggedInExploreSubmenu, // switch to the explore submenu if successful
  914. FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
  915. &app->view_dispatcher); // view dispatcher
  916. break;
  917. case FlipSocialSubmenuLoggedInIndexCompose:
  918. free_pre_saved_messages();
  919. if (!pre_saved_messages_alloc())
  920. {
  921. FURI_LOG_E(TAG, "Failed to allocate pre-saved messages");
  922. return;
  923. }
  924. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  925. break;
  926. case FlipSocialSubmenuLoggedInIndexSettings:
  927. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
  928. break;
  929. case FlipSocialSubmenuLoggedInSignOutButton:
  930. free_flip_social_group();
  931. app->is_logged_in = "false";
  932. 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->is_logged_in);
  933. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
  934. break;
  935. case FlipSocialSubmenuComposeIndexAddPreSave:
  936. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInComposeAddPreSaveInput);
  937. break;
  938. default:
  939. // Handle the pre-saved message selection (has a max of 25 items)
  940. if (index >= FlipSocialSubemnuComposeIndexStartIndex && index < FlipSocialSubemnuComposeIndexStartIndex + MAX_PRE_SAVED_MESSAGES)
  941. {
  942. app->pre_saved_messages.index = index - FlipSocialSubemnuComposeIndexStartIndex;
  943. snprintf(selected_message, MAX_MESSAGE_LENGTH, "%s", app->pre_saved_messages.messages[app->pre_saved_messages.index]);
  944. if (!selected_message)
  945. {
  946. FURI_LOG_E(TAG, "Selected message is NULL");
  947. return;
  948. }
  949. flip_social_free_compose_dialog();
  950. if (!app->dialog_compose)
  951. {
  952. if (!easy_flipper_set_dialog_ex(
  953. &app->dialog_compose,
  954. FlipSocialViewComposeDialog,
  955. "New Feed Post",
  956. 0,
  957. 0,
  958. selected_message,
  959. 0,
  960. 10,
  961. "Delete",
  962. "Post",
  963. NULL,
  964. compose_dialog_callback,
  965. flip_social_callback_to_compose_logged_in,
  966. &app->view_dispatcher,
  967. app))
  968. {
  969. return;
  970. }
  971. }
  972. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewComposeDialog);
  973. }
  974. // Handle the explore selection
  975. else if (index >= FlipSocialSubmenuExploreIndexStartIndex && index < FlipSocialSubmenuExploreIndexStartIndex + MAX_EXPLORE_USERS)
  976. {
  977. if (!flip_social_explore)
  978. {
  979. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  980. return;
  981. }
  982. flip_social_explore->index = index - FlipSocialSubmenuExploreIndexStartIndex;
  983. flip_social_free_explore_dialog();
  984. if (!app->dialog_explore)
  985. {
  986. if (!easy_flipper_set_dialog_ex(
  987. &app->dialog_explore,
  988. FlipSocialViewExploreDialog,
  989. "User Options",
  990. 0,
  991. 0,
  992. flip_social_explore->usernames[flip_social_explore->index],
  993. 0,
  994. 10,
  995. "Remove",
  996. "Add",
  997. NULL,
  998. explore_dialog_callback,
  999. flip_social_callback_to_explore_logged_in,
  1000. &app->view_dispatcher,
  1001. app))
  1002. {
  1003. return;
  1004. }
  1005. }
  1006. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewExploreDialog);
  1007. }
  1008. // handle the friends selection
  1009. else if (index >= FlipSocialSubmenuLoggedInIndexFriendsStart && index < FlipSocialSubmenuLoggedInIndexFriendsStart + MAX_FRIENDS)
  1010. {
  1011. if (!flip_social_friends)
  1012. {
  1013. FURI_LOG_E(TAG, "FlipSocialFriends is NULL");
  1014. return;
  1015. }
  1016. flip_social_friends->index = index - FlipSocialSubmenuLoggedInIndexFriendsStart;
  1017. flip_social_free_friends_dialog();
  1018. if (!app->dialog_friends)
  1019. {
  1020. if (!easy_flipper_set_dialog_ex(
  1021. &app->dialog_friends,
  1022. FlipSocialViewFriendsDialog,
  1023. "User Options",
  1024. 0,
  1025. 0,
  1026. flip_social_friends->usernames[flip_social_friends->index],
  1027. 0,
  1028. 10,
  1029. "Remove",
  1030. "",
  1031. NULL,
  1032. friends_dialog_callback,
  1033. flip_social_callback_to_friends_logged_in,
  1034. &app->view_dispatcher,
  1035. app))
  1036. {
  1037. return;
  1038. }
  1039. }
  1040. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFriendsDialog);
  1041. }
  1042. // handle the messages selection
  1043. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUsersStart && index < FlipSocialSubmenuLoggedInIndexMessagesUsersStart + MAX_MESSAGE_USERS)
  1044. {
  1045. if (!flip_social_message_users)
  1046. {
  1047. FURI_LOG_E(TAG, "FlipSocialMessageUsers is NULL");
  1048. return;
  1049. }
  1050. flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
  1051. flipper_http_loading_task(
  1052. flip_social_get_messages_with_user, // get the messages with the selected user
  1053. flip_social_parse_json_messages, // parse the messages
  1054. FlipSocialViewMessagesDialog, // switch to the messages process if successful
  1055. FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
  1056. &app->view_dispatcher // view dispatcher
  1057. );
  1058. }
  1059. // handle the messages user choices selection
  1060. else if (index >= FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart && index < FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + MAX_EXPLORE_USERS)
  1061. {
  1062. if (!flip_social_explore)
  1063. {
  1064. FURI_LOG_E(TAG, "FlipSocialExplore is NULL");
  1065. return;
  1066. }
  1067. flip_social_explore->index = index - FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart;
  1068. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  1069. }
  1070. else
  1071. {
  1072. FURI_LOG_E(TAG, "Unknown submenu index");
  1073. }
  1074. break;
  1075. }
  1076. }
  1077. /**
  1078. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged out) screen.
  1079. * @param context The context - FlipSocialApp object.
  1080. * @return void
  1081. */
  1082. void flip_social_logged_out_wifi_settings_ssid_updated(void *context)
  1083. {
  1084. FlipSocialApp *app = (FlipSocialApp *)context;
  1085. if (!app)
  1086. {
  1087. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1088. return;
  1089. }
  1090. // Store the entered name
  1091. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1092. // Store the entered name in the logged in name field
  1093. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1094. strncpy(app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_out_temp_buffer_size);
  1095. // Ensure null-termination
  1096. app->wifi_ssid_logged_out[app->wifi_ssid_logged_out_temp_buffer_size - 1] = '\0';
  1097. // Update the name item text
  1098. if (app->variable_item_logged_out_wifi_settings_ssid)
  1099. {
  1100. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_ssid, app->wifi_ssid_logged_out);
  1101. }
  1102. // update the wifi settings
  1103. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1104. {
  1105. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1106. {
  1107. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1108. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1109. }
  1110. }
  1111. // Save the settings
  1112. 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->is_logged_in);
  1113. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  1114. }
  1115. /**
  1116. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged out) screen.
  1117. * @param context The context - FlipSocialApp object.
  1118. * @return void
  1119. */
  1120. void flip_social_logged_out_wifi_settings_password_updated(void *context)
  1121. {
  1122. FlipSocialApp *app = (FlipSocialApp *)context;
  1123. if (!app)
  1124. {
  1125. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1126. return;
  1127. }
  1128. // Store the entered WiFi password
  1129. strncpy(app->wifi_password_logged_out, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1130. // Store the entered WiFi password in the logged in password field
  1131. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1132. strncpy(app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_out_temp_buffer, app->wifi_password_logged_out_temp_buffer_size);
  1133. // Ensure null-termination
  1134. app->wifi_password_logged_out[app->wifi_password_logged_out_temp_buffer_size - 1] = '\0';
  1135. // Update the password item text
  1136. if (app->variable_item_logged_out_wifi_settings_password)
  1137. {
  1138. variable_item_set_current_value_text(app->variable_item_logged_out_wifi_settings_password, app->wifi_password_logged_out);
  1139. }
  1140. // update the wifi settings
  1141. if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
  1142. {
  1143. if (!flipper_http_save_wifi(app->wifi_ssid_logged_out, app->wifi_password_logged_out))
  1144. {
  1145. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1146. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1147. }
  1148. }
  1149. // Save the settings
  1150. 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->is_logged_in);
  1151. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettings);
  1152. }
  1153. /**
  1154. * @brief Callback when the user selects a menu item in the wifi settings (logged out) screen.
  1155. * @param context The context - FlipSocialApp object.
  1156. * @param index The index of the selected item.
  1157. * @return void
  1158. */
  1159. void flip_social_text_input_logged_out_wifi_settings_item_selected(void *context, uint32_t index)
  1160. {
  1161. FlipSocialApp *app = (FlipSocialApp *)context;
  1162. if (!app)
  1163. {
  1164. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1165. return;
  1166. }
  1167. switch (index)
  1168. {
  1169. case 0: // Input SSID
  1170. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsSSIDInput);
  1171. break;
  1172. case 1: // Input Password
  1173. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutWifiSettingsPasswordInput);
  1174. break;
  1175. default:
  1176. FURI_LOG_E(TAG, "Unknown configuration item index");
  1177. break;
  1178. }
  1179. }
  1180. /**
  1181. * @brief Text input callback for when the user finishes entering their username on the login (logged out) screen.
  1182. * @param context The context - FlipSocialApp object.
  1183. * @return void
  1184. */
  1185. void flip_social_logged_out_login_username_updated(void *context)
  1186. {
  1187. FlipSocialApp *app = (FlipSocialApp *)context;
  1188. if (!app)
  1189. {
  1190. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1191. return;
  1192. }
  1193. // Store the entered name
  1194. strncpy(app->login_username_logged_out, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1195. // Store the entered name in the logged in username field
  1196. strncpy(app->login_username_logged_in, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1197. strncpy(app->login_username_logged_in_temp_buffer, app->login_username_logged_out_temp_buffer, app->login_username_logged_out_temp_buffer_size);
  1198. // Ensure null-termination
  1199. app->login_username_logged_out[app->login_username_logged_out_temp_buffer_size - 1] = '\0';
  1200. // Update the name item text
  1201. if (app->variable_item_logged_out_login_username)
  1202. {
  1203. variable_item_set_current_value_text(app->variable_item_logged_out_login_username, app->login_username_logged_out);
  1204. }
  1205. // Save the settings
  1206. 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->is_logged_in);
  1207. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  1208. }
  1209. /**
  1210. * @brief Text input callback for when the user finishes entering their password on the login (logged out) screen.
  1211. * @param context The context - FlipSocialApp object.
  1212. * @return void
  1213. */
  1214. void flip_social_logged_out_login_password_updated(void *context)
  1215. {
  1216. FlipSocialApp *app = (FlipSocialApp *)context;
  1217. if (!app)
  1218. {
  1219. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1220. return;
  1221. }
  1222. // Store the entered password
  1223. strncpy(app->login_password_logged_out, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1224. // Store the entered password in the change password field
  1225. strncpy(app->change_password_logged_in, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1226. strncpy(app->change_password_logged_in_temp_buffer, app->login_password_logged_out_temp_buffer, app->login_password_logged_out_temp_buffer_size);
  1227. // Ensure null-termination
  1228. app->login_password_logged_out[app->login_password_logged_out_temp_buffer_size - 1] = '\0';
  1229. // Update the password item text
  1230. if (app->variable_item_logged_out_login_password)
  1231. {
  1232. // dont show the password on the screen (version 0.2)
  1233. // variable_item_set_current_value_text(app->variable_item_logged_out_login_password, app->login_password_logged_out);
  1234. }
  1235. // Save the settings
  1236. 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->is_logged_in);
  1237. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLogin);
  1238. }
  1239. /**
  1240. * @brief Callback when the user selects a menu item in the login (logged out) screen.
  1241. * @param context The context - FlipSocialApp object.
  1242. * @param index The index of the selected item.
  1243. * @return void
  1244. */
  1245. void flip_social_text_input_logged_out_login_item_selected(void *context, uint32_t index)
  1246. {
  1247. FlipSocialApp *app = (FlipSocialApp *)context;
  1248. if (!app)
  1249. {
  1250. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1251. return;
  1252. }
  1253. switch (index)
  1254. {
  1255. case 0: // Input Username
  1256. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginUsernameInput);
  1257. break;
  1258. case 1: // Input Password
  1259. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutLoginPasswordInput);
  1260. break;
  1261. case 2: // Login Button
  1262. flip_social_login_switch_to_view(app);
  1263. break;
  1264. default:
  1265. FURI_LOG_E(TAG, "Unknown configuration item index");
  1266. break;
  1267. }
  1268. }
  1269. /**
  1270. * @brief Text input callback for when the user finishes entering their username on the register (logged out) screen.
  1271. * @param context The context - FlipSocialApp object.
  1272. * @return void
  1273. */
  1274. void flip_social_logged_out_register_username_updated(void *context)
  1275. {
  1276. FlipSocialApp *app = (FlipSocialApp *)context;
  1277. if (!app)
  1278. {
  1279. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1280. return;
  1281. }
  1282. // Store the entered name
  1283. strncpy(app->register_username_logged_out, app->register_username_logged_out_temp_buffer, app->register_username_logged_out_temp_buffer_size);
  1284. // Ensure null-termination
  1285. app->register_username_logged_out[app->register_username_logged_out_temp_buffer_size - 1] = '\0';
  1286. // Update the name item text
  1287. if (app->variable_item_logged_out_register_username)
  1288. {
  1289. variable_item_set_current_value_text(app->variable_item_logged_out_register_username, app->register_username_logged_out);
  1290. }
  1291. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1292. }
  1293. /**
  1294. * @brief Text input callback for when the user finishes entering their password on the register (logged out) screen.
  1295. * @param context The context - FlipSocialApp object.
  1296. * @return void
  1297. */
  1298. void flip_social_logged_out_register_password_updated(void *context)
  1299. {
  1300. FlipSocialApp *app = (FlipSocialApp *)context;
  1301. if (!app)
  1302. {
  1303. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1304. return;
  1305. }
  1306. // Store the entered password
  1307. strncpy(app->register_password_logged_out, app->register_password_logged_out_temp_buffer, app->register_password_logged_out_temp_buffer_size);
  1308. // Ensure null-termination
  1309. app->register_password_logged_out[app->register_password_logged_out_temp_buffer_size - 1] = '\0';
  1310. // Update the password item text
  1311. if (app->variable_item_logged_out_register_password)
  1312. {
  1313. variable_item_set_current_value_text(app->variable_item_logged_out_register_password, app->register_password_logged_out);
  1314. }
  1315. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1316. }
  1317. /**
  1318. * @brief Text input callback for when the user finishes entering their password 2 on the register (logged out) screen.
  1319. * @param context The context - FlipSocialApp object.
  1320. * @return void
  1321. */
  1322. void flip_social_logged_out_register_password_2_updated(void *context)
  1323. {
  1324. FlipSocialApp *app = (FlipSocialApp *)context;
  1325. if (!app)
  1326. {
  1327. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1328. return;
  1329. }
  1330. // Store the entered password
  1331. strncpy(app->register_password_2_logged_out, app->register_password_2_logged_out_temp_buffer, app->register_password_2_logged_out_temp_buffer_size);
  1332. // Ensure null-termination
  1333. app->register_password_2_logged_out[app->register_password_2_logged_out_temp_buffer_size - 1] = '\0';
  1334. // Update the password item text
  1335. if (app->variable_item_logged_out_register_password_2)
  1336. {
  1337. variable_item_set_current_value_text(app->variable_item_logged_out_register_password_2, app->register_password_2_logged_out);
  1338. }
  1339. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegister);
  1340. }
  1341. /**
  1342. * @brief Callback when the user selects a menu item in the register (logged out) screen.
  1343. * @param context The context - FlipSocialApp object.
  1344. * @param index The index of the selected item.
  1345. * @return void
  1346. */
  1347. void flip_social_text_input_logged_out_register_item_selected(void *context, uint32_t index)
  1348. {
  1349. FlipSocialApp *app = (FlipSocialApp *)context;
  1350. if (!app)
  1351. {
  1352. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1353. return;
  1354. }
  1355. switch (index)
  1356. {
  1357. case 0: // Input Username
  1358. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterUsernameInput);
  1359. break;
  1360. case 1: // Input Password
  1361. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPasswordInput);
  1362. break;
  1363. case 2: // Input Password 2
  1364. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutRegisterPassword2Input);
  1365. break;
  1366. case 3: // Register button
  1367. flip_social_register_switch_to_view(app);
  1368. break;
  1369. default:
  1370. FURI_LOG_E(TAG, "Unknown configuration item index");
  1371. break;
  1372. }
  1373. }
  1374. /**
  1375. * @brief Text input callback for when the user finishes entering their SSID on the wifi settings (logged in) screen.
  1376. * @param context The context - FlipSocialApp object.
  1377. * @return void
  1378. */
  1379. void flip_social_logged_in_wifi_settings_ssid_updated(void *context)
  1380. {
  1381. FlipSocialApp *app = (FlipSocialApp *)context;
  1382. if (!app)
  1383. {
  1384. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1385. return;
  1386. }
  1387. // Store the entered SSID
  1388. strncpy(app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer, app->wifi_ssid_logged_in_temp_buffer_size);
  1389. // Store the entered SSID in the logged out SSID
  1390. strncpy(app->wifi_ssid_logged_out, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1391. strncpy(app->wifi_ssid_logged_out_temp_buffer, app->wifi_ssid_logged_in, app->wifi_ssid_logged_in_temp_buffer_size);
  1392. // Ensure null-termination
  1393. app->wifi_ssid_logged_in[app->wifi_ssid_logged_in_temp_buffer_size - 1] = '\0';
  1394. // Update the name item text
  1395. if (app->variable_item_logged_in_wifi_settings_ssid)
  1396. {
  1397. variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_ssid, app->wifi_ssid_logged_in);
  1398. }
  1399. // Save the settings
  1400. 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->is_logged_in);
  1401. // update the wifi settings
  1402. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1403. {
  1404. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1405. {
  1406. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1407. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1408. }
  1409. }
  1410. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1411. }
  1412. /**
  1413. * @brief Text input callback for when the user finishes entering their password on the wifi settings (logged in) screen.
  1414. * @param context The context - FlipSocialApp object.
  1415. * @return void
  1416. */
  1417. void flip_social_logged_in_wifi_settings_password_updated(void *context)
  1418. {
  1419. FlipSocialApp *app = (FlipSocialApp *)context;
  1420. if (!app)
  1421. {
  1422. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1423. return;
  1424. }
  1425. // Store the entered password
  1426. strncpy(app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer, app->wifi_password_logged_in_temp_buffer_size);
  1427. // Store the entered password in the logged out password
  1428. strncpy(app->login_password_logged_out, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1429. strncpy(app->login_password_logged_out_temp_buffer, app->wifi_password_logged_in, app->wifi_password_logged_in_temp_buffer_size);
  1430. // Ensure null-termination
  1431. app->wifi_password_logged_in[app->wifi_password_logged_in_temp_buffer_size - 1] = '\0';
  1432. // Update the password item text
  1433. if (app->variable_item_logged_in_wifi_settings_password)
  1434. {
  1435. // dont show the password on the screen (version 0.2)
  1436. // variable_item_set_current_value_text(app->variable_item_logged_in_wifi_settings_password, app->wifi_password_logged_in);
  1437. }
  1438. // Save the settings
  1439. 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->is_logged_in);
  1440. // update the wifi settings
  1441. if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
  1442. {
  1443. if (!flipper_http_save_wifi(app->wifi_ssid_logged_in, app->wifi_password_logged_in))
  1444. {
  1445. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1446. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1447. }
  1448. }
  1449. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1450. }
  1451. /**
  1452. * @brief Callback when the user selects a menu item in the wifi settings (logged in) screen.
  1453. * @param context The context - FlipSocialApp object.
  1454. * @param index The index of the selected item.
  1455. * @return void
  1456. */
  1457. void flip_social_text_input_logged_in_wifi_settings_item_selected(void *context, uint32_t index)
  1458. {
  1459. FlipSocialApp *app = (FlipSocialApp *)context;
  1460. if (!app)
  1461. {
  1462. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1463. return;
  1464. }
  1465. switch (index)
  1466. {
  1467. case 0: // Input SSID
  1468. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
  1469. break;
  1470. case 1: // Input Password
  1471. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
  1472. break;
  1473. default:
  1474. FURI_LOG_E(TAG, "Unknown configuration item index");
  1475. break;
  1476. }
  1477. }
  1478. /**
  1479. * @brief Text input callback for when the user finishes entering their message on the compose (logged in) screen for Add Text
  1480. * @param context The context - FlipSocialApp object.
  1481. * @return void
  1482. */
  1483. void flip_social_logged_in_compose_pre_save_updated(void *context)
  1484. {
  1485. FlipSocialApp *app = (FlipSocialApp *)context;
  1486. if (!app)
  1487. {
  1488. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1489. return;
  1490. }
  1491. // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
  1492. if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
  1493. {
  1494. FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
  1495. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1496. return;
  1497. }
  1498. // Store the entered message
  1499. strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
  1500. // Ensure null-termination
  1501. app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
  1502. // add the item to the submenu
  1503. submenu_reset(app->submenu_compose);
  1504. // loop through the items and add them to the submenu
  1505. app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
  1506. app->pre_saved_messages.count++;
  1507. submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
  1508. for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
  1509. {
  1510. submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
  1511. }
  1512. // save playlist
  1513. save_playlist(&app->pre_saved_messages);
  1514. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
  1515. }
  1516. /**
  1517. * @brief Text input callback for when the user finishes entering their message on the profile (logged in) screen for change password
  1518. * @param context The context - FlipSocialApp object.
  1519. * @return void
  1520. */
  1521. void flip_social_logged_in_profile_change_password_updated(void *context)
  1522. {
  1523. FlipSocialApp *app = (FlipSocialApp *)context;
  1524. if (!app)
  1525. {
  1526. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1527. return;
  1528. }
  1529. // Correct type: old_pass should be a pointer to a string (char *)
  1530. const char *old_password = app->login_password_logged_out;
  1531. // Store the entered message
  1532. strncpy(app->change_password_logged_in, app->change_password_logged_in_temp_buffer, app->change_password_logged_in_temp_buffer_size);
  1533. // store the entered password in the logged out password
  1534. strncpy(app->login_password_logged_out, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1535. strncpy(app->login_password_logged_out_temp_buffer, app->change_password_logged_in, app->login_password_logged_out_temp_buffer_size);
  1536. // Ensure null-termination
  1537. app->change_password_logged_in[app->change_password_logged_in_temp_buffer_size - 1] = '\0';
  1538. // Update the message item text
  1539. if (app->variable_item_logged_in_profile_change_password)
  1540. {
  1541. // dont show the password on the screen (version 0.2)
  1542. // variable_item_set_current_value_text(app->variable_item_logged_in_profile_change_password, app->change_password_logged_in);
  1543. }
  1544. // send post request to change password
  1545. auth_headers_alloc();
  1546. char payload[256];
  1547. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
  1548. if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
  1549. {
  1550. FURI_LOG_E(TAG, "Failed to send post request to change password");
  1551. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1552. return;
  1553. }
  1554. // Save the settings
  1555. 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->is_logged_in);
  1556. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
  1557. }
  1558. /**
  1559. * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  1560. * @param context The context - FlipSocialApp object.
  1561. * @param index The index of the selected item.
  1562. * @return void
  1563. */
  1564. void flip_social_text_input_logged_in_profile_item_selected(void *context, uint32_t index)
  1565. {
  1566. FlipSocialApp *app = (FlipSocialApp *)context;
  1567. if (!app)
  1568. {
  1569. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1570. return;
  1571. }
  1572. switch (index)
  1573. {
  1574. case 0: // Change Username
  1575. // do nothing since username cannot be changed
  1576. break;
  1577. case 1: // Change Password
  1578. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
  1579. break;
  1580. case 2: // Friends
  1581. flip_social_free_friends();
  1582. free_flip_social_group();
  1583. if (!app->submenu_friends)
  1584. {
  1585. if (!easy_flipper_set_submenu(&app->submenu_friends, FlipSocialViewLoggedInFriendsSubmenu, "Friends", flip_social_callback_to_profile_logged_in, &app->view_dispatcher))
  1586. {
  1587. FURI_LOG_DEV(TAG, "Failed to set submenu for friends");
  1588. return;
  1589. }
  1590. }
  1591. flipper_http_loading_task(flip_social_get_friends, flip_social_parse_json_friends, FlipSocialViewLoggedInFriendsSubmenu, FlipSocialViewLoggedInProfile, &app->view_dispatcher);
  1592. break;
  1593. default:
  1594. FURI_LOG_E(TAG, "Unknown configuration item index");
  1595. break;
  1596. }
  1597. }
  1598. /**
  1599. * @brief Callback when a user selects a menu item in the settings (logged in) screen.
  1600. * @param context The context - FlipSocialApp object.
  1601. * @param index The index of the selected item.
  1602. * @return void
  1603. */
  1604. void flip_social_text_input_logged_in_settings_item_selected(void *context, uint32_t index)
  1605. {
  1606. FlipSocialApp *app = (FlipSocialApp *)context;
  1607. if (!app)
  1608. {
  1609. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1610. return;
  1611. }
  1612. switch (index)
  1613. {
  1614. case 0: // About
  1615. if (!about_widget_alloc(true))
  1616. {
  1617. return;
  1618. }
  1619. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsAbout);
  1620. break;
  1621. case 1: // Wifi
  1622. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettingsWifi);
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. }
  1628. /**
  1629. * @brief Text input callback for when the user finishes entering their message to send to the selected user choice (user choice messages view)
  1630. * @param context The context - FlipSocialApp object.
  1631. * @return void
  1632. */
  1633. void flip_social_logged_in_messages_user_choice_message_updated(void *context)
  1634. {
  1635. FlipSocialApp *app = (FlipSocialApp *)context;
  1636. if (!app)
  1637. {
  1638. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1639. return;
  1640. }
  1641. // check if the message is empty
  1642. if (app->message_user_choice_logged_in_temp_buffer_size == 0)
  1643. {
  1644. FURI_LOG_E(TAG, "Message is empty");
  1645. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageUserChoicesInput);
  1646. return;
  1647. }
  1648. // Store the entered message
  1649. strncpy(app->message_user_choice_logged_in, app->message_user_choice_logged_in_temp_buffer, app->message_user_choice_logged_in_temp_buffer_size);
  1650. // Ensure null-termination
  1651. app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
  1652. // send post request to send message
  1653. auth_headers_alloc();
  1654. char url[128];
  1655. char payload[256];
  1656. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1657. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
  1658. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1659. {
  1660. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1661. fhttp.state = RECEIVING;
  1662. }
  1663. else
  1664. {
  1665. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1666. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1667. fhttp.state = ISSUE;
  1668. return;
  1669. }
  1670. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1671. {
  1672. // Wait for the request to be received
  1673. furi_delay_ms(10);
  1674. }
  1675. furi_timer_stop(fhttp.get_timeout_timer);
  1676. // add user to the message list
  1677. 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]));
  1678. flip_social_message_users->count++;
  1679. // redraw submenu
  1680. flip_social_update_messages_submenu();
  1681. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1682. }
  1683. /**
  1684. * @brief Text input callback for when the user finishes entering their message to the selected user (messages view)
  1685. * @param context The context - FlipSocialApp object.
  1686. * @return void
  1687. */
  1688. void flip_social_logged_in_messages_new_message_updated(void *context)
  1689. {
  1690. FlipSocialApp *app = (FlipSocialApp *)context;
  1691. if (!app)
  1692. {
  1693. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  1694. return;
  1695. }
  1696. // check if the message is empty
  1697. if (app->messages_new_message_logged_in_temp_buffer_size == 0)
  1698. {
  1699. FURI_LOG_E(TAG, "Message is empty");
  1700. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesNewMessageInput);
  1701. return;
  1702. }
  1703. // Store the entered message
  1704. strncpy(app->messages_new_message_logged_in, app->messages_new_message_logged_in_temp_buffer, app->messages_new_message_logged_in_temp_buffer_size);
  1705. // Ensure null-termination
  1706. app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
  1707. // send post request to send message
  1708. auth_headers_alloc();
  1709. char url[128];
  1710. char payload[256];
  1711. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
  1712. snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
  1713. if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
  1714. {
  1715. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1716. fhttp.state = RECEIVING;
  1717. }
  1718. else
  1719. {
  1720. FURI_LOG_E(TAG, "Failed to send post request to send message");
  1721. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1722. fhttp.state = ISSUE;
  1723. return;
  1724. }
  1725. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  1726. {
  1727. // Wait for the request to be received
  1728. furi_delay_ms(10);
  1729. }
  1730. furi_timer_stop(fhttp.get_timeout_timer);
  1731. view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  1732. }
  1733. static void flip_social_widget_set_text(char *message, Widget **widget)
  1734. {
  1735. if (widget == NULL)
  1736. {
  1737. FURI_LOG_E(TAG, "flip_social_set_widget_text - widget is NULL");
  1738. DEV_CRASH();
  1739. return;
  1740. }
  1741. if (message == NULL)
  1742. {
  1743. FURI_LOG_E(TAG, "flip_social_set_widget_text - message is NULL");
  1744. DEV_CRASH();
  1745. return;
  1746. }
  1747. widget_reset(*widget);
  1748. uint32_t message_length = strlen(message); // Length of the message
  1749. uint32_t i = 0; // Index tracker
  1750. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1751. char *formatted_message; // Buffer to hold the final formatted message
  1752. // Allocate buffer with double the message length plus one for safety
  1753. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1754. {
  1755. return;
  1756. }
  1757. while (i < message_length)
  1758. {
  1759. uint32_t max_line_length = 31; // Maximum characters per line
  1760. uint32_t remaining_length = message_length - i; // Remaining characters
  1761. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1762. // Check for newline character within the current segment
  1763. uint32_t newline_pos = i;
  1764. bool found_newline = false;
  1765. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1766. {
  1767. if (message[newline_pos] == '\n')
  1768. {
  1769. found_newline = true;
  1770. break;
  1771. }
  1772. }
  1773. if (found_newline)
  1774. {
  1775. // If newline found, set line_length up to the newline
  1776. line_length = newline_pos - i;
  1777. }
  1778. // Temporary buffer to hold the current line
  1779. char line[32];
  1780. strncpy(line, message + i, line_length);
  1781. line[line_length] = '\0';
  1782. // If newline was found, skip it for the next iteration
  1783. if (found_newline)
  1784. {
  1785. i += line_length + 1; // +1 to skip the '\n' character
  1786. }
  1787. else
  1788. {
  1789. // Check if the line ends in the middle of a word and adjust accordingly
  1790. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1791. {
  1792. // Find the last space within the current line to avoid breaking a word
  1793. char *last_space = strrchr(line, ' ');
  1794. if (last_space != NULL)
  1795. {
  1796. // Adjust the line_length to avoid cutting the word
  1797. line_length = last_space - line;
  1798. line[line_length] = '\0'; // Null-terminate at the space
  1799. }
  1800. }
  1801. // Move the index forward by the determined line_length
  1802. i += line_length;
  1803. // Skip any spaces at the beginning of the next line
  1804. while (i < message_length && message[i] == ' ')
  1805. {
  1806. i++;
  1807. }
  1808. }
  1809. // Manually copy the fixed line into the formatted_message buffer
  1810. for (uint32_t j = 0; j < line_length; j++)
  1811. {
  1812. formatted_message[formatted_index++] = line[j];
  1813. }
  1814. // Add a newline character for line spacing
  1815. formatted_message[formatted_index++] = '\n';
  1816. }
  1817. // Null-terminate the formatted_message
  1818. formatted_message[formatted_index] = '\0';
  1819. // Add the formatted message to the widget
  1820. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1821. }
  1822. void flip_social_loader_draw_callback(Canvas *canvas, void *model)
  1823. {
  1824. if (!canvas || !model)
  1825. {
  1826. FURI_LOG_E(TAG, "flip_social_loader_draw_callback - canvas or model is NULL");
  1827. return;
  1828. }
  1829. SerialState http_state = fhttp.state;
  1830. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1831. DataState data_state = data_loader_model->data_state;
  1832. char *title = data_loader_model->title;
  1833. canvas_set_font(canvas, FontSecondary);
  1834. if (http_state == INACTIVE)
  1835. {
  1836. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1837. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1838. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1839. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1840. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1841. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1842. return;
  1843. }
  1844. if (data_state == DataStateError || data_state == DataStateParseError)
  1845. {
  1846. flip_social_request_error_draw(canvas);
  1847. return;
  1848. }
  1849. canvas_draw_str(canvas, 0, 7, title);
  1850. canvas_draw_str(canvas, 0, 17, "Loading...");
  1851. if (data_state == DataStateInitial)
  1852. {
  1853. return;
  1854. }
  1855. if (http_state == SENDING)
  1856. {
  1857. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1858. return;
  1859. }
  1860. if (http_state == RECEIVING || data_state == DataStateRequested)
  1861. {
  1862. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1863. return;
  1864. }
  1865. if (http_state == IDLE && data_state == DataStateReceived)
  1866. {
  1867. canvas_draw_str(canvas, 0, 27, "Processing...");
  1868. return;
  1869. }
  1870. if (http_state == IDLE && data_state == DataStateParsed)
  1871. {
  1872. canvas_draw_str(canvas, 0, 27, "Processed...");
  1873. return;
  1874. }
  1875. }
  1876. static void flip_social_loader_process_callback(void *context)
  1877. {
  1878. if (context == NULL)
  1879. {
  1880. FURI_LOG_E(TAG, "flip_social_loader_process_callback - context is NULL");
  1881. DEV_CRASH();
  1882. return;
  1883. }
  1884. FlipSocialApp *app = (FlipSocialApp *)context;
  1885. View *view = app->view_loader;
  1886. DataState current_data_state;
  1887. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1888. if (current_data_state == DataStateInitial)
  1889. {
  1890. with_view_model(
  1891. view,
  1892. DataLoaderModel * model,
  1893. {
  1894. model->data_state = DataStateRequested;
  1895. DataLoaderFetch fetch = model->fetcher;
  1896. if (fetch == NULL)
  1897. {
  1898. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1899. model->data_state = DataStateError;
  1900. return;
  1901. }
  1902. // Clear any previous responses
  1903. strncpy(fhttp.last_response, "", 1);
  1904. bool request_status = fetch(model);
  1905. if (!request_status)
  1906. {
  1907. model->data_state = DataStateError;
  1908. }
  1909. },
  1910. true);
  1911. }
  1912. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1913. {
  1914. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1915. {
  1916. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1917. {
  1918. FURI_LOG_DEV(TAG, "PONG received.");
  1919. }
  1920. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1921. {
  1922. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1923. }
  1924. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1925. {
  1926. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1927. }
  1928. else if (strlen(fhttp.last_response) == 0)
  1929. {
  1930. // Still waiting on response
  1931. }
  1932. else
  1933. {
  1934. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1935. }
  1936. }
  1937. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1938. {
  1939. // continue waiting
  1940. }
  1941. else if (fhttp.state == INACTIVE)
  1942. {
  1943. // inactive. try again
  1944. }
  1945. else if (fhttp.state == ISSUE)
  1946. {
  1947. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1948. }
  1949. else
  1950. {
  1951. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1952. DEV_CRASH();
  1953. }
  1954. }
  1955. else if (current_data_state == DataStateReceived)
  1956. {
  1957. with_view_model(
  1958. view,
  1959. DataLoaderModel * model,
  1960. {
  1961. char *data_text;
  1962. if (model->parser == NULL)
  1963. {
  1964. data_text = NULL;
  1965. FURI_LOG_DEV(TAG, "Parser is NULL");
  1966. DEV_CRASH();
  1967. }
  1968. else
  1969. {
  1970. data_text = model->parser(model);
  1971. }
  1972. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1973. model->data_text = data_text;
  1974. if (data_text == NULL)
  1975. {
  1976. model->data_state = DataStateParseError;
  1977. }
  1978. else
  1979. {
  1980. model->data_state = DataStateParsed;
  1981. }
  1982. },
  1983. true);
  1984. }
  1985. else if (current_data_state == DataStateParsed)
  1986. {
  1987. with_view_model(
  1988. view,
  1989. DataLoaderModel * model,
  1990. {
  1991. if (++model->request_index < model->request_count)
  1992. {
  1993. model->data_state = DataStateInitial;
  1994. }
  1995. else
  1996. {
  1997. flip_social_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app_instance->widget_result);
  1998. if (model->data_text != NULL)
  1999. {
  2000. free(model->data_text);
  2001. model->data_text = NULL;
  2002. }
  2003. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  2004. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
  2005. }
  2006. },
  2007. true);
  2008. }
  2009. }
  2010. static void flip_social_loader_timer_callback(void *context)
  2011. {
  2012. if (context == NULL)
  2013. {
  2014. FURI_LOG_E(TAG, "flip_social_loader_timer_callback - context is NULL");
  2015. DEV_CRASH();
  2016. return;
  2017. }
  2018. FlipSocialApp *app = (FlipSocialApp *)context;
  2019. view_dispatcher_send_custom_event(app->view_dispatcher, FlipSocialCustomEventProcess);
  2020. }
  2021. static void flip_social_loader_on_enter(void *context)
  2022. {
  2023. if (context == NULL)
  2024. {
  2025. FURI_LOG_E(TAG, "flip_social_loader_on_enter - context is NULL");
  2026. DEV_CRASH();
  2027. return;
  2028. }
  2029. FlipSocialApp *app = (FlipSocialApp *)context;
  2030. View *view = app->view_loader;
  2031. with_view_model(
  2032. view,
  2033. DataLoaderModel * model,
  2034. {
  2035. view_set_previous_callback(view, model->back_callback);
  2036. if (model->timer == NULL)
  2037. {
  2038. model->timer = furi_timer_alloc(flip_social_loader_timer_callback, FuriTimerTypePeriodic, app);
  2039. }
  2040. furi_timer_start(model->timer, 250);
  2041. },
  2042. true);
  2043. }
  2044. static void flip_social_loader_on_exit(void *context)
  2045. {
  2046. if (context == NULL)
  2047. {
  2048. FURI_LOG_E(TAG, "flip_social_loader_on_exit - context is NULL");
  2049. DEV_CRASH();
  2050. return;
  2051. }
  2052. FlipSocialApp *app = (FlipSocialApp *)context;
  2053. View *view = app->view_loader;
  2054. with_view_model(
  2055. view,
  2056. DataLoaderModel * model,
  2057. {
  2058. if (model->timer)
  2059. {
  2060. furi_timer_stop(model->timer);
  2061. }
  2062. },
  2063. false);
  2064. }
  2065. void flip_social_loader_init(View *view)
  2066. {
  2067. if (view == NULL)
  2068. {
  2069. FURI_LOG_E(TAG, "flip_social_loader_init - view is NULL");
  2070. DEV_CRASH();
  2071. return;
  2072. }
  2073. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  2074. view_set_enter_callback(view, flip_social_loader_on_enter);
  2075. view_set_exit_callback(view, flip_social_loader_on_exit);
  2076. }
  2077. void flip_social_loader_free_model(View *view)
  2078. {
  2079. if (view == NULL)
  2080. {
  2081. FURI_LOG_E(TAG, "flip_social_loader_free_model - view is NULL");
  2082. DEV_CRASH();
  2083. return;
  2084. }
  2085. with_view_model(
  2086. view,
  2087. DataLoaderModel * model,
  2088. {
  2089. if (model->timer)
  2090. {
  2091. furi_timer_free(model->timer);
  2092. model->timer = NULL;
  2093. }
  2094. if (model->parser_context)
  2095. {
  2096. free(model->parser_context);
  2097. model->parser_context = NULL;
  2098. }
  2099. },
  2100. false);
  2101. }
  2102. bool flip_social_custom_event_callback(void *context, uint32_t index)
  2103. {
  2104. if (context == NULL)
  2105. {
  2106. FURI_LOG_E(TAG, "flip_social_custom_event_callback - context is NULL");
  2107. DEV_CRASH();
  2108. return false;
  2109. }
  2110. switch (index)
  2111. {
  2112. case FlipSocialCustomEventProcess:
  2113. flip_social_loader_process_callback(context);
  2114. return true;
  2115. default:
  2116. FURI_LOG_DEV(TAG, "flip_social_custom_event_callback. Unknown index: %ld", index);
  2117. return false;
  2118. }
  2119. }
  2120. 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)
  2121. {
  2122. if (app == NULL)
  2123. {
  2124. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - app is NULL");
  2125. DEV_CRASH();
  2126. return;
  2127. }
  2128. View *view = app->view_loader;
  2129. if (view == NULL)
  2130. {
  2131. FURI_LOG_E(TAG, "flip_social_generic_switch_to_view - view is NULL");
  2132. DEV_CRASH();
  2133. return;
  2134. }
  2135. with_view_model(
  2136. view,
  2137. DataLoaderModel * model,
  2138. {
  2139. model->title = title;
  2140. model->fetcher = fetcher;
  2141. model->parser = parser;
  2142. model->request_index = 0;
  2143. model->request_count = request_count;
  2144. model->back_callback = back;
  2145. model->data_state = DataStateInitial;
  2146. model->data_text = NULL;
  2147. },
  2148. true);
  2149. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2150. }