home_controller.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 '../../../../pigeons/core_api.g.dart';
  5. import '../../../../utils/awesome_notifications_helper.dart';
  6. import '../../../../utils/log/logger.dart';
  7. import '../../../base/base_controller.dart';
  8. import '../../../constants/enums.dart';
  9. import '../../../controllers/core_controller.dart';
  10. import '../../../data/models/launch/groups.dart';
  11. import '../../../data/sp/ix_sp.dart';
  12. /// 主页控制器
  13. class HomeController extends BaseController {
  14. final coreController = Get.find<CoreController>();
  15. final apiController = Get.find<ApiController>();
  16. final TAG = 'HomeController';
  17. final _refreshController = RefreshController(initialRefresh: false);
  18. RefreshController get refreshController => _refreshController;
  19. final _isOn = false.obs;
  20. bool get isOn => _isOn.value;
  21. set isOn(bool value) => _isOn.value = value;
  22. final _currentIndex = 0.obs;
  23. int get currentIndex => _currentIndex.value;
  24. set currentIndex(int value) => _currentIndex.value = value;
  25. final _lastIndex = 0.obs;
  26. int get lastIndex => _lastIndex.value;
  27. set lastIndex(int value) => _lastIndex.value = value;
  28. // 最近位置是否展开
  29. final _isRecentLocationsExpanded = false.obs;
  30. bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
  31. set isRecentLocationsExpanded(bool value) =>
  32. _isRecentLocationsExpanded.value = value;
  33. // 统计信息
  34. final _uplinkBytes = 0.obs;
  35. final _downlinkBytes = 0.obs;
  36. int get uplinkBytes => _uplinkBytes.value;
  37. int get downlinkBytes => _downlinkBytes.value;
  38. // 延迟信息
  39. final _currentDelay = 0.obs;
  40. int get currentDelay => _currentDelay.value;
  41. // 当前选择的位置
  42. final _selectedLocation = Rxn<Locations>();
  43. Locations? get selectedLocation => _selectedLocation.value;
  44. set selectedLocation(Locations? value) => _selectedLocation.value = value;
  45. // 最近使用的位置列表
  46. final _recentLocations = <Locations>[].obs;
  47. List<Locations> get recentLocations =>
  48. _recentLocations.where((loc) => loc.id != selectedLocation?.id).toList();
  49. // 是否是会员
  50. final _isPremium = false.obs;
  51. bool get isPremium => _isPremium.value;
  52. set isPremium(bool value) => _isPremium.value = value;
  53. @override
  54. void onInit() {
  55. super.onInit();
  56. _initializeLocations();
  57. AwesomeNotificationsHelper.init();
  58. }
  59. /// 初始化位置数据
  60. void _initializeLocations() {
  61. // 从 SharedPreferences 加载保存的节点数据
  62. _loadSavedLocations();
  63. }
  64. /// 从 SharedPreferences 加载保存的节点数据
  65. void _loadSavedLocations() {
  66. try {
  67. // 加载当前选中的节点
  68. final selectedLocationData = IXSP.getSelectedLocation();
  69. if (selectedLocationData != null) {
  70. final savedLocation = Locations.fromJson(selectedLocationData);
  71. // 检查保存的节点是否存在于当前 groups 中
  72. if (_isLocationExistsInGroups(savedLocation)) {
  73. selectedLocation = savedLocation;
  74. } else {
  75. // 如果节点不存在于 groups 中,选中第一个可用节点
  76. // 如果当前节点是连接中的状态,则断开连接
  77. if (coreController.state != ConnectionState.disconnected) {
  78. CoreApi().disconnect();
  79. }
  80. log(
  81. TAG,
  82. 'Saved location not found in groups, selecting first available',
  83. );
  84. _selectFirstAvailableLocation();
  85. }
  86. } else {
  87. // 如果没有保存的节点,选中第一个可用节点
  88. _selectFirstAvailableLocation();
  89. }
  90. // 加载最近选择的节点列表
  91. final recentLocationsData = IXSP.getRecentLocations();
  92. if (recentLocationsData.isNotEmpty) {
  93. _recentLocations.assignAll(
  94. recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
  95. );
  96. }
  97. } catch (e) {
  98. log(TAG, 'Error loading saved locations: $e');
  99. }
  100. }
  101. /// 检查节点是否存在于当前 groups 中
  102. bool _isLocationExistsInGroups(Locations location) {
  103. final launch = IXSP.getLaunch();
  104. final groups = launch?.groups;
  105. if (groups == null) return false;
  106. // 检查 normal 列表
  107. if (groups.normal?.list != null) {
  108. for (var locationList in groups.normal!.list!) {
  109. if (locationList.locations != null) {
  110. for (var loc in locationList.locations!) {
  111. if (loc.id == location.id) {
  112. return true;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. // 检查 streaming 列表
  119. if (groups.streaming?.list != null) {
  120. for (var locationList in groups.streaming!.list!) {
  121. if (locationList.locations != null) {
  122. for (var loc in locationList.locations!) {
  123. if (loc.id == location.id) {
  124. return true;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. /// 选中第一个可用节点
  133. void _selectFirstAvailableLocation() {
  134. try {
  135. final launch = IXSP.getLaunch();
  136. final normalList = launch?.groups?.normal?.list;
  137. if (normalList != null && normalList.isNotEmpty) {
  138. // 遍历找到第一个有可用节点的国家
  139. for (var locationList in normalList) {
  140. if (locationList.locations != null &&
  141. locationList.locations!.isNotEmpty) {
  142. // 选中第一个节点
  143. final firstLocation = locationList.locations!.first;
  144. selectLocation(firstLocation);
  145. break;
  146. }
  147. }
  148. }
  149. } catch (e) {
  150. log(TAG, 'Error selecting first available location: $e');
  151. }
  152. }
  153. /// 选择位置
  154. void selectLocation(
  155. Locations location, {
  156. String locationSelectionType = 'auto',
  157. }) {
  158. selectedLocation = location;
  159. coreController.locationSelectionType = locationSelectionType;
  160. // 更新最近使用列表
  161. _updateRecentLocations(location);
  162. // 保存到 SharedPreferences
  163. _saveLocationsToStorage();
  164. }
  165. void handleConnect({bool delay = false}) {
  166. if (delay) {
  167. // 延迟300ms
  168. Future.delayed(const Duration(milliseconds: 300), () {
  169. coreController.selectLocationConnect();
  170. });
  171. } else {
  172. coreController.selectLocationConnect();
  173. }
  174. }
  175. /// 更新最近使用的位置列表
  176. void _updateRecentLocations(Locations location) {
  177. // 移除已存在的位置
  178. _recentLocations.removeWhere((loc) => loc.id == location.id);
  179. // 添加到列表开头
  180. _recentLocations.insert(0, location);
  181. // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
  182. if (_recentLocations.length > 4) {
  183. _recentLocations.removeRange(4, _recentLocations.length);
  184. }
  185. }
  186. /// 保存位置数据到 SharedPreferences
  187. void _saveLocationsToStorage() {
  188. try {
  189. // 保存当前选中的节点
  190. if (selectedLocation != null) {
  191. IXSP.saveSelectedLocation(selectedLocation!.toJson());
  192. }
  193. // 保存最近选择的节点列表
  194. final recentLocationsJson = _recentLocations
  195. .map((e) => e.toJson())
  196. .toList();
  197. IXSP.saveRecentLocations(recentLocationsJson);
  198. } catch (e) {
  199. log(TAG, 'Error saving locations to storage: $e');
  200. }
  201. }
  202. void onRefresh() async {
  203. try {
  204. await apiController.launch();
  205. refreshController.refreshCompleted();
  206. } catch (e) {
  207. refreshController.refreshFailed();
  208. }
  209. }
  210. /// 当 Launch 数据更新时刷新节点
  211. void refreshOnLaunchChanged() {
  212. log(TAG, 'Launch data changed, refreshing locations');
  213. _loadSavedLocations();
  214. }
  215. // 设置默认auto连接
  216. void setDefaultAutoConnect() {
  217. coreController.locationSelectionType = 'auto';
  218. coreController.handleConnection();
  219. }
  220. }