callback.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. #include <callback/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 void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
  12. {
  13. UNUSED(engine);
  14. GameManager *game_manager = context;
  15. game_manager_input_set(game_manager, input);
  16. game_manager_update(game_manager);
  17. game_manager_render(game_manager, canvas);
  18. }
  19. static int32_t game_app(void *p)
  20. {
  21. UNUSED(p);
  22. GameManager *game_manager = game_manager_alloc();
  23. if (!game_manager)
  24. {
  25. FURI_LOG_E("Game", "Failed to allocate game manager");
  26. return -1;
  27. }
  28. GameEngineSettings settings = game_engine_settings_init();
  29. settings.target_fps = game_fps_choices_2[game_fps_index];
  30. settings.show_fps = game.show_fps;
  31. settings.always_backlight = game.always_backlight;
  32. settings.frame_callback = frame_cb;
  33. settings.context = game_manager;
  34. GameEngine *engine = game_engine_alloc(settings);
  35. if (!engine)
  36. {
  37. FURI_LOG_E("Game", "Failed to allocate game engine");
  38. game_manager_free(game_manager);
  39. return -1;
  40. }
  41. game_manager_engine_set(game_manager, engine);
  42. void *game_context = NULL;
  43. if (game.context_size > 0)
  44. {
  45. game_context = malloc(game.context_size);
  46. game_manager_game_context_set(game_manager, game_context);
  47. }
  48. game.start(game_manager, game_context);
  49. game_engine_run(engine);
  50. game_engine_free(engine);
  51. game_manager_free(game_manager);
  52. game.stop(game_context);
  53. if (game_context)
  54. {
  55. free(game_context);
  56. }
  57. int32_t entities = entities_get_count();
  58. if (entities != 0)
  59. {
  60. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. static void flip_world_request_error_draw(Canvas *canvas)
  66. {
  67. if (canvas == NULL)
  68. {
  69. FURI_LOG_E(TAG, "flip_world_request_error_draw - canvas is NULL");
  70. DEV_CRASH();
  71. return;
  72. }
  73. if (fhttp.last_response != NULL)
  74. {
  75. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  76. {
  77. canvas_clear(canvas);
  78. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  79. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  80. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  81. }
  82. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  83. {
  84. canvas_clear(canvas);
  85. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  86. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  87. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  88. }
  89. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  90. {
  91. canvas_clear(canvas);
  92. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  93. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  94. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  95. }
  96. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  97. {
  98. canvas_clear(canvas);
  99. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  100. }
  101. else
  102. {
  103. canvas_clear(canvas);
  104. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  105. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  106. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  107. }
  108. }
  109. else
  110. {
  111. canvas_clear(canvas);
  112. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  113. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  114. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  115. }
  116. }
  117. static bool alloc_about_view(void *context);
  118. static bool alloc_text_input_view(void *context, char *title);
  119. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  120. //
  121. static void wifi_settings_item_selected(void *context, uint32_t index);
  122. static void text_updated_ssid(void *context);
  123. static void text_updated_pass(void *context);
  124. //
  125. static void flip_world_game_fps_change(VariableItem *item);
  126. static void game_settings_item_selected(void *context, uint32_t index);
  127. uint32_t callback_to_submenu(void *context)
  128. {
  129. UNUSED(context);
  130. return FlipWorldViewSubmenu;
  131. }
  132. static uint32_t callback_to_wifi_settings(void *context)
  133. {
  134. UNUSED(context);
  135. return FlipWorldViewVariableItemList;
  136. }
  137. static uint32_t callback_to_settings(void *context)
  138. {
  139. UNUSED(context);
  140. return FlipWorldViewSettings;
  141. }
  142. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  143. {
  144. UNUSED(model);
  145. canvas_clear(canvas);
  146. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  147. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  148. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  149. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  150. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  151. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  152. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  153. }
  154. // alloc
  155. static bool alloc_about_view(void *context)
  156. {
  157. FlipWorldApp *app = (FlipWorldApp *)context;
  158. if (!app)
  159. {
  160. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  161. return false;
  162. }
  163. if (!app->view_about)
  164. {
  165. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  166. {
  167. return false;
  168. }
  169. if (!app->view_about)
  170. {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. static bool alloc_text_input_view(void *context, char *title)
  177. {
  178. FlipWorldApp *app = (FlipWorldApp *)context;
  179. if (!app)
  180. {
  181. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  182. return false;
  183. }
  184. if (!title)
  185. {
  186. FURI_LOG_E(TAG, "Title is NULL");
  187. return false;
  188. }
  189. app->text_input_buffer_size = 64;
  190. if (!app->text_input_buffer)
  191. {
  192. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  193. {
  194. return false;
  195. }
  196. }
  197. if (!app->text_input_temp_buffer)
  198. {
  199. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  200. {
  201. return false;
  202. }
  203. }
  204. if (!app->text_input)
  205. {
  206. if (!easy_flipper_set_uart_text_input(
  207. &app->text_input,
  208. FlipWorldViewTextInput,
  209. title,
  210. app->text_input_temp_buffer,
  211. app->text_input_buffer_size,
  212. strcmp(title, "SSID") == 0 ? text_updated_ssid : text_updated_pass,
  213. callback_to_wifi_settings,
  214. &app->view_dispatcher,
  215. app))
  216. {
  217. return false;
  218. }
  219. if (!app->text_input)
  220. {
  221. return false;
  222. }
  223. char ssid[64];
  224. char pass[64];
  225. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  226. {
  227. if (strcmp(title, "SSID") == 0)
  228. {
  229. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  230. }
  231. else
  232. {
  233. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  234. }
  235. }
  236. }
  237. return true;
  238. }
  239. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  240. {
  241. FlipWorldApp *app = (FlipWorldApp *)context;
  242. if (!app)
  243. {
  244. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  245. return false;
  246. }
  247. if (!app->variable_item_list)
  248. {
  249. switch (view_id)
  250. {
  251. case FlipWorldSubmenuIndexWiFiSettings:
  252. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  253. {
  254. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  255. return false;
  256. }
  257. if (!app->variable_item_list)
  258. {
  259. FURI_LOG_E(TAG, "Variable item list is NULL");
  260. return false;
  261. }
  262. if (!app->variable_item_wifi_ssid)
  263. {
  264. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  265. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  266. }
  267. if (!app->variable_item_wifi_pass)
  268. {
  269. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  270. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  271. }
  272. char ssid[64];
  273. char pass[64];
  274. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  275. {
  276. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  277. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  278. save_char("WiFi-SSID", ssid);
  279. save_char("WiFi-Password", pass);
  280. }
  281. break;
  282. case FlipWorldSubmenuIndexGameSettings:
  283. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  284. {
  285. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  286. return false;
  287. }
  288. if (!app->variable_item_list)
  289. {
  290. FURI_LOG_E(TAG, "Variable item list is NULL");
  291. return false;
  292. }
  293. if (!app->variable_item_game_fps)
  294. {
  295. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  296. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  297. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  298. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  299. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  300. }
  301. char _game_fps[8];
  302. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  303. {
  304. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  305. : strcmp(_game_fps, "120") == 0 ? 2
  306. : strcmp(_game_fps, "240") == 0 ? 3
  307. : 0;
  308. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  309. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  310. }
  311. break;
  312. case FlipWorldSubmenuIndexUserSettings:
  313. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, NULL, callback_to_settings, &app->view_dispatcher, app))
  314. {
  315. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  316. return false;
  317. }
  318. if (!app->variable_item_list)
  319. {
  320. FURI_LOG_E(TAG, "Variable item list is NULL");
  321. return false;
  322. }
  323. // if logged in, show profile info, otherwise show login/register
  324. if (is_logged_in_to_flip_social())
  325. {
  326. if (!app->variable_item_user_username)
  327. {
  328. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  329. variable_item_set_current_value_text(app->variable_item_user_username, "");
  330. }
  331. if (!app->variable_item_user_password)
  332. {
  333. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  334. variable_item_set_current_value_text(app->variable_item_user_password, "");
  335. }
  336. variable_item_set_current_value_text(app->variable_item_user_username, furi_string_get_cstr(flip_social_info("login_username_logged_in")));
  337. variable_item_set_current_value_text(app->variable_item_user_password, "");
  338. }
  339. else
  340. {
  341. if (!app->variable_item_user_username)
  342. {
  343. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  344. variable_item_set_current_value_text(app->variable_item_user_username, "");
  345. }
  346. if (!app->variable_item_user_password)
  347. {
  348. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  349. variable_item_set_current_value_text(app->variable_item_user_password, "");
  350. }
  351. variable_item_list_add(app->variable_item_list, "Login", 0, NULL, NULL);
  352. variable_item_list_add(app->variable_item_list, "Register", 0, NULL, NULL);
  353. }
  354. break;
  355. }
  356. }
  357. return true;
  358. }
  359. static bool alloc_submenu_settings(void *context)
  360. {
  361. FlipWorldApp *app = (FlipWorldApp *)context;
  362. if (!app)
  363. {
  364. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  365. return false;
  366. }
  367. if (!app->submenu_settings)
  368. {
  369. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  370. {
  371. return NULL;
  372. }
  373. if (!app->submenu_settings)
  374. {
  375. return false;
  376. }
  377. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  378. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  379. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  380. }
  381. return true;
  382. }
  383. // free
  384. static void free_about_view(void *context)
  385. {
  386. FlipWorldApp *app = (FlipWorldApp *)context;
  387. if (!app)
  388. {
  389. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  390. return;
  391. }
  392. if (app->view_about)
  393. {
  394. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  395. view_free(app->view_about);
  396. app->view_about = NULL;
  397. }
  398. }
  399. static void free_main_view(void *context)
  400. {
  401. FlipWorldApp *app = (FlipWorldApp *)context;
  402. if (!app)
  403. {
  404. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  405. return;
  406. }
  407. }
  408. static void free_text_input_view(void *context)
  409. {
  410. FlipWorldApp *app = (FlipWorldApp *)context;
  411. if (!app)
  412. {
  413. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  414. return;
  415. }
  416. if (app->text_input)
  417. {
  418. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  419. uart_text_input_free(app->text_input);
  420. app->text_input = NULL;
  421. }
  422. if (app->text_input_buffer)
  423. {
  424. free(app->text_input_buffer);
  425. app->text_input_buffer = NULL;
  426. }
  427. if (app->text_input_temp_buffer)
  428. {
  429. free(app->text_input_temp_buffer);
  430. app->text_input_temp_buffer = NULL;
  431. }
  432. }
  433. static void free_variable_item_list(void *context)
  434. {
  435. FlipWorldApp *app = (FlipWorldApp *)context;
  436. if (!app)
  437. {
  438. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  439. return;
  440. }
  441. if (app->variable_item_list)
  442. {
  443. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  444. variable_item_list_free(app->variable_item_list);
  445. app->variable_item_list = NULL;
  446. }
  447. if (app->variable_item_wifi_ssid)
  448. {
  449. free(app->variable_item_wifi_ssid);
  450. app->variable_item_wifi_ssid = NULL;
  451. }
  452. if (app->variable_item_wifi_pass)
  453. {
  454. free(app->variable_item_wifi_pass);
  455. app->variable_item_wifi_pass = NULL;
  456. }
  457. if (app->variable_item_game_fps)
  458. {
  459. free(app->variable_item_game_fps);
  460. app->variable_item_game_fps = NULL;
  461. }
  462. if (app->variable_item_user_username)
  463. {
  464. free(app->variable_item_user_username);
  465. app->variable_item_user_username = NULL;
  466. }
  467. if (app->variable_item_user_password)
  468. {
  469. free(app->variable_item_user_password);
  470. app->variable_item_user_password = NULL;
  471. }
  472. }
  473. static void free_submenu_settings(void *context)
  474. {
  475. FlipWorldApp *app = (FlipWorldApp *)context;
  476. if (!app)
  477. {
  478. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  479. return;
  480. }
  481. if (app->submenu_settings)
  482. {
  483. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  484. submenu_free(app->submenu_settings);
  485. app->submenu_settings = NULL;
  486. }
  487. }
  488. static FlipWorldApp *app_instance = NULL;
  489. static FuriThreadId thread_id;
  490. static bool game_thread_running = false;
  491. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  492. {
  493. FlipWorldApp *app = (FlipWorldApp *)context;
  494. if (!app)
  495. {
  496. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  497. return;
  498. }
  499. if (should_free_variable_item_list)
  500. {
  501. free_variable_item_list(app);
  502. }
  503. free_about_view(app);
  504. free_main_view(app);
  505. free_text_input_view(app);
  506. // free game thread
  507. if (game_thread_running)
  508. {
  509. game_thread_running = false;
  510. furi_thread_flags_set(thread_id, WorkerEvtStop);
  511. furi_thread_free(thread_id);
  512. }
  513. if (should_free_submenu_settings)
  514. free_submenu_settings(app);
  515. if (app_instance)
  516. {
  517. free(app_instance);
  518. app_instance = NULL;
  519. }
  520. }
  521. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  522. {
  523. if (model->request_index == 0)
  524. {
  525. // Create the directory for saving worlds
  526. char directory_path[128];
  527. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  528. // Create the directory
  529. Storage *storage = furi_record_open(RECORD_STORAGE);
  530. storage_common_mkdir(storage, directory_path);
  531. // free storage
  532. furi_record_close(RECORD_STORAGE);
  533. snprintf(
  534. fhttp.file_path,
  535. sizeof(fhttp.file_path),
  536. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  537. fhttp.save_received_data = true;
  538. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  539. }
  540. else if (model->request_index == 1)
  541. {
  542. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  543. if (!world_list)
  544. {
  545. FURI_LOG_E(TAG, "Failed to load world list");
  546. return "Failed to load world list";
  547. }
  548. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  549. if (!first_world)
  550. {
  551. FURI_LOG_E(TAG, "Failed to get first world");
  552. return "Failed to get first world";
  553. }
  554. // if (world_exists(furi_string_get_cstr(first_world)))
  555. // {
  556. // furi_string_free(world_list);
  557. // furi_string_free(first_world);
  558. // FURI_LOG_I(TAG, "World already exists");
  559. // fhttp.state = IDLE;
  560. // return true;
  561. // }
  562. snprintf(
  563. fhttp.file_path,
  564. sizeof(fhttp.file_path),
  565. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  566. fhttp.save_received_data = true;
  567. char url[128];
  568. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  569. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  570. }
  571. return false;
  572. }
  573. static char *flip_world_parse_world_list(DataLoaderModel *model)
  574. {
  575. if (model->request_index == 0)
  576. {
  577. return "World List Fetched";
  578. }
  579. else if (model->request_index == 1)
  580. {
  581. flipper_http_deinit();
  582. // free game thread
  583. if (game_thread_running)
  584. {
  585. game_thread_running = false;
  586. furi_thread_flags_set(thread_id, WorkerEvtStop);
  587. furi_thread_free(thread_id);
  588. }
  589. // free_all_views(app_instance, true, true);
  590. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  591. if (!thread)
  592. {
  593. FURI_LOG_E(TAG, "Failed to allocate game thread");
  594. return "Failed to allocate game thread";
  595. }
  596. furi_thread_start(thread);
  597. thread_id = furi_thread_get_id(thread);
  598. game_thread_running = true;
  599. return "Thanks for playing!\nPress BACK to return.";
  600. }
  601. return "Unknown error";
  602. }
  603. static void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
  604. {
  605. flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 2, callback_to_submenu, FlipWorldViewLoader);
  606. }
  607. static bool flip_social_login_fetch(DataLoaderModel *model)
  608. {
  609. UNUSED(model);
  610. if (model->request_index == 0)
  611. {
  612. if (!app_instance)
  613. {
  614. FURI_LOG_E(TAG, "app_instance is NULL");
  615. return false;
  616. }
  617. free_text_input_view(app_instance);
  618. if (!alloc_text_input_view(app_instance, "Username"))
  619. {
  620. FURI_LOG_E(TAG, "Failed to allocate text input view");
  621. return false;
  622. }
  623. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
  624. return true;
  625. }
  626. else if (model->request_index == 1)
  627. {
  628. if (!app_instance)
  629. {
  630. FURI_LOG_E(TAG, "app_instance is NULL");
  631. return false;
  632. }
  633. free_text_input_view(app_instance);
  634. if (!alloc_text_input_view(app_instance, "Password"))
  635. {
  636. FURI_LOG_E(TAG, "Failed to allocate text input view");
  637. return false;
  638. }
  639. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
  640. return true;
  641. }
  642. else if (model->request_index == 2)
  643. {
  644. if (!app_instance)
  645. {
  646. FURI_LOG_E(TAG, "app_instance is NULL");
  647. return false;
  648. }
  649. return true;
  650. }
  651. return false;
  652. }
  653. static char *flip_social_login_parse(DataLoaderModel *model)
  654. {
  655. if (model->request_index == 0)
  656. {
  657. return "Enter your username...";
  658. }
  659. else if (model->request_index == 1)
  660. {
  661. return "Enter your password...";
  662. }
  663. else if (model->request_index == 2)
  664. {
  665. if (!app_instance)
  666. {
  667. FURI_LOG_E(TAG, "app_instance is NULL");
  668. return "Failed to login...";
  669. }
  670. if (!fhttp.last_response)
  671. {
  672. flipper_http_deinit();
  673. return "Failed to login...";
  674. }
  675. // read response
  676. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  677. {
  678. flipper_http_deinit();
  679. save_char("is_logged_in", "true");
  680. return "Login successful!";
  681. }
  682. else if (strstr(fhttp.last_response, "User not found") != NULL)
  683. {
  684. flipper_http_deinit();
  685. return "Account not found...";
  686. }
  687. else
  688. {
  689. flipper_http_deinit();
  690. return "Failed to login...";
  691. }
  692. }
  693. return NULL;
  694. }
  695. void flip_social_login_switch_to_view(FlipWorldApp *app)
  696. {
  697. flip_world_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 3, callback_to_settings, FlipWorldViewTextInput);
  698. }
  699. void callback_submenu_choices(void *context, uint32_t index)
  700. {
  701. FlipWorldApp *app = (FlipWorldApp *)context;
  702. if (!app)
  703. {
  704. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  705. return;
  706. }
  707. switch (index)
  708. {
  709. case FlipWorldSubmenuIndexRun:
  710. if (!flipper_http_init(flipper_http_rx_callback, app))
  711. {
  712. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  713. return;
  714. }
  715. app_instance = malloc(sizeof(FlipWorldApp));
  716. if (!app_instance)
  717. {
  718. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  719. return;
  720. }
  721. memcpy(app_instance, app, sizeof(FlipWorldApp));
  722. flip_world_switch_to_view_get_world_list(app);
  723. break;
  724. case FlipWorldSubmenuIndexAbout:
  725. free_all_views(app, true, true);
  726. if (!alloc_about_view(app))
  727. {
  728. FURI_LOG_E(TAG, "Failed to allocate about view");
  729. return;
  730. }
  731. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  732. break;
  733. case FlipWorldSubmenuIndexSettings:
  734. free_all_views(app, true, true);
  735. if (!alloc_submenu_settings(app))
  736. {
  737. FURI_LOG_E(TAG, "Failed to allocate settings view");
  738. return;
  739. }
  740. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  741. break;
  742. case FlipWorldSubmenuIndexWiFiSettings:
  743. free_all_views(app, true, false);
  744. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  745. {
  746. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  747. return;
  748. }
  749. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  750. break;
  751. case FlipWorldSubmenuIndexGameSettings:
  752. free_all_views(app, true, false);
  753. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  754. {
  755. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  756. return;
  757. }
  758. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  759. break;
  760. case FlipWorldSubmenuIndexUserSettings:
  761. free_all_views(app, true, false);
  762. // if (!flipper_http_init(flipper_http_rx_callback, app))
  763. // {
  764. // FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  765. // return;
  766. // }
  767. // app_instance = malloc(sizeof(FlipWorldApp));
  768. // if (!app_instance)
  769. // {
  770. // FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  771. // return;
  772. // }
  773. // memcpy(app_instance, app, sizeof(FlipWorldApp));
  774. // flip_social_login_switch_to_view(app);
  775. easy_flipper_dialog("User Settings", "Coming soon...");
  776. break;
  777. default:
  778. break;
  779. }
  780. }
  781. static void text_updated_ssid(void *context)
  782. {
  783. FlipWorldApp *app = (FlipWorldApp *)context;
  784. if (!app)
  785. {
  786. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  787. return;
  788. }
  789. // store the entered text
  790. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  791. // Ensure null-termination
  792. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  793. // save the setting
  794. save_char("WiFi-SSID", app->text_input_buffer);
  795. // update the variable item text
  796. if (app->variable_item_wifi_ssid)
  797. {
  798. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  799. // get value of password
  800. char pass[64];
  801. if (load_char("WiFi-Password", pass, sizeof(pass)))
  802. {
  803. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  804. {
  805. // save the settings
  806. save_settings(app->text_input_buffer, pass);
  807. // initialize the http
  808. if (flipper_http_init(flipper_http_rx_callback, app))
  809. {
  810. // save the wifi if the device is connected
  811. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  812. {
  813. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  814. }
  815. // free the resources
  816. flipper_http_deinit();
  817. }
  818. else
  819. {
  820. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  821. }
  822. }
  823. }
  824. }
  825. // switch to the settings view
  826. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  827. }
  828. static void text_updated_pass(void *context)
  829. {
  830. FlipWorldApp *app = (FlipWorldApp *)context;
  831. if (!app)
  832. {
  833. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  834. return;
  835. }
  836. // store the entered text
  837. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  838. // Ensure null-termination
  839. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  840. // save the setting
  841. save_char("WiFi-Password", app->text_input_buffer);
  842. // update the variable item text
  843. if (app->variable_item_wifi_pass)
  844. {
  845. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  846. }
  847. // get value of ssid
  848. char ssid[64];
  849. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  850. {
  851. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  852. {
  853. // save the settings
  854. save_settings(ssid, app->text_input_buffer);
  855. // initialize the http
  856. if (flipper_http_init(flipper_http_rx_callback, app))
  857. {
  858. // save the wifi if the device is connected
  859. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  860. {
  861. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  862. }
  863. // free the resources
  864. flipper_http_deinit();
  865. }
  866. else
  867. {
  868. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  869. }
  870. }
  871. }
  872. // switch to the settings view
  873. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  874. }
  875. static void wifi_settings_item_selected(void *context, uint32_t index)
  876. {
  877. FlipWorldApp *app = (FlipWorldApp *)context;
  878. if (!app)
  879. {
  880. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  881. return;
  882. }
  883. char ssid[64];
  884. char pass[64];
  885. switch (index)
  886. {
  887. case 0: // Input SSID
  888. free_all_views(app, false, false);
  889. if (!alloc_text_input_view(app, "SSID"))
  890. {
  891. FURI_LOG_E(TAG, "Failed to allocate text input view");
  892. return;
  893. }
  894. // load SSID
  895. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  896. {
  897. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  898. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  899. }
  900. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  901. break;
  902. case 1: // Input Password
  903. free_all_views(app, false, false);
  904. if (!alloc_text_input_view(app, "Password"))
  905. {
  906. FURI_LOG_E(TAG, "Failed to allocate text input view");
  907. return;
  908. }
  909. // load password
  910. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  911. {
  912. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  913. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  914. }
  915. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  916. break;
  917. default:
  918. FURI_LOG_E(TAG, "Unknown configuration item index");
  919. break;
  920. }
  921. }
  922. static void flip_world_game_fps_change(VariableItem *item)
  923. {
  924. uint8_t index = variable_item_get_current_value_index(item);
  925. game_fps_index = index;
  926. variable_item_set_current_value_text(item, game_fps_choices[index]);
  927. // save the fps
  928. save_char("Game-FPS", game_fps_choices[index]);
  929. }
  930. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  931. {
  932. UNUSED(model);
  933. snprintf(
  934. fhttp.file_path,
  935. sizeof(fhttp.file_path),
  936. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  937. fhttp.save_received_data = true;
  938. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  939. }
  940. static char *flip_world_parse_worlds(DataLoaderModel *model)
  941. {
  942. UNUSED(model);
  943. flipper_http_deinit();
  944. return "World Pack Installed";
  945. }
  946. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  947. {
  948. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  949. }
  950. static void game_settings_item_selected(void *context, uint32_t index)
  951. {
  952. FlipWorldApp *app = (FlipWorldApp *)context;
  953. if (!app)
  954. {
  955. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  956. return;
  957. }
  958. switch (index)
  959. {
  960. case 0: // Game FPS
  961. break; // handled by flip_world_game_fps_change
  962. case 1: // Download Worlds
  963. // TODO: Implement download worlds
  964. if (!flipper_http_init(flipper_http_rx_callback, app))
  965. {
  966. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  967. return;
  968. }
  969. flip_world_switch_to_view_get_worlds(app);
  970. break;
  971. }
  972. }
  973. static void flip_world_widget_set_text(char *message, Widget **widget)
  974. {
  975. if (widget == NULL)
  976. {
  977. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  978. DEV_CRASH();
  979. return;
  980. }
  981. if (message == NULL)
  982. {
  983. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  984. DEV_CRASH();
  985. return;
  986. }
  987. widget_reset(*widget);
  988. uint32_t message_length = strlen(message); // Length of the message
  989. uint32_t i = 0; // Index tracker
  990. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  991. char *formatted_message; // Buffer to hold the final formatted message
  992. // Allocate buffer with double the message length plus one for safety
  993. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  994. {
  995. return;
  996. }
  997. while (i < message_length)
  998. {
  999. uint32_t max_line_length = 31; // Maximum characters per line
  1000. uint32_t remaining_length = message_length - i; // Remaining characters
  1001. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1002. // Check for newline character within the current segment
  1003. uint32_t newline_pos = i;
  1004. bool found_newline = false;
  1005. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1006. {
  1007. if (message[newline_pos] == '\n')
  1008. {
  1009. found_newline = true;
  1010. break;
  1011. }
  1012. }
  1013. if (found_newline)
  1014. {
  1015. // If newline found, set line_length up to the newline
  1016. line_length = newline_pos - i;
  1017. }
  1018. // Temporary buffer to hold the current line
  1019. char line[32];
  1020. strncpy(line, message + i, line_length);
  1021. line[line_length] = '\0';
  1022. // If newline was found, skip it for the next iteration
  1023. if (found_newline)
  1024. {
  1025. i += line_length + 1; // +1 to skip the '\n' character
  1026. }
  1027. else
  1028. {
  1029. // Check if the line ends in the middle of a word and adjust accordingly
  1030. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1031. {
  1032. // Find the last space within the current line to avoid breaking a word
  1033. char *last_space = strrchr(line, ' ');
  1034. if (last_space != NULL)
  1035. {
  1036. // Adjust the line_length to avoid cutting the word
  1037. line_length = last_space - line;
  1038. line[line_length] = '\0'; // Null-terminate at the space
  1039. }
  1040. }
  1041. // Move the index forward by the determined line_length
  1042. i += line_length;
  1043. // Skip any spaces at the beginning of the next line
  1044. while (i < message_length && message[i] == ' ')
  1045. {
  1046. i++;
  1047. }
  1048. }
  1049. // Manually copy the fixed line into the formatted_message buffer
  1050. for (uint32_t j = 0; j < line_length; j++)
  1051. {
  1052. formatted_message[formatted_index++] = line[j];
  1053. }
  1054. // Add a newline character for line spacing
  1055. formatted_message[formatted_index++] = '\n';
  1056. }
  1057. // Null-terminate the formatted_message
  1058. formatted_message[formatted_index] = '\0';
  1059. // Add the formatted message to the widget
  1060. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1061. }
  1062. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1063. {
  1064. if (!canvas || !model)
  1065. {
  1066. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1067. return;
  1068. }
  1069. SerialState http_state = fhttp.state;
  1070. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1071. DataState data_state = data_loader_model->data_state;
  1072. char *title = data_loader_model->title;
  1073. canvas_set_font(canvas, FontSecondary);
  1074. if (http_state == INACTIVE)
  1075. {
  1076. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1077. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1078. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1079. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1080. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1081. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1082. return;
  1083. }
  1084. if (data_state == DataStateError || data_state == DataStateParseError)
  1085. {
  1086. flip_world_request_error_draw(canvas);
  1087. return;
  1088. }
  1089. canvas_draw_str(canvas, 0, 7, title);
  1090. canvas_draw_str(canvas, 0, 17, "Loading...");
  1091. if (data_state == DataStateInitial)
  1092. {
  1093. return;
  1094. }
  1095. if (http_state == SENDING)
  1096. {
  1097. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1098. return;
  1099. }
  1100. if (http_state == RECEIVING || data_state == DataStateRequested)
  1101. {
  1102. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1103. return;
  1104. }
  1105. if (http_state == IDLE && data_state == DataStateReceived)
  1106. {
  1107. canvas_draw_str(canvas, 0, 27, "Processing...");
  1108. return;
  1109. }
  1110. if (http_state == IDLE && data_state == DataStateParsed)
  1111. {
  1112. canvas_draw_str(canvas, 0, 27, "Processed...");
  1113. return;
  1114. }
  1115. }
  1116. static void flip_world_loader_process_callback(void *context)
  1117. {
  1118. if (context == NULL)
  1119. {
  1120. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1121. DEV_CRASH();
  1122. return;
  1123. }
  1124. FlipWorldApp *app = (FlipWorldApp *)context;
  1125. View *view = app->view_loader;
  1126. DataState current_data_state;
  1127. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1128. if (current_data_state == DataStateInitial)
  1129. {
  1130. with_view_model(
  1131. view,
  1132. DataLoaderModel * model,
  1133. {
  1134. model->data_state = DataStateRequested;
  1135. DataLoaderFetch fetch = model->fetcher;
  1136. if (fetch == NULL)
  1137. {
  1138. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1139. model->data_state = DataStateError;
  1140. return;
  1141. }
  1142. // Clear any previous responses
  1143. strncpy(fhttp.last_response, "", 1);
  1144. bool request_status = fetch(model);
  1145. if (!request_status)
  1146. {
  1147. model->data_state = DataStateError;
  1148. }
  1149. },
  1150. true);
  1151. }
  1152. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1153. {
  1154. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1155. {
  1156. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1157. {
  1158. FURI_LOG_DEV(TAG, "PONG received.");
  1159. }
  1160. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1161. {
  1162. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1163. }
  1164. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1165. {
  1166. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1167. }
  1168. else if (strlen(fhttp.last_response) == 0)
  1169. {
  1170. // Still waiting on response
  1171. }
  1172. else
  1173. {
  1174. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1175. }
  1176. }
  1177. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1178. {
  1179. // continue waiting
  1180. }
  1181. else if (fhttp.state == INACTIVE)
  1182. {
  1183. // inactive. try again
  1184. }
  1185. else if (fhttp.state == ISSUE)
  1186. {
  1187. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1188. }
  1189. else
  1190. {
  1191. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1192. DEV_CRASH();
  1193. }
  1194. }
  1195. else if (current_data_state == DataStateReceived)
  1196. {
  1197. with_view_model(
  1198. view,
  1199. DataLoaderModel * model,
  1200. {
  1201. char *data_text;
  1202. if (model->parser == NULL)
  1203. {
  1204. data_text = NULL;
  1205. FURI_LOG_DEV(TAG, "Parser is NULL");
  1206. DEV_CRASH();
  1207. }
  1208. else
  1209. {
  1210. data_text = model->parser(model);
  1211. }
  1212. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1213. model->data_text = data_text;
  1214. if (data_text == NULL)
  1215. {
  1216. model->data_state = DataStateParseError;
  1217. }
  1218. else
  1219. {
  1220. model->data_state = DataStateParsed;
  1221. }
  1222. },
  1223. true);
  1224. }
  1225. else if (current_data_state == DataStateParsed)
  1226. {
  1227. with_view_model(
  1228. view,
  1229. DataLoaderModel * model,
  1230. {
  1231. if (++model->request_index < model->request_count)
  1232. {
  1233. model->data_state = DataStateInitial;
  1234. }
  1235. else
  1236. {
  1237. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1238. if (model->data_text != NULL)
  1239. {
  1240. free(model->data_text);
  1241. model->data_text = NULL;
  1242. }
  1243. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1244. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1245. }
  1246. },
  1247. true);
  1248. }
  1249. }
  1250. static void flip_world_loader_timer_callback(void *context)
  1251. {
  1252. if (context == NULL)
  1253. {
  1254. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1255. DEV_CRASH();
  1256. return;
  1257. }
  1258. FlipWorldApp *app = (FlipWorldApp *)context;
  1259. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1260. }
  1261. static void flip_world_loader_on_enter(void *context)
  1262. {
  1263. if (context == NULL)
  1264. {
  1265. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1266. DEV_CRASH();
  1267. return;
  1268. }
  1269. FlipWorldApp *app = (FlipWorldApp *)context;
  1270. View *view = app->view_loader;
  1271. with_view_model(
  1272. view,
  1273. DataLoaderModel * model,
  1274. {
  1275. view_set_previous_callback(view, model->back_callback);
  1276. if (model->timer == NULL)
  1277. {
  1278. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1279. }
  1280. furi_timer_start(model->timer, 250);
  1281. },
  1282. true);
  1283. }
  1284. static void flip_world_loader_on_exit(void *context)
  1285. {
  1286. if (context == NULL)
  1287. {
  1288. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1289. DEV_CRASH();
  1290. return;
  1291. }
  1292. FlipWorldApp *app = (FlipWorldApp *)context;
  1293. View *view = app->view_loader;
  1294. with_view_model(
  1295. view,
  1296. DataLoaderModel * model,
  1297. {
  1298. if (model->timer)
  1299. {
  1300. furi_timer_stop(model->timer);
  1301. }
  1302. },
  1303. false);
  1304. }
  1305. void flip_world_loader_init(View *view)
  1306. {
  1307. if (view == NULL)
  1308. {
  1309. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1310. DEV_CRASH();
  1311. return;
  1312. }
  1313. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1314. view_set_enter_callback(view, flip_world_loader_on_enter);
  1315. view_set_exit_callback(view, flip_world_loader_on_exit);
  1316. }
  1317. void flip_world_loader_free_model(View *view)
  1318. {
  1319. if (view == NULL)
  1320. {
  1321. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1322. DEV_CRASH();
  1323. return;
  1324. }
  1325. with_view_model(
  1326. view,
  1327. DataLoaderModel * model,
  1328. {
  1329. if (model->timer)
  1330. {
  1331. furi_timer_free(model->timer);
  1332. model->timer = NULL;
  1333. }
  1334. if (model->parser_context)
  1335. {
  1336. free(model->parser_context);
  1337. model->parser_context = NULL;
  1338. }
  1339. },
  1340. false);
  1341. }
  1342. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1343. {
  1344. if (context == NULL)
  1345. {
  1346. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1347. DEV_CRASH();
  1348. return false;
  1349. }
  1350. switch (index)
  1351. {
  1352. case FlipWorldCustomEventProcess:
  1353. flip_world_loader_process_callback(context);
  1354. return true;
  1355. default:
  1356. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1357. return false;
  1358. }
  1359. }
  1360. void flip_world_generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1361. {
  1362. if (app == NULL)
  1363. {
  1364. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1365. DEV_CRASH();
  1366. return;
  1367. }
  1368. View *view = app->view_loader;
  1369. if (view == NULL)
  1370. {
  1371. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1372. DEV_CRASH();
  1373. return;
  1374. }
  1375. with_view_model(
  1376. view,
  1377. DataLoaderModel * model,
  1378. {
  1379. model->title = title;
  1380. model->fetcher = fetcher;
  1381. model->parser = parser;
  1382. model->request_index = 0;
  1383. model->request_count = request_count;
  1384. model->back_callback = back;
  1385. model->data_state = DataStateInitial;
  1386. model->data_text = NULL;
  1387. },
  1388. true);
  1389. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1390. }