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