import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:injectable/injectable.dart'; import 'package:keyboard/base/base_controller.dart'; import 'package:keyboard/data/bean/character_info.dart'; import 'package:keyboard/data/consts/event_report.dart'; import 'package:keyboard/data/repository/account_repository.dart'; import 'package:keyboard/data/repository/keyboard_repository.dart'; import 'package:keyboard/dialog/character_add_dialog.dart'; import 'package:keyboard/dialog/custom_character/custom_character_add_dialog.dart'; import 'package:keyboard/dialog/login/login_dialog.dart'; import 'package:keyboard/handler/event_handler.dart'; import 'package:keyboard/resource/string.gen.dart'; import 'package:keyboard/utils/atmob_log.dart'; import 'package:keyboard/utils/toast_util.dart'; import '../../data/bean/keyboard_info.dart'; import 'keyboard_type_controller.dart'; enum KeyboardType { system, // 通用键盘 custom, // 自定义键盘 } @injectable class KeyboardManageController extends BaseController with GetTickerProviderStateMixin { final String tag = 'KeyboardManageController'; final AccountRepository accountRepository; final KeyboardRepository keyboardRepository; // 登录状态 RxBool get isLogin => accountRepository.isLogin; // 键盘列表 RxList get customKeyboardInfoList => keyboardRepository.customKeyboardInfoList; RxList get generalKeyboardInfoList => keyboardRepository.generalKeyboardInfoList; // 子控制器 late KeyboardTypeController customKeyboardController; late KeyboardTypeController generalKeyboardController; // UI控制器 late TabController tabController; late PageController pageController; // 键盘管理类型 List keyboardManageType = [ StringName.keyboardCustom, StringName.generalKeyboard, ]; // worker late Worker _keyboardInfoListWorker; // 首次加载数据标志 final isFirstLoad = true.obs; KeyboardManageController(this.keyboardRepository, this.accountRepository) { // 初始化子控制器 customKeyboardController = KeyboardTypeController( tag: '$tag-Custom', keyboardRepository: keyboardRepository, isCustom: true, ); generalKeyboardController = KeyboardTypeController( tag: '$tag-General', keyboardRepository: keyboardRepository, isCustom: false, ); } @override void onInit() async { super.onInit(); // 处理传入的自定义键盘信息 final args = Get.arguments; if (args is Map && args["customKeyboardInfo"] is KeyboardInfo) { customKeyboardController.keyboardInfo.value = args["customKeyboardInfo"]; } await _initializeControllers(); } // 初始化控制器 Future _initializeControllers() async { // 初始化Tab控制器 tabController = TabController( length: keyboardManageType.length, vsync: this, initialIndex: customKeyboardInfoList.isEmpty ? 1 : 0, ); tabController.addListener(() { if (tabController.indexIsChanging) { switchTabKeyboardType(tabController.index); if (tabController.index == 0) { EventHandler.report(EventId.event_13001); } else { EventHandler.report(EventId.event_13003); } } }); // 初始化Page控制器 pageController = PageController( initialPage: customKeyboardInfoList.isEmpty ? 1 : 0, ); // 监听键盘列表变化 _keyboardInfoListWorker = everAll([ customKeyboardInfoList, generalKeyboardInfoList, ], (_) => _loadKeyboardData()); // 首次加载数据 _loadKeyboardData(); } // 加载键盘数据 void _loadKeyboardData() { // 加载自定义键盘数据 if (customKeyboardInfoList.isNotEmpty) { KeyboardInfo keyboard; // 如果已有选择的键盘,就用它 if (customKeyboardController.keyboardInfo.value.id != null) { keyboard = customKeyboardController.keyboardInfo.value; } else { // 否则选择默认选中的或第一个键盘 keyboard = customKeyboardInfoList.firstWhere( (element) => element.isChoose == true, orElse: () => customKeyboardInfoList.first, ); } customKeyboardController.loadKeyboard(keyboard); } else { customKeyboardController.characterList.clear(); customKeyboardController.oldCharacterList = []; customKeyboardController.keyboardInfo.value = KeyboardInfo(); } // 加载通用键盘数据 if (generalKeyboardInfoList.isNotEmpty) { generalKeyboardController.loadKeyboard(generalKeyboardInfoList.first); } } // 切换自定义键盘 void switchCustomKeyboard(String? keyboardName) { if (keyboardName == null) return; KeyboardInfo keyboard = customKeyboardInfoList.firstWhere( (element) => element.name == keyboardName, orElse: () => customKeyboardInfoList.first, ); customKeyboardController.loadKeyboard(keyboard); } // Tab切换 void switchTabKeyboardType(int index) { pageController.animateToPage( index, duration: const Duration(milliseconds: 300), curve: Curves.easeInToLinear, ); } // Page切换 void switchPageKeyboardType(int index) { tabController.animateTo(index, duration: const Duration(milliseconds: 300)); if (index == 0) { // 切换到自定义键盘,重置通用键盘状态 generalKeyboardController.intimacy.value = generalKeyboardController.keyboardInfo.value.intimacy ?? 0; generalKeyboardController.characterList.value = generalKeyboardController.oldCharacterList; generalKeyboardController.intimacyChanged.value = false; generalKeyboardController.characterListChanged.value = false; // 刷新自定义键盘数据 if (customKeyboardController.keyboardInfo.value.id != null) { customKeyboardController.getKeyboardCharacterList( keyboardId: customKeyboardController.keyboardInfo.value.id!, ); } } else { // 切换到通用键盘,重置自定义键盘状态 customKeyboardController.intimacy.value = customKeyboardController.keyboardInfo.value.intimacy ?? 0; if (customKeyboardController.oldCharacterList.isNotEmpty) { customKeyboardController.characterList.value = customKeyboardController.oldCharacterList; } customKeyboardController.intimacyChanged.value = false; customKeyboardController.characterListChanged.value = false; // 刷新通用键盘数据 if (generalKeyboardController.keyboardInfo.value.id != null) { generalKeyboardController.getKeyboardCharacterList( keyboardId: generalKeyboardController.keyboardInfo.value.id!, ); } } } // 点击返回 void clickBack() { AtmobLog.i(tag, 'clickBack'); Get.back(); } // 更新亲密度 void updateIntimacy(int intimacy, bool isCustom) { if (isCustom) { customKeyboardController.updateIntimacy(intimacy); } else { generalKeyboardController.updateIntimacy(intimacy); } } // 排序 void onReorder(int oldIndex, int newIndex, bool isCustom) { if (isCustom) { customKeyboardController.reorderCharacters(oldIndex, newIndex); } else { generalKeyboardController.reorderCharacters(oldIndex, newIndex); } } // 保存 void clickSave(bool isCustom) { final controller = isCustom ? customKeyboardController : generalKeyboardController; final eventId = isCustom ? EventId.event_13002 : EventId.event_13004; EventHandler.report(eventId); controller.saveKeyboardInfo( onSuccess: () { if (isCustom) { saveSuccessGetBack(); } }, ); } // 移除人设 void clickRemoveCharacter(CharacterInfo characterInfo, bool isCustom) { final controller = isCustom ? customKeyboardController : generalKeyboardController; controller.removeCharacter(characterInfo); } // 添加人设 void clickAddCharacter({required bool isCustom}) { if (!isLogin.value) { ToastUtil.show("请先登录"); LoginDialog.show(); return; } final KeyboardInfo currentKeyboard = isCustom ? customKeyboardController.keyboardInfo.value : generalKeyboardController.keyboardInfo.value; CharacterAddDialog.show( currentKeyboardInfo: currentKeyboard, clickCallback: () { if (isCustom) { customKeyboardController.getKeyboardCharacterList( keyboardId: customKeyboardController.keyboardInfo.value.id ?? "", ); } else { generalKeyboardController.getKeyboardCharacterList( keyboardId: generalKeyboardController.keyboardInfo.value.id ?? "", ); } }, ); } // 自定义人设 void clickCustomCharacter() { if (!isLogin.value) { ToastUtil.show("请先登录"); LoginDialog.show(); return; } AtmobLog.i(tag, 'clickCustomCharacter'); CustomCharacterAddDialog.show( currentKeyboardInfo: customKeyboardController.keyboardInfo.value, clickCallback: () { String? keyboardId = customKeyboardController.keyboardInfo.value.id; if (keyboardId != null) { customKeyboardController.getKeyboardCharacterList( keyboardId: keyboardId, ); } }, ); } @override void onClose() { tabController.dispose(); pageController.dispose(); _keyboardInfoListWorker.dispose(); super.onClose(); } void saveSuccessGetBack() { Get.back(result: customKeyboardController.characterList); } }