callback.c 70 KB

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