home_controller.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:get/get.dart';
  2. import '../../../base/base_controller.dart';
  3. import '../../../controllers/core_controller.dart';
  4. import '../../../data/models/launch/groups.dart';
  5. /// 主页控制器
  6. class HomeController extends BaseController {
  7. final coreController = Get.find<CoreController>();
  8. final TAG = 'HomeController';
  9. final _isOn = false.obs;
  10. bool get isOn => _isOn.value;
  11. set isOn(bool value) => _isOn.value = value;
  12. final _currentIndex = 0.obs;
  13. int get currentIndex => _currentIndex.value;
  14. set currentIndex(int value) => _currentIndex.value = value;
  15. final _lastIndex = 0.obs;
  16. int get lastIndex => _lastIndex.value;
  17. set lastIndex(int value) => _lastIndex.value = value;
  18. // 最近位置是否展开
  19. final _isRecentLocationsExpanded = false.obs;
  20. bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
  21. set isRecentLocationsExpanded(bool value) =>
  22. _isRecentLocationsExpanded.value = value;
  23. // 统计信息
  24. final _uplinkBytes = 0.obs;
  25. final _downlinkBytes = 0.obs;
  26. int get uplinkBytes => _uplinkBytes.value;
  27. int get downlinkBytes => _downlinkBytes.value;
  28. // 延迟信息
  29. final _currentDelay = 0.obs;
  30. int get currentDelay => _currentDelay.value;
  31. // 当前选择的位置
  32. final _selectedLocation = Rxn<Locations>();
  33. Locations? get selectedLocation => _selectedLocation.value;
  34. set selectedLocation(Locations? value) => _selectedLocation.value = value;
  35. // 最近使用的位置列表
  36. final _recentLocations = <Locations>[].obs;
  37. List<Locations> get recentLocations => _recentLocations;
  38. // 所有可用位置列表
  39. final _allLocations = <Locations>[].obs;
  40. List<Locations> get allLocations => _allLocations;
  41. // 是否是会员
  42. final _isPremium = false.obs;
  43. bool get isPremium => _isPremium.value;
  44. set isPremium(bool value) => _isPremium.value = value;
  45. @override
  46. void onInit() {
  47. super.onInit();
  48. _initializeLocations();
  49. }
  50. /// 初始化位置数据
  51. void _initializeLocations() {
  52. // 模拟数据 - 实际应用中应该从API获取
  53. _allLocations.addAll([
  54. const Locations(
  55. id: 1,
  56. name: 'LosAngles',
  57. code: 'US',
  58. country: 'United States',
  59. latency: 15,
  60. ),
  61. const Locations(
  62. id: 2,
  63. name: 'Singapore',
  64. code: 'SG',
  65. country: 'Singapore',
  66. latency: 25,
  67. ),
  68. const Locations(
  69. id: 3,
  70. name: 'Japan',
  71. code: 'JP',
  72. country: 'Japan',
  73. latency: 35,
  74. ),
  75. const Locations(
  76. id: 4,
  77. name: 'Madrid',
  78. code: 'ES',
  79. country: 'Spain',
  80. latency: 30,
  81. ),
  82. ]);
  83. // 设置默认选择的位置
  84. selectedLocation = _allLocations.first;
  85. // 设置最近使用的位置
  86. _recentLocations.addAll(_allLocations.skip(1).take(3));
  87. }
  88. /// 选择位置
  89. void selectLocation(Locations location) {
  90. selectedLocation = location;
  91. // 更新最近使用列表
  92. _updateRecentLocations(location);
  93. }
  94. /// 更新最近使用的位置列表
  95. void _updateRecentLocations(Locations location) {
  96. // 移除已存在的位置
  97. _recentLocations.removeWhere((loc) => loc.id == location.id);
  98. // 添加到列表开头
  99. _recentLocations.insert(0, location);
  100. // 保持最多3个最近使用的位置
  101. if (_recentLocations.length > 3) {
  102. _recentLocations.removeRange(3, _recentLocations.length);
  103. }
  104. }
  105. /// 判断位置是否快速
  106. bool isLocationFast(Locations location) {
  107. return location.latency != null && location.latency! < 30;
  108. }
  109. }