callback.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. GameEngineSettings settings = game_engine_settings_init();
  29. settings.target_fps = game_fps_choices_2[game_fps_index];
  30. settings.show_fps = game.show_fps;
  31. settings.always_backlight = strstr(game_screen_always_on_choices[game_screen_always_on_index], "Yes") != NULL;
  32. settings.frame_callback = frame_cb;
  33. settings.context = game_manager;
  34. GameEngine *engine = game_engine_alloc(settings);
  35. if (!engine)
  36. {
  37. FURI_LOG_E("Game", "Failed to allocate game engine");
  38. game_manager_free(game_manager);
  39. return -1;
  40. }
  41. game_manager_engine_set(game_manager, engine);
  42. void *game_context = NULL;
  43. if (game.context_size > 0)
  44. {
  45. game_context = malloc(game.context_size);
  46. game_manager_game_context_set(game_manager, game_context);
  47. }
  48. game.start(game_manager, game_context);
  49. game_engine_run(engine);
  50. game_engine_free(engine);
  51. game_manager_free(game_manager);
  52. game.stop(game_context);
  53. if (game_context)
  54. {
  55. free(game_context);
  56. }
  57. int32_t entities = entities_get_count();
  58. if (entities != 0)
  59. {
  60. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. static void flip_world_request_error_draw(Canvas *canvas)
  66. {
  67. if (canvas == NULL)
  68. {
  69. FURI_LOG_E(TAG, "flip_world_request_error_draw - canvas is NULL");
  70. DEV_CRASH();
  71. return;
  72. }
  73. if (fhttp.last_response != NULL)
  74. {
  75. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  76. {
  77. canvas_clear(canvas);
  78. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  79. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  80. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  81. }
  82. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  83. {
  84. canvas_clear(canvas);
  85. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  86. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  87. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  88. }
  89. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  90. {
  91. canvas_clear(canvas);
  92. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  93. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  94. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  95. }
  96. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  97. {
  98. canvas_clear(canvas);
  99. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  100. }
  101. else
  102. {
  103. canvas_clear(canvas);
  104. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  105. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  106. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  107. }
  108. }
  109. else
  110. {
  111. canvas_clear(canvas);
  112. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  113. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  114. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  115. }
  116. }
  117. static bool alloc_about_view(void *context);
  118. static bool alloc_text_input_view(void *context, char *title);
  119. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  120. //
  121. static void wifi_settings_item_selected(void *context, uint32_t index);
  122. static void text_updated_ssid(void *context);
  123. static void text_updated_pass(void *context);
  124. //
  125. static void flip_world_game_fps_change(VariableItem *item);
  126. static void game_settings_item_selected(void *context, uint32_t index);
  127. static void flip_world_game_screen_always_on_change(VariableItem *item);
  128. uint32_t callback_to_submenu(void *context)
  129. {
  130. UNUSED(context);
  131. return FlipWorldViewSubmenu;
  132. }
  133. static uint32_t callback_to_wifi_settings(void *context)
  134. {
  135. UNUSED(context);
  136. return FlipWorldViewVariableItemList;
  137. }
  138. static uint32_t callback_to_settings(void *context)
  139. {
  140. UNUSED(context);
  141. return FlipWorldViewSettings;
  142. }
  143. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  144. {
  145. UNUSED(model);
  146. canvas_clear(canvas);
  147. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  148. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  149. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  150. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  151. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  152. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  153. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  154. }
  155. // alloc
  156. static bool alloc_about_view(void *context)
  157. {
  158. FlipWorldApp *app = (FlipWorldApp *)context;
  159. if (!app)
  160. {
  161. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  162. return false;
  163. }
  164. if (!app->view_about)
  165. {
  166. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  167. {
  168. return false;
  169. }
  170. if (!app->view_about)
  171. {
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. static bool alloc_text_input_view(void *context, char *title)
  178. {
  179. FlipWorldApp *app = (FlipWorldApp *)context;
  180. if (!app)
  181. {
  182. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  183. return false;
  184. }
  185. if (!title)
  186. {
  187. FURI_LOG_E(TAG, "Title is NULL");
  188. return false;
  189. }
  190. app->text_input_buffer_size = 64;
  191. if (!app->text_input_buffer)
  192. {
  193. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  194. {
  195. return false;
  196. }
  197. }
  198. if (!app->text_input_temp_buffer)
  199. {
  200. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  201. {
  202. return false;
  203. }
  204. }
  205. if (!app->text_input)
  206. {
  207. if (!easy_flipper_set_uart_text_input(
  208. &app->text_input,
  209. FlipWorldViewTextInput,
  210. title,
  211. app->text_input_temp_buffer,
  212. app->text_input_buffer_size,
  213. strcmp(title, "SSID") == 0 ? text_updated_ssid : text_updated_pass,
  214. callback_to_wifi_settings,
  215. &app->view_dispatcher,
  216. app))
  217. {
  218. return false;
  219. }
  220. if (!app->text_input)
  221. {
  222. return false;
  223. }
  224. char ssid[64];
  225. char pass[64];
  226. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  227. {
  228. if (strcmp(title, "SSID") == 0)
  229. {
  230. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  231. }
  232. else
  233. {
  234. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  235. }
  236. }
  237. }
  238. return true;
  239. }
  240. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  241. {
  242. FlipWorldApp *app = (FlipWorldApp *)context;
  243. if (!app)
  244. {
  245. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  246. return false;
  247. }
  248. if (!app->variable_item_list)
  249. {
  250. switch (view_id)
  251. {
  252. case FlipWorldSubmenuIndexWiFiSettings:
  253. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  254. {
  255. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  256. return false;
  257. }
  258. if (!app->variable_item_list)
  259. {
  260. FURI_LOG_E(TAG, "Variable item list is NULL");
  261. return false;
  262. }
  263. if (!app->variable_item_wifi_ssid)
  264. {
  265. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  266. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  267. }
  268. if (!app->variable_item_wifi_pass)
  269. {
  270. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  271. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  272. }
  273. char ssid[64];
  274. char pass[64];
  275. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  276. {
  277. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  278. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  279. save_char("WiFi-SSID", ssid);
  280. save_char("WiFi-Password", pass);
  281. }
  282. break;
  283. case FlipWorldSubmenuIndexGameSettings:
  284. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  285. {
  286. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  287. return false;
  288. }
  289. if (!app->variable_item_list)
  290. {
  291. FURI_LOG_E(TAG, "Variable item list is NULL");
  292. return false;
  293. }
  294. if (!app->variable_item_game_download_world)
  295. {
  296. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  297. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  298. }
  299. if (!app->variable_item_game_fps)
  300. {
  301. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  302. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  303. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  304. }
  305. if (!app->variable_item_game_screen_always_on)
  306. {
  307. 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);
  308. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  309. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[1]);
  310. }
  311. char _game_fps[8];
  312. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  313. {
  314. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  315. : strcmp(_game_fps, "120") == 0 ? 2
  316. : strcmp(_game_fps, "240") == 0 ? 3
  317. : 0;
  318. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  319. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  320. }
  321. char _game_screen_always_on[8];
  322. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  323. {
  324. int index = strcmp(_game_screen_always_on, "No") == 0 ? 0 : strcmp(_game_screen_always_on, "Yes") == 0 ? 1
  325. : 0;
  326. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[index]);
  327. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  328. }
  329. break;
  330. case FlipWorldSubmenuIndexUserSettings:
  331. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, NULL, callback_to_settings, &app->view_dispatcher, app))
  332. {
  333. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  334. return false;
  335. }
  336. if (!app->variable_item_list)
  337. {
  338. FURI_LOG_E(TAG, "Variable item list is NULL");
  339. return false;
  340. }
  341. // if logged in, show profile info, otherwise show login/register
  342. if (is_logged_in_to_flip_social())
  343. {
  344. if (!app->variable_item_user_username)
  345. {
  346. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  347. variable_item_set_current_value_text(app->variable_item_user_username, "");
  348. }
  349. if (!app->variable_item_user_password)
  350. {
  351. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  352. variable_item_set_current_value_text(app->variable_item_user_password, "");
  353. }
  354. variable_item_set_current_value_text(app->variable_item_user_username, furi_string_get_cstr(flip_social_info("login_username_logged_in")));
  355. variable_item_set_current_value_text(app->variable_item_user_password, "");
  356. }
  357. else
  358. {
  359. if (!app->variable_item_user_username)
  360. {
  361. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  362. variable_item_set_current_value_text(app->variable_item_user_username, "");
  363. }
  364. if (!app->variable_item_user_password)
  365. {
  366. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  367. variable_item_set_current_value_text(app->variable_item_user_password, "");
  368. }
  369. variable_item_list_add(app->variable_item_list, "Login", 0, NULL, NULL);
  370. variable_item_list_add(app->variable_item_list, "Register", 0, NULL, NULL);
  371. }
  372. break;
  373. }
  374. }
  375. return true;
  376. }
  377. static bool alloc_submenu_settings(void *context)
  378. {
  379. FlipWorldApp *app = (FlipWorldApp *)context;
  380. if (!app)
  381. {
  382. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  383. return false;
  384. }
  385. if (!app->submenu_settings)
  386. {
  387. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  388. {
  389. return NULL;
  390. }
  391. if (!app->submenu_settings)
  392. {
  393. return false;
  394. }
  395. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  396. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  397. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  398. }
  399. return true;
  400. }
  401. // free
  402. static void free_about_view(void *context)
  403. {
  404. FlipWorldApp *app = (FlipWorldApp *)context;
  405. if (!app)
  406. {
  407. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  408. return;
  409. }
  410. if (app->view_about)
  411. {
  412. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  413. view_free(app->view_about);
  414. app->view_about = NULL;
  415. }
  416. }
  417. static void free_main_view(void *context)
  418. {
  419. FlipWorldApp *app = (FlipWorldApp *)context;
  420. if (!app)
  421. {
  422. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  423. return;
  424. }
  425. }
  426. static void free_text_input_view(void *context)
  427. {
  428. FlipWorldApp *app = (FlipWorldApp *)context;
  429. if (!app)
  430. {
  431. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  432. return;
  433. }
  434. if (app->text_input)
  435. {
  436. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  437. uart_text_input_free(app->text_input);
  438. app->text_input = NULL;
  439. }
  440. if (app->text_input_buffer)
  441. {
  442. free(app->text_input_buffer);
  443. app->text_input_buffer = NULL;
  444. }
  445. if (app->text_input_temp_buffer)
  446. {
  447. free(app->text_input_temp_buffer);
  448. app->text_input_temp_buffer = NULL;
  449. }
  450. }
  451. static void free_variable_item_list(void *context)
  452. {
  453. FlipWorldApp *app = (FlipWorldApp *)context;
  454. if (!app)
  455. {
  456. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  457. return;
  458. }
  459. if (app->variable_item_list)
  460. {
  461. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  462. variable_item_list_free(app->variable_item_list);
  463. app->variable_item_list = NULL;
  464. }
  465. if (app->variable_item_wifi_ssid)
  466. {
  467. free(app->variable_item_wifi_ssid);
  468. app->variable_item_wifi_ssid = NULL;
  469. }
  470. if (app->variable_item_wifi_pass)
  471. {
  472. free(app->variable_item_wifi_pass);
  473. app->variable_item_wifi_pass = NULL;
  474. }
  475. if (app->variable_item_game_fps)
  476. {
  477. free(app->variable_item_game_fps);
  478. app->variable_item_game_fps = NULL;
  479. }
  480. if (app->variable_item_game_screen_always_on)
  481. {
  482. free(app->variable_item_game_screen_always_on);
  483. app->variable_item_game_screen_always_on = NULL;
  484. }
  485. if (app->variable_item_game_download_world)
  486. {
  487. free(app->variable_item_game_download_world);
  488. app->variable_item_game_download_world = NULL;
  489. }
  490. if (app->variable_item_user_username)
  491. {
  492. free(app->variable_item_user_username);
  493. app->variable_item_user_username = NULL;
  494. }
  495. if (app->variable_item_user_password)
  496. {
  497. free(app->variable_item_user_password);
  498. app->variable_item_user_password = NULL;
  499. }
  500. }
  501. static void free_submenu_settings(void *context)
  502. {
  503. FlipWorldApp *app = (FlipWorldApp *)context;
  504. if (!app)
  505. {
  506. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  507. return;
  508. }
  509. if (app->submenu_settings)
  510. {
  511. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  512. submenu_free(app->submenu_settings);
  513. app->submenu_settings = NULL;
  514. }
  515. }
  516. static FlipWorldApp *app_instance = NULL;
  517. static FuriThreadId thread_id;
  518. static bool game_thread_running = false;
  519. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  520. {
  521. FlipWorldApp *app = (FlipWorldApp *)context;
  522. if (!app)
  523. {
  524. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  525. return;
  526. }
  527. if (should_free_variable_item_list)
  528. {
  529. free_variable_item_list(app);
  530. }
  531. free_about_view(app);
  532. free_main_view(app);
  533. free_text_input_view(app);
  534. // free game thread
  535. if (game_thread_running)
  536. {
  537. game_thread_running = false;
  538. furi_thread_flags_set(thread_id, WorkerEvtStop);
  539. furi_thread_free(thread_id);
  540. }
  541. if (should_free_submenu_settings)
  542. free_submenu_settings(app);
  543. if (app_instance)
  544. {
  545. free(app_instance);
  546. app_instance = NULL;
  547. }
  548. }
  549. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  550. {
  551. if (model->request_index == 0)
  552. {
  553. // Create the directory for saving worlds
  554. char directory_path[128];
  555. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  556. // Create the directory
  557. Storage *storage = furi_record_open(RECORD_STORAGE);
  558. storage_common_mkdir(storage, directory_path);
  559. // free storage
  560. furi_record_close(RECORD_STORAGE);
  561. snprintf(
  562. fhttp.file_path,
  563. sizeof(fhttp.file_path),
  564. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  565. fhttp.save_received_data = true;
  566. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  567. }
  568. else if (model->request_index == 1)
  569. {
  570. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  571. if (!world_list)
  572. {
  573. FURI_LOG_E(TAG, "Failed to load world list");
  574. return "Failed to load world list";
  575. }
  576. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  577. if (!first_world)
  578. {
  579. FURI_LOG_E(TAG, "Failed to get first world");
  580. return "Failed to get first world";
  581. }
  582. // if (world_exists(furi_string_get_cstr(first_world)))
  583. // {
  584. // furi_string_free(world_list);
  585. // furi_string_free(first_world);
  586. // FURI_LOG_I(TAG, "World already exists");
  587. // fhttp.state = IDLE;
  588. // return true;
  589. // }
  590. snprintf(
  591. fhttp.file_path,
  592. sizeof(fhttp.file_path),
  593. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  594. fhttp.save_received_data = true;
  595. char url[128];
  596. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  597. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  598. }
  599. return false;
  600. }
  601. static char *flip_world_parse_world_list(DataLoaderModel *model)
  602. {
  603. if (model->request_index == 0)
  604. {
  605. return "World List Fetched";
  606. }
  607. else if (model->request_index == 1)
  608. {
  609. flipper_http_deinit();
  610. // free game thread
  611. if (game_thread_running)
  612. {
  613. game_thread_running = false;
  614. furi_thread_flags_set(thread_id, WorkerEvtStop);
  615. furi_thread_free(thread_id);
  616. }
  617. // free_all_views(app_instance, true, true);
  618. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  619. if (!thread)
  620. {
  621. FURI_LOG_E(TAG, "Failed to allocate game thread");
  622. return "Failed to allocate game thread";
  623. }
  624. furi_thread_start(thread);
  625. thread_id = furi_thread_get_id(thread);
  626. game_thread_running = true;
  627. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  628. }
  629. return "Unknown error";
  630. }
  631. static void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
  632. {
  633. flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 2, callback_to_submenu, FlipWorldViewLoader);
  634. }
  635. static bool flip_social_login_fetch(DataLoaderModel *model)
  636. {
  637. UNUSED(model);
  638. if (model->request_index == 0)
  639. {
  640. if (!app_instance)
  641. {
  642. FURI_LOG_E(TAG, "app_instance is NULL");
  643. return false;
  644. }
  645. free_text_input_view(app_instance);
  646. if (!alloc_text_input_view(app_instance, "Username"))
  647. {
  648. FURI_LOG_E(TAG, "Failed to allocate text input view");
  649. return false;
  650. }
  651. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
  652. return true;
  653. }
  654. else if (model->request_index == 1)
  655. {
  656. if (!app_instance)
  657. {
  658. FURI_LOG_E(TAG, "app_instance is NULL");
  659. return false;
  660. }
  661. free_text_input_view(app_instance);
  662. if (!alloc_text_input_view(app_instance, "Password"))
  663. {
  664. FURI_LOG_E(TAG, "Failed to allocate text input view");
  665. return false;
  666. }
  667. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
  668. return true;
  669. }
  670. else if (model->request_index == 2)
  671. {
  672. if (!app_instance)
  673. {
  674. FURI_LOG_E(TAG, "app_instance is NULL");
  675. return false;
  676. }
  677. return true;
  678. }
  679. return false;
  680. }
  681. static char *flip_social_login_parse(DataLoaderModel *model)
  682. {
  683. if (model->request_index == 0)
  684. {
  685. return "Enter your username...";
  686. }
  687. else if (model->request_index == 1)
  688. {
  689. return "Enter your password...";
  690. }
  691. else if (model->request_index == 2)
  692. {
  693. if (!app_instance)
  694. {
  695. FURI_LOG_E(TAG, "app_instance is NULL");
  696. return "Failed to login...";
  697. }
  698. if (!fhttp.last_response)
  699. {
  700. flipper_http_deinit();
  701. return "Failed to login...";
  702. }
  703. // read response
  704. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  705. {
  706. flipper_http_deinit();
  707. save_char("is_logged_in", "true");
  708. return "Login successful!";
  709. }
  710. else if (strstr(fhttp.last_response, "User not found") != NULL)
  711. {
  712. flipper_http_deinit();
  713. return "Account not found...";
  714. }
  715. else
  716. {
  717. flipper_http_deinit();
  718. return "Failed to login...";
  719. }
  720. }
  721. return NULL;
  722. }
  723. void flip_social_login_switch_to_view(FlipWorldApp *app)
  724. {
  725. flip_world_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 3, callback_to_settings, FlipWorldViewTextInput);
  726. }
  727. void callback_submenu_choices(void *context, uint32_t index)
  728. {
  729. FlipWorldApp *app = (FlipWorldApp *)context;
  730. if (!app)
  731. {
  732. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  733. return;
  734. }
  735. switch (index)
  736. {
  737. case FlipWorldSubmenuIndexRun:
  738. if (!flipper_http_init(flipper_http_rx_callback, app))
  739. {
  740. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  741. return;
  742. }
  743. app_instance = malloc(sizeof(FlipWorldApp));
  744. if (!app_instance)
  745. {
  746. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  747. return;
  748. }
  749. memcpy(app_instance, app, sizeof(FlipWorldApp));
  750. flip_world_switch_to_view_get_world_list(app);
  751. break;
  752. case FlipWorldSubmenuIndexAbout:
  753. free_all_views(app, true, true);
  754. if (!alloc_about_view(app))
  755. {
  756. FURI_LOG_E(TAG, "Failed to allocate about view");
  757. return;
  758. }
  759. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  760. break;
  761. case FlipWorldSubmenuIndexSettings:
  762. free_all_views(app, true, true);
  763. if (!alloc_submenu_settings(app))
  764. {
  765. FURI_LOG_E(TAG, "Failed to allocate settings view");
  766. return;
  767. }
  768. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  769. break;
  770. case FlipWorldSubmenuIndexWiFiSettings:
  771. free_all_views(app, true, false);
  772. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  773. {
  774. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  775. return;
  776. }
  777. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  778. break;
  779. case FlipWorldSubmenuIndexGameSettings:
  780. free_all_views(app, true, false);
  781. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  782. {
  783. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  784. return;
  785. }
  786. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  787. break;
  788. case FlipWorldSubmenuIndexUserSettings:
  789. free_all_views(app, true, false);
  790. if (!flipper_http_init(flipper_http_rx_callback, app))
  791. {
  792. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  793. return;
  794. }
  795. // free_text_input_view(app_instance);
  796. // if (!alloc_text_input_view(app_instance, "Username"))
  797. // {
  798. // FURI_LOG_E(TAG, "Failed to allocate text input view");
  799. // return false;
  800. // }
  801. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
  802. easy_flipper_dialog("User Settings", "Coming soon...");
  803. break;
  804. default:
  805. break;
  806. }
  807. }
  808. static void text_updated_ssid(void *context)
  809. {
  810. FlipWorldApp *app = (FlipWorldApp *)context;
  811. if (!app)
  812. {
  813. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  814. return;
  815. }
  816. // store the entered text
  817. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  818. // Ensure null-termination
  819. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  820. // save the setting
  821. save_char("WiFi-SSID", app->text_input_buffer);
  822. // update the variable item text
  823. if (app->variable_item_wifi_ssid)
  824. {
  825. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  826. // get value of password
  827. char pass[64];
  828. if (load_char("WiFi-Password", pass, sizeof(pass)))
  829. {
  830. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  831. {
  832. // save the settings
  833. save_settings(app->text_input_buffer, pass);
  834. // initialize the http
  835. if (flipper_http_init(flipper_http_rx_callback, app))
  836. {
  837. // save the wifi if the device is connected
  838. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  839. {
  840. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  841. }
  842. // free the resources
  843. flipper_http_deinit();
  844. }
  845. else
  846. {
  847. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  848. }
  849. }
  850. }
  851. }
  852. // switch to the settings view
  853. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  854. }
  855. static void text_updated_pass(void *context)
  856. {
  857. FlipWorldApp *app = (FlipWorldApp *)context;
  858. if (!app)
  859. {
  860. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  861. return;
  862. }
  863. // store the entered text
  864. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  865. // Ensure null-termination
  866. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  867. // save the setting
  868. save_char("WiFi-Password", app->text_input_buffer);
  869. // update the variable item text
  870. if (app->variable_item_wifi_pass)
  871. {
  872. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  873. }
  874. // get value of ssid
  875. char ssid[64];
  876. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  877. {
  878. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  879. {
  880. // save the settings
  881. save_settings(ssid, app->text_input_buffer);
  882. // initialize the http
  883. if (flipper_http_init(flipper_http_rx_callback, app))
  884. {
  885. // save the wifi if the device is connected
  886. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  887. {
  888. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  889. }
  890. // free the resources
  891. flipper_http_deinit();
  892. }
  893. else
  894. {
  895. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  896. }
  897. }
  898. }
  899. // switch to the settings view
  900. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  901. }
  902. static void wifi_settings_item_selected(void *context, uint32_t index)
  903. {
  904. FlipWorldApp *app = (FlipWorldApp *)context;
  905. if (!app)
  906. {
  907. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  908. return;
  909. }
  910. char ssid[64];
  911. char pass[64];
  912. switch (index)
  913. {
  914. case 0: // Input SSID
  915. free_all_views(app, false, false);
  916. if (!alloc_text_input_view(app, "SSID"))
  917. {
  918. FURI_LOG_E(TAG, "Failed to allocate text input view");
  919. return;
  920. }
  921. // load SSID
  922. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  923. {
  924. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  925. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  926. }
  927. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  928. break;
  929. case 1: // Input Password
  930. free_all_views(app, false, false);
  931. if (!alloc_text_input_view(app, "Password"))
  932. {
  933. FURI_LOG_E(TAG, "Failed to allocate text input view");
  934. return;
  935. }
  936. // load password
  937. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  938. {
  939. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  940. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  941. }
  942. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  943. break;
  944. default:
  945. FURI_LOG_E(TAG, "Unknown configuration item index");
  946. break;
  947. }
  948. }
  949. static void flip_world_game_fps_change(VariableItem *item)
  950. {
  951. uint8_t index = variable_item_get_current_value_index(item);
  952. game_fps_index = index;
  953. variable_item_set_current_value_text(item, game_fps_choices[index]);
  954. // save the fps
  955. save_char("Game-FPS", game_fps_choices[index]);
  956. }
  957. static void flip_world_game_screen_always_on_change(VariableItem *item)
  958. {
  959. uint8_t index = variable_item_get_current_value_index(item);
  960. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  961. // save the screen always on
  962. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  963. }
  964. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  965. {
  966. UNUSED(model);
  967. snprintf(
  968. fhttp.file_path,
  969. sizeof(fhttp.file_path),
  970. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  971. fhttp.save_received_data = true;
  972. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  973. }
  974. static char *flip_world_parse_worlds(DataLoaderModel *model)
  975. {
  976. UNUSED(model);
  977. flipper_http_deinit();
  978. return "World Pack Installed";
  979. }
  980. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  981. {
  982. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  983. }
  984. static void game_settings_item_selected(void *context, uint32_t index)
  985. {
  986. FlipWorldApp *app = (FlipWorldApp *)context;
  987. if (!app)
  988. {
  989. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  990. return;
  991. }
  992. switch (index)
  993. {
  994. case 0: // Download Worlds
  995. if (!flipper_http_init(flipper_http_rx_callback, app))
  996. {
  997. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  998. return;
  999. }
  1000. flip_world_switch_to_view_get_worlds(app);
  1001. case 1: // Change FPS
  1002. break;
  1003. case 2: // Screen Always On
  1004. break;
  1005. }
  1006. }
  1007. static void flip_world_widget_set_text(char *message, Widget **widget)
  1008. {
  1009. if (widget == NULL)
  1010. {
  1011. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1012. DEV_CRASH();
  1013. return;
  1014. }
  1015. if (message == NULL)
  1016. {
  1017. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1018. DEV_CRASH();
  1019. return;
  1020. }
  1021. widget_reset(*widget);
  1022. uint32_t message_length = strlen(message); // Length of the message
  1023. uint32_t i = 0; // Index tracker
  1024. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1025. char *formatted_message; // Buffer to hold the final formatted message
  1026. // Allocate buffer with double the message length plus one for safety
  1027. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1028. {
  1029. return;
  1030. }
  1031. while (i < message_length)
  1032. {
  1033. uint32_t max_line_length = 31; // Maximum characters per line
  1034. uint32_t remaining_length = message_length - i; // Remaining characters
  1035. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1036. // Check for newline character within the current segment
  1037. uint32_t newline_pos = i;
  1038. bool found_newline = false;
  1039. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1040. {
  1041. if (message[newline_pos] == '\n')
  1042. {
  1043. found_newline = true;
  1044. break;
  1045. }
  1046. }
  1047. if (found_newline)
  1048. {
  1049. // If newline found, set line_length up to the newline
  1050. line_length = newline_pos - i;
  1051. }
  1052. // Temporary buffer to hold the current line
  1053. char line[32];
  1054. strncpy(line, message + i, line_length);
  1055. line[line_length] = '\0';
  1056. // If newline was found, skip it for the next iteration
  1057. if (found_newline)
  1058. {
  1059. i += line_length + 1; // +1 to skip the '\n' character
  1060. }
  1061. else
  1062. {
  1063. // Check if the line ends in the middle of a word and adjust accordingly
  1064. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1065. {
  1066. // Find the last space within the current line to avoid breaking a word
  1067. char *last_space = strrchr(line, ' ');
  1068. if (last_space != NULL)
  1069. {
  1070. // Adjust the line_length to avoid cutting the word
  1071. line_length = last_space - line;
  1072. line[line_length] = '\0'; // Null-terminate at the space
  1073. }
  1074. }
  1075. // Move the index forward by the determined line_length
  1076. i += line_length;
  1077. // Skip any spaces at the beginning of the next line
  1078. while (i < message_length && message[i] == ' ')
  1079. {
  1080. i++;
  1081. }
  1082. }
  1083. // Manually copy the fixed line into the formatted_message buffer
  1084. for (uint32_t j = 0; j < line_length; j++)
  1085. {
  1086. formatted_message[formatted_index++] = line[j];
  1087. }
  1088. // Add a newline character for line spacing
  1089. formatted_message[formatted_index++] = '\n';
  1090. }
  1091. // Null-terminate the formatted_message
  1092. formatted_message[formatted_index] = '\0';
  1093. // Add the formatted message to the widget
  1094. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1095. }
  1096. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1097. {
  1098. if (!canvas || !model)
  1099. {
  1100. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1101. return;
  1102. }
  1103. SerialState http_state = fhttp.state;
  1104. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1105. DataState data_state = data_loader_model->data_state;
  1106. char *title = data_loader_model->title;
  1107. canvas_set_font(canvas, FontSecondary);
  1108. if (http_state == INACTIVE)
  1109. {
  1110. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1111. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1112. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1113. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1114. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1115. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1116. return;
  1117. }
  1118. if (data_state == DataStateError || data_state == DataStateParseError)
  1119. {
  1120. flip_world_request_error_draw(canvas);
  1121. return;
  1122. }
  1123. canvas_draw_str(canvas, 0, 7, title);
  1124. canvas_draw_str(canvas, 0, 17, "Loading...");
  1125. if (data_state == DataStateInitial)
  1126. {
  1127. return;
  1128. }
  1129. if (http_state == SENDING)
  1130. {
  1131. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1132. return;
  1133. }
  1134. if (http_state == RECEIVING || data_state == DataStateRequested)
  1135. {
  1136. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1137. return;
  1138. }
  1139. if (http_state == IDLE && data_state == DataStateReceived)
  1140. {
  1141. canvas_draw_str(canvas, 0, 27, "Processing...");
  1142. return;
  1143. }
  1144. if (http_state == IDLE && data_state == DataStateParsed)
  1145. {
  1146. canvas_draw_str(canvas, 0, 27, "Processed...");
  1147. return;
  1148. }
  1149. }
  1150. static void flip_world_loader_process_callback(void *context)
  1151. {
  1152. if (context == NULL)
  1153. {
  1154. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1155. DEV_CRASH();
  1156. return;
  1157. }
  1158. FlipWorldApp *app = (FlipWorldApp *)context;
  1159. View *view = app->view_loader;
  1160. DataState current_data_state;
  1161. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1162. if (current_data_state == DataStateInitial)
  1163. {
  1164. with_view_model(
  1165. view,
  1166. DataLoaderModel * model,
  1167. {
  1168. model->data_state = DataStateRequested;
  1169. DataLoaderFetch fetch = model->fetcher;
  1170. if (fetch == NULL)
  1171. {
  1172. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1173. model->data_state = DataStateError;
  1174. return;
  1175. }
  1176. // Clear any previous responses
  1177. strncpy(fhttp.last_response, "", 1);
  1178. bool request_status = fetch(model);
  1179. if (!request_status)
  1180. {
  1181. model->data_state = DataStateError;
  1182. }
  1183. },
  1184. true);
  1185. }
  1186. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1187. {
  1188. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1189. {
  1190. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1191. {
  1192. FURI_LOG_DEV(TAG, "PONG received.");
  1193. }
  1194. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1195. {
  1196. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1197. }
  1198. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1199. {
  1200. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1201. }
  1202. else if (strlen(fhttp.last_response) == 0)
  1203. {
  1204. // Still waiting on response
  1205. }
  1206. else
  1207. {
  1208. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1209. }
  1210. }
  1211. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1212. {
  1213. // continue waiting
  1214. }
  1215. else if (fhttp.state == INACTIVE)
  1216. {
  1217. // inactive. try again
  1218. }
  1219. else if (fhttp.state == ISSUE)
  1220. {
  1221. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1222. }
  1223. else
  1224. {
  1225. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1226. DEV_CRASH();
  1227. }
  1228. }
  1229. else if (current_data_state == DataStateReceived)
  1230. {
  1231. with_view_model(
  1232. view,
  1233. DataLoaderModel * model,
  1234. {
  1235. char *data_text;
  1236. if (model->parser == NULL)
  1237. {
  1238. data_text = NULL;
  1239. FURI_LOG_DEV(TAG, "Parser is NULL");
  1240. DEV_CRASH();
  1241. }
  1242. else
  1243. {
  1244. data_text = model->parser(model);
  1245. }
  1246. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1247. model->data_text = data_text;
  1248. if (data_text == NULL)
  1249. {
  1250. model->data_state = DataStateParseError;
  1251. }
  1252. else
  1253. {
  1254. model->data_state = DataStateParsed;
  1255. }
  1256. },
  1257. true);
  1258. }
  1259. else if (current_data_state == DataStateParsed)
  1260. {
  1261. with_view_model(
  1262. view,
  1263. DataLoaderModel * model,
  1264. {
  1265. if (++model->request_index < model->request_count)
  1266. {
  1267. model->data_state = DataStateInitial;
  1268. }
  1269. else
  1270. {
  1271. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1272. if (model->data_text != NULL)
  1273. {
  1274. free(model->data_text);
  1275. model->data_text = NULL;
  1276. }
  1277. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1278. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1279. }
  1280. },
  1281. true);
  1282. }
  1283. }
  1284. static void flip_world_loader_timer_callback(void *context)
  1285. {
  1286. if (context == NULL)
  1287. {
  1288. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1289. DEV_CRASH();
  1290. return;
  1291. }
  1292. FlipWorldApp *app = (FlipWorldApp *)context;
  1293. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1294. }
  1295. static void flip_world_loader_on_enter(void *context)
  1296. {
  1297. if (context == NULL)
  1298. {
  1299. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1300. DEV_CRASH();
  1301. return;
  1302. }
  1303. FlipWorldApp *app = (FlipWorldApp *)context;
  1304. View *view = app->view_loader;
  1305. with_view_model(
  1306. view,
  1307. DataLoaderModel * model,
  1308. {
  1309. view_set_previous_callback(view, model->back_callback);
  1310. if (model->timer == NULL)
  1311. {
  1312. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1313. }
  1314. furi_timer_start(model->timer, 250);
  1315. },
  1316. true);
  1317. }
  1318. static void flip_world_loader_on_exit(void *context)
  1319. {
  1320. if (context == NULL)
  1321. {
  1322. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1323. DEV_CRASH();
  1324. return;
  1325. }
  1326. FlipWorldApp *app = (FlipWorldApp *)context;
  1327. View *view = app->view_loader;
  1328. with_view_model(
  1329. view,
  1330. DataLoaderModel * model,
  1331. {
  1332. if (model->timer)
  1333. {
  1334. furi_timer_stop(model->timer);
  1335. }
  1336. },
  1337. false);
  1338. }
  1339. void flip_world_loader_init(View *view)
  1340. {
  1341. if (view == NULL)
  1342. {
  1343. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1344. DEV_CRASH();
  1345. return;
  1346. }
  1347. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1348. view_set_enter_callback(view, flip_world_loader_on_enter);
  1349. view_set_exit_callback(view, flip_world_loader_on_exit);
  1350. }
  1351. void flip_world_loader_free_model(View *view)
  1352. {
  1353. if (view == NULL)
  1354. {
  1355. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1356. DEV_CRASH();
  1357. return;
  1358. }
  1359. with_view_model(
  1360. view,
  1361. DataLoaderModel * model,
  1362. {
  1363. if (model->timer)
  1364. {
  1365. furi_timer_free(model->timer);
  1366. model->timer = NULL;
  1367. }
  1368. if (model->parser_context)
  1369. {
  1370. free(model->parser_context);
  1371. model->parser_context = NULL;
  1372. }
  1373. },
  1374. false);
  1375. }
  1376. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1377. {
  1378. if (context == NULL)
  1379. {
  1380. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1381. DEV_CRASH();
  1382. return false;
  1383. }
  1384. switch (index)
  1385. {
  1386. case FlipWorldCustomEventProcess:
  1387. flip_world_loader_process_callback(context);
  1388. return true;
  1389. default:
  1390. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1391. return false;
  1392. }
  1393. }
  1394. 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)
  1395. {
  1396. if (app == NULL)
  1397. {
  1398. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1399. DEV_CRASH();
  1400. return;
  1401. }
  1402. View *view = app->view_loader;
  1403. if (view == NULL)
  1404. {
  1405. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1406. DEV_CRASH();
  1407. return;
  1408. }
  1409. with_view_model(
  1410. view,
  1411. DataLoaderModel * model,
  1412. {
  1413. model->title = title;
  1414. model->fetcher = fetcher;
  1415. model->parser = parser;
  1416. model->request_index = 0;
  1417. model->request_count = request_count;
  1418. model->back_callback = back;
  1419. model->data_state = DataStateInitial;
  1420. model->data_text = NULL;
  1421. },
  1422. true);
  1423. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1424. }