| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- import 'package:get/get.dart';
- import 'package:nomo/app/controllers/api_controller.dart';
- import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
- import '../../../../pigeons/core_api.g.dart';
- import '../../../../utils/awesome_notifications_helper.dart';
- import '../../../../utils/log/logger.dart';
- import '../../../base/base_controller.dart';
- import '../../../constants/enums.dart';
- import '../../../controllers/core_controller.dart';
- import '../../../data/models/launch/groups.dart';
- import '../../../data/sp/ix_sp.dart';
- /// 主页控制器
- class HomeController extends BaseController {
- final coreController = Get.find<CoreController>();
- final apiController = Get.find<ApiController>();
- final TAG = 'HomeController';
- final _refreshController = RefreshController(initialRefresh: false);
- RefreshController get refreshController => _refreshController;
- final _isOn = false.obs;
- bool get isOn => _isOn.value;
- set isOn(bool value) => _isOn.value = value;
- final _currentIndex = 0.obs;
- int get currentIndex => _currentIndex.value;
- set currentIndex(int value) => _currentIndex.value = value;
- final _lastIndex = 0.obs;
- int get lastIndex => _lastIndex.value;
- set lastIndex(int value) => _lastIndex.value = value;
- // 最近位置是否展开
- final _isRecentLocationsExpanded = false.obs;
- bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
- set isRecentLocationsExpanded(bool value) =>
- _isRecentLocationsExpanded.value = value;
- // 统计信息
- final _uplinkBytes = 0.obs;
- final _downlinkBytes = 0.obs;
- int get uplinkBytes => _uplinkBytes.value;
- int get downlinkBytes => _downlinkBytes.value;
- // 延迟信息
- final _currentDelay = 0.obs;
- int get currentDelay => _currentDelay.value;
- // 当前选择的位置
- final _selectedLocation = Rxn<Locations>();
- Locations? get selectedLocation => _selectedLocation.value;
- set selectedLocation(Locations? value) => _selectedLocation.value = value;
- // 最近使用的位置列表
- final _recentLocations = <Locations>[].obs;
- List<Locations> get recentLocations =>
- _recentLocations.where((loc) => loc.id != selectedLocation?.id).toList();
- @override
- void onInit() {
- super.onInit();
- _initializeLocations();
- AwesomeNotificationsHelper.init();
- }
- /// 初始化位置数据
- void _initializeLocations() {
- // 从 SharedPreferences 加载保存的节点数据
- _loadSavedLocations();
- }
- /// 从 SharedPreferences 加载保存的节点数据
- void _loadSavedLocations() {
- try {
- // 加载当前选中的节点
- final selectedLocationData = IXSP.getSelectedLocation();
- if (selectedLocationData != null) {
- final savedLocation = Locations.fromJson(selectedLocationData);
- // 检查保存的节点是否存在于当前 groups 中
- if (_isLocationExistsInGroups(savedLocation)) {
- selectedLocation = savedLocation;
- } else {
- // 如果节点不存在于 groups 中,选中第一个可用节点
- // 如果当前节点是连接中的状态,则断开连接
- if (coreController.state != ConnectionState.disconnected) {
- CoreApi().disconnect();
- }
- log(
- TAG,
- 'Saved location not found in groups, selecting first available',
- );
- _selectFirstAvailableLocation();
- }
- } else {
- // 如果没有保存的节点,选中第一个可用节点
- _selectFirstAvailableLocation();
- }
- // 加载最近选择的节点列表
- final recentLocationsData = IXSP.getRecentLocations();
- if (recentLocationsData.isNotEmpty) {
- _recentLocations.assignAll(
- recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
- );
- }
- } catch (e) {
- log(TAG, 'Error loading saved locations: $e');
- }
- }
- /// 检查节点是否存在于当前 groups 中
- bool _isLocationExistsInGroups(Locations location) {
- final launch = IXSP.getLaunch();
- final groups = launch?.groups;
- if (groups == null) return false;
- // 检查 normal 列表
- if (groups.normal?.list != null) {
- for (var locationList in groups.normal!.list!) {
- if (locationList.locations != null) {
- for (var loc in locationList.locations!) {
- if (loc.id == location.id) {
- return true;
- }
- }
- }
- }
- }
- // 检查 streaming 列表
- if (groups.streaming?.list != null) {
- for (var locationList in groups.streaming!.list!) {
- if (locationList.locations != null) {
- for (var loc in locationList.locations!) {
- if (loc.id == location.id) {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- /// 选中第一个可用节点
- void _selectFirstAvailableLocation() {
- try {
- final launch = IXSP.getLaunch();
- final normalList = launch?.groups?.normal?.list;
- if (normalList != null && normalList.isNotEmpty) {
- // 遍历找到第一个有可用节点的国家
- for (var locationList in normalList) {
- if (locationList.locations != null &&
- locationList.locations!.isNotEmpty) {
- // 选中第一个节点
- final firstLocation = locationList.locations!.first;
- selectLocation(firstLocation);
- break;
- }
- }
- }
- } catch (e) {
- log(TAG, 'Error selecting first available location: $e');
- }
- }
- /// 选择位置
- void selectLocation(
- Locations location, {
- String locationSelectionType = 'auto',
- }) {
- selectedLocation = location;
- coreController.locationSelectionType = locationSelectionType;
- // 更新最近使用列表
- _updateRecentLocations(location);
- // 保存到 SharedPreferences
- _saveLocationsToStorage();
- }
- void handleConnect({bool delay = false}) {
- if (delay) {
- // 延迟300ms
- Future.delayed(const Duration(milliseconds: 300), () {
- coreController.selectLocationConnect();
- });
- } else {
- coreController.selectLocationConnect();
- }
- }
- /// 更新最近使用的位置列表
- void _updateRecentLocations(Locations location) {
- // 移除已存在的位置
- _recentLocations.removeWhere((loc) => loc.id == location.id);
- // 添加到列表开头
- _recentLocations.insert(0, location);
- // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
- if (_recentLocations.length > 4) {
- _recentLocations.removeRange(4, _recentLocations.length);
- }
- }
- /// 保存位置数据到 SharedPreferences
- void _saveLocationsToStorage() {
- try {
- // 保存当前选中的节点
- if (selectedLocation != null) {
- IXSP.saveSelectedLocation(selectedLocation!.toJson());
- }
- // 保存最近选择的节点列表
- final recentLocationsJson = _recentLocations
- .map((e) => e.toJson())
- .toList();
- IXSP.saveRecentLocations(recentLocationsJson);
- } catch (e) {
- log(TAG, 'Error saving locations to storage: $e');
- }
- }
- void onRefresh() async {
- try {
- await apiController.launch();
- refreshController.refreshCompleted();
- } catch (e) {
- refreshController.refreshFailed();
- }
- }
- /// 当 Launch 数据更新时刷新节点
- void refreshOnLaunchChanged() {
- log(TAG, 'Launch data changed, refreshing locations');
- _loadSavedLocations();
- }
- // 设置默认auto连接
- void setDefaultAutoConnect() {
- coreController.locationSelectionType = 'auto';
- coreController.handleConnection();
- }
- }
|