home_controller.dart 5.4 KB

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