callback.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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, DataLoaderModel *model)
  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 (model->fhttp->last_response != NULL)
  89. {
  90. if (strstr(model->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(model->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(model->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(model->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", model->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(FlipperHTTP *fhttp)
  609. {
  610. if (!fhttp)
  611. {
  612. FURI_LOG_E(TAG, "fhttp is NULL");
  613. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  614. return false;
  615. }
  616. // Create the directory for saving worlds
  617. char directory_path[128];
  618. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  619. // Create the directory
  620. Storage *storage = furi_record_open(RECORD_STORAGE);
  621. storage_common_mkdir(storage, directory_path);
  622. // free storage
  623. furi_record_close(RECORD_STORAGE);
  624. snprintf(
  625. fhttp->file_path,
  626. sizeof(fhttp->file_path),
  627. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  628. fhttp->save_received_data = true;
  629. return flipper_http_get_request_with_headers(fhttp, "https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  630. }
  631. static bool start_game_thread(void *context)
  632. {
  633. FlipWorldApp *app = (FlipWorldApp *)context;
  634. if (!app)
  635. {
  636. FURI_LOG_E(TAG, "app is NULL");
  637. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  638. return false;
  639. }
  640. // free game thread
  641. if (game_thread_running)
  642. {
  643. game_thread_running = false;
  644. furi_thread_flags_set(thread_id, WorkerEvtStop);
  645. furi_thread_free(thread_id);
  646. }
  647. // start game thread
  648. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  649. if (!thread)
  650. {
  651. FURI_LOG_E(TAG, "Failed to allocate game thread");
  652. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  653. return false;
  654. }
  655. furi_thread_start(thread);
  656. thread_id = furi_thread_get_id(thread);
  657. game_thread_running = true;
  658. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  659. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventPlay);
  660. return true;
  661. }
  662. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  663. {
  664. return fetch_world_list(model->fhttp);
  665. }
  666. static char *flip_world_parse_world_list(DataLoaderModel *model)
  667. {
  668. // free game thread
  669. if (game_thread_running)
  670. {
  671. game_thread_running = false;
  672. furi_thread_flags_set(thread_id, WorkerEvtStop);
  673. furi_thread_free(thread_id);
  674. }
  675. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  676. if (!start_game_thread(app))
  677. {
  678. FURI_LOG_E(TAG, "Failed to start game thread");
  679. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  680. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  681. return "Failed to start game thread";
  682. }
  683. return "Game starting... please wait :D";
  684. }
  685. static void flip_world_world_list_switch_to_view(FlipWorldApp *app)
  686. {
  687. 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);
  688. }
  689. // combine register, login, and world list fetch into one function to switch to the loader view
  690. static bool flip_world_fetch_game(DataLoaderModel *model)
  691. {
  692. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  693. if (!app)
  694. {
  695. FURI_LOG_E(TAG, "app is NULL");
  696. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  697. return false;
  698. }
  699. if (model->request_index == 0)
  700. {
  701. // login
  702. char username[64];
  703. char password[64];
  704. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  705. {
  706. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  707. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  708. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  709. return false;
  710. }
  711. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  712. {
  713. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  714. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  715. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  716. return false;
  717. }
  718. char payload[256];
  719. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  720. return flipper_http_post_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  721. }
  722. else if (model->request_index == 1)
  723. {
  724. // check if login was successful
  725. char is_logged_in[8];
  726. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  727. {
  728. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  729. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  730. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  731. return false;
  732. }
  733. if (strcmp(is_logged_in, "false") == 0 && strcmp(model->title, "Registering...") == 0)
  734. {
  735. // register
  736. char username[64];
  737. char password[64];
  738. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  739. {
  740. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  741. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  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. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  750. return false;
  751. }
  752. char payload[172];
  753. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  754. model->title = "Registering...";
  755. return flipper_http_post_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  756. }
  757. else
  758. {
  759. model->title = "Fetching World List..";
  760. return fetch_world_list(model->fhttp);
  761. }
  762. }
  763. else if (model->request_index == 2)
  764. {
  765. model->title = "Fetching World List..";
  766. return fetch_world_list(model->fhttp);
  767. }
  768. else if (model->request_index == 3)
  769. {
  770. snprintf(
  771. model->fhttp->file_path,
  772. sizeof(model->fhttp->file_path),
  773. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  774. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  775. if (!world_list)
  776. {
  777. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  778. FURI_LOG_E(TAG, "Failed to load world list");
  779. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  780. return false;
  781. }
  782. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  783. if (!first_world)
  784. {
  785. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  786. FURI_LOG_E(TAG, "Failed to get first world");
  787. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  788. furi_string_free(world_list);
  789. return false;
  790. }
  791. if (world_exists(furi_string_get_cstr(first_world)))
  792. {
  793. furi_string_free(world_list);
  794. furi_string_free(first_world);
  795. if (!start_game_thread(app))
  796. {
  797. FURI_LOG_E(TAG, "Failed to start game thread");
  798. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  799. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  800. return "Failed to start game thread";
  801. }
  802. return true;
  803. }
  804. snprintf(
  805. model->fhttp->file_path,
  806. sizeof(model->fhttp->file_path),
  807. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  808. model->fhttp->save_received_data = true;
  809. char url[128];
  810. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v2/get/world/%s/", furi_string_get_cstr(first_world));
  811. furi_string_free(world_list);
  812. furi_string_free(first_world);
  813. return flipper_http_get_request_with_headers(model->fhttp, url, "{\"Content-Type\":\"application/json\"}");
  814. }
  815. FURI_LOG_E(TAG, "Unknown request index");
  816. return false;
  817. }
  818. static char *flip_world_parse_game(DataLoaderModel *model)
  819. {
  820. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  821. if (model->request_index == 0)
  822. {
  823. if (!model->fhttp->last_response)
  824. {
  825. save_char("is_logged_in", "false");
  826. // Go back to the main menu
  827. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  828. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  829. return "Response is empty...";
  830. }
  831. // Check for successful conditions
  832. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  833. {
  834. save_char("is_logged_in", "true");
  835. model->title = "Login successful!";
  836. model->title = "Fetching World List..";
  837. return "Login successful!";
  838. }
  839. // Check if user not found
  840. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  841. {
  842. save_char("is_logged_in", "false");
  843. model->title = "Registering...";
  844. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  845. }
  846. // If not success, not found, check length conditions
  847. size_t resp_len = strlen(model->fhttp->last_response);
  848. if (resp_len == 0 || resp_len > 127)
  849. {
  850. // Empty or too long means failed login
  851. save_char("is_logged_in", "false");
  852. // Go back to the main menu
  853. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  854. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  855. return "Failed to login...";
  856. }
  857. // Handle any other unknown response as a failure
  858. save_char("is_logged_in", "false");
  859. // Go back to the main menu
  860. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  861. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  862. return "Failed to login...";
  863. }
  864. else if (model->request_index == 1)
  865. {
  866. if (strcmp(model->title, "Registering...") == 0)
  867. {
  868. // check registration response
  869. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  870. {
  871. save_char("is_logged_in", "true");
  872. char username[64];
  873. char password[64];
  874. // load the username and password, then save them
  875. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  876. {
  877. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  878. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  879. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  880. return "Failed to load Flip-Social-Username";
  881. }
  882. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  883. {
  884. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  885. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  886. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  887. return "Failed to load Flip-Social-Password";
  888. }
  889. // load wifi ssid,pass then save
  890. char ssid[64];
  891. char pass[64];
  892. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  893. {
  894. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  895. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  896. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  897. return "Failed to load WiFi-SSID";
  898. }
  899. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  900. {
  901. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  902. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  903. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  904. return "Failed to load WiFi-Password";
  905. }
  906. save_settings(ssid, pass, username, password);
  907. model->title = "Fetching World List..";
  908. return "Account created!";
  909. }
  910. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  911. {
  912. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  913. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  914. return "Please enter your credentials.";
  915. }
  916. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  917. {
  918. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  919. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  920. return "Username already exists.";
  921. }
  922. else
  923. {
  924. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  925. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  926. return "Registration failed...";
  927. }
  928. }
  929. else
  930. {
  931. if (!start_game_thread(app))
  932. {
  933. FURI_LOG_E(TAG, "Failed to start game thread");
  934. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  935. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  936. return "Failed to start game thread";
  937. }
  938. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  939. }
  940. }
  941. else if (model->request_index == 2)
  942. {
  943. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  944. }
  945. else if (model->request_index == 3)
  946. {
  947. if (!start_game_thread(app))
  948. {
  949. FURI_LOG_E(TAG, "Failed to start game thread");
  950. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  951. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  952. return "Failed to start game thread";
  953. }
  954. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  955. }
  956. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  957. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  958. return "Unknown error";
  959. }
  960. void flip_world_switch_to_view_get_game(FlipWorldApp *app)
  961. {
  962. flip_world_generic_switch_to_view(app, "Starting Game..", flip_world_fetch_game, flip_world_parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  963. }
  964. void callback_submenu_choices(void *context, uint32_t index)
  965. {
  966. FlipWorldApp *app = (FlipWorldApp *)context;
  967. if (!app)
  968. {
  969. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  970. return;
  971. }
  972. switch (index)
  973. {
  974. case FlipWorldSubmenuIndexRun:
  975. free_all_views(app, true, true);
  976. if (!is_enough_heap(60000))
  977. {
  978. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  979. return;
  980. }
  981. // check if logged in
  982. if (is_logged_in() || is_logged_in_to_flip_social())
  983. {
  984. flip_world_world_list_switch_to_view(app);
  985. }
  986. else
  987. {
  988. flip_world_switch_to_view_get_game(app);
  989. }
  990. break;
  991. case FlipWorldSubmenuIndexAbout:
  992. free_all_views(app, true, true);
  993. if (!alloc_about_view(app))
  994. {
  995. FURI_LOG_E(TAG, "Failed to allocate about view");
  996. return;
  997. }
  998. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  999. break;
  1000. case FlipWorldSubmenuIndexSettings:
  1001. free_all_views(app, true, true);
  1002. if (!alloc_submenu_settings(app))
  1003. {
  1004. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1005. return;
  1006. }
  1007. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1008. break;
  1009. case FlipWorldSubmenuIndexWiFiSettings:
  1010. free_all_views(app, true, false);
  1011. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1012. {
  1013. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1014. return;
  1015. }
  1016. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1017. break;
  1018. case FlipWorldSubmenuIndexGameSettings:
  1019. free_all_views(app, true, false);
  1020. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1021. {
  1022. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1023. return;
  1024. }
  1025. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1026. break;
  1027. case FlipWorldSubmenuIndexUserSettings:
  1028. free_all_views(app, true, false);
  1029. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1030. {
  1031. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1032. return;
  1033. }
  1034. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1035. break;
  1036. default:
  1037. break;
  1038. }
  1039. }
  1040. static void text_updated_wifi_ssid(void *context)
  1041. {
  1042. FlipWorldApp *app = (FlipWorldApp *)context;
  1043. if (!app)
  1044. {
  1045. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1046. return;
  1047. }
  1048. // store the entered text
  1049. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1050. // Ensure null-termination
  1051. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1052. // save the setting
  1053. save_char("WiFi-SSID", app->text_input_buffer);
  1054. // update the variable item text
  1055. if (app->variable_item_wifi_ssid)
  1056. {
  1057. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1058. // get value of password
  1059. char pass[64];
  1060. char username[64];
  1061. char password[64];
  1062. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1063. {
  1064. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1065. {
  1066. // save the settings
  1067. load_char("Flip-Social-Username", username, sizeof(username));
  1068. load_char("Flip-Social-Password", password, sizeof(password));
  1069. save_settings(app->text_input_buffer, pass, username, password);
  1070. // initialize the http
  1071. FlipperHTTP *fhttp = flipper_http_alloc();
  1072. if (fhttp)
  1073. {
  1074. // save the wifi if the device is connected
  1075. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1076. {
  1077. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1078. }
  1079. // free the resources
  1080. flipper_http_free(fhttp);
  1081. }
  1082. else
  1083. {
  1084. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1085. }
  1086. }
  1087. }
  1088. }
  1089. // switch to the settings view
  1090. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1091. }
  1092. static void text_updated_wifi_pass(void *context)
  1093. {
  1094. FlipWorldApp *app = (FlipWorldApp *)context;
  1095. if (!app)
  1096. {
  1097. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1098. return;
  1099. }
  1100. // store the entered text
  1101. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1102. // Ensure null-termination
  1103. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1104. // save the setting
  1105. save_char("WiFi-Password", app->text_input_buffer);
  1106. // update the variable item text
  1107. if (app->variable_item_wifi_pass)
  1108. {
  1109. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1110. }
  1111. // get value of ssid
  1112. char ssid[64];
  1113. char username[64];
  1114. char password[64];
  1115. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1116. {
  1117. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1118. {
  1119. // save the settings
  1120. load_char("Flip-Social-Username", username, sizeof(username));
  1121. load_char("Flip-Social-Password", password, sizeof(password));
  1122. save_settings(ssid, app->text_input_buffer, username, password);
  1123. // initialize the http
  1124. FlipperHTTP *fhttp = flipper_http_alloc();
  1125. if (fhttp)
  1126. {
  1127. // save the wifi if the device is connected
  1128. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1129. {
  1130. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1131. }
  1132. // free the resources
  1133. flipper_http_free(fhttp);
  1134. }
  1135. else
  1136. {
  1137. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1138. }
  1139. }
  1140. }
  1141. // switch to the settings view
  1142. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1143. }
  1144. static void text_updated_username(void *context)
  1145. {
  1146. FlipWorldApp *app = (FlipWorldApp *)context;
  1147. if (!app)
  1148. {
  1149. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1150. return;
  1151. }
  1152. // store the entered text
  1153. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1154. // Ensure null-termination
  1155. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1156. // save the setting
  1157. save_char("Flip-Social-Username", app->text_input_buffer);
  1158. // update the variable item text
  1159. if (app->variable_item_user_username)
  1160. {
  1161. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1162. }
  1163. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1164. }
  1165. static void text_updated_password(void *context)
  1166. {
  1167. FlipWorldApp *app = (FlipWorldApp *)context;
  1168. if (!app)
  1169. {
  1170. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1171. return;
  1172. }
  1173. // store the entered text
  1174. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1175. // Ensure null-termination
  1176. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1177. // save the setting
  1178. save_char("Flip-Social-Password", app->text_input_buffer);
  1179. // update the variable item text
  1180. if (app->variable_item_user_password)
  1181. {
  1182. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1183. }
  1184. // get value of username
  1185. char username[64];
  1186. char ssid[64];
  1187. char pass[64];
  1188. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1189. {
  1190. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1191. {
  1192. // save the settings
  1193. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1194. load_char("WiFi-Password", pass, sizeof(pass));
  1195. save_settings(ssid, pass, username, app->text_input_buffer);
  1196. }
  1197. }
  1198. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1199. }
  1200. static void wifi_settings_item_selected(void *context, uint32_t index)
  1201. {
  1202. FlipWorldApp *app = (FlipWorldApp *)context;
  1203. if (!app)
  1204. {
  1205. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1206. return;
  1207. }
  1208. char ssid[64];
  1209. char pass[64];
  1210. char username[64];
  1211. char password[64];
  1212. switch (index)
  1213. {
  1214. case 0: // Input SSID
  1215. free_all_views(app, false, false);
  1216. if (!alloc_text_input_view(app, "SSID"))
  1217. {
  1218. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1219. return;
  1220. }
  1221. // load SSID
  1222. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1223. {
  1224. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1225. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1226. }
  1227. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1228. break;
  1229. case 1: // Input Password
  1230. free_all_views(app, false, false);
  1231. if (!alloc_text_input_view(app, "Password"))
  1232. {
  1233. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1234. return;
  1235. }
  1236. // load password
  1237. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1238. {
  1239. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1240. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1241. }
  1242. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1243. break;
  1244. default:
  1245. FURI_LOG_E(TAG, "Unknown configuration item index");
  1246. break;
  1247. }
  1248. }
  1249. static void flip_world_game_fps_change(VariableItem *item)
  1250. {
  1251. uint8_t index = variable_item_get_current_value_index(item);
  1252. game_fps_index = index;
  1253. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1254. variable_item_set_current_value_index(item, index);
  1255. // save the fps
  1256. save_char("Game-FPS", game_fps_choices[index]);
  1257. }
  1258. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1259. {
  1260. uint8_t index = variable_item_get_current_value_index(item);
  1261. game_screen_always_on_index = index;
  1262. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1263. variable_item_set_current_value_index(item, index);
  1264. // save the screen always on
  1265. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1266. }
  1267. static void flip_world_game_sound_on_change(VariableItem *item)
  1268. {
  1269. uint8_t index = variable_item_get_current_value_index(item);
  1270. game_sound_on_index = index;
  1271. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1272. variable_item_set_current_value_index(item, index);
  1273. // save the screen always on
  1274. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1275. }
  1276. static void flip_world_game_vibration_on_change(VariableItem *item)
  1277. {
  1278. uint8_t index = variable_item_get_current_value_index(item);
  1279. game_vibration_on_index = index;
  1280. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1281. variable_item_set_current_value_index(item, index);
  1282. // save the screen always on
  1283. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1284. }
  1285. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1286. {
  1287. if (!model || !model->fhttp)
  1288. {
  1289. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1290. return false;
  1291. }
  1292. // Create the directory for saving settings
  1293. char directory_path[256];
  1294. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1295. // Create the directory
  1296. Storage *storage = furi_record_open(RECORD_STORAGE);
  1297. storage_common_mkdir(storage, directory_path);
  1298. furi_record_close(RECORD_STORAGE);
  1299. snprintf(
  1300. model->fhttp->file_path,
  1301. sizeof(model->fhttp->file_path),
  1302. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1303. model->fhttp->save_received_data = true;
  1304. return flipper_http_get_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/world/v2/get/10/", "{\"Content-Type\":\"application/json\"}");
  1305. }
  1306. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1307. {
  1308. UNUSED(model);
  1309. return "World Pack Installed";
  1310. }
  1311. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1312. {
  1313. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1314. }
  1315. static void game_settings_item_selected(void *context, uint32_t index)
  1316. {
  1317. FlipWorldApp *app = (FlipWorldApp *)context;
  1318. if (!app)
  1319. {
  1320. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1321. return;
  1322. }
  1323. switch (index)
  1324. {
  1325. case 0: // Download all world data s one huge json
  1326. flip_world_switch_to_view_get_worlds(app);
  1327. case 1: // Change FPS
  1328. break;
  1329. case 2: // Screen Always On
  1330. break;
  1331. }
  1332. }
  1333. static void user_settings_item_selected(void *context, uint32_t index)
  1334. {
  1335. FlipWorldApp *app = (FlipWorldApp *)context;
  1336. if (!app)
  1337. {
  1338. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1339. return;
  1340. }
  1341. switch (index)
  1342. {
  1343. case 0: // Username
  1344. free_all_views(app, false, false);
  1345. if (!alloc_text_input_view(app, "Username-Login"))
  1346. {
  1347. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1348. return;
  1349. }
  1350. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1351. break;
  1352. case 1: // Password
  1353. free_all_views(app, false, false);
  1354. if (!alloc_text_input_view(app, "Password-Login"))
  1355. {
  1356. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1357. return;
  1358. }
  1359. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1360. break;
  1361. }
  1362. }
  1363. static void flip_world_widget_set_text(char *message, Widget **widget)
  1364. {
  1365. if (widget == NULL)
  1366. {
  1367. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1368. DEV_CRASH();
  1369. return;
  1370. }
  1371. if (message == NULL)
  1372. {
  1373. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1374. DEV_CRASH();
  1375. return;
  1376. }
  1377. widget_reset(*widget);
  1378. uint32_t message_length = strlen(message); // Length of the message
  1379. uint32_t i = 0; // Index tracker
  1380. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1381. char *formatted_message; // Buffer to hold the final formatted message
  1382. // Allocate buffer with double the message length plus one for safety
  1383. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1384. {
  1385. return;
  1386. }
  1387. while (i < message_length)
  1388. {
  1389. uint32_t max_line_length = 31; // Maximum characters per line
  1390. uint32_t remaining_length = message_length - i; // Remaining characters
  1391. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1392. // Check for newline character within the current segment
  1393. uint32_t newline_pos = i;
  1394. bool found_newline = false;
  1395. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1396. {
  1397. if (message[newline_pos] == '\n')
  1398. {
  1399. found_newline = true;
  1400. break;
  1401. }
  1402. }
  1403. if (found_newline)
  1404. {
  1405. // If newline found, set line_length up to the newline
  1406. line_length = newline_pos - i;
  1407. }
  1408. // Temporary buffer to hold the current line
  1409. char line[32];
  1410. strncpy(line, message + i, line_length);
  1411. line[line_length] = '\0';
  1412. // If newline was found, skip it for the next iteration
  1413. if (found_newline)
  1414. {
  1415. i += line_length + 1; // +1 to skip the '\n' character
  1416. }
  1417. else
  1418. {
  1419. // Check if the line ends in the middle of a word and adjust accordingly
  1420. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1421. {
  1422. // Find the last space within the current line to avoid breaking a word
  1423. char *last_space = strrchr(line, ' ');
  1424. if (last_space != NULL)
  1425. {
  1426. // Adjust the line_length to avoid cutting the word
  1427. line_length = last_space - line;
  1428. line[line_length] = '\0'; // Null-terminate at the space
  1429. }
  1430. }
  1431. // Move the index forward by the determined line_length
  1432. i += line_length;
  1433. // Skip any spaces at the beginning of the next line
  1434. while (i < message_length && message[i] == ' ')
  1435. {
  1436. i++;
  1437. }
  1438. }
  1439. // Manually copy the fixed line into the formatted_message buffer
  1440. for (uint32_t j = 0; j < line_length; j++)
  1441. {
  1442. formatted_message[formatted_index++] = line[j];
  1443. }
  1444. // Add a newline character for line spacing
  1445. formatted_message[formatted_index++] = '\n';
  1446. }
  1447. // Null-terminate the formatted_message
  1448. formatted_message[formatted_index] = '\0';
  1449. // Add the formatted message to the widget
  1450. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1451. }
  1452. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1453. {
  1454. if (!canvas || !model)
  1455. {
  1456. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1457. return;
  1458. }
  1459. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1460. SerialState http_state = data_loader_model->fhttp->state;
  1461. DataState data_state = data_loader_model->data_state;
  1462. char *title = data_loader_model->title;
  1463. canvas_set_font(canvas, FontSecondary);
  1464. if (http_state == INACTIVE)
  1465. {
  1466. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1467. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1468. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1469. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1470. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1471. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1472. return;
  1473. }
  1474. if (data_state == DataStateError || data_state == DataStateParseError)
  1475. {
  1476. flip_world_request_error_draw(canvas, data_loader_model);
  1477. return;
  1478. }
  1479. canvas_draw_str(canvas, 0, 7, title);
  1480. canvas_draw_str(canvas, 0, 17, "Loading...");
  1481. if (data_state == DataStateInitial)
  1482. {
  1483. return;
  1484. }
  1485. if (http_state == SENDING)
  1486. {
  1487. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1488. return;
  1489. }
  1490. if (http_state == RECEIVING || data_state == DataStateRequested)
  1491. {
  1492. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1493. return;
  1494. }
  1495. if (http_state == IDLE && data_state == DataStateReceived)
  1496. {
  1497. canvas_draw_str(canvas, 0, 27, "Processing...");
  1498. return;
  1499. }
  1500. if (http_state == IDLE && data_state == DataStateParsed)
  1501. {
  1502. canvas_draw_str(canvas, 0, 27, "Processed...");
  1503. return;
  1504. }
  1505. }
  1506. static void flip_world_loader_process_callback(void *context)
  1507. {
  1508. if (context == NULL)
  1509. {
  1510. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1511. DEV_CRASH();
  1512. return;
  1513. }
  1514. FlipWorldApp *app = (FlipWorldApp *)context;
  1515. View *view = app->view_loader;
  1516. DataState current_data_state;
  1517. DataLoaderModel *loader_model = NULL;
  1518. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1519. if (!loader_model || !loader_model->fhttp)
  1520. {
  1521. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1522. DEV_CRASH();
  1523. return;
  1524. }
  1525. if (current_data_state == DataStateInitial)
  1526. {
  1527. with_view_model(
  1528. view,
  1529. DataLoaderModel * model,
  1530. {
  1531. model->data_state = DataStateRequested;
  1532. DataLoaderFetch fetch = model->fetcher;
  1533. if (fetch == NULL)
  1534. {
  1535. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1536. model->data_state = DataStateError;
  1537. return;
  1538. }
  1539. // Clear any previous responses
  1540. strncpy(model->fhttp->last_response, "", 1);
  1541. bool request_status = fetch(model);
  1542. if (!request_status)
  1543. {
  1544. model->data_state = DataStateError;
  1545. }
  1546. },
  1547. true);
  1548. }
  1549. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1550. {
  1551. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1552. {
  1553. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1554. {
  1555. FURI_LOG_DEV(TAG, "PONG received.");
  1556. }
  1557. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1558. {
  1559. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1560. }
  1561. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1562. {
  1563. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1564. }
  1565. else if (strlen(loader_model->fhttp->last_response) == 0)
  1566. {
  1567. // Still waiting on response
  1568. }
  1569. else
  1570. {
  1571. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1572. }
  1573. }
  1574. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1575. {
  1576. // continue waiting
  1577. }
  1578. else if (loader_model->fhttp->state == INACTIVE)
  1579. {
  1580. // inactive. try again
  1581. }
  1582. else if (loader_model->fhttp->state == ISSUE)
  1583. {
  1584. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1585. }
  1586. else
  1587. {
  1588. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1589. DEV_CRASH();
  1590. }
  1591. }
  1592. else if (current_data_state == DataStateReceived)
  1593. {
  1594. with_view_model(
  1595. view,
  1596. DataLoaderModel * model,
  1597. {
  1598. char *data_text;
  1599. if (model->parser == NULL)
  1600. {
  1601. data_text = NULL;
  1602. FURI_LOG_DEV(TAG, "Parser is NULL");
  1603. DEV_CRASH();
  1604. }
  1605. else
  1606. {
  1607. data_text = model->parser(model);
  1608. }
  1609. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1610. model->data_text = data_text;
  1611. if (data_text == NULL)
  1612. {
  1613. model->data_state = DataStateParseError;
  1614. }
  1615. else
  1616. {
  1617. model->data_state = DataStateParsed;
  1618. }
  1619. },
  1620. true);
  1621. }
  1622. else if (current_data_state == DataStateParsed)
  1623. {
  1624. with_view_model(
  1625. view,
  1626. DataLoaderModel * model,
  1627. {
  1628. if (++model->request_index < model->request_count)
  1629. {
  1630. model->data_state = DataStateInitial;
  1631. }
  1632. else
  1633. {
  1634. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1635. if (model->data_text != NULL)
  1636. {
  1637. free(model->data_text);
  1638. model->data_text = NULL;
  1639. }
  1640. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1641. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1642. }
  1643. },
  1644. true);
  1645. }
  1646. }
  1647. static void flip_world_loader_timer_callback(void *context)
  1648. {
  1649. if (context == NULL)
  1650. {
  1651. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1652. DEV_CRASH();
  1653. return;
  1654. }
  1655. FlipWorldApp *app = (FlipWorldApp *)context;
  1656. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1657. }
  1658. static void flip_world_loader_on_enter(void *context)
  1659. {
  1660. if (context == NULL)
  1661. {
  1662. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1663. DEV_CRASH();
  1664. return;
  1665. }
  1666. FlipWorldApp *app = (FlipWorldApp *)context;
  1667. View *view = app->view_loader;
  1668. with_view_model(
  1669. view,
  1670. DataLoaderModel * model,
  1671. {
  1672. view_set_previous_callback(view, model->back_callback);
  1673. if (model->timer == NULL)
  1674. {
  1675. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1676. }
  1677. furi_timer_start(model->timer, 250);
  1678. },
  1679. true);
  1680. }
  1681. static void flip_world_loader_on_exit(void *context)
  1682. {
  1683. if (context == NULL)
  1684. {
  1685. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1686. DEV_CRASH();
  1687. return;
  1688. }
  1689. FlipWorldApp *app = (FlipWorldApp *)context;
  1690. View *view = app->view_loader;
  1691. with_view_model(
  1692. view,
  1693. DataLoaderModel * model,
  1694. {
  1695. if (model->timer)
  1696. {
  1697. furi_timer_stop(model->timer);
  1698. }
  1699. },
  1700. false);
  1701. }
  1702. void flip_world_loader_init(View *view)
  1703. {
  1704. if (view == NULL)
  1705. {
  1706. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1707. DEV_CRASH();
  1708. return;
  1709. }
  1710. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1711. view_set_enter_callback(view, flip_world_loader_on_enter);
  1712. view_set_exit_callback(view, flip_world_loader_on_exit);
  1713. }
  1714. void flip_world_loader_free_model(View *view)
  1715. {
  1716. if (view == NULL)
  1717. {
  1718. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1719. DEV_CRASH();
  1720. return;
  1721. }
  1722. with_view_model(
  1723. view,
  1724. DataLoaderModel * model,
  1725. {
  1726. if (model->timer)
  1727. {
  1728. furi_timer_free(model->timer);
  1729. model->timer = NULL;
  1730. }
  1731. if (model->parser_context)
  1732. {
  1733. // do not free the context here, it is the app context
  1734. // free(model->parser_context);
  1735. // model->parser_context = NULL;
  1736. }
  1737. if (model->fhttp)
  1738. {
  1739. flipper_http_free(model->fhttp);
  1740. model->fhttp = NULL;
  1741. }
  1742. },
  1743. false);
  1744. }
  1745. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1746. {
  1747. if (context == NULL)
  1748. {
  1749. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1750. DEV_CRASH();
  1751. return false;
  1752. }
  1753. switch (index)
  1754. {
  1755. case FlipWorldCustomEventProcess:
  1756. flip_world_loader_process_callback(context);
  1757. return true;
  1758. default:
  1759. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1760. return false;
  1761. }
  1762. }
  1763. 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)
  1764. {
  1765. if (app == NULL)
  1766. {
  1767. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1768. DEV_CRASH();
  1769. return;
  1770. }
  1771. View *view = app->view_loader;
  1772. if (view == NULL)
  1773. {
  1774. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1775. DEV_CRASH();
  1776. return;
  1777. }
  1778. with_view_model(
  1779. view,
  1780. DataLoaderModel * model,
  1781. {
  1782. model->title = title;
  1783. model->fetcher = fetcher;
  1784. model->parser = parser;
  1785. model->request_index = 0;
  1786. model->request_count = request_count;
  1787. model->back_callback = back;
  1788. model->data_state = DataStateInitial;
  1789. model->data_text = NULL;
  1790. //
  1791. model->parser_context = app;
  1792. model->fhttp = flipper_http_alloc();
  1793. },
  1794. true);
  1795. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1796. }