home_controller.dart 5.6 KB

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