|
|
@@ -1,7 +1,9 @@
|
|
|
import 'package:get/get.dart';
|
|
|
+import '../../../../utils/log/logger.dart';
|
|
|
import '../../../base/base_controller.dart';
|
|
|
import '../../../controllers/core_controller.dart';
|
|
|
import '../../../data/models/launch/groups.dart';
|
|
|
+import '../../../data/sp/ix_sp.dart';
|
|
|
|
|
|
/// 主页控制器
|
|
|
class HomeController extends BaseController {
|
|
|
@@ -45,10 +47,6 @@ class HomeController extends BaseController {
|
|
|
final _recentLocations = <Locations>[].obs;
|
|
|
List<Locations> get recentLocations => _recentLocations;
|
|
|
|
|
|
- // 所有可用位置列表
|
|
|
- final _allLocations = <Locations>[].obs;
|
|
|
- List<Locations> get allLocations => _allLocations;
|
|
|
-
|
|
|
// 是否是会员
|
|
|
final _isPremium = false.obs;
|
|
|
bool get isPremium => _isPremium.value;
|
|
|
@@ -62,43 +60,55 @@ class HomeController extends BaseController {
|
|
|
|
|
|
/// 初始化位置数据
|
|
|
void _initializeLocations() {
|
|
|
- // 模拟数据 - 实际应用中应该从API获取
|
|
|
- _allLocations.addAll([
|
|
|
- const Locations(
|
|
|
- id: 1,
|
|
|
- name: 'LosAngles',
|
|
|
- code: 'US',
|
|
|
- country: 'United States',
|
|
|
- latency: 15,
|
|
|
- ),
|
|
|
- const Locations(
|
|
|
- id: 2,
|
|
|
- name: 'Singapore',
|
|
|
- code: 'SG',
|
|
|
- country: 'Singapore',
|
|
|
- latency: 25,
|
|
|
- ),
|
|
|
- const Locations(
|
|
|
- id: 3,
|
|
|
- name: 'Japan',
|
|
|
- code: 'JP',
|
|
|
- country: 'Japan',
|
|
|
- latency: 35,
|
|
|
- ),
|
|
|
- const Locations(
|
|
|
- id: 4,
|
|
|
- name: 'Madrid',
|
|
|
- code: 'ES',
|
|
|
- country: 'Spain',
|
|
|
- latency: 30,
|
|
|
- ),
|
|
|
- ]);
|
|
|
-
|
|
|
- // 设置默认选择的位置
|
|
|
- selectedLocation = _allLocations.first;
|
|
|
-
|
|
|
- // 设置最近使用的位置
|
|
|
- _recentLocations.addAll(_allLocations.skip(1).take(3));
|
|
|
+ // 从 SharedPreferences 加载保存的节点数据
|
|
|
+ _loadSavedLocations();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 从 SharedPreferences 加载保存的节点数据
|
|
|
+ void _loadSavedLocations() {
|
|
|
+ try {
|
|
|
+ // 加载当前选中的节点
|
|
|
+ final selectedLocationData = IXSP.getSelectedLocation();
|
|
|
+ if (selectedLocationData != null) {
|
|
|
+ selectedLocation = Locations.fromJson(selectedLocationData);
|
|
|
+ } 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');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 选中第一个可用节点
|
|
|
+ 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');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// 选择位置
|
|
|
@@ -107,6 +117,13 @@ class HomeController extends BaseController {
|
|
|
|
|
|
// 更新最近使用列表
|
|
|
_updateRecentLocations(location);
|
|
|
+
|
|
|
+ // 保存到 SharedPreferences
|
|
|
+ _saveLocationsToStorage();
|
|
|
+ }
|
|
|
+
|
|
|
+ void handleConnect() {
|
|
|
+ coreController.selectLocationConnect();
|
|
|
}
|
|
|
|
|
|
/// 更新最近使用的位置列表
|
|
|
@@ -123,8 +140,21 @@ class HomeController extends BaseController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// 判断位置是否快速
|
|
|
- bool isLocationFast(Locations location) {
|
|
|
- return location.latency != null && location.latency! < 30;
|
|
|
+ /// 保存位置数据到 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');
|
|
|
+ }
|
|
|
}
|
|
|
}
|