callback.c 70 KB

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