callback.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. }
  313. }
  314. return true;
  315. }
  316. static bool alloc_submenu_settings(void *context)
  317. {
  318. FlipWorldApp *app = (FlipWorldApp *)context;
  319. if (!app)
  320. {
  321. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  322. return false;
  323. }
  324. if (!app->submenu_settings)
  325. {
  326. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  327. {
  328. return NULL;
  329. }
  330. if (!app->submenu_settings)
  331. {
  332. return false;
  333. }
  334. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  335. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  336. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  337. }
  338. return true;
  339. }
  340. // free
  341. static void free_about_view(void *context)
  342. {
  343. FlipWorldApp *app = (FlipWorldApp *)context;
  344. if (!app)
  345. {
  346. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  347. return;
  348. }
  349. if (app->view_about)
  350. {
  351. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  352. view_free(app->view_about);
  353. app->view_about = NULL;
  354. }
  355. }
  356. static void free_main_view(void *context)
  357. {
  358. FlipWorldApp *app = (FlipWorldApp *)context;
  359. if (!app)
  360. {
  361. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  362. return;
  363. }
  364. }
  365. static void free_text_input_view(void *context)
  366. {
  367. FlipWorldApp *app = (FlipWorldApp *)context;
  368. if (!app)
  369. {
  370. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  371. return;
  372. }
  373. if (app->text_input)
  374. {
  375. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  376. uart_text_input_free(app->text_input);
  377. app->text_input = NULL;
  378. }
  379. if (app->text_input_buffer)
  380. {
  381. free(app->text_input_buffer);
  382. app->text_input_buffer = NULL;
  383. }
  384. if (app->text_input_temp_buffer)
  385. {
  386. free(app->text_input_temp_buffer);
  387. app->text_input_temp_buffer = NULL;
  388. }
  389. }
  390. static void free_variable_item_list(void *context)
  391. {
  392. FlipWorldApp *app = (FlipWorldApp *)context;
  393. if (!app)
  394. {
  395. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  396. return;
  397. }
  398. if (app->variable_item_list)
  399. {
  400. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  401. variable_item_list_free(app->variable_item_list);
  402. app->variable_item_list = NULL;
  403. }
  404. if (app->variable_item_wifi_ssid)
  405. {
  406. free(app->variable_item_wifi_ssid);
  407. app->variable_item_wifi_ssid = NULL;
  408. }
  409. if (app->variable_item_wifi_pass)
  410. {
  411. free(app->variable_item_wifi_pass);
  412. app->variable_item_wifi_pass = NULL;
  413. }
  414. if (app->variable_item_game_fps)
  415. {
  416. free(app->variable_item_game_fps);
  417. app->variable_item_game_fps = NULL;
  418. }
  419. if (app->variable_item_user_username)
  420. {
  421. free(app->variable_item_user_username);
  422. app->variable_item_user_username = NULL;
  423. }
  424. if (app->variable_item_user_password)
  425. {
  426. free(app->variable_item_user_password);
  427. app->variable_item_user_password = NULL;
  428. }
  429. }
  430. static void free_submenu_settings(void *context)
  431. {
  432. FlipWorldApp *app = (FlipWorldApp *)context;
  433. if (!app)
  434. {
  435. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  436. return;
  437. }
  438. if (app->submenu_settings)
  439. {
  440. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  441. submenu_free(app->submenu_settings);
  442. app->submenu_settings = NULL;
  443. }
  444. }
  445. static FlipWorldApp *app_instance = NULL;
  446. static FuriThreadId thread_id;
  447. static bool game_thread_running = false;
  448. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  449. {
  450. FlipWorldApp *app = (FlipWorldApp *)context;
  451. if (!app)
  452. {
  453. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  454. return;
  455. }
  456. if (should_free_variable_item_list)
  457. {
  458. free_variable_item_list(app);
  459. }
  460. free_about_view(app);
  461. free_main_view(app);
  462. free_text_input_view(app);
  463. // free game thread
  464. if (game_thread_running)
  465. {
  466. game_thread_running = false;
  467. furi_thread_flags_set(thread_id, WorkerEvtStop);
  468. furi_thread_free(thread_id);
  469. }
  470. if (should_free_submenu_settings)
  471. free_submenu_settings(app);
  472. if (app_instance)
  473. {
  474. free(app_instance);
  475. app_instance = NULL;
  476. }
  477. }
  478. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  479. {
  480. if (model->request_index == 0)
  481. {
  482. // Create the directory for saving worlds
  483. char directory_path[128];
  484. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  485. // Create the directory
  486. Storage *storage = furi_record_open(RECORD_STORAGE);
  487. storage_common_mkdir(storage, directory_path);
  488. // free storage
  489. furi_record_close(RECORD_STORAGE);
  490. snprintf(
  491. fhttp.file_path,
  492. sizeof(fhttp.file_path),
  493. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  494. fhttp.save_received_data = true;
  495. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  496. }
  497. else if (model->request_index == 1)
  498. {
  499. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  500. if (!world_list)
  501. {
  502. FURI_LOG_E(TAG, "Failed to load world list");
  503. return "Failed to load world list";
  504. }
  505. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  506. if (!first_world)
  507. {
  508. FURI_LOG_E(TAG, "Failed to get first world");
  509. return "Failed to get first world";
  510. }
  511. // if (world_exists(furi_string_get_cstr(first_world)))
  512. // {
  513. // furi_string_free(world_list);
  514. // furi_string_free(first_world);
  515. // FURI_LOG_I(TAG, "World already exists");
  516. // fhttp.state = IDLE;
  517. // return true;
  518. // }
  519. snprintf(
  520. fhttp.file_path,
  521. sizeof(fhttp.file_path),
  522. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  523. fhttp.save_received_data = true;
  524. char url[128];
  525. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  526. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  527. }
  528. return false;
  529. }
  530. static char *flip_world_parse_world_list(DataLoaderModel *model)
  531. {
  532. if (model->request_index == 0)
  533. {
  534. return "World List Fetched";
  535. }
  536. else if (model->request_index == 1)
  537. {
  538. flipper_http_deinit();
  539. // free game thread
  540. if (game_thread_running)
  541. {
  542. game_thread_running = false;
  543. furi_thread_flags_set(thread_id, WorkerEvtStop);
  544. furi_thread_free(thread_id);
  545. }
  546. // free_all_views(app_instance, true, true);
  547. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  548. if (!thread)
  549. {
  550. FURI_LOG_E(TAG, "Failed to allocate game thread");
  551. return "Failed to allocate game thread";
  552. }
  553. furi_thread_start(thread);
  554. thread_id = furi_thread_get_id(thread);
  555. game_thread_running = true;
  556. return "Thanks for playing!\nPress BACK to return.";
  557. }
  558. return "Unknown error";
  559. }
  560. static void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
  561. {
  562. 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);
  563. }
  564. void callback_submenu_choices(void *context, uint32_t index)
  565. {
  566. FlipWorldApp *app = (FlipWorldApp *)context;
  567. if (!app)
  568. {
  569. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  570. return;
  571. }
  572. switch (index)
  573. {
  574. case FlipWorldSubmenuIndexRun:
  575. if (!flipper_http_init(flipper_http_rx_callback, app))
  576. {
  577. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  578. return;
  579. }
  580. app_instance = malloc(sizeof(FlipWorldApp));
  581. if (!app_instance)
  582. {
  583. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  584. return;
  585. }
  586. memcpy(app_instance, app, sizeof(FlipWorldApp));
  587. flip_world_switch_to_view_get_world_list(app);
  588. break;
  589. case FlipWorldSubmenuIndexAbout:
  590. free_all_views(app, true, true);
  591. if (!alloc_about_view(app))
  592. {
  593. FURI_LOG_E(TAG, "Failed to allocate about view");
  594. return;
  595. }
  596. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  597. break;
  598. case FlipWorldSubmenuIndexSettings:
  599. free_all_views(app, true, true);
  600. if (!alloc_submenu_settings(app))
  601. {
  602. FURI_LOG_E(TAG, "Failed to allocate settings view");
  603. return;
  604. }
  605. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  606. break;
  607. case FlipWorldSubmenuIndexWiFiSettings:
  608. free_all_views(app, true, false);
  609. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  610. {
  611. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  612. return;
  613. }
  614. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  615. break;
  616. case FlipWorldSubmenuIndexGameSettings:
  617. free_all_views(app, true, false);
  618. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  619. {
  620. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  621. return;
  622. }
  623. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  624. break;
  625. case FlipWorldSubmenuIndexUserSettings:
  626. easy_flipper_dialog("User Settings", "Coming soon...");
  627. break;
  628. default:
  629. break;
  630. }
  631. }
  632. static void text_updated_ssid(void *context)
  633. {
  634. FlipWorldApp *app = (FlipWorldApp *)context;
  635. if (!app)
  636. {
  637. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  638. return;
  639. }
  640. // store the entered text
  641. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  642. // Ensure null-termination
  643. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  644. // save the setting
  645. save_char("WiFi-SSID", app->text_input_buffer);
  646. // update the variable item text
  647. if (app->variable_item_wifi_ssid)
  648. {
  649. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  650. // get value of password
  651. char pass[64];
  652. if (load_char("WiFi-Password", pass, sizeof(pass)))
  653. {
  654. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  655. {
  656. // save the settings
  657. save_settings(app->text_input_buffer, pass);
  658. // initialize the http
  659. if (flipper_http_init(flipper_http_rx_callback, app))
  660. {
  661. // save the wifi if the device is connected
  662. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  663. {
  664. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  665. }
  666. // free the resources
  667. flipper_http_deinit();
  668. }
  669. else
  670. {
  671. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  672. }
  673. }
  674. }
  675. }
  676. // switch to the settings view
  677. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  678. }
  679. static void text_updated_pass(void *context)
  680. {
  681. FlipWorldApp *app = (FlipWorldApp *)context;
  682. if (!app)
  683. {
  684. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  685. return;
  686. }
  687. // store the entered text
  688. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  689. // Ensure null-termination
  690. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  691. // save the setting
  692. save_char("WiFi-Password", app->text_input_buffer);
  693. // update the variable item text
  694. if (app->variable_item_wifi_pass)
  695. {
  696. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  697. }
  698. // get value of ssid
  699. char ssid[64];
  700. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  701. {
  702. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  703. {
  704. // save the settings
  705. save_settings(ssid, app->text_input_buffer);
  706. // initialize the http
  707. if (flipper_http_init(flipper_http_rx_callback, app))
  708. {
  709. // save the wifi if the device is connected
  710. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  711. {
  712. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  713. }
  714. // free the resources
  715. flipper_http_deinit();
  716. }
  717. else
  718. {
  719. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  720. }
  721. }
  722. }
  723. // switch to the settings view
  724. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  725. }
  726. static void wifi_settings_item_selected(void *context, uint32_t index)
  727. {
  728. FlipWorldApp *app = (FlipWorldApp *)context;
  729. if (!app)
  730. {
  731. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  732. return;
  733. }
  734. char ssid[64];
  735. char pass[64];
  736. switch (index)
  737. {
  738. case 0: // Input SSID
  739. free_all_views(app, false, false);
  740. if (!alloc_text_input_view(app, "SSID"))
  741. {
  742. FURI_LOG_E(TAG, "Failed to allocate text input view");
  743. return;
  744. }
  745. // load SSID
  746. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  747. {
  748. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  749. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  750. }
  751. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  752. break;
  753. case 1: // Input Password
  754. free_all_views(app, false, false);
  755. if (!alloc_text_input_view(app, "Password"))
  756. {
  757. FURI_LOG_E(TAG, "Failed to allocate text input view");
  758. return;
  759. }
  760. // load password
  761. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  762. {
  763. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  764. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  765. }
  766. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  767. break;
  768. default:
  769. FURI_LOG_E(TAG, "Unknown configuration item index");
  770. break;
  771. }
  772. }
  773. static void flip_world_game_fps_change(VariableItem *item)
  774. {
  775. uint8_t index = variable_item_get_current_value_index(item);
  776. game_fps_index = index;
  777. variable_item_set_current_value_text(item, game_fps_choices[index]);
  778. // save the fps
  779. save_char("Game-FPS", game_fps_choices[index]);
  780. }
  781. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  782. {
  783. UNUSED(model);
  784. snprintf(
  785. fhttp.file_path,
  786. sizeof(fhttp.file_path),
  787. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  788. fhttp.save_received_data = true;
  789. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  790. }
  791. static char *flip_world_parse_worlds(DataLoaderModel *model)
  792. {
  793. UNUSED(model);
  794. flipper_http_deinit();
  795. return "World Pack Installed";
  796. }
  797. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  798. {
  799. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  800. }
  801. static void game_settings_item_selected(void *context, uint32_t index)
  802. {
  803. FlipWorldApp *app = (FlipWorldApp *)context;
  804. if (!app)
  805. {
  806. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  807. return;
  808. }
  809. switch (index)
  810. {
  811. case 0: // Game FPS
  812. break; // handled by flip_world_game_fps_change
  813. case 1: // Download Worlds
  814. // TODO: Implement download worlds
  815. if (!flipper_http_init(flipper_http_rx_callback, app))
  816. {
  817. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  818. return;
  819. }
  820. flip_world_switch_to_view_get_worlds(app);
  821. break;
  822. }
  823. }
  824. static void flip_world_widget_set_text(char *message, Widget **widget)
  825. {
  826. if (widget == NULL)
  827. {
  828. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  829. DEV_CRASH();
  830. return;
  831. }
  832. if (message == NULL)
  833. {
  834. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  835. DEV_CRASH();
  836. return;
  837. }
  838. widget_reset(*widget);
  839. uint32_t message_length = strlen(message); // Length of the message
  840. uint32_t i = 0; // Index tracker
  841. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  842. char *formatted_message; // Buffer to hold the final formatted message
  843. // Allocate buffer with double the message length plus one for safety
  844. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  845. {
  846. return;
  847. }
  848. while (i < message_length)
  849. {
  850. uint32_t max_line_length = 31; // Maximum characters per line
  851. uint32_t remaining_length = message_length - i; // Remaining characters
  852. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  853. // Check for newline character within the current segment
  854. uint32_t newline_pos = i;
  855. bool found_newline = false;
  856. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  857. {
  858. if (message[newline_pos] == '\n')
  859. {
  860. found_newline = true;
  861. break;
  862. }
  863. }
  864. if (found_newline)
  865. {
  866. // If newline found, set line_length up to the newline
  867. line_length = newline_pos - i;
  868. }
  869. // Temporary buffer to hold the current line
  870. char line[32];
  871. strncpy(line, message + i, line_length);
  872. line[line_length] = '\0';
  873. // If newline was found, skip it for the next iteration
  874. if (found_newline)
  875. {
  876. i += line_length + 1; // +1 to skip the '\n' character
  877. }
  878. else
  879. {
  880. // Check if the line ends in the middle of a word and adjust accordingly
  881. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  882. {
  883. // Find the last space within the current line to avoid breaking a word
  884. char *last_space = strrchr(line, ' ');
  885. if (last_space != NULL)
  886. {
  887. // Adjust the line_length to avoid cutting the word
  888. line_length = last_space - line;
  889. line[line_length] = '\0'; // Null-terminate at the space
  890. }
  891. }
  892. // Move the index forward by the determined line_length
  893. i += line_length;
  894. // Skip any spaces at the beginning of the next line
  895. while (i < message_length && message[i] == ' ')
  896. {
  897. i++;
  898. }
  899. }
  900. // Manually copy the fixed line into the formatted_message buffer
  901. for (uint32_t j = 0; j < line_length; j++)
  902. {
  903. formatted_message[formatted_index++] = line[j];
  904. }
  905. // Add a newline character for line spacing
  906. formatted_message[formatted_index++] = '\n';
  907. }
  908. // Null-terminate the formatted_message
  909. formatted_message[formatted_index] = '\0';
  910. // Add the formatted message to the widget
  911. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  912. }
  913. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  914. {
  915. if (!canvas || !model)
  916. {
  917. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  918. return;
  919. }
  920. SerialState http_state = fhttp.state;
  921. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  922. DataState data_state = data_loader_model->data_state;
  923. char *title = data_loader_model->title;
  924. canvas_set_font(canvas, FontSecondary);
  925. if (http_state == INACTIVE)
  926. {
  927. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  928. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  929. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  930. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  931. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  932. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  933. return;
  934. }
  935. if (data_state == DataStateError || data_state == DataStateParseError)
  936. {
  937. flip_world_request_error_draw(canvas);
  938. return;
  939. }
  940. canvas_draw_str(canvas, 0, 7, title);
  941. canvas_draw_str(canvas, 0, 17, "Loading...");
  942. if (data_state == DataStateInitial)
  943. {
  944. return;
  945. }
  946. if (http_state == SENDING)
  947. {
  948. canvas_draw_str(canvas, 0, 27, "Fetching...");
  949. return;
  950. }
  951. if (http_state == RECEIVING || data_state == DataStateRequested)
  952. {
  953. canvas_draw_str(canvas, 0, 27, "Receiving...");
  954. return;
  955. }
  956. if (http_state == IDLE && data_state == DataStateReceived)
  957. {
  958. canvas_draw_str(canvas, 0, 27, "Processing...");
  959. return;
  960. }
  961. if (http_state == IDLE && data_state == DataStateParsed)
  962. {
  963. canvas_draw_str(canvas, 0, 27, "Processed...");
  964. return;
  965. }
  966. }
  967. static void flip_world_loader_process_callback(void *context)
  968. {
  969. if (context == NULL)
  970. {
  971. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  972. DEV_CRASH();
  973. return;
  974. }
  975. FlipWorldApp *app = (FlipWorldApp *)context;
  976. View *view = app->view_loader;
  977. DataState current_data_state;
  978. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  979. if (current_data_state == DataStateInitial)
  980. {
  981. with_view_model(
  982. view,
  983. DataLoaderModel * model,
  984. {
  985. model->data_state = DataStateRequested;
  986. DataLoaderFetch fetch = model->fetcher;
  987. if (fetch == NULL)
  988. {
  989. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  990. model->data_state = DataStateError;
  991. return;
  992. }
  993. // Clear any previous responses
  994. strncpy(fhttp.last_response, "", 1);
  995. bool request_status = fetch(model);
  996. if (!request_status)
  997. {
  998. model->data_state = DataStateError;
  999. }
  1000. },
  1001. true);
  1002. }
  1003. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1004. {
  1005. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1006. {
  1007. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1008. {
  1009. FURI_LOG_DEV(TAG, "PONG received.");
  1010. }
  1011. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1012. {
  1013. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1014. }
  1015. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1016. {
  1017. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1018. }
  1019. else if (strlen(fhttp.last_response) == 0)
  1020. {
  1021. // Still waiting on response
  1022. }
  1023. else
  1024. {
  1025. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1026. }
  1027. }
  1028. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1029. {
  1030. // continue waiting
  1031. }
  1032. else if (fhttp.state == INACTIVE)
  1033. {
  1034. // inactive. try again
  1035. }
  1036. else if (fhttp.state == ISSUE)
  1037. {
  1038. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1039. }
  1040. else
  1041. {
  1042. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1043. DEV_CRASH();
  1044. }
  1045. }
  1046. else if (current_data_state == DataStateReceived)
  1047. {
  1048. with_view_model(
  1049. view,
  1050. DataLoaderModel * model,
  1051. {
  1052. char *data_text;
  1053. if (model->parser == NULL)
  1054. {
  1055. data_text = NULL;
  1056. FURI_LOG_DEV(TAG, "Parser is NULL");
  1057. DEV_CRASH();
  1058. }
  1059. else
  1060. {
  1061. data_text = model->parser(model);
  1062. }
  1063. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1064. model->data_text = data_text;
  1065. if (data_text == NULL)
  1066. {
  1067. model->data_state = DataStateParseError;
  1068. }
  1069. else
  1070. {
  1071. model->data_state = DataStateParsed;
  1072. }
  1073. },
  1074. true);
  1075. }
  1076. else if (current_data_state == DataStateParsed)
  1077. {
  1078. with_view_model(
  1079. view,
  1080. DataLoaderModel * model,
  1081. {
  1082. if (++model->request_index < model->request_count)
  1083. {
  1084. model->data_state = DataStateInitial;
  1085. }
  1086. else
  1087. {
  1088. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1089. if (model->data_text != NULL)
  1090. {
  1091. free(model->data_text);
  1092. model->data_text = NULL;
  1093. }
  1094. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1095. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1096. }
  1097. },
  1098. true);
  1099. }
  1100. }
  1101. static void flip_world_loader_timer_callback(void *context)
  1102. {
  1103. if (context == NULL)
  1104. {
  1105. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1106. DEV_CRASH();
  1107. return;
  1108. }
  1109. FlipWorldApp *app = (FlipWorldApp *)context;
  1110. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1111. }
  1112. static void flip_world_loader_on_enter(void *context)
  1113. {
  1114. if (context == NULL)
  1115. {
  1116. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1117. DEV_CRASH();
  1118. return;
  1119. }
  1120. FlipWorldApp *app = (FlipWorldApp *)context;
  1121. View *view = app->view_loader;
  1122. with_view_model(
  1123. view,
  1124. DataLoaderModel * model,
  1125. {
  1126. view_set_previous_callback(view, model->back_callback);
  1127. if (model->timer == NULL)
  1128. {
  1129. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1130. }
  1131. furi_timer_start(model->timer, 250);
  1132. },
  1133. true);
  1134. }
  1135. static void flip_world_loader_on_exit(void *context)
  1136. {
  1137. if (context == NULL)
  1138. {
  1139. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1140. DEV_CRASH();
  1141. return;
  1142. }
  1143. FlipWorldApp *app = (FlipWorldApp *)context;
  1144. View *view = app->view_loader;
  1145. with_view_model(
  1146. view,
  1147. DataLoaderModel * model,
  1148. {
  1149. if (model->timer)
  1150. {
  1151. furi_timer_stop(model->timer);
  1152. }
  1153. },
  1154. false);
  1155. }
  1156. void flip_world_loader_init(View *view)
  1157. {
  1158. if (view == NULL)
  1159. {
  1160. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1161. DEV_CRASH();
  1162. return;
  1163. }
  1164. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1165. view_set_enter_callback(view, flip_world_loader_on_enter);
  1166. view_set_exit_callback(view, flip_world_loader_on_exit);
  1167. }
  1168. void flip_world_loader_free_model(View *view)
  1169. {
  1170. if (view == NULL)
  1171. {
  1172. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1173. DEV_CRASH();
  1174. return;
  1175. }
  1176. with_view_model(
  1177. view,
  1178. DataLoaderModel * model,
  1179. {
  1180. if (model->timer)
  1181. {
  1182. furi_timer_free(model->timer);
  1183. model->timer = NULL;
  1184. }
  1185. if (model->parser_context)
  1186. {
  1187. free(model->parser_context);
  1188. model->parser_context = NULL;
  1189. }
  1190. },
  1191. false);
  1192. }
  1193. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1194. {
  1195. if (context == NULL)
  1196. {
  1197. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1198. DEV_CRASH();
  1199. return false;
  1200. }
  1201. switch (index)
  1202. {
  1203. case FlipWorldCustomEventProcess:
  1204. flip_world_loader_process_callback(context);
  1205. return true;
  1206. default:
  1207. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1208. return false;
  1209. }
  1210. }
  1211. 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)
  1212. {
  1213. if (app == NULL)
  1214. {
  1215. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1216. DEV_CRASH();
  1217. return;
  1218. }
  1219. View *view = app->view_loader;
  1220. if (view == NULL)
  1221. {
  1222. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1223. DEV_CRASH();
  1224. return;
  1225. }
  1226. with_view_model(
  1227. view,
  1228. DataLoaderModel * model,
  1229. {
  1230. model->title = title;
  1231. model->fetcher = fetcher;
  1232. model->parser = parser;
  1233. model->request_index = 0;
  1234. model->request_count = request_count;
  1235. model->back_callback = back;
  1236. model->data_state = DataStateInitial;
  1237. model->data_text = NULL;
  1238. },
  1239. true);
  1240. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1241. }