character_controller.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/base/base_controller.dart';
  5. import 'package:keyboard/data/bean/keyboard_info.dart';
  6. import 'package:keyboard/data/repository/account_repository.dart';
  7. import 'package:keyboard/data/repository/config_repository.dart';
  8. import 'package:keyboard/module/keyboard_manage/keyboard_manage_page.dart';
  9. import 'package:keyboard/utils/atmob_log.dart';
  10. import '../../data/api/response/user_info_response.dart';
  11. import '../../data/bean/character_group_info.dart';
  12. import '../../data/consts/event_report.dart';
  13. import '../../data/repository/characters_repository.dart';
  14. import '../../data/repository/keyboard_repository.dart';
  15. import '../../handler/event_handler.dart';
  16. import '../character_custom/character_custom_page.dart';
  17. @injectable
  18. class CharacterController extends BaseController
  19. with GetTickerProviderStateMixin {
  20. final String tag = "CharacterController";
  21. final CharactersRepository charactersRepository;
  22. final ConfigRepository configRepository;
  23. final KeyboardRepository keyboardRepository;
  24. final AccountRepository accountRepository;
  25. Rxn<UserInfoResponse> get userInfo => accountRepository.userInfo;
  26. CharacterController(
  27. this.accountRepository,
  28. this.charactersRepository,
  29. this.configRepository,
  30. this.keyboardRepository,
  31. );
  32. // 人设主题
  33. List<CharacterGroupInfo> get characterGroupList =>
  34. charactersRepository.characterGroupList;
  35. // 键盘列表
  36. RxList<KeyboardInfo> get keyboardInfoList =>
  37. keyboardRepository.keyboardInfoList;
  38. Rx<TabController?> tabController = Rx(null);
  39. PageController pageController = PageController();
  40. RxInt currentTabBarIndex = 0.obs;
  41. Rx<CharacterGroupInfo> currentCharacterGroupInfo = CharacterGroupInfo().obs;
  42. Rx<KeyboardInfo> currentKeyboardInfo = KeyboardInfo().obs;
  43. @override
  44. void onInit() {
  45. super.onInit();
  46. if (characterGroupList.isNotEmpty) {
  47. currentCharacterGroupInfo.value = characterGroupList.first;
  48. }
  49. if (keyboardInfoList.isNotEmpty) {
  50. currentKeyboardInfo.value = keyboardInfoList.firstWhere(
  51. (element) => element.isChoose == true,
  52. orElse: () => keyboardInfoList.first,
  53. );
  54. }
  55. _dataLoad();
  56. }
  57. void _dataLoad() async {
  58. AtmobLog.d(tag, "_dataLoad");
  59. tabController.value = TabController(
  60. length: characterGroupList.length,
  61. vsync: this,
  62. initialIndex: 0,
  63. );
  64. debugPrint("qqq tabController: ${tabController.hashCode}");
  65. // ever(charactersRepository.characterGroupList, (value) {
  66. // AtmobLog.d(tag, "characterGroupList changed");
  67. // if (value.isNotEmpty) {
  68. // tabController.value?.dispose();
  69. // tabController.value = TabController(
  70. // length: characterGroupList.length,
  71. // vsync: this,
  72. // initialIndex: 0,
  73. // );
  74. // currentCharacterGroupInfo.value = characterGroupList.first;
  75. // AtmobLog.d(
  76. // tag,
  77. // "currentCharacterGroupInfo.value: ${characterGroupList.first.id}",
  78. // );
  79. // }
  80. // });
  81. ever(keyboardInfoList, (value) {
  82. AtmobLog.d(tag, "keyboardInfoList1 changed ${keyboardInfoList.length}");
  83. if (value.isNotEmpty) {
  84. currentKeyboardInfo.value = keyboardInfoList.first;
  85. print("currentKeyboardInfo.value: ${currentKeyboardInfo.value}");
  86. }
  87. });
  88. }
  89. void onTabChanged(int index) {
  90. if (index >= characterGroupList.length) {
  91. return;
  92. }
  93. pageController.animateToPage(
  94. index,
  95. duration: const Duration(milliseconds: 300),
  96. curve: Curves.easeInToLinear,
  97. );
  98. }
  99. void onPageChanged(int index) {
  100. if (index >= characterGroupList.length) {
  101. return;
  102. }
  103. currentTabBarIndex.value = index;
  104. currentCharacterGroupInfo.value = characterGroupList[index];
  105. tabController.value?.animateTo(
  106. index,
  107. duration: const Duration(milliseconds: 300),
  108. );
  109. }
  110. @override
  111. void onReady() {
  112. super.onReady();
  113. EventHandler.report(EventId.event_11000);
  114. }
  115. @override
  116. void onClose() {
  117. pageController.dispose();
  118. tabController.value?.dispose();
  119. super.onClose();
  120. }
  121. void clickMyKeyboard() {
  122. AtmobLog.d(tag, "clickMyKeyboard");
  123. EventHandler.report(EventId.event_13000);
  124. KeyboardManagePage.start();
  125. }
  126. void clickCustomCharacter() {
  127. AtmobLog.d(tag, "clickCustomCharacter");
  128. EventHandler.report(EventId.event_12000);
  129. CharacterCustomPage.start();
  130. }
  131. // 切换键盘
  132. void switchKeyboard(String? newValue) {
  133. currentKeyboardInfo.value = keyboardInfoList.firstWhere(
  134. (element) => element.name == newValue,
  135. orElse: () => keyboardInfoList.first,
  136. );
  137. }
  138. }