callback.c 41 KB

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