character_controller.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. ever(keyboardInfoList, (value) {
  60. AtmobLog.d(tag, "keyboardInfoList1 changed ${keyboardInfoList.length}");
  61. if (value.isNotEmpty) {
  62. currentKeyboardInfo.value = keyboardInfoList.first;
  63. print("currentKeyboardInfo.value: ${currentKeyboardInfo.value}");
  64. tabController.value = TabController(
  65. length: characterGroupList.length,
  66. vsync: this,
  67. initialIndex: currentTabBarIndex.value,
  68. );
  69. }
  70. });
  71. }
  72. void onTabChanged(int index) {
  73. if (index >= characterGroupList.length) {
  74. return;
  75. }
  76. pageController.animateToPage(
  77. index,
  78. duration: const Duration(milliseconds: 300),
  79. curve: Curves.easeInToLinear,
  80. );
  81. }
  82. void onPageChanged(int index) {
  83. if (index >= characterGroupList.length) {
  84. return;
  85. }
  86. currentTabBarIndex.value = index;
  87. currentCharacterGroupInfo.value = characterGroupList[index];
  88. tabController.value?.animateTo(
  89. index,
  90. duration: const Duration(milliseconds: 300),
  91. );
  92. }
  93. @override
  94. void onReady() {
  95. super.onReady();
  96. EventHandler.report(EventId.event_11000);
  97. }
  98. @override
  99. void onClose() {
  100. pageController.dispose();
  101. tabController.value?.dispose();
  102. super.onClose();
  103. }
  104. void clickMyKeyboard() {
  105. AtmobLog.d(tag, "clickMyKeyboard");
  106. EventHandler.report(EventId.event_13000);
  107. KeyboardManagePage.start();
  108. }
  109. void clickCustomCharacter() {
  110. AtmobLog.d(tag, "clickCustomCharacter");
  111. EventHandler.report(EventId.event_12000);
  112. CharacterCustomPage.start();
  113. }
  114. // 切换键盘
  115. void switchKeyboard(String? newValue) {
  116. currentKeyboardInfo.value = keyboardInfoList.firstWhere(
  117. (element) => element.name == newValue,
  118. orElse: () => keyboardInfoList.first,
  119. );
  120. }
  121. }