callback.c 42 KB

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