home_controller.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 =>
  40. _recentLocations.where((loc) => loc.id != selectedLocation?.id).toList();
  41. // 是否是会员
  42. final _isPremium = false.obs;
  43. bool get isPremium => _isPremium.value;
  44. set isPremium(bool value) => _isPremium.value = value;
  45. @override
  46. void onInit() {
  47. super.onInit();
  48. _initializeLocations();
  49. }
  50. /// 初始化位置数据
  51. void _initializeLocations() {
  52. // 从 SharedPreferences 加载保存的节点数据
  53. _loadSavedLocations();
  54. }
  55. /// 从 SharedPreferences 加载保存的节点数据
  56. void _loadSavedLocations() {
  57. try {
  58. // 加载当前选中的节点
  59. final selectedLocationData = IXSP.getSelectedLocation();
  60. if (selectedLocationData != null) {
  61. selectedLocation = Locations.fromJson(selectedLocationData);
  62. } else {
  63. // 如果没有保存的节点,选中第一个可用节点
  64. _selectFirstAvailableLocation();
  65. }
  66. // 加载最近选择的节点列表
  67. final recentLocationsData = IXSP.getRecentLocations();
  68. if (recentLocationsData.isNotEmpty) {
  69. _recentLocations.assignAll(
  70. recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
  71. );
  72. }
  73. } catch (e) {
  74. log(TAG, 'Error loading saved locations: $e');
  75. }
  76. }
  77. /// 选中第一个可用节点
  78. void _selectFirstAvailableLocation() {
  79. try {
  80. final launch = IXSP.getLaunch();
  81. final normalList = launch?.groups?.normal?.list;
  82. if (normalList != null && normalList.isNotEmpty) {
  83. // 遍历找到第一个有可用节点的国家
  84. for (var locationList in normalList) {
  85. if (locationList.locations != null &&
  86. locationList.locations!.isNotEmpty) {
  87. // 选中第一个节点
  88. final firstLocation = locationList.locations!.first;
  89. selectLocation(firstLocation);
  90. break;
  91. }
  92. }
  93. }
  94. } catch (e) {
  95. log(TAG, 'Error selecting first available location: $e');
  96. }
  97. }
  98. /// 选择位置
  99. void selectLocation(Locations location) {
  100. selectedLocation = location;
  101. // 更新最近使用列表
  102. _updateRecentLocations(location);
  103. // 保存到 SharedPreferences
  104. _saveLocationsToStorage();
  105. }
  106. void handleConnect({bool delay = false}) {
  107. if (delay) {
  108. // 延迟300ms
  109. Future.delayed(const Duration(milliseconds: 300), () {
  110. coreController.selectLocationConnect();
  111. });
  112. } else {
  113. coreController.selectLocationConnect();
  114. }
  115. }
  116. /// 更新最近使用的位置列表
  117. void _updateRecentLocations(Locations location) {
  118. // 移除已存在的位置
  119. _recentLocations.removeWhere((loc) => loc.id == location.id);
  120. // 添加到列表开头
  121. _recentLocations.insert(0, location);
  122. // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
  123. if (_recentLocations.length > 4) {
  124. _recentLocations.removeRange(4, _recentLocations.length);
  125. }
  126. }
  127. /// 保存位置数据到 SharedPreferences
  128. void _saveLocationsToStorage() {
  129. try {
  130. // 保存当前选中的节点
  131. if (selectedLocation != null) {
  132. IXSP.saveSelectedLocation(selectedLocation!.toJson());
  133. }
  134. // 保存最近选择的节点列表
  135. final recentLocationsJson = _recentLocations
  136. .map((e) => e.toJson())
  137. .toList();
  138. IXSP.saveRecentLocations(recentLocationsJson);
  139. } catch (e) {
  140. log(TAG, 'Error saving locations to storage: $e');
  141. }
  142. }
  143. }