home_controller.dart 4.7 KB

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