callback.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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);
  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 = malloc(sizeof(FlipWorldApp));
  1012. if (!app_instance)
  1013. {
  1014. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  1015. return;
  1016. }
  1017. memcpy(app_instance, app, sizeof(FlipWorldApp));
  1018. // check if logged in
  1019. if (is_logged_in() || is_logged_in_to_flip_social())
  1020. {
  1021. size_t heap_size = memmgr_get_free_heap();
  1022. size_t total_heap_size = memmgr_get_total_heap();
  1023. FURI_LOG_I(TAG, "Heap size: %d", heap_size);
  1024. FURI_LOG_I(TAG, "Total heap size: %d", total_heap_size);
  1025. flip_world_world_list_switch_to_view(app_instance);
  1026. }
  1027. else
  1028. {
  1029. flip_world_switch_to_view_get_game(app);
  1030. }
  1031. break;
  1032. case FlipWorldSubmenuIndexAbout:
  1033. free_all_views(app, true, true);
  1034. if (!alloc_about_view(app))
  1035. {
  1036. FURI_LOG_E(TAG, "Failed to allocate about view");
  1037. return;
  1038. }
  1039. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  1040. break;
  1041. case FlipWorldSubmenuIndexSettings:
  1042. free_all_views(app, true, true);
  1043. if (!alloc_submenu_settings(app))
  1044. {
  1045. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1046. return;
  1047. }
  1048. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1049. break;
  1050. case FlipWorldSubmenuIndexWiFiSettings:
  1051. free_all_views(app, true, false);
  1052. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1053. {
  1054. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1055. return;
  1056. }
  1057. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1058. break;
  1059. case FlipWorldSubmenuIndexGameSettings:
  1060. free_all_views(app, true, false);
  1061. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1062. {
  1063. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1064. return;
  1065. }
  1066. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1067. break;
  1068. case FlipWorldSubmenuIndexUserSettings:
  1069. free_all_views(app, true, false);
  1070. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1071. {
  1072. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1073. return;
  1074. }
  1075. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1076. break;
  1077. default:
  1078. break;
  1079. }
  1080. }
  1081. static void text_updated_wifi_ssid(void *context)
  1082. {
  1083. FlipWorldApp *app = (FlipWorldApp *)context;
  1084. if (!app)
  1085. {
  1086. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1087. return;
  1088. }
  1089. // store the entered text
  1090. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1091. // Ensure null-termination
  1092. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1093. // save the setting
  1094. save_char("WiFi-SSID", app->text_input_buffer);
  1095. // update the variable item text
  1096. if (app->variable_item_wifi_ssid)
  1097. {
  1098. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1099. // get value of password
  1100. char pass[64];
  1101. char username[64];
  1102. char password[64];
  1103. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1104. {
  1105. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1106. {
  1107. // save the settings
  1108. load_char("Flip-Social-Username", username, sizeof(username));
  1109. load_char("Flip-Social-Password", password, sizeof(password));
  1110. save_settings(app->text_input_buffer, pass, username, password);
  1111. // initialize the http
  1112. if (flipper_http_init(flipper_http_rx_callback, app))
  1113. {
  1114. // save the wifi if the device is connected
  1115. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  1116. {
  1117. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1118. }
  1119. // free the resources
  1120. flipper_http_deinit();
  1121. }
  1122. else
  1123. {
  1124. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1125. }
  1126. }
  1127. }
  1128. }
  1129. // switch to the settings view
  1130. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1131. }
  1132. static void text_updated_wifi_pass(void *context)
  1133. {
  1134. FlipWorldApp *app = (FlipWorldApp *)context;
  1135. if (!app)
  1136. {
  1137. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1138. return;
  1139. }
  1140. // store the entered text
  1141. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1142. // Ensure null-termination
  1143. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1144. // save the setting
  1145. save_char("WiFi-Password", app->text_input_buffer);
  1146. // update the variable item text
  1147. if (app->variable_item_wifi_pass)
  1148. {
  1149. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1150. }
  1151. // get value of ssid
  1152. char ssid[64];
  1153. char username[64];
  1154. char password[64];
  1155. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1156. {
  1157. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1158. {
  1159. // save the settings
  1160. load_char("Flip-Social-Username", username, sizeof(username));
  1161. load_char("Flip-Social-Password", password, sizeof(password));
  1162. save_settings(ssid, app->text_input_buffer, username, password);
  1163. // initialize the http
  1164. if (flipper_http_init(flipper_http_rx_callback, app))
  1165. {
  1166. // save the wifi if the device is connected
  1167. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  1168. {
  1169. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1170. }
  1171. // free the resources
  1172. flipper_http_deinit();
  1173. }
  1174. else
  1175. {
  1176. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1177. }
  1178. }
  1179. }
  1180. // switch to the settings view
  1181. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1182. }
  1183. static void text_updated_username(void *context)
  1184. {
  1185. FlipWorldApp *app = (FlipWorldApp *)context;
  1186. if (!app)
  1187. {
  1188. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1189. return;
  1190. }
  1191. // store the entered text
  1192. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1193. // Ensure null-termination
  1194. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1195. // save the setting
  1196. save_char("Flip-Social-Username", app->text_input_buffer);
  1197. // update the variable item text
  1198. if (app->variable_item_user_username)
  1199. {
  1200. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1201. }
  1202. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1203. }
  1204. static void text_updated_password(void *context)
  1205. {
  1206. FlipWorldApp *app = (FlipWorldApp *)context;
  1207. if (!app)
  1208. {
  1209. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1210. return;
  1211. }
  1212. // store the entered text
  1213. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1214. // Ensure null-termination
  1215. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1216. // save the setting
  1217. save_char("Flip-Social-Password", app->text_input_buffer);
  1218. // update the variable item text
  1219. if (app->variable_item_user_password)
  1220. {
  1221. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1222. }
  1223. // get value of username
  1224. char username[64];
  1225. char ssid[64];
  1226. char pass[64];
  1227. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1228. {
  1229. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1230. {
  1231. // save the settings
  1232. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1233. load_char("WiFi-Password", pass, sizeof(pass));
  1234. save_settings(ssid, pass, username, app->text_input_buffer);
  1235. }
  1236. }
  1237. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1238. }
  1239. static void wifi_settings_item_selected(void *context, uint32_t index)
  1240. {
  1241. FlipWorldApp *app = (FlipWorldApp *)context;
  1242. if (!app)
  1243. {
  1244. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1245. return;
  1246. }
  1247. char ssid[64];
  1248. char pass[64];
  1249. char username[64];
  1250. char password[64];
  1251. switch (index)
  1252. {
  1253. case 0: // Input SSID
  1254. free_all_views(app, false, false);
  1255. if (!alloc_text_input_view(app, "SSID"))
  1256. {
  1257. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1258. return;
  1259. }
  1260. // load SSID
  1261. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1262. {
  1263. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1264. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1265. }
  1266. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1267. break;
  1268. case 1: // Input Password
  1269. free_all_views(app, false, false);
  1270. if (!alloc_text_input_view(app, "Password"))
  1271. {
  1272. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1273. return;
  1274. }
  1275. // load password
  1276. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1277. {
  1278. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1279. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1280. }
  1281. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1282. break;
  1283. default:
  1284. FURI_LOG_E(TAG, "Unknown configuration item index");
  1285. break;
  1286. }
  1287. }
  1288. static void flip_world_game_fps_change(VariableItem *item)
  1289. {
  1290. uint8_t index = variable_item_get_current_value_index(item);
  1291. game_fps_index = index;
  1292. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1293. // save the fps
  1294. save_char("Game-FPS", game_fps_choices[index]);
  1295. }
  1296. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1297. {
  1298. uint8_t index = variable_item_get_current_value_index(item);
  1299. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  1300. // save the screen always on
  1301. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  1302. }
  1303. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1304. {
  1305. UNUSED(model);
  1306. // Create the directory for saving settings
  1307. char directory_path[256];
  1308. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1309. // Create the directory
  1310. Storage *storage = furi_record_open(RECORD_STORAGE);
  1311. storage_common_mkdir(storage, directory_path);
  1312. furi_record_close(RECORD_STORAGE);
  1313. snprintf(
  1314. fhttp.file_path,
  1315. sizeof(fhttp.file_path),
  1316. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1317. fhttp.save_received_data = true;
  1318. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/get/10/", "{\"Content-Type\":\"application/json\"}");
  1319. }
  1320. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1321. {
  1322. UNUSED(model);
  1323. flipper_http_deinit();
  1324. return "World Pack Installed";
  1325. }
  1326. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1327. {
  1328. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1329. }
  1330. static void game_settings_item_selected(void *context, uint32_t index)
  1331. {
  1332. FlipWorldApp *app = (FlipWorldApp *)context;
  1333. if (!app)
  1334. {
  1335. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1336. return;
  1337. }
  1338. switch (index)
  1339. {
  1340. case 0: // Download all world data s one huge json
  1341. if (!flipper_http_init(flipper_http_rx_callback, app))
  1342. {
  1343. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1344. return;
  1345. }
  1346. flip_world_switch_to_view_get_worlds(app);
  1347. case 1: // Change FPS
  1348. break;
  1349. case 2: // Screen Always On
  1350. break;
  1351. }
  1352. }
  1353. static void user_settings_item_selected(void *context, uint32_t index)
  1354. {
  1355. FlipWorldApp *app = (FlipWorldApp *)context;
  1356. if (!app)
  1357. {
  1358. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1359. return;
  1360. }
  1361. switch (index)
  1362. {
  1363. case 0: // Username
  1364. free_all_views(app, false, false);
  1365. if (!alloc_text_input_view(app, "Username-Login"))
  1366. {
  1367. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1368. return;
  1369. }
  1370. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1371. break;
  1372. case 1: // Password
  1373. free_all_views(app, false, false);
  1374. if (!alloc_text_input_view(app, "Password-Login"))
  1375. {
  1376. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1377. return;
  1378. }
  1379. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1380. break;
  1381. }
  1382. }
  1383. static void flip_world_widget_set_text(char *message, Widget **widget)
  1384. {
  1385. if (widget == NULL)
  1386. {
  1387. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1388. DEV_CRASH();
  1389. return;
  1390. }
  1391. if (message == NULL)
  1392. {
  1393. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1394. DEV_CRASH();
  1395. return;
  1396. }
  1397. widget_reset(*widget);
  1398. uint32_t message_length = strlen(message); // Length of the message
  1399. uint32_t i = 0; // Index tracker
  1400. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1401. char *formatted_message; // Buffer to hold the final formatted message
  1402. // Allocate buffer with double the message length plus one for safety
  1403. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1404. {
  1405. return;
  1406. }
  1407. while (i < message_length)
  1408. {
  1409. uint32_t max_line_length = 31; // Maximum characters per line
  1410. uint32_t remaining_length = message_length - i; // Remaining characters
  1411. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1412. // Check for newline character within the current segment
  1413. uint32_t newline_pos = i;
  1414. bool found_newline = false;
  1415. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1416. {
  1417. if (message[newline_pos] == '\n')
  1418. {
  1419. found_newline = true;
  1420. break;
  1421. }
  1422. }
  1423. if (found_newline)
  1424. {
  1425. // If newline found, set line_length up to the newline
  1426. line_length = newline_pos - i;
  1427. }
  1428. // Temporary buffer to hold the current line
  1429. char line[32];
  1430. strncpy(line, message + i, line_length);
  1431. line[line_length] = '\0';
  1432. // If newline was found, skip it for the next iteration
  1433. if (found_newline)
  1434. {
  1435. i += line_length + 1; // +1 to skip the '\n' character
  1436. }
  1437. else
  1438. {
  1439. // Check if the line ends in the middle of a word and adjust accordingly
  1440. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1441. {
  1442. // Find the last space within the current line to avoid breaking a word
  1443. char *last_space = strrchr(line, ' ');
  1444. if (last_space != NULL)
  1445. {
  1446. // Adjust the line_length to avoid cutting the word
  1447. line_length = last_space - line;
  1448. line[line_length] = '\0'; // Null-terminate at the space
  1449. }
  1450. }
  1451. // Move the index forward by the determined line_length
  1452. i += line_length;
  1453. // Skip any spaces at the beginning of the next line
  1454. while (i < message_length && message[i] == ' ')
  1455. {
  1456. i++;
  1457. }
  1458. }
  1459. // Manually copy the fixed line into the formatted_message buffer
  1460. for (uint32_t j = 0; j < line_length; j++)
  1461. {
  1462. formatted_message[formatted_index++] = line[j];
  1463. }
  1464. // Add a newline character for line spacing
  1465. formatted_message[formatted_index++] = '\n';
  1466. }
  1467. // Null-terminate the formatted_message
  1468. formatted_message[formatted_index] = '\0';
  1469. // Add the formatted message to the widget
  1470. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1471. }
  1472. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1473. {
  1474. if (!canvas || !model)
  1475. {
  1476. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1477. return;
  1478. }
  1479. SerialState http_state = fhttp.state;
  1480. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1481. DataState data_state = data_loader_model->data_state;
  1482. char *title = data_loader_model->title;
  1483. canvas_set_font(canvas, FontSecondary);
  1484. if (http_state == INACTIVE)
  1485. {
  1486. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1487. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1488. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1489. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1490. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1491. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1492. return;
  1493. }
  1494. if (data_state == DataStateError || data_state == DataStateParseError)
  1495. {
  1496. flip_world_request_error_draw(canvas);
  1497. return;
  1498. }
  1499. canvas_draw_str(canvas, 0, 7, title);
  1500. canvas_draw_str(canvas, 0, 17, "Loading...");
  1501. if (data_state == DataStateInitial)
  1502. {
  1503. return;
  1504. }
  1505. if (http_state == SENDING)
  1506. {
  1507. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1508. return;
  1509. }
  1510. if (http_state == RECEIVING || data_state == DataStateRequested)
  1511. {
  1512. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1513. return;
  1514. }
  1515. if (http_state == IDLE && data_state == DataStateReceived)
  1516. {
  1517. canvas_draw_str(canvas, 0, 27, "Processing...");
  1518. return;
  1519. }
  1520. if (http_state == IDLE && data_state == DataStateParsed)
  1521. {
  1522. canvas_draw_str(canvas, 0, 27, "Processed...");
  1523. return;
  1524. }
  1525. }
  1526. static void flip_world_loader_process_callback(void *context)
  1527. {
  1528. if (context == NULL)
  1529. {
  1530. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1531. DEV_CRASH();
  1532. return;
  1533. }
  1534. FlipWorldApp *app = (FlipWorldApp *)context;
  1535. View *view = app->view_loader;
  1536. DataState current_data_state;
  1537. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1538. if (current_data_state == DataStateInitial)
  1539. {
  1540. with_view_model(
  1541. view,
  1542. DataLoaderModel * model,
  1543. {
  1544. model->data_state = DataStateRequested;
  1545. DataLoaderFetch fetch = model->fetcher;
  1546. if (fetch == NULL)
  1547. {
  1548. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1549. model->data_state = DataStateError;
  1550. return;
  1551. }
  1552. // Clear any previous responses
  1553. strncpy(fhttp.last_response, "", 1);
  1554. bool request_status = fetch(model);
  1555. if (!request_status)
  1556. {
  1557. model->data_state = DataStateError;
  1558. }
  1559. },
  1560. true);
  1561. }
  1562. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1563. {
  1564. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1565. {
  1566. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1567. {
  1568. FURI_LOG_DEV(TAG, "PONG received.");
  1569. }
  1570. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1571. {
  1572. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1573. }
  1574. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1575. {
  1576. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1577. }
  1578. else if (strlen(fhttp.last_response) == 0)
  1579. {
  1580. // Still waiting on response
  1581. }
  1582. else
  1583. {
  1584. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1585. }
  1586. }
  1587. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1588. {
  1589. // continue waiting
  1590. }
  1591. else if (fhttp.state == INACTIVE)
  1592. {
  1593. // inactive. try again
  1594. }
  1595. else if (fhttp.state == ISSUE)
  1596. {
  1597. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1598. }
  1599. else
  1600. {
  1601. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1602. DEV_CRASH();
  1603. }
  1604. }
  1605. else if (current_data_state == DataStateReceived)
  1606. {
  1607. with_view_model(
  1608. view,
  1609. DataLoaderModel * model,
  1610. {
  1611. char *data_text;
  1612. if (model->parser == NULL)
  1613. {
  1614. data_text = NULL;
  1615. FURI_LOG_DEV(TAG, "Parser is NULL");
  1616. DEV_CRASH();
  1617. }
  1618. else
  1619. {
  1620. data_text = model->parser(model);
  1621. }
  1622. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1623. model->data_text = data_text;
  1624. if (data_text == NULL)
  1625. {
  1626. model->data_state = DataStateParseError;
  1627. }
  1628. else
  1629. {
  1630. model->data_state = DataStateParsed;
  1631. }
  1632. },
  1633. true);
  1634. }
  1635. else if (current_data_state == DataStateParsed)
  1636. {
  1637. with_view_model(
  1638. view,
  1639. DataLoaderModel * model,
  1640. {
  1641. if (++model->request_index < model->request_count)
  1642. {
  1643. model->data_state = DataStateInitial;
  1644. }
  1645. else
  1646. {
  1647. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1648. if (model->data_text != NULL)
  1649. {
  1650. free(model->data_text);
  1651. model->data_text = NULL;
  1652. }
  1653. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1654. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1655. }
  1656. },
  1657. true);
  1658. }
  1659. }
  1660. static void flip_world_loader_timer_callback(void *context)
  1661. {
  1662. if (context == NULL)
  1663. {
  1664. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1665. DEV_CRASH();
  1666. return;
  1667. }
  1668. FlipWorldApp *app = (FlipWorldApp *)context;
  1669. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1670. }
  1671. static void flip_world_loader_on_enter(void *context)
  1672. {
  1673. if (context == NULL)
  1674. {
  1675. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1676. DEV_CRASH();
  1677. return;
  1678. }
  1679. FlipWorldApp *app = (FlipWorldApp *)context;
  1680. View *view = app->view_loader;
  1681. with_view_model(
  1682. view,
  1683. DataLoaderModel * model,
  1684. {
  1685. view_set_previous_callback(view, model->back_callback);
  1686. if (model->timer == NULL)
  1687. {
  1688. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1689. }
  1690. furi_timer_start(model->timer, 250);
  1691. },
  1692. true);
  1693. }
  1694. static void flip_world_loader_on_exit(void *context)
  1695. {
  1696. if (context == NULL)
  1697. {
  1698. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1699. DEV_CRASH();
  1700. return;
  1701. }
  1702. FlipWorldApp *app = (FlipWorldApp *)context;
  1703. View *view = app->view_loader;
  1704. with_view_model(
  1705. view,
  1706. DataLoaderModel * model,
  1707. {
  1708. if (model->timer)
  1709. {
  1710. furi_timer_stop(model->timer);
  1711. }
  1712. },
  1713. false);
  1714. }
  1715. void flip_world_loader_init(View *view)
  1716. {
  1717. if (view == NULL)
  1718. {
  1719. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1720. DEV_CRASH();
  1721. return;
  1722. }
  1723. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1724. view_set_enter_callback(view, flip_world_loader_on_enter);
  1725. view_set_exit_callback(view, flip_world_loader_on_exit);
  1726. }
  1727. void flip_world_loader_free_model(View *view)
  1728. {
  1729. if (view == NULL)
  1730. {
  1731. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1732. DEV_CRASH();
  1733. return;
  1734. }
  1735. with_view_model(
  1736. view,
  1737. DataLoaderModel * model,
  1738. {
  1739. if (model->timer)
  1740. {
  1741. furi_timer_free(model->timer);
  1742. model->timer = NULL;
  1743. }
  1744. if (model->parser_context)
  1745. {
  1746. free(model->parser_context);
  1747. model->parser_context = NULL;
  1748. }
  1749. },
  1750. false);
  1751. }
  1752. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1753. {
  1754. if (context == NULL)
  1755. {
  1756. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1757. DEV_CRASH();
  1758. return false;
  1759. }
  1760. switch (index)
  1761. {
  1762. case FlipWorldCustomEventProcess:
  1763. flip_world_loader_process_callback(context);
  1764. return true;
  1765. default:
  1766. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1767. return false;
  1768. }
  1769. }
  1770. 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)
  1771. {
  1772. if (app == NULL)
  1773. {
  1774. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1775. DEV_CRASH();
  1776. return;
  1777. }
  1778. View *view = app->view_loader;
  1779. if (view == NULL)
  1780. {
  1781. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1782. DEV_CRASH();
  1783. return;
  1784. }
  1785. with_view_model(
  1786. view,
  1787. DataLoaderModel * model,
  1788. {
  1789. model->title = title;
  1790. model->fetcher = fetcher;
  1791. model->parser = parser;
  1792. model->request_index = 0;
  1793. model->request_count = request_count;
  1794. model->back_callback = back;
  1795. model->data_state = DataStateInitial;
  1796. model->data_text = NULL;
  1797. },
  1798. true);
  1799. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1800. }