home_controller.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. @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. final savedLocation = Locations.fromJson(selectedLocationData);
  67. // 检查保存的节点是否存在于当前 groups 中
  68. if (_isLocationExistsInGroups(savedLocation)) {
  69. selectedLocation = savedLocation;
  70. } else {
  71. // 如果节点不存在于 groups 中,选中第一个可用节点
  72. // 如果当前节点是连接中的状态,则断开连接
  73. if (coreController.state != ConnectionState.disconnected) {
  74. CoreApi().disconnect();
  75. }
  76. log(
  77. TAG,
  78. 'Saved location not found in groups, selecting first available',
  79. );
  80. _selectFirstAvailableLocation();
  81. }
  82. } else {
  83. // 如果没有保存的节点,选中第一个可用节点
  84. _selectFirstAvailableLocation();
  85. }
  86. // 加载最近选择的节点列表
  87. final recentLocationsData = IXSP.getRecentLocations();
  88. if (recentLocationsData.isNotEmpty) {
  89. _recentLocations.assignAll(
  90. recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
  91. );
  92. }
  93. } catch (e) {
  94. log(TAG, 'Error loading saved locations: $e');
  95. }
  96. }
  97. /// 检查节点是否存在于当前 groups 中
  98. bool _isLocationExistsInGroups(Locations location) {
  99. final launch = IXSP.getLaunch();
  100. final groups = launch?.groups;
  101. if (groups == null) return false;
  102. // 检查 normal 列表
  103. if (groups.normal?.list != null) {
  104. for (var locationList in groups.normal!.list!) {
  105. if (locationList.locations != null) {
  106. for (var loc in locationList.locations!) {
  107. if (loc.id == location.id) {
  108. return true;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // 检查 streaming 列表
  115. if (groups.streaming?.list != null) {
  116. for (var locationList in groups.streaming!.list!) {
  117. if (locationList.locations != null) {
  118. for (var loc in locationList.locations!) {
  119. if (loc.id == location.id) {
  120. return true;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. /// 选中第一个可用节点
  129. void _selectFirstAvailableLocation() {
  130. try {
  131. final launch = IXSP.getLaunch();
  132. final normalList = launch?.groups?.normal?.list;
  133. if (normalList != null && normalList.isNotEmpty) {
  134. // 遍历找到第一个有可用节点的国家
  135. for (var locationList in normalList) {
  136. if (locationList.locations != null &&
  137. locationList.locations!.isNotEmpty) {
  138. // 选中第一个节点
  139. final firstLocation = locationList.locations!.first;
  140. selectLocation(firstLocation);
  141. break;
  142. }
  143. }
  144. }
  145. } catch (e) {
  146. log(TAG, 'Error selecting first available location: $e');
  147. }
  148. }
  149. /// 选择位置
  150. void selectLocation(
  151. Locations location, {
  152. String locationSelectionType = 'auto',
  153. }) {
  154. selectedLocation = location;
  155. coreController.locationSelectionType = locationSelectionType;
  156. // 更新最近使用列表
  157. _updateRecentLocations(location);
  158. // 保存到 SharedPreferences
  159. _saveLocationsToStorage();
  160. }
  161. void handleConnect({bool delay = false}) {
  162. if (delay) {
  163. // 延迟300ms
  164. Future.delayed(const Duration(milliseconds: 300), () {
  165. coreController.selectLocationConnect();
  166. });
  167. } else {
  168. coreController.selectLocationConnect();
  169. }
  170. }
  171. /// 更新最近使用的位置列表
  172. void _updateRecentLocations(Locations location) {
  173. // 移除已存在的位置
  174. _recentLocations.removeWhere((loc) => loc.id == location.id);
  175. // 添加到列表开头
  176. _recentLocations.insert(0, location);
  177. // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
  178. if (_recentLocations.length > 4) {
  179. _recentLocations.removeRange(4, _recentLocations.length);
  180. }
  181. }
  182. /// 保存位置数据到 SharedPreferences
  183. void _saveLocationsToStorage() {
  184. try {
  185. // 保存当前选中的节点
  186. if (selectedLocation != null) {
  187. IXSP.saveSelectedLocation(selectedLocation!.toJson());
  188. }
  189. // 保存最近选择的节点列表
  190. final recentLocationsJson = _recentLocations
  191. .map((e) => e.toJson())
  192. .toList();
  193. IXSP.saveRecentLocations(recentLocationsJson);
  194. } catch (e) {
  195. log(TAG, 'Error saving locations to storage: $e');
  196. }
  197. }
  198. void onRefresh() async {
  199. try {
  200. await apiController.launch();
  201. refreshController.refreshCompleted();
  202. } catch (e) {
  203. refreshController.refreshFailed();
  204. }
  205. }
  206. /// 当 Launch 数据更新时刷新节点
  207. void refreshOnLaunchChanged() {
  208. log(TAG, 'Launch data changed, refreshing locations');
  209. _loadSavedLocations();
  210. }
  211. // 设置默认auto连接
  212. void setDefaultAutoConnect() {
  213. coreController.locationSelectionType = 'auto';
  214. coreController.handleConnection();
  215. }
  216. }