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