node_controller.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import 'package:get/get.dart';
  2. import '../../../data/models/launch/groups.dart';
  3. import '../../../data/sp/ix_sp.dart';
  4. class NodeController extends GetxController {
  5. // Groups 数据
  6. final _groups = Rxn<Groups>();
  7. Groups? get groups => _groups.value;
  8. set groups(Groups? value) => _groups.value = value;
  9. // 游戏tab列表
  10. final _tabTextList = <String>[].obs;
  11. List<String> get tabTextList => _tabTextList;
  12. set tabTextList(List<String> value) => _tabTextList.assignAll(value);
  13. // 当前选中的 Tab 索引
  14. final _currentTabIndex = 0.obs;
  15. int get currentTabIndex => _currentTabIndex.value;
  16. set currentTabIndex(int value) => _currentTabIndex.value = value;
  17. // 保存每个 tab 中每个国家的展开/收缩状态
  18. // key 格式: "tabIndex_countryCode"
  19. final Map<String, bool> expandedStates = {};
  20. @override
  21. void onInit() {
  22. super.onInit();
  23. _loadGroups();
  24. }
  25. /// 加载 Groups 数据
  26. void _loadGroups() {
  27. final launch = IXSP.getLaunch();
  28. if (launch?.groups != null) {
  29. groups = launch!.groups;
  30. _updateTabList();
  31. }
  32. }
  33. /// 更新 Tab 列表
  34. void _updateTabList() {
  35. final tabs = <String>[];
  36. // 添加 Normal tab
  37. if (groups?.normal != null && groups!.normal!.list?.isNotEmpty == true) {
  38. tabs.add('All');
  39. }
  40. // 添加 Streaming tab
  41. if (groups?.streaming != null &&
  42. groups!.streaming!.list?.isNotEmpty == true) {
  43. tabs.add('Streaming');
  44. }
  45. tabTextList = tabs;
  46. }
  47. /// 根据 tab 索引获取对应的数据
  48. Normal? getDataByTabIndex(int tabIndex) {
  49. if (tabIndex == 0) {
  50. return groups?.normal;
  51. } else if (tabIndex == 1) {
  52. return groups?.streaming;
  53. }
  54. return null;
  55. }
  56. /// 获取国家的展开状态
  57. bool getExpandedState(int tabIndex, String countryCode) {
  58. final key = '${tabIndex}_$countryCode';
  59. return expandedStates[key] ?? false; // 默认收缩
  60. }
  61. /// 设置国家的展开状态
  62. void setExpandedState(int tabIndex, String countryCode, bool expanded) {
  63. final key = '${tabIndex}_$countryCode';
  64. expandedStates[key] = expanded;
  65. }
  66. }