home_controller.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:get/get.dart';
  2. import '../../../../utils/log/logger.dart';
  3. import '../../../../utils/permission_manager.dart';
  4. import '../../../base/base_controller.dart';
  5. import '../../../controllers/core_controller.dart';
  6. import '../../../data/models/launch/groups.dart';
  7. import '../../../data/sp/ix_sp.dart';
  8. /// 主页控制器
  9. class HomeController extends BaseController {
  10. final coreController = Get.find<CoreController>();
  11. final TAG = 'HomeController';
  12. final _isOn = false.obs;
  13. bool get isOn => _isOn.value;
  14. set isOn(bool value) => _isOn.value = value;
  15. final _currentIndex = 0.obs;
  16. int get currentIndex => _currentIndex.value;
  17. set currentIndex(int value) => _currentIndex.value = value;
  18. final _lastIndex = 0.obs;
  19. int get lastIndex => _lastIndex.value;
  20. set lastIndex(int value) => _lastIndex.value = value;
  21. // 最近位置是否展开
  22. final _isRecentLocationsExpanded = false.obs;
  23. bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
  24. set isRecentLocationsExpanded(bool value) =>
  25. _isRecentLocationsExpanded.value = value;
  26. // 统计信息
  27. final _uplinkBytes = 0.obs;
  28. final _downlinkBytes = 0.obs;
  29. int get uplinkBytes => _uplinkBytes.value;
  30. int get downlinkBytes => _downlinkBytes.value;
  31. // 延迟信息
  32. final _currentDelay = 0.obs;
  33. int get currentDelay => _currentDelay.value;
  34. // 当前选择的位置
  35. final _selectedLocation = Rxn<Locations>();
  36. Locations? get selectedLocation => _selectedLocation.value;
  37. set selectedLocation(Locations? value) => _selectedLocation.value = value;
  38. // 最近使用的位置列表
  39. final _recentLocations = <Locations>[].obs;
  40. List<Locations> get recentLocations =>
  41. _recentLocations.where((loc) => loc.id != selectedLocation?.id).toList();
  42. // 是否是会员
  43. final _isPremium = false.obs;
  44. bool get isPremium => _isPremium.value;
  45. set isPremium(bool value) => _isPremium.value = value;
  46. @override
  47. void onInit() {
  48. super.onInit();
  49. _initializeLocations();
  50. PermissionManager.requestNotificationPermission();
  51. }
  52. /// 初始化位置数据
  53. void _initializeLocations() {
  54. // 从 SharedPreferences 加载保存的节点数据
  55. _loadSavedLocations();
  56. }
  57. /// 从 SharedPreferences 加载保存的节点数据
  58. void _loadSavedLocations() {
  59. try {
  60. // 加载当前选中的节点
  61. final selectedLocationData = IXSP.getSelectedLocation();
  62. if (selectedLocationData != null) {
  63. selectedLocation = Locations.fromJson(selectedLocationData);
  64. } else {
  65. // 如果没有保存的节点,选中第一个可用节点
  66. _selectFirstAvailableLocation();
  67. }
  68. // 加载最近选择的节点列表
  69. final recentLocationsData = IXSP.getRecentLocations();
  70. if (recentLocationsData.isNotEmpty) {
  71. _recentLocations.assignAll(
  72. recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
  73. );
  74. }
  75. } catch (e) {
  76. log(TAG, 'Error loading saved locations: $e');
  77. }
  78. }
  79. /// 选中第一个可用节点
  80. void _selectFirstAvailableLocation() {
  81. try {
  82. final launch = IXSP.getLaunch();
  83. final normalList = launch?.groups?.normal?.list;
  84. if (normalList != null && normalList.isNotEmpty) {
  85. // 遍历找到第一个有可用节点的国家
  86. for (var locationList in normalList) {
  87. if (locationList.locations != null &&
  88. locationList.locations!.isNotEmpty) {
  89. // 选中第一个节点
  90. final firstLocation = locationList.locations!.first;
  91. selectLocation(firstLocation);
  92. break;
  93. }
  94. }
  95. }
  96. } catch (e) {
  97. log(TAG, 'Error selecting first available location: $e');
  98. }
  99. }
  100. /// 选择位置
  101. void selectLocation(Locations location) {
  102. selectedLocation = location;
  103. // 更新最近使用列表
  104. _updateRecentLocations(location);
  105. // 保存到 SharedPreferences
  106. _saveLocationsToStorage();
  107. }
  108. void handleConnect({bool delay = false}) {
  109. if (delay) {
  110. // 延迟300ms
  111. Future.delayed(const Duration(milliseconds: 300), () {
  112. coreController.selectLocationConnect();
  113. });
  114. } else {
  115. coreController.selectLocationConnect();
  116. }
  117. }
  118. /// 更新最近使用的位置列表
  119. void _updateRecentLocations(Locations location) {
  120. // 移除已存在的位置
  121. _recentLocations.removeWhere((loc) => loc.id == location.id);
  122. // 添加到列表开头
  123. _recentLocations.insert(0, location);
  124. // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
  125. if (_recentLocations.length > 4) {
  126. _recentLocations.removeRange(4, _recentLocations.length);
  127. }
  128. }
  129. /// 保存位置数据到 SharedPreferences
  130. void _saveLocationsToStorage() {
  131. try {
  132. // 保存当前选中的节点
  133. if (selectedLocation != null) {
  134. IXSP.saveSelectedLocation(selectedLocation!.toJson());
  135. }
  136. // 保存最近选择的节点列表
  137. final recentLocationsJson = _recentLocations
  138. .map((e) => e.toJson())
  139. .toList();
  140. IXSP.saveRecentLocations(recentLocationsJson);
  141. } catch (e) {
  142. log(TAG, 'Error saving locations to storage: $e');
  143. }
  144. }
  145. }