callback.c 43 KB

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