home_controller.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. @override
  42. void onInit() {
  43. super.onInit();
  44. _initializeLocations();
  45. }
  46. /// 初始化位置数据
  47. void _initializeLocations() {
  48. // 模拟数据 - 实际应用中应该从API获取
  49. _allLocations.addAll([
  50. const Locations(
  51. id: 1,
  52. name: 'LosAngles',
  53. code: 'US',
  54. country: 'United States',
  55. latency: 15,
  56. ),
  57. const Locations(
  58. id: 2,
  59. name: 'Singapore',
  60. code: 'SG',
  61. country: 'Singapore',
  62. latency: 25,
  63. ),
  64. const Locations(
  65. id: 3,
  66. name: 'Japan',
  67. code: 'JP',
  68. country: 'Japan',
  69. latency: 35,
  70. ),
  71. const Locations(
  72. id: 4,
  73. name: 'Madrid',
  74. code: 'ES',
  75. country: 'Spain',
  76. latency: 30,
  77. ),
  78. ]);
  79. // 设置默认选择的位置
  80. selectedLocation = _allLocations.first;
  81. // 设置最近使用的位置
  82. _recentLocations.addAll(_allLocations.skip(1).take(3));
  83. }
  84. /// 选择位置
  85. void selectLocation(Locations location) {
  86. selectedLocation = location;
  87. // 更新最近使用列表
  88. _updateRecentLocations(location);
  89. }
  90. /// 更新最近使用的位置列表
  91. void _updateRecentLocations(Locations location) {
  92. // 移除已存在的位置
  93. _recentLocations.removeWhere((loc) => loc.id == location.id);
  94. // 添加到列表开头
  95. _recentLocations.insert(0, location);
  96. // 保持最多3个最近使用的位置
  97. if (_recentLocations.length > 3) {
  98. _recentLocations.removeRange(3, _recentLocations.length);
  99. }
  100. }
  101. /// 判断位置是否快速
  102. bool isLocationFast(Locations location) {
  103. return location.latency != null && location.latency! < 30;
  104. }
  105. }