keyboard_manage_controller.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/character_info.dart';
  6. import 'package:keyboard/data/consts/event_report.dart';
  7. import 'package:keyboard/data/repository/account_repository.dart';
  8. import 'package:keyboard/data/repository/keyboard_repository.dart';
  9. import 'package:keyboard/dialog/character_add_dialog.dart';
  10. import 'package:keyboard/dialog/custom_character/custom_character_add_dialog.dart';
  11. import 'package:keyboard/dialog/login/login_dialog.dart';
  12. import 'package:keyboard/handler/event_handler.dart';
  13. import 'package:keyboard/resource/string.gen.dart';
  14. import 'package:keyboard/utils/atmob_log.dart';
  15. import 'package:keyboard/utils/toast_util.dart';
  16. import '../../data/bean/keyboard_info.dart';
  17. import 'keyboard_type_controller.dart';
  18. enum KeyboardType {
  19. system, // 通用键盘
  20. custom, // 自定义键盘
  21. }
  22. @injectable
  23. class KeyboardManageController extends BaseController
  24. with GetTickerProviderStateMixin {
  25. final String tag = 'KeyboardManageController';
  26. final AccountRepository accountRepository;
  27. final KeyboardRepository keyboardRepository;
  28. // 登录状态
  29. RxBool get isLogin => accountRepository.isLogin;
  30. // 键盘列表
  31. RxList<KeyboardInfo> get customKeyboardInfoList =>
  32. keyboardRepository.customKeyboardInfoList;
  33. RxList<KeyboardInfo> get generalKeyboardInfoList =>
  34. keyboardRepository.generalKeyboardInfoList;
  35. // 子控制器
  36. late KeyboardTypeController customKeyboardController;
  37. late KeyboardTypeController generalKeyboardController;
  38. // UI控制器
  39. late TabController tabController;
  40. late PageController pageController;
  41. // 键盘管理类型
  42. List<String> keyboardManageType = [
  43. StringName.keyboardCustom,
  44. StringName.generalKeyboard,
  45. ];
  46. // worker
  47. late Worker _keyboardInfoListWorker;
  48. // 首次加载数据标志
  49. final isFirstLoad = true.obs;
  50. KeyboardManageController(this.keyboardRepository, this.accountRepository) {
  51. // 初始化子控制器
  52. customKeyboardController = KeyboardTypeController(
  53. tag: '$tag-Custom',
  54. keyboardRepository: keyboardRepository,
  55. isCustom: true,
  56. );
  57. generalKeyboardController = KeyboardTypeController(
  58. tag: '$tag-General',
  59. keyboardRepository: keyboardRepository,
  60. isCustom: false,
  61. );
  62. }
  63. @override
  64. void onInit() async {
  65. super.onInit();
  66. // 处理传入的自定义键盘信息
  67. final args = Get.arguments;
  68. if (args is Map && args["customKeyboardInfo"] is KeyboardInfo) {
  69. customKeyboardController.keyboardInfo.value = args["customKeyboardInfo"];
  70. }
  71. await _initializeControllers();
  72. }
  73. // 初始化控制器
  74. Future<void> _initializeControllers() async {
  75. // 初始化Tab控制器
  76. tabController = TabController(
  77. length: keyboardManageType.length,
  78. vsync: this,
  79. initialIndex: customKeyboardInfoList.isEmpty ? 1 : 0,
  80. );
  81. tabController.addListener(() {
  82. if (tabController.indexIsChanging) {
  83. switchTabKeyboardType(tabController.index);
  84. if (tabController.index == 0) {
  85. EventHandler.report(EventId.event_13001);
  86. } else {
  87. EventHandler.report(EventId.event_13003);
  88. }
  89. }
  90. });
  91. // 初始化Page控制器
  92. pageController = PageController(
  93. initialPage: customKeyboardInfoList.isEmpty ? 1 : 0,
  94. );
  95. // 监听键盘列表变化
  96. _keyboardInfoListWorker = everAll([
  97. customKeyboardInfoList,
  98. generalKeyboardInfoList,
  99. ], (_) => _loadKeyboardData());
  100. // 首次加载数据
  101. _loadKeyboardData();
  102. }
  103. // 加载键盘数据
  104. void _loadKeyboardData() {
  105. // 加载自定义键盘数据
  106. if (customKeyboardInfoList.isNotEmpty) {
  107. KeyboardInfo keyboard;
  108. // 如果已有选择的键盘,就用它
  109. if (customKeyboardController.keyboardInfo.value.id != null) {
  110. keyboard = customKeyboardController.keyboardInfo.value;
  111. } else {
  112. // 否则选择默认选中的或第一个键盘
  113. keyboard = customKeyboardInfoList.firstWhere(
  114. (element) => element.isChoose == true,
  115. orElse: () => customKeyboardInfoList.first,
  116. );
  117. }
  118. customKeyboardController.loadKeyboard(keyboard);
  119. } else {
  120. customKeyboardController.characterList.clear();
  121. customKeyboardController.oldCharacterList = [];
  122. customKeyboardController.keyboardInfo.value = KeyboardInfo();
  123. }
  124. // 加载通用键盘数据
  125. if (generalKeyboardInfoList.isNotEmpty) {
  126. generalKeyboardController.loadKeyboard(generalKeyboardInfoList.first);
  127. }
  128. }
  129. // 切换自定义键盘
  130. void switchCustomKeyboard(String? keyboardName) {
  131. if (keyboardName == null) return;
  132. KeyboardInfo keyboard = customKeyboardInfoList.firstWhere(
  133. (element) => element.name == keyboardName,
  134. orElse: () => customKeyboardInfoList.first,
  135. );
  136. customKeyboardController.loadKeyboard(keyboard);
  137. }
  138. // Tab切换
  139. void switchTabKeyboardType(int index) {
  140. pageController.animateToPage(
  141. index,
  142. duration: const Duration(milliseconds: 300),
  143. curve: Curves.easeInToLinear,
  144. );
  145. }
  146. // Page切换
  147. void switchPageKeyboardType(int index) {
  148. tabController.animateTo(index, duration: const Duration(milliseconds: 300));
  149. if (index == 0) {
  150. // 切换到自定义键盘,重置通用键盘状态
  151. generalKeyboardController.intimacy.value =
  152. generalKeyboardController.keyboardInfo.value.intimacy ?? 0;
  153. generalKeyboardController.characterList.value =
  154. generalKeyboardController.oldCharacterList;
  155. generalKeyboardController.intimacyChanged.value = false;
  156. generalKeyboardController.characterListChanged.value = false;
  157. // 刷新自定义键盘数据
  158. if (customKeyboardController.keyboardInfo.value.id != null) {
  159. customKeyboardController.getKeyboardCharacterList(
  160. keyboardId: customKeyboardController.keyboardInfo.value.id!,
  161. );
  162. }
  163. } else {
  164. // 切换到通用键盘,重置自定义键盘状态
  165. customKeyboardController.intimacy.value =
  166. customKeyboardController.keyboardInfo.value.intimacy ?? 0;
  167. if (customKeyboardController.oldCharacterList.isNotEmpty) {
  168. customKeyboardController.characterList.value =
  169. customKeyboardController.oldCharacterList;
  170. }
  171. customKeyboardController.intimacyChanged.value = false;
  172. customKeyboardController.characterListChanged.value = false;
  173. // 刷新通用键盘数据
  174. if (generalKeyboardController.keyboardInfo.value.id != null) {
  175. generalKeyboardController.getKeyboardCharacterList(
  176. keyboardId: generalKeyboardController.keyboardInfo.value.id!,
  177. );
  178. }
  179. }
  180. }
  181. // 点击返回
  182. void clickBack() {
  183. AtmobLog.i(tag, 'clickBack');
  184. Get.back();
  185. }
  186. // 更新亲密度
  187. void updateIntimacy(int intimacy, bool isCustom) {
  188. if (isCustom) {
  189. customKeyboardController.updateIntimacy(intimacy);
  190. } else {
  191. generalKeyboardController.updateIntimacy(intimacy);
  192. }
  193. }
  194. // 排序
  195. void onReorder(int oldIndex, int newIndex, bool isCustom) {
  196. if (isCustom) {
  197. customKeyboardController.reorderCharacters(oldIndex, newIndex);
  198. } else {
  199. generalKeyboardController.reorderCharacters(oldIndex, newIndex);
  200. }
  201. }
  202. // 保存
  203. void clickSave(bool isCustom) {
  204. final controller =
  205. isCustom ? customKeyboardController : generalKeyboardController;
  206. final eventId = isCustom ? EventId.event_13002 : EventId.event_13004;
  207. EventHandler.report(eventId);
  208. controller.saveKeyboardInfo(
  209. onSuccess: () {
  210. if (isCustom) {
  211. saveSuccessGetBack();
  212. }
  213. },
  214. );
  215. }
  216. // 移除人设
  217. void clickRemoveCharacter(CharacterInfo characterInfo, bool isCustom) {
  218. final controller =
  219. isCustom ? customKeyboardController : generalKeyboardController;
  220. controller.removeCharacter(characterInfo);
  221. }
  222. // 添加人设
  223. void clickAddCharacter({required bool isCustom}) {
  224. if (!isLogin.value) {
  225. ToastUtil.show("请先登录");
  226. LoginDialog.show();
  227. return;
  228. }
  229. final KeyboardInfo currentKeyboard =
  230. isCustom
  231. ? customKeyboardController.keyboardInfo.value
  232. : generalKeyboardController.keyboardInfo.value;
  233. CharacterAddDialog.show(
  234. currentKeyboardInfo: currentKeyboard,
  235. clickCallback: () {
  236. if (isCustom) {
  237. customKeyboardController.getKeyboardCharacterList(
  238. keyboardId: customKeyboardController.keyboardInfo.value.id ?? "",
  239. );
  240. } else {
  241. generalKeyboardController.getKeyboardCharacterList(
  242. keyboardId: generalKeyboardController.keyboardInfo.value.id ?? "",
  243. );
  244. }
  245. },
  246. );
  247. }
  248. // 自定义人设
  249. void clickCustomCharacter() {
  250. if (!isLogin.value) {
  251. ToastUtil.show("请先登录");
  252. LoginDialog.show();
  253. return;
  254. }
  255. AtmobLog.i(tag, 'clickCustomCharacter');
  256. CustomCharacterAddDialog.show(
  257. currentKeyboardInfo: customKeyboardController.keyboardInfo.value,
  258. clickCallback: () {
  259. String? keyboardId = customKeyboardController.keyboardInfo.value.id;
  260. if (keyboardId != null) {
  261. customKeyboardController.getKeyboardCharacterList(
  262. keyboardId: keyboardId,
  263. );
  264. }
  265. },
  266. );
  267. }
  268. @override
  269. void onClose() {
  270. tabController.dispose();
  271. pageController.dispose();
  272. _keyboardInfoListWorker.dispose();
  273. super.onClose();
  274. }
  275. void saveSuccessGetBack() {
  276. Get.back(result: customKeyboardController.characterList);
  277. }
  278. }