home_controller.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import 'package:get/get.dart';
  2. import 'package:nomo/app/controllers/api_controller.dart';
  3. import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
  4. import '../../../controllers/base_core_api.dart';
  5. import '../../../../utils/awesome_notifications_helper.dart';
  6. import '../../../../utils/log/logger.dart';
  7. import '../../../base/base_controller.dart';
  8. import '../../../constants/enums.dart';
  9. import '../../../controllers/core_controller.dart';
  10. import '../../../data/models/launch/groups.dart';
  11. import '../../../data/sp/ix_sp.dart';
  12. /// 主页控制器
  13. class HomeController extends BaseController {
  14. final coreController = Get.find<CoreController>();
  15. final apiController = Get.find<ApiController>();
  16. final TAG = 'HomeController';
  17. final _refreshController = RefreshController(initialRefresh: false);
  18. RefreshController get refreshController => _refreshController;
  19. final _isOn = false.obs;
  20. bool get isOn => _isOn.value;
  21. set isOn(bool value) => _isOn.value = value;
  22. final _currentIndex = 0.obs;
  23. int get currentIndex => _currentIndex.value;
  24. set currentIndex(int value) => _currentIndex.value = value;
  25. final _lastIndex = 0.obs;
  26. int get lastIndex => _lastIndex.value;
  27. set lastIndex(int value) => _lastIndex.value = value;
  28. // 最近位置是否展开
  29. final _isRecentLocationsExpanded = false.obs;
  30. bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
  31. set isRecentLocationsExpanded(bool value) =>
  32. _isRecentLocationsExpanded.value = value;
  33. /// 收起最近位置列表
  34. void collapseRecentLocations() {
  35. if (_isRecentLocationsExpanded.value) {
  36. _isRecentLocationsExpanded.value = false;
  37. }
  38. }
  39. // 统计信息
  40. final _uplinkBytes = 0.obs;
  41. final _downlinkBytes = 0.obs;
  42. int get uplinkBytes => _uplinkBytes.value;
  43. int get downlinkBytes => _downlinkBytes.value;
  44. // 延迟信息
  45. final _currentDelay = 0.obs;
  46. int get currentDelay => _currentDelay.value;
  47. // 当前选择的位置
  48. final _selectedLocation = Rxn<Locations>();
  49. Locations? get selectedLocation => _selectedLocation.value;
  50. set selectedLocation(Locations? value) => _selectedLocation.value = value;
  51. // 最近使用的位置列表
  52. final _recentLocations = <Locations>[].obs;
  53. List<Locations> get recentLocations =>
  54. _recentLocations.where((loc) => loc.id != selectedLocation?.id).toList();
  55. @override
  56. void onInit() {
  57. super.onInit();
  58. _initializeLocations();
  59. AwesomeNotificationsHelper.init();
  60. }
  61. /// 初始化位置数据
  62. void _initializeLocations() {
  63. // 从 SharedPreferences 加载保存的节点数据
  64. _loadSavedLocations();
  65. }
  66. /// 从 SharedPreferences 加载保存的节点数据
  67. void _loadSavedLocations() {
  68. try {
  69. // 加载当前选中的节点
  70. final selectedLocationData = IXSP.getSelectedLocation();
  71. if (selectedLocationData != null) {
  72. final savedLocation = Locations.fromJson(selectedLocationData);
  73. // 检查保存的节点是否存在于当前 groups 中
  74. if (_isLocationExistsInGroups(savedLocation)) {
  75. selectedLocation = savedLocation;
  76. } else {
  77. // 如果节点不存在于 groups 中,选中第一个可用节点
  78. // 如果当前节点是连接中的状态,则断开连接
  79. if (coreController.state != ConnectionState.disconnected) {
  80. BaseCoreApi().disconnect();
  81. }
  82. log(
  83. TAG,
  84. 'Saved location not found in groups, selecting first available',
  85. );
  86. _selectFirstAvailableLocation();
  87. }
  88. } else {
  89. // 如果没有保存的节点,选中第一个可用节点
  90. _selectFirstAvailableLocation();
  91. }
  92. // 加载最近选择的节点列表
  93. final recentLocationsData = IXSP.getRecentLocations();
  94. if (recentLocationsData.isNotEmpty) {
  95. _recentLocations.assignAll(
  96. recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
  97. );
  98. }
  99. } catch (e) {
  100. log(TAG, 'Error loading saved locations: $e');
  101. }
  102. }
  103. /// 检查节点是否存在于当前 groups 中
  104. bool _isLocationExistsInGroups(Locations location) {
  105. final launch = IXSP.getLaunch();
  106. final groups = launch?.groups;
  107. if (groups == null) return false;
  108. // 检查 normal 列表
  109. if (groups.normal?.list != null) {
  110. for (var locationList in groups.normal!.list!) {
  111. if (locationList.locations != null) {
  112. for (var loc in locationList.locations!) {
  113. if (loc.id == location.id) {
  114. return true;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. // 检查 streaming 列表
  121. if (groups.streaming?.list != null) {
  122. for (var locationList in groups.streaming!.list!) {
  123. if (locationList.locations != null) {
  124. for (var loc in locationList.locations!) {
  125. if (loc.id == location.id) {
  126. return true;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. return false;
  133. }
  134. /// 选中第一个可用节点
  135. void _selectFirstAvailableLocation() {
  136. try {
  137. final launch = IXSP.getLaunch();
  138. final normalList = launch?.groups?.normal?.list;
  139. if (normalList != null && normalList.isNotEmpty) {
  140. // 遍历找到第一个有可用节点的国家
  141. for (var locationList in normalList) {
  142. if (locationList.locations != null &&
  143. locationList.locations!.isNotEmpty) {
  144. // 选中第一个节点
  145. final firstLocation = locationList.locations!.first;
  146. selectLocation(firstLocation);
  147. break;
  148. }
  149. }
  150. }
  151. } catch (e) {
  152. log(TAG, 'Error selecting first available location: $e');
  153. }
  154. }
  155. /// 选择位置
  156. void selectLocation(
  157. Locations location, {
  158. String locationSelectionType = 'auto',
  159. }) {
  160. selectedLocation = location;
  161. coreController.locationSelectionType = locationSelectionType;
  162. // 更新最近使用列表
  163. _updateRecentLocations(location);
  164. // 保存到 SharedPreferences
  165. _saveLocationsToStorage();
  166. }
  167. void handleConnect({bool delay = false}) {
  168. if (delay) {
  169. // 延迟300ms
  170. Future.delayed(const Duration(milliseconds: 300), () {
  171. coreController.selectLocationConnect();
  172. });
  173. } else {
  174. coreController.selectLocationConnect();
  175. }
  176. }
  177. /// 更新最近使用的位置列表
  178. void _updateRecentLocations(Locations location) {
  179. // 移除已存在的位置
  180. _recentLocations.removeWhere((loc) => loc.id == location.id);
  181. // 添加到列表开头
  182. _recentLocations.insert(0, location);
  183. // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
  184. if (_recentLocations.length > 4) {
  185. _recentLocations.removeRange(4, _recentLocations.length);
  186. }
  187. }
  188. /// 保存位置数据到 SharedPreferences
  189. void _saveLocationsToStorage() {
  190. try {
  191. // 保存当前选中的节点
  192. if (selectedLocation != null) {
  193. IXSP.saveSelectedLocation(selectedLocation!.toJson());
  194. }
  195. // 保存最近选择的节点列表
  196. final recentLocationsJson = _recentLocations
  197. .map((e) => e.toJson())
  198. .toList();
  199. IXSP.saveRecentLocations(recentLocationsJson);
  200. } catch (e) {
  201. log(TAG, 'Error saving locations to storage: $e');
  202. }
  203. }
  204. void onRefresh() async {
  205. try {
  206. await apiController.launch();
  207. refreshController.refreshCompleted();
  208. } catch (e) {
  209. refreshController.refreshFailed();
  210. }
  211. }
  212. /// 当 Launch 数据更新时刷新节点
  213. void refreshOnLaunchChanged() {
  214. log(TAG, 'Launch data changed, refreshing locations');
  215. _loadSavedLocations();
  216. }
  217. // 设置默认auto连接
  218. void setDefaultAutoConnect() {
  219. coreController.locationSelectionType = 'auto';
  220. coreController.handleConnection();
  221. }
  222. }