callback.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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. // Setup game engine settings...
  29. GameEngineSettings settings = game_engine_settings_init();
  30. settings.target_fps = game_fps_choices_2[game_fps_index];
  31. settings.show_fps = game.show_fps;
  32. settings.always_backlight = strstr(game_screen_always_on_choices[game_screen_always_on_index], "Yes") != NULL;
  33. settings.frame_callback = frame_cb;
  34. settings.context = game_manager;
  35. GameEngine *engine = game_engine_alloc(settings);
  36. if (!engine)
  37. {
  38. FURI_LOG_E("Game", "Failed to allocate game engine");
  39. game_manager_free(game_manager);
  40. return -1;
  41. }
  42. game_manager_engine_set(game_manager, engine);
  43. // Allocate custom game context if needed
  44. void *game_context = NULL;
  45. if (game.context_size > 0)
  46. {
  47. game_context = malloc(game.context_size);
  48. game_manager_game_context_set(game_manager, game_context);
  49. }
  50. // Start the game
  51. game.start(game_manager, game_context);
  52. // 1) Run the engine
  53. game_engine_run(engine);
  54. // 2) Stop the game FIRST, so it can do any internal cleanup
  55. game.stop(game_context);
  56. // 3) Now free the engine
  57. game_engine_free(engine);
  58. // 4) Now free the manager
  59. game_manager_free(game_manager);
  60. // 5) Finally, free your custom context if it was allocated
  61. if (game_context)
  62. {
  63. free(game_context);
  64. }
  65. // 6) Check for leftover entities
  66. int32_t entities = entities_get_count();
  67. if (entities != 0)
  68. {
  69. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static void flip_world_request_error_draw(Canvas *canvas)
  75. {
  76. if (canvas == NULL)
  77. {
  78. FURI_LOG_E(TAG, "flip_world_request_error_draw - canvas is NULL");
  79. DEV_CRASH();
  80. return;
  81. }
  82. if (fhttp.last_response != NULL)
  83. {
  84. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  85. {
  86. canvas_clear(canvas);
  87. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  88. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  89. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  90. }
  91. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  92. {
  93. canvas_clear(canvas);
  94. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  95. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  96. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  97. }
  98. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  99. {
  100. canvas_clear(canvas);
  101. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  102. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  103. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  104. }
  105. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  106. {
  107. canvas_clear(canvas);
  108. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  109. }
  110. else
  111. {
  112. canvas_clear(canvas);
  113. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  114. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  115. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  116. }
  117. }
  118. else
  119. {
  120. canvas_clear(canvas);
  121. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  122. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  123. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  124. }
  125. }
  126. static bool alloc_about_view(void *context);
  127. static bool alloc_text_input_view(void *context, char *title);
  128. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  129. //
  130. static void wifi_settings_item_selected(void *context, uint32_t index);
  131. static void text_updated_wifi_ssid(void *context);
  132. static void text_updated_wifi_pass(void *context);
  133. static void text_updated_username(void *context);
  134. static void text_updated_password(void *context);
  135. //
  136. static void flip_world_game_fps_change(VariableItem *item);
  137. static void game_settings_item_selected(void *context, uint32_t index);
  138. static void user_settings_item_selected(void *context, uint32_t index);
  139. static void flip_world_game_screen_always_on_change(VariableItem *item);
  140. uint32_t callback_to_submenu(void *context)
  141. {
  142. UNUSED(context);
  143. return FlipWorldViewSubmenu;
  144. }
  145. static uint32_t callback_to_wifi_settings(void *context)
  146. {
  147. UNUSED(context);
  148. return FlipWorldViewVariableItemList;
  149. }
  150. static uint32_t callback_to_settings(void *context)
  151. {
  152. UNUSED(context);
  153. return FlipWorldViewSettings;
  154. }
  155. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  156. {
  157. UNUSED(model);
  158. canvas_clear(canvas);
  159. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  160. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  161. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  162. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  163. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  164. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  165. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  166. }
  167. // alloc
  168. static bool alloc_about_view(void *context)
  169. {
  170. FlipWorldApp *app = (FlipWorldApp *)context;
  171. if (!app)
  172. {
  173. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  174. return false;
  175. }
  176. if (!app->view_about)
  177. {
  178. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  179. {
  180. return false;
  181. }
  182. if (!app->view_about)
  183. {
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. static bool alloc_text_input_view(void *context, char *title)
  190. {
  191. FlipWorldApp *app = (FlipWorldApp *)context;
  192. if (!app)
  193. {
  194. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  195. return false;
  196. }
  197. if (!title)
  198. {
  199. FURI_LOG_E(TAG, "Title is NULL");
  200. return false;
  201. }
  202. app->text_input_buffer_size = 64;
  203. if (!app->text_input_buffer)
  204. {
  205. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  206. {
  207. return false;
  208. }
  209. }
  210. if (!app->text_input_temp_buffer)
  211. {
  212. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  213. {
  214. return false;
  215. }
  216. }
  217. if (!app->text_input)
  218. {
  219. if (!easy_flipper_set_uart_text_input(
  220. &app->text_input,
  221. FlipWorldViewTextInput,
  222. title,
  223. app->text_input_temp_buffer,
  224. app->text_input_buffer_size,
  225. strcmp(title, "SSID") == 0 ? text_updated_wifi_ssid : strcmp(title, "Password") == 0 ? text_updated_wifi_pass
  226. : strcmp(title, "Username-Login") == 0 ? text_updated_username
  227. : text_updated_password,
  228. callback_to_wifi_settings,
  229. &app->view_dispatcher,
  230. app))
  231. {
  232. return false;
  233. }
  234. if (!app->text_input)
  235. {
  236. return false;
  237. }
  238. char ssid[64];
  239. char pass[64];
  240. char username[64];
  241. char password[64];
  242. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  243. {
  244. if (strcmp(title, "SSID") == 0)
  245. {
  246. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  247. }
  248. else if (strcmp(title, "Password") == 0)
  249. {
  250. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  251. }
  252. else if (strcmp(title, "Username-Login") == 0)
  253. {
  254. strncpy(app->text_input_temp_buffer, username, app->text_input_buffer_size);
  255. }
  256. else if (strcmp(title, "Password-Login") == 0)
  257. {
  258. strncpy(app->text_input_temp_buffer, password, app->text_input_buffer_size);
  259. }
  260. }
  261. }
  262. return true;
  263. }
  264. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  265. {
  266. FlipWorldApp *app = (FlipWorldApp *)context;
  267. if (!app)
  268. {
  269. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  270. return false;
  271. }
  272. char ssid[64];
  273. char pass[64];
  274. char username[64];
  275. char password[64];
  276. if (!app->variable_item_list)
  277. {
  278. switch (view_id)
  279. {
  280. case FlipWorldSubmenuIndexWiFiSettings:
  281. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  282. {
  283. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  284. return false;
  285. }
  286. if (!app->variable_item_list)
  287. {
  288. FURI_LOG_E(TAG, "Variable item list is NULL");
  289. return false;
  290. }
  291. if (!app->variable_item_wifi_ssid)
  292. {
  293. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  294. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  295. }
  296. if (!app->variable_item_wifi_pass)
  297. {
  298. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  299. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  300. }
  301. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  302. {
  303. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  304. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  305. save_char("WiFi-SSID", ssid);
  306. save_char("WiFi-Password", pass);
  307. save_char("Flip-Social-Username", username);
  308. save_char("Flip-Social-Password", password);
  309. }
  310. break;
  311. case FlipWorldSubmenuIndexGameSettings:
  312. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  313. {
  314. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  315. return false;
  316. }
  317. if (!app->variable_item_list)
  318. {
  319. FURI_LOG_E(TAG, "Variable item list is NULL");
  320. return false;
  321. }
  322. if (!app->variable_item_game_download_world)
  323. {
  324. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  325. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  326. }
  327. if (!app->variable_item_game_fps)
  328. {
  329. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  330. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  331. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  332. }
  333. if (!app->variable_item_game_screen_always_on)
  334. {
  335. app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, flip_world_game_screen_always_on_change, NULL);
  336. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  337. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[1]);
  338. }
  339. char _game_fps[8];
  340. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  341. {
  342. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  343. : strcmp(_game_fps, "120") == 0 ? 2
  344. : strcmp(_game_fps, "240") == 0 ? 3
  345. : 0;
  346. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  347. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  348. }
  349. char _game_screen_always_on[8];
  350. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  351. {
  352. int index = strcmp(_game_screen_always_on, "No") == 0 ? 0 : strcmp(_game_screen_always_on, "Yes") == 0 ? 1
  353. : 0;
  354. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[index]);
  355. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  356. }
  357. break;
  358. case FlipWorldSubmenuIndexUserSettings:
  359. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  360. {
  361. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  362. return false;
  363. }
  364. if (!app->variable_item_list)
  365. {
  366. FURI_LOG_E(TAG, "Variable item list is NULL");
  367. return false;
  368. }
  369. // if logged in, show profile info, otherwise show login/register
  370. if (is_logged_in() || is_logged_in_to_flip_social())
  371. {
  372. if (!app->variable_item_user_username)
  373. {
  374. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  375. variable_item_set_current_value_text(app->variable_item_user_username, "");
  376. }
  377. if (!app->variable_item_user_password)
  378. {
  379. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  380. variable_item_set_current_value_text(app->variable_item_user_password, "");
  381. }
  382. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  383. {
  384. variable_item_set_current_value_text(app->variable_item_user_username, username);
  385. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  386. }
  387. }
  388. else
  389. {
  390. if (!app->variable_item_user_username)
  391. {
  392. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  393. variable_item_set_current_value_text(app->variable_item_user_username, "");
  394. }
  395. if (!app->variable_item_user_password)
  396. {
  397. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  398. variable_item_set_current_value_text(app->variable_item_user_password, "");
  399. }
  400. }
  401. break;
  402. }
  403. }
  404. return true;
  405. }
  406. static bool alloc_submenu_settings(void *context)
  407. {
  408. FlipWorldApp *app = (FlipWorldApp *)context;
  409. if (!app)
  410. {
  411. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  412. return false;
  413. }
  414. if (!app->submenu_settings)
  415. {
  416. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  417. {
  418. return NULL;
  419. }
  420. if (!app->submenu_settings)
  421. {
  422. return false;
  423. }
  424. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  425. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  426. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  427. }
  428. return true;
  429. }
  430. // free
  431. static void free_about_view(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->view_about)
  440. {
  441. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  442. view_free(app->view_about);
  443. app->view_about = NULL;
  444. }
  445. }
  446. static void free_text_input_view(void *context)
  447. {
  448. FlipWorldApp *app = (FlipWorldApp *)context;
  449. if (!app)
  450. {
  451. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  452. return;
  453. }
  454. if (app->text_input)
  455. {
  456. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  457. uart_text_input_free(app->text_input);
  458. app->text_input = NULL;
  459. }
  460. if (app->text_input_buffer)
  461. {
  462. free(app->text_input_buffer);
  463. app->text_input_buffer = NULL;
  464. }
  465. if (app->text_input_temp_buffer)
  466. {
  467. free(app->text_input_temp_buffer);
  468. app->text_input_temp_buffer = NULL;
  469. }
  470. }
  471. static void free_variable_item_list(void *context)
  472. {
  473. FlipWorldApp *app = (FlipWorldApp *)context;
  474. if (!app)
  475. {
  476. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  477. return;
  478. }
  479. if (app->variable_item_list)
  480. {
  481. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  482. variable_item_list_free(app->variable_item_list);
  483. app->variable_item_list = NULL;
  484. }
  485. if (app->variable_item_wifi_ssid)
  486. {
  487. free(app->variable_item_wifi_ssid);
  488. app->variable_item_wifi_ssid = NULL;
  489. }
  490. if (app->variable_item_wifi_pass)
  491. {
  492. free(app->variable_item_wifi_pass);
  493. app->variable_item_wifi_pass = NULL;
  494. }
  495. if (app->variable_item_game_fps)
  496. {
  497. free(app->variable_item_game_fps);
  498. app->variable_item_game_fps = NULL;
  499. }
  500. if (app->variable_item_game_screen_always_on)
  501. {
  502. free(app->variable_item_game_screen_always_on);
  503. app->variable_item_game_screen_always_on = NULL;
  504. }
  505. if (app->variable_item_game_download_world)
  506. {
  507. free(app->variable_item_game_download_world);
  508. app->variable_item_game_download_world = NULL;
  509. }
  510. if (app->variable_item_user_username)
  511. {
  512. free(app->variable_item_user_username);
  513. app->variable_item_user_username = NULL;
  514. }
  515. if (app->variable_item_user_password)
  516. {
  517. free(app->variable_item_user_password);
  518. app->variable_item_user_password = NULL;
  519. }
  520. }
  521. static void free_submenu_settings(void *context)
  522. {
  523. FlipWorldApp *app = (FlipWorldApp *)context;
  524. if (!app)
  525. {
  526. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  527. return;
  528. }
  529. if (app->submenu_settings)
  530. {
  531. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  532. submenu_free(app->submenu_settings);
  533. app->submenu_settings = NULL;
  534. }
  535. }
  536. static FuriThreadId thread_id;
  537. static bool game_thread_running = false;
  538. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  539. {
  540. FlipWorldApp *app = (FlipWorldApp *)context;
  541. if (!app)
  542. {
  543. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  544. return;
  545. }
  546. if (should_free_variable_item_list)
  547. {
  548. free_variable_item_list(app);
  549. }
  550. free_about_view(app);
  551. free_text_input_view(app);
  552. // free game thread
  553. if (game_thread_running)
  554. {
  555. game_thread_running = false;
  556. furi_thread_flags_set(thread_id, WorkerEvtStop);
  557. furi_thread_free(thread_id);
  558. }
  559. if (should_free_submenu_settings)
  560. free_submenu_settings(app);
  561. if (app_instance)
  562. {
  563. free(app_instance);
  564. app_instance = NULL;
  565. }
  566. }
  567. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  568. {
  569. UNUSED(model);
  570. // Create the directory for saving worlds
  571. char directory_path[128];
  572. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  573. // Create the directory
  574. Storage *storage = furi_record_open(RECORD_STORAGE);
  575. storage_common_mkdir(storage, directory_path);
  576. // free storage
  577. furi_record_close(RECORD_STORAGE);
  578. snprintf(
  579. fhttp.file_path,
  580. sizeof(fhttp.file_path),
  581. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  582. fhttp.save_received_data = true;
  583. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  584. }
  585. static char *flip_world_parse_world_list(DataLoaderModel *model)
  586. {
  587. UNUSED(model);
  588. flipper_http_deinit();
  589. // free game thread
  590. if (game_thread_running)
  591. {
  592. game_thread_running = false;
  593. furi_thread_flags_set(thread_id, WorkerEvtStop);
  594. furi_thread_free(thread_id);
  595. }
  596. if (!app_instance)
  597. {
  598. FURI_LOG_E(TAG, "app_instance is NULL");
  599. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  600. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  601. return "app_instance is NULL";
  602. }
  603. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  604. if (!thread)
  605. {
  606. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  607. FURI_LOG_E(TAG, "Failed to allocate game thread");
  608. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  609. return "Failed to allocate game thread";
  610. }
  611. furi_thread_start(thread);
  612. thread_id = furi_thread_get_id(thread);
  613. game_thread_running = true;
  614. return "Game starting... please wait :D";
  615. }
  616. static void flip_world_world_list_switch_to_view(FlipWorldApp *app)
  617. {
  618. return flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 1, callback_to_submenu, FlipWorldViewLoader);
  619. }
  620. // combine register, login, and world list fetch into one function to switch to the loader view
  621. static bool flip_world_fetch_game(DataLoaderModel *model)
  622. {
  623. if (model->request_index == 0)
  624. {
  625. // login
  626. char username[64];
  627. char password[64];
  628. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  629. {
  630. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  631. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  632. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  633. flipper_http_deinit();
  634. return false;
  635. }
  636. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  637. {
  638. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  639. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  640. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  641. flipper_http_deinit();
  642. return false;
  643. }
  644. char payload[256];
  645. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  646. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  647. }
  648. else if (model->request_index == 1)
  649. {
  650. // check if login was successful
  651. char is_logged_in[8];
  652. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  653. {
  654. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  655. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  656. flipper_http_deinit();
  657. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  658. return false;
  659. }
  660. if (strcmp(is_logged_in, "false") == 0 && strcmp(model->title, "Registering...") == 0)
  661. {
  662. // register
  663. char username[64];
  664. char password[64];
  665. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  666. {
  667. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  668. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  669. flipper_http_deinit();
  670. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  671. return false;
  672. }
  673. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  674. {
  675. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  676. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  677. flipper_http_deinit();
  678. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  679. return false;
  680. }
  681. char payload[172];
  682. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  683. model->title = "Registering...";
  684. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  685. }
  686. else
  687. {
  688. // Create the directory for saving worlds
  689. char directory_path[128];
  690. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  691. // Create the directory
  692. Storage *storage = furi_record_open(RECORD_STORAGE);
  693. storage_common_mkdir(storage, directory_path);
  694. // free storage
  695. furi_record_close(RECORD_STORAGE);
  696. snprintf(
  697. fhttp.file_path,
  698. sizeof(fhttp.file_path),
  699. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  700. model->title = "Fetching World List..";
  701. fhttp.save_received_data = true;
  702. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  703. }
  704. }
  705. else if (model->request_index == 2)
  706. {
  707. // Create the directory for saving worlds
  708. char directory_path[128];
  709. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  710. // Create the directory
  711. Storage *storage = furi_record_open(RECORD_STORAGE);
  712. storage_common_mkdir(storage, directory_path);
  713. // free storage
  714. furi_record_close(RECORD_STORAGE);
  715. snprintf(
  716. fhttp.file_path,
  717. sizeof(fhttp.file_path),
  718. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  719. model->title = "Fetching World List..";
  720. fhttp.save_received_data = true;
  721. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  722. }
  723. else if (model->request_index == 3)
  724. {
  725. snprintf(
  726. fhttp.file_path,
  727. sizeof(fhttp.file_path),
  728. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  729. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  730. if (!world_list)
  731. {
  732. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  733. FURI_LOG_E(TAG, "Failed to load world list");
  734. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  735. return false;
  736. }
  737. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  738. if (!first_world)
  739. {
  740. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  741. FURI_LOG_E(TAG, "Failed to get first world");
  742. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  743. furi_string_free(world_list);
  744. return false;
  745. }
  746. if (world_exists(furi_string_get_cstr(first_world)))
  747. {
  748. furi_string_free(world_list);
  749. furi_string_free(first_world);
  750. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  751. flipper_http_deinit();
  752. // free game thread
  753. if (game_thread_running)
  754. {
  755. game_thread_running = false;
  756. furi_thread_flags_set(thread_id, WorkerEvtStop);
  757. furi_thread_free(thread_id);
  758. }
  759. if (!app_instance)
  760. {
  761. FURI_LOG_E(TAG, "app_instance is NULL");
  762. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  763. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  764. return "app_instance is NULL";
  765. }
  766. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  767. if (!thread)
  768. {
  769. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  770. FURI_LOG_E(TAG, "Failed to allocate game thread");
  771. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  772. return false;
  773. }
  774. furi_thread_start(thread);
  775. thread_id = furi_thread_get_id(thread);
  776. game_thread_running = true;
  777. return true;
  778. }
  779. snprintf(
  780. fhttp.file_path,
  781. sizeof(fhttp.file_path),
  782. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  783. fhttp.save_received_data = true;
  784. char url[128];
  785. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v2/get/world/%s/", furi_string_get_cstr(first_world));
  786. furi_string_free(world_list);
  787. furi_string_free(first_world);
  788. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  789. }
  790. FURI_LOG_E(TAG, "Unknown request index");
  791. return false;
  792. }
  793. static char *flip_world_parse_game(DataLoaderModel *model)
  794. {
  795. if (model->request_index == 0)
  796. {
  797. if (!fhttp.last_response)
  798. {
  799. save_char("is_logged_in", "false");
  800. flipper_http_deinit();
  801. // Go back to the main menu
  802. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  803. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  804. return "Response is empty...";
  805. }
  806. // Check for successful conditions
  807. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  808. {
  809. save_char("is_logged_in", "true");
  810. model->title = "Login successful!";
  811. model->title = "Fetching World List..";
  812. return "Login successful!";
  813. }
  814. // Check if user not found
  815. if (strstr(fhttp.last_response, "User not found") != NULL)
  816. {
  817. save_char("is_logged_in", "false");
  818. model->title = "Registering...";
  819. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  820. }
  821. // If not success, not found, check length conditions
  822. size_t resp_len = strlen(fhttp.last_response);
  823. if (resp_len == 0 || resp_len > 127)
  824. {
  825. // Empty or too long means failed login
  826. flipper_http_deinit();
  827. save_char("is_logged_in", "false");
  828. // Go back to the main menu
  829. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  830. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  831. return "Failed to login...";
  832. }
  833. // Handle any other unknown response as a failure
  834. flipper_http_deinit();
  835. save_char("is_logged_in", "false");
  836. // Go back to the main menu
  837. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  838. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  839. return "Failed to login...";
  840. }
  841. else if (model->request_index == 1)
  842. {
  843. if (strcmp(model->title, "Registering...") == 0)
  844. {
  845. // check registration response
  846. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  847. {
  848. save_char("is_logged_in", "true");
  849. char username[64];
  850. char password[64];
  851. // load the username and password, then save them
  852. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  853. {
  854. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  855. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  856. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  857. flipper_http_deinit();
  858. return "Failed to load Flip-Social-Username";
  859. }
  860. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  861. {
  862. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  863. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  864. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  865. flipper_http_deinit();
  866. return "Failed to load Flip-Social-Password";
  867. }
  868. // load wifi ssid,pass then save
  869. char ssid[64];
  870. char pass[64];
  871. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  872. {
  873. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  874. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  875. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  876. flipper_http_deinit();
  877. return "Failed to load WiFi-SSID";
  878. }
  879. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  880. {
  881. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  882. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  883. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  884. flipper_http_deinit();
  885. return "Failed to load WiFi-Password";
  886. }
  887. save_settings(ssid, pass, username, password);
  888. model->title = "Fetching World List..";
  889. return "Account created!";
  890. }
  891. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  892. {
  893. flipper_http_deinit();
  894. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  895. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  896. return "Please enter your credentials.";
  897. }
  898. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  899. {
  900. flipper_http_deinit();
  901. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  902. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  903. return "Username already exists.";
  904. }
  905. else
  906. {
  907. flipper_http_deinit();
  908. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  909. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  910. return "Registration failed...";
  911. }
  912. }
  913. else
  914. {
  915. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  916. flipper_http_deinit();
  917. // free game thread
  918. if (game_thread_running)
  919. {
  920. game_thread_running = false;
  921. furi_thread_flags_set(thread_id, WorkerEvtStop);
  922. furi_thread_free(thread_id);
  923. }
  924. if (!app_instance)
  925. {
  926. FURI_LOG_E(TAG, "app_instance is NULL");
  927. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  928. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  929. return "app_instance is NULL";
  930. }
  931. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  932. if (!thread)
  933. {
  934. FURI_LOG_E(TAG, "Failed to allocate game thread");
  935. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  936. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  937. return "Failed to allocate game thread";
  938. }
  939. furi_thread_start(thread);
  940. thread_id = furi_thread_get_id(thread);
  941. game_thread_running = true;
  942. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  943. }
  944. }
  945. else if (model->request_index == 2)
  946. {
  947. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  948. }
  949. else if (model->request_index == 3)
  950. {
  951. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  952. flipper_http_deinit();
  953. // free game thread
  954. if (game_thread_running)
  955. {
  956. game_thread_running = false;
  957. furi_thread_flags_set(thread_id, WorkerEvtStop);
  958. furi_thread_free(thread_id);
  959. }
  960. if (!app_instance)
  961. {
  962. FURI_LOG_E(TAG, "app_instance is NULL");
  963. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  964. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  965. return "app_instance is NULL";
  966. }
  967. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  968. if (!thread)
  969. {
  970. FURI_LOG_E(TAG, "Failed to allocate game thread");
  971. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  972. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  973. return "Failed to allocate game thread";
  974. }
  975. furi_thread_start(thread);
  976. thread_id = furi_thread_get_id(thread);
  977. game_thread_running = true;
  978. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  979. }
  980. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  981. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  982. return "Unknown error";
  983. }
  984. void flip_world_switch_to_view_get_game(FlipWorldApp *app)
  985. {
  986. flip_world_generic_switch_to_view(app, "Starting Game..", flip_world_fetch_game, flip_world_parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  987. }
  988. void callback_submenu_choices(void *context, uint32_t index)
  989. {
  990. FlipWorldApp *app = (FlipWorldApp *)context;
  991. if (!app)
  992. {
  993. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  994. return;
  995. }
  996. switch (index)
  997. {
  998. case FlipWorldSubmenuIndexRun:
  999. free_all_views(app, true, true);
  1000. if (!flipper_http_init(flipper_http_rx_callback, app))
  1001. {
  1002. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1003. return;
  1004. }
  1005. app_instance = malloc(sizeof(FlipWorldApp));
  1006. if (!app_instance)
  1007. {
  1008. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  1009. return;
  1010. }
  1011. memcpy(app_instance, app, sizeof(FlipWorldApp));
  1012. // check if logged in
  1013. if (is_logged_in() || is_logged_in_to_flip_social())
  1014. {
  1015. size_t heap_size = memmgr_get_free_heap();
  1016. size_t total_heap_size = memmgr_get_total_heap();
  1017. FURI_LOG_I(TAG, "Heap size: %d", heap_size);
  1018. FURI_LOG_I(TAG, "Total heap size: %d", total_heap_size);
  1019. flip_world_world_list_switch_to_view(app_instance);
  1020. }
  1021. else
  1022. {
  1023. flip_world_switch_to_view_get_game(app);
  1024. }
  1025. break;
  1026. case FlipWorldSubmenuIndexAbout:
  1027. free_all_views(app, true, true);
  1028. if (!alloc_about_view(app))
  1029. {
  1030. FURI_LOG_E(TAG, "Failed to allocate about view");
  1031. return;
  1032. }
  1033. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  1034. break;
  1035. case FlipWorldSubmenuIndexSettings:
  1036. free_all_views(app, true, true);
  1037. if (!alloc_submenu_settings(app))
  1038. {
  1039. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1040. return;
  1041. }
  1042. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1043. break;
  1044. case FlipWorldSubmenuIndexWiFiSettings:
  1045. free_all_views(app, true, false);
  1046. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1047. {
  1048. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1049. return;
  1050. }
  1051. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1052. break;
  1053. case FlipWorldSubmenuIndexGameSettings:
  1054. free_all_views(app, true, false);
  1055. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1056. {
  1057. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1058. return;
  1059. }
  1060. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1061. break;
  1062. case FlipWorldSubmenuIndexUserSettings:
  1063. free_all_views(app, true, false);
  1064. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1065. {
  1066. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1067. return;
  1068. }
  1069. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1070. break;
  1071. default:
  1072. break;
  1073. }
  1074. }
  1075. static void text_updated_wifi_ssid(void *context)
  1076. {
  1077. FlipWorldApp *app = (FlipWorldApp *)context;
  1078. if (!app)
  1079. {
  1080. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1081. return;
  1082. }
  1083. // store the entered text
  1084. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1085. // Ensure null-termination
  1086. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1087. // save the setting
  1088. save_char("WiFi-SSID", app->text_input_buffer);
  1089. // update the variable item text
  1090. if (app->variable_item_wifi_ssid)
  1091. {
  1092. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1093. // get value of password
  1094. char pass[64];
  1095. char username[64];
  1096. char password[64];
  1097. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1098. {
  1099. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1100. {
  1101. // save the settings
  1102. load_char("Flip-Social-Username", username, sizeof(username));
  1103. load_char("Flip-Social-Password", password, sizeof(password));
  1104. save_settings(app->text_input_buffer, pass, username, password);
  1105. // initialize the http
  1106. if (flipper_http_init(flipper_http_rx_callback, app))
  1107. {
  1108. // save the wifi if the device is connected
  1109. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  1110. {
  1111. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1112. }
  1113. // free the resources
  1114. flipper_http_deinit();
  1115. }
  1116. else
  1117. {
  1118. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1119. }
  1120. }
  1121. }
  1122. }
  1123. // switch to the settings view
  1124. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1125. }
  1126. static void text_updated_wifi_pass(void *context)
  1127. {
  1128. FlipWorldApp *app = (FlipWorldApp *)context;
  1129. if (!app)
  1130. {
  1131. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1132. return;
  1133. }
  1134. // store the entered text
  1135. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1136. // Ensure null-termination
  1137. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1138. // save the setting
  1139. save_char("WiFi-Password", app->text_input_buffer);
  1140. // update the variable item text
  1141. if (app->variable_item_wifi_pass)
  1142. {
  1143. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1144. }
  1145. // get value of ssid
  1146. char ssid[64];
  1147. char username[64];
  1148. char password[64];
  1149. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1150. {
  1151. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1152. {
  1153. // save the settings
  1154. load_char("Flip-Social-Username", username, sizeof(username));
  1155. load_char("Flip-Social-Password", password, sizeof(password));
  1156. save_settings(ssid, app->text_input_buffer, username, password);
  1157. // initialize the http
  1158. if (flipper_http_init(flipper_http_rx_callback, app))
  1159. {
  1160. // save the wifi if the device is connected
  1161. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  1162. {
  1163. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1164. }
  1165. // free the resources
  1166. flipper_http_deinit();
  1167. }
  1168. else
  1169. {
  1170. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1171. }
  1172. }
  1173. }
  1174. // switch to the settings view
  1175. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1176. }
  1177. static void text_updated_username(void *context)
  1178. {
  1179. FlipWorldApp *app = (FlipWorldApp *)context;
  1180. if (!app)
  1181. {
  1182. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1183. return;
  1184. }
  1185. // store the entered text
  1186. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1187. // Ensure null-termination
  1188. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1189. // save the setting
  1190. save_char("Flip-Social-Username", app->text_input_buffer);
  1191. // update the variable item text
  1192. if (app->variable_item_user_username)
  1193. {
  1194. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1195. }
  1196. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1197. }
  1198. static void text_updated_password(void *context)
  1199. {
  1200. FlipWorldApp *app = (FlipWorldApp *)context;
  1201. if (!app)
  1202. {
  1203. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1204. return;
  1205. }
  1206. // store the entered text
  1207. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1208. // Ensure null-termination
  1209. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1210. // save the setting
  1211. save_char("Flip-Social-Password", app->text_input_buffer);
  1212. // update the variable item text
  1213. if (app->variable_item_user_password)
  1214. {
  1215. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1216. }
  1217. // get value of username
  1218. char username[64];
  1219. char ssid[64];
  1220. char pass[64];
  1221. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1222. {
  1223. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1224. {
  1225. // save the settings
  1226. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1227. load_char("WiFi-Password", pass, sizeof(pass));
  1228. save_settings(ssid, pass, username, app->text_input_buffer);
  1229. }
  1230. }
  1231. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1232. }
  1233. static void wifi_settings_item_selected(void *context, uint32_t index)
  1234. {
  1235. FlipWorldApp *app = (FlipWorldApp *)context;
  1236. if (!app)
  1237. {
  1238. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1239. return;
  1240. }
  1241. char ssid[64];
  1242. char pass[64];
  1243. char username[64];
  1244. char password[64];
  1245. switch (index)
  1246. {
  1247. case 0: // Input SSID
  1248. free_all_views(app, false, false);
  1249. if (!alloc_text_input_view(app, "SSID"))
  1250. {
  1251. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1252. return;
  1253. }
  1254. // load SSID
  1255. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1256. {
  1257. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1258. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1259. }
  1260. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1261. break;
  1262. case 1: // Input Password
  1263. free_all_views(app, false, false);
  1264. if (!alloc_text_input_view(app, "Password"))
  1265. {
  1266. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1267. return;
  1268. }
  1269. // load password
  1270. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1271. {
  1272. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1273. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1274. }
  1275. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1276. break;
  1277. default:
  1278. FURI_LOG_E(TAG, "Unknown configuration item index");
  1279. break;
  1280. }
  1281. }
  1282. static void flip_world_game_fps_change(VariableItem *item)
  1283. {
  1284. uint8_t index = variable_item_get_current_value_index(item);
  1285. game_fps_index = index;
  1286. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1287. // save the fps
  1288. save_char("Game-FPS", game_fps_choices[index]);
  1289. }
  1290. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1291. {
  1292. uint8_t index = variable_item_get_current_value_index(item);
  1293. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  1294. // save the screen always on
  1295. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  1296. }
  1297. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1298. {
  1299. UNUSED(model);
  1300. // Create the directory for saving settings
  1301. char directory_path[256];
  1302. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1303. // Create the directory
  1304. Storage *storage = furi_record_open(RECORD_STORAGE);
  1305. storage_common_mkdir(storage, directory_path);
  1306. furi_record_close(RECORD_STORAGE);
  1307. snprintf(
  1308. fhttp.file_path,
  1309. sizeof(fhttp.file_path),
  1310. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1311. fhttp.save_received_data = true;
  1312. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/get/10/", "{\"Content-Type\":\"application/json\"}");
  1313. }
  1314. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1315. {
  1316. UNUSED(model);
  1317. flipper_http_deinit();
  1318. return "World Pack Installed";
  1319. }
  1320. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1321. {
  1322. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1323. }
  1324. static void game_settings_item_selected(void *context, uint32_t index)
  1325. {
  1326. FlipWorldApp *app = (FlipWorldApp *)context;
  1327. if (!app)
  1328. {
  1329. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1330. return;
  1331. }
  1332. switch (index)
  1333. {
  1334. case 0: // Download all world data s one huge json
  1335. if (!flipper_http_init(flipper_http_rx_callback, app))
  1336. {
  1337. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1338. return;
  1339. }
  1340. flip_world_switch_to_view_get_worlds(app);
  1341. case 1: // Change FPS
  1342. break;
  1343. case 2: // Screen Always On
  1344. break;
  1345. }
  1346. }
  1347. static void user_settings_item_selected(void *context, uint32_t index)
  1348. {
  1349. FlipWorldApp *app = (FlipWorldApp *)context;
  1350. if (!app)
  1351. {
  1352. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1353. return;
  1354. }
  1355. switch (index)
  1356. {
  1357. case 0: // Username
  1358. free_all_views(app, false, false);
  1359. if (!alloc_text_input_view(app, "Username-Login"))
  1360. {
  1361. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1362. return;
  1363. }
  1364. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1365. break;
  1366. case 1: // Password
  1367. free_all_views(app, false, false);
  1368. if (!alloc_text_input_view(app, "Password-Login"))
  1369. {
  1370. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1371. return;
  1372. }
  1373. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1374. break;
  1375. }
  1376. }
  1377. static void flip_world_widget_set_text(char *message, Widget **widget)
  1378. {
  1379. if (widget == NULL)
  1380. {
  1381. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1382. DEV_CRASH();
  1383. return;
  1384. }
  1385. if (message == NULL)
  1386. {
  1387. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1388. DEV_CRASH();
  1389. return;
  1390. }
  1391. widget_reset(*widget);
  1392. uint32_t message_length = strlen(message); // Length of the message
  1393. uint32_t i = 0; // Index tracker
  1394. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1395. char *formatted_message; // Buffer to hold the final formatted message
  1396. // Allocate buffer with double the message length plus one for safety
  1397. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1398. {
  1399. return;
  1400. }
  1401. while (i < message_length)
  1402. {
  1403. uint32_t max_line_length = 31; // Maximum characters per line
  1404. uint32_t remaining_length = message_length - i; // Remaining characters
  1405. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1406. // Check for newline character within the current segment
  1407. uint32_t newline_pos = i;
  1408. bool found_newline = false;
  1409. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1410. {
  1411. if (message[newline_pos] == '\n')
  1412. {
  1413. found_newline = true;
  1414. break;
  1415. }
  1416. }
  1417. if (found_newline)
  1418. {
  1419. // If newline found, set line_length up to the newline
  1420. line_length = newline_pos - i;
  1421. }
  1422. // Temporary buffer to hold the current line
  1423. char line[32];
  1424. strncpy(line, message + i, line_length);
  1425. line[line_length] = '\0';
  1426. // If newline was found, skip it for the next iteration
  1427. if (found_newline)
  1428. {
  1429. i += line_length + 1; // +1 to skip the '\n' character
  1430. }
  1431. else
  1432. {
  1433. // Check if the line ends in the middle of a word and adjust accordingly
  1434. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1435. {
  1436. // Find the last space within the current line to avoid breaking a word
  1437. char *last_space = strrchr(line, ' ');
  1438. if (last_space != NULL)
  1439. {
  1440. // Adjust the line_length to avoid cutting the word
  1441. line_length = last_space - line;
  1442. line[line_length] = '\0'; // Null-terminate at the space
  1443. }
  1444. }
  1445. // Move the index forward by the determined line_length
  1446. i += line_length;
  1447. // Skip any spaces at the beginning of the next line
  1448. while (i < message_length && message[i] == ' ')
  1449. {
  1450. i++;
  1451. }
  1452. }
  1453. // Manually copy the fixed line into the formatted_message buffer
  1454. for (uint32_t j = 0; j < line_length; j++)
  1455. {
  1456. formatted_message[formatted_index++] = line[j];
  1457. }
  1458. // Add a newline character for line spacing
  1459. formatted_message[formatted_index++] = '\n';
  1460. }
  1461. // Null-terminate the formatted_message
  1462. formatted_message[formatted_index] = '\0';
  1463. // Add the formatted message to the widget
  1464. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1465. }
  1466. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1467. {
  1468. if (!canvas || !model)
  1469. {
  1470. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1471. return;
  1472. }
  1473. SerialState http_state = fhttp.state;
  1474. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1475. DataState data_state = data_loader_model->data_state;
  1476. char *title = data_loader_model->title;
  1477. canvas_set_font(canvas, FontSecondary);
  1478. if (http_state == INACTIVE)
  1479. {
  1480. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1481. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1482. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1483. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1484. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1485. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1486. return;
  1487. }
  1488. if (data_state == DataStateError || data_state == DataStateParseError)
  1489. {
  1490. flip_world_request_error_draw(canvas);
  1491. return;
  1492. }
  1493. canvas_draw_str(canvas, 0, 7, title);
  1494. canvas_draw_str(canvas, 0, 17, "Loading...");
  1495. if (data_state == DataStateInitial)
  1496. {
  1497. return;
  1498. }
  1499. if (http_state == SENDING)
  1500. {
  1501. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1502. return;
  1503. }
  1504. if (http_state == RECEIVING || data_state == DataStateRequested)
  1505. {
  1506. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1507. return;
  1508. }
  1509. if (http_state == IDLE && data_state == DataStateReceived)
  1510. {
  1511. canvas_draw_str(canvas, 0, 27, "Processing...");
  1512. return;
  1513. }
  1514. if (http_state == IDLE && data_state == DataStateParsed)
  1515. {
  1516. canvas_draw_str(canvas, 0, 27, "Processed...");
  1517. return;
  1518. }
  1519. }
  1520. static void flip_world_loader_process_callback(void *context)
  1521. {
  1522. if (context == NULL)
  1523. {
  1524. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1525. DEV_CRASH();
  1526. return;
  1527. }
  1528. FlipWorldApp *app = (FlipWorldApp *)context;
  1529. View *view = app->view_loader;
  1530. DataState current_data_state;
  1531. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1532. if (current_data_state == DataStateInitial)
  1533. {
  1534. with_view_model(
  1535. view,
  1536. DataLoaderModel * model,
  1537. {
  1538. model->data_state = DataStateRequested;
  1539. DataLoaderFetch fetch = model->fetcher;
  1540. if (fetch == NULL)
  1541. {
  1542. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1543. model->data_state = DataStateError;
  1544. return;
  1545. }
  1546. // Clear any previous responses
  1547. strncpy(fhttp.last_response, "", 1);
  1548. bool request_status = fetch(model);
  1549. if (!request_status)
  1550. {
  1551. model->data_state = DataStateError;
  1552. }
  1553. },
  1554. true);
  1555. }
  1556. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1557. {
  1558. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1559. {
  1560. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1561. {
  1562. FURI_LOG_DEV(TAG, "PONG received.");
  1563. }
  1564. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1565. {
  1566. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1567. }
  1568. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1569. {
  1570. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1571. }
  1572. else if (strlen(fhttp.last_response) == 0)
  1573. {
  1574. // Still waiting on response
  1575. }
  1576. else
  1577. {
  1578. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1579. }
  1580. }
  1581. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1582. {
  1583. // continue waiting
  1584. }
  1585. else if (fhttp.state == INACTIVE)
  1586. {
  1587. // inactive. try again
  1588. }
  1589. else if (fhttp.state == ISSUE)
  1590. {
  1591. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1592. }
  1593. else
  1594. {
  1595. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1596. DEV_CRASH();
  1597. }
  1598. }
  1599. else if (current_data_state == DataStateReceived)
  1600. {
  1601. with_view_model(
  1602. view,
  1603. DataLoaderModel * model,
  1604. {
  1605. char *data_text;
  1606. if (model->parser == NULL)
  1607. {
  1608. data_text = NULL;
  1609. FURI_LOG_DEV(TAG, "Parser is NULL");
  1610. DEV_CRASH();
  1611. }
  1612. else
  1613. {
  1614. data_text = model->parser(model);
  1615. }
  1616. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1617. model->data_text = data_text;
  1618. if (data_text == NULL)
  1619. {
  1620. model->data_state = DataStateParseError;
  1621. }
  1622. else
  1623. {
  1624. model->data_state = DataStateParsed;
  1625. }
  1626. },
  1627. true);
  1628. }
  1629. else if (current_data_state == DataStateParsed)
  1630. {
  1631. with_view_model(
  1632. view,
  1633. DataLoaderModel * model,
  1634. {
  1635. if (++model->request_index < model->request_count)
  1636. {
  1637. model->data_state = DataStateInitial;
  1638. }
  1639. else
  1640. {
  1641. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1642. if (model->data_text != NULL)
  1643. {
  1644. free(model->data_text);
  1645. model->data_text = NULL;
  1646. }
  1647. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1648. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1649. }
  1650. },
  1651. true);
  1652. }
  1653. }
  1654. static void flip_world_loader_timer_callback(void *context)
  1655. {
  1656. if (context == NULL)
  1657. {
  1658. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1659. DEV_CRASH();
  1660. return;
  1661. }
  1662. FlipWorldApp *app = (FlipWorldApp *)context;
  1663. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1664. }
  1665. static void flip_world_loader_on_enter(void *context)
  1666. {
  1667. if (context == NULL)
  1668. {
  1669. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1670. DEV_CRASH();
  1671. return;
  1672. }
  1673. FlipWorldApp *app = (FlipWorldApp *)context;
  1674. View *view = app->view_loader;
  1675. with_view_model(
  1676. view,
  1677. DataLoaderModel * model,
  1678. {
  1679. view_set_previous_callback(view, model->back_callback);
  1680. if (model->timer == NULL)
  1681. {
  1682. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1683. }
  1684. furi_timer_start(model->timer, 250);
  1685. },
  1686. true);
  1687. }
  1688. static void flip_world_loader_on_exit(void *context)
  1689. {
  1690. if (context == NULL)
  1691. {
  1692. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1693. DEV_CRASH();
  1694. return;
  1695. }
  1696. FlipWorldApp *app = (FlipWorldApp *)context;
  1697. View *view = app->view_loader;
  1698. with_view_model(
  1699. view,
  1700. DataLoaderModel * model,
  1701. {
  1702. if (model->timer)
  1703. {
  1704. furi_timer_stop(model->timer);
  1705. }
  1706. },
  1707. false);
  1708. }
  1709. void flip_world_loader_init(View *view)
  1710. {
  1711. if (view == NULL)
  1712. {
  1713. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1714. DEV_CRASH();
  1715. return;
  1716. }
  1717. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1718. view_set_enter_callback(view, flip_world_loader_on_enter);
  1719. view_set_exit_callback(view, flip_world_loader_on_exit);
  1720. }
  1721. void flip_world_loader_free_model(View *view)
  1722. {
  1723. if (view == NULL)
  1724. {
  1725. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1726. DEV_CRASH();
  1727. return;
  1728. }
  1729. with_view_model(
  1730. view,
  1731. DataLoaderModel * model,
  1732. {
  1733. if (model->timer)
  1734. {
  1735. furi_timer_free(model->timer);
  1736. model->timer = NULL;
  1737. }
  1738. if (model->parser_context)
  1739. {
  1740. free(model->parser_context);
  1741. model->parser_context = NULL;
  1742. }
  1743. },
  1744. false);
  1745. }
  1746. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1747. {
  1748. if (context == NULL)
  1749. {
  1750. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1751. DEV_CRASH();
  1752. return false;
  1753. }
  1754. switch (index)
  1755. {
  1756. case FlipWorldCustomEventProcess:
  1757. flip_world_loader_process_callback(context);
  1758. return true;
  1759. default:
  1760. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1761. return false;
  1762. }
  1763. }
  1764. 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)
  1765. {
  1766. if (app == NULL)
  1767. {
  1768. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1769. DEV_CRASH();
  1770. return;
  1771. }
  1772. View *view = app->view_loader;
  1773. if (view == NULL)
  1774. {
  1775. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1776. DEV_CRASH();
  1777. return;
  1778. }
  1779. with_view_model(
  1780. view,
  1781. DataLoaderModel * model,
  1782. {
  1783. model->title = title;
  1784. model->fetcher = fetcher;
  1785. model->parser = parser;
  1786. model->request_index = 0;
  1787. model->request_count = request_count;
  1788. model->back_callback = back;
  1789. model->data_state = DataStateInitial;
  1790. model->data_text = NULL;
  1791. },
  1792. true);
  1793. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1794. }