node_controller.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../../constants/iconfont/iconfont.dart';
  4. import '../../../controllers/api_controller.dart';
  5. import '../../../controllers/core_controller.dart';
  6. import '../../../data/models/launch/groups.dart';
  7. import '../../../data/sp/ix_sp.dart';
  8. class NodeController extends GetxController {
  9. final coreController = Get.find<CoreController>();
  10. final ApiController _apiController = Get.find<ApiController>();
  11. // Groups 数据
  12. final _groups = Rxn<Groups>();
  13. Groups? get groups => _groups.value;
  14. set groups(Groups? value) => _groups.value = value;
  15. // 游戏tab列表
  16. final _tabTextList = <String>[].obs;
  17. List<String> get tabTextList => _tabTextList;
  18. set tabTextList(List<String> value) => _tabTextList.assignAll(value);
  19. // 当前选中的 Tab 索引
  20. final _currentTabIndex = 0.obs;
  21. int get currentTabIndex => _currentTabIndex.value;
  22. set currentTabIndex(int value) => _currentTabIndex.value = value;
  23. // 保存每个 tab 中每个国家的展开/收缩状态
  24. // key 格式: "tabIndex_countryCode"
  25. final Map<String, bool> expandedStates = {};
  26. // 当前选中的tab
  27. String getCurrentTab() {
  28. return tabTextList[currentTabIndex];
  29. }
  30. // 列表刷新状态
  31. final _isRefreshing = false.obs;
  32. bool get isRefreshing => _isRefreshing.value;
  33. @override
  34. void onInit() {
  35. super.onInit();
  36. _loadGroups();
  37. }
  38. /// 加载 Groups 数据
  39. void _loadGroups() {
  40. final launch = IXSP.getLaunch();
  41. if (launch?.groups != null) {
  42. groups = launch!.groups;
  43. _updateTabList();
  44. }
  45. }
  46. void updateGroups(Groups? groups) {
  47. this.groups = groups;
  48. _updateTabList();
  49. }
  50. /// 更新 Tab 列表
  51. void _updateTabList() {
  52. final tabs = <String>[];
  53. // 添加 Normal tab
  54. if (groups?.normal != null && groups!.normal!.list?.isNotEmpty == true) {
  55. tabs.add('All');
  56. }
  57. // 添加 Streaming tab
  58. if (groups?.streaming != null &&
  59. groups!.streaming!.list?.isNotEmpty == true) {
  60. tabs.add('Streaming');
  61. }
  62. tabTextList = tabs;
  63. }
  64. /// 根据 tab 索引获取对应的数据
  65. Normal? getDataByTabIndex(int tabIndex) {
  66. if (tabIndex == 0) {
  67. return groups?.normal;
  68. } else if (tabIndex == 1) {
  69. return groups?.streaming;
  70. }
  71. return null;
  72. }
  73. IconData getTabIcon(int tabIndex) {
  74. if (tabIndex == 0) {
  75. return IconFont.icon24;
  76. } else if (tabIndex == 1) {
  77. return IconFont.icon25;
  78. }
  79. return IconFont.icon24;
  80. }
  81. /// 获取国家的展开状态
  82. bool getExpandedState(int tabIndex, String countryCode) {
  83. final key = '${tabIndex}_$countryCode';
  84. return expandedStates[key] ?? false; // 默认收缩
  85. }
  86. /// 设置国家的展开状态
  87. void setExpandedState(int tabIndex, String countryCode, bool expanded) {
  88. final key = '${tabIndex}_$countryCode';
  89. expandedStates[key] = expanded;
  90. }
  91. /// 当 Launch 数据更新时刷新节点
  92. void refreshOnLaunchChanged() {
  93. _loadGroups();
  94. }
  95. // 设置tab选中状态
  96. void setTabSelected(int tabIndex) {
  97. currentTabIndex = tabIndex;
  98. coreController.locationSelectionType = tabTextList[tabIndex];
  99. }
  100. /// 主动刷新列表
  101. void refreshLocations() async {
  102. try {
  103. _isRefreshing.value = true;
  104. // 保持至少2秒的刷新过程
  105. final results = await Future.wait([
  106. _apiController.getLocations(),
  107. Future.delayed(const Duration(milliseconds: 1500)),
  108. ]);
  109. final groups = results[0] as Groups;
  110. updateGroups(groups);
  111. _isRefreshing.value = false;
  112. } catch (_) {
  113. // TODO: 捕捉异常写入日志
  114. Future.delayed(const Duration(milliseconds: 1500), () {
  115. _isRefreshing.value = false;
  116. });
  117. }
  118. }
  119. }