import 'package:easy_refresh/easy_refresh.dart'; import 'package:injectable/injectable.dart'; import 'package:keyboard/base/base_controller.dart'; import 'package:get/get.dart'; import 'package:keyboard/data/repository/characters_repository.dart'; import '../../data/api/response/character_custom_update_response.dart'; import '../../data/bean/character_info.dart'; import '../../data/bean/keyboard_info.dart'; import '../../data/repository/keyboard_repository.dart'; import '../../module/character_custom/character_custom_page.dart'; import '../../utils/atmob_log.dart'; import '../../utils/http_handler.dart'; import '../../utils/toast_util.dart'; @injectable class CustomCharacterAddController extends BaseController { final String tag = "CustomCharacterAddController"; final CharactersRepository charactersRepository; final KeyboardRepository keyboardRepository; final RxInt _currentPage = 1.obs; final RxInt _currentListCount = 0.obs; final RxList _characterList = [].obs; List get characterList => _characterList; KeyboardInfo currentKeyboardInfo; late EasyRefreshController refreshController; @factoryMethod CustomCharacterAddController( this.charactersRepository, this.keyboardRepository, { @factoryParam required this.currentKeyboardInfo, }); @override void onInit() { super.onInit(); refreshController = EasyRefreshController( controlFinishLoad: true, controlFinishRefresh: true, ); refreshData(); } @override void onReady() { super.onReady(); AtmobLog.d(tag, "onReady"); } // 下拉刷新 Future refreshData() async { _currentPage.value = 1; await getCurrentCharacterListInfo(isRefresh: true); refreshController.finishRefresh(); refreshController.resetFooter(); // 允许加载更多 } // 上拉加载更多 Future loadMoreData() async { if (characterList.length >= _currentListCount.value) { refreshController.finishLoad(IndicatorResult.noMore); return; } _currentPage.value++; await getCurrentCharacterListInfo(isRefresh: false); refreshController.finishLoad(IndicatorResult.success); } @override void onClose() { super.onClose(); refreshController.dispose(); } // 获取角色列表 Future getCurrentCharacterListInfo({bool isRefresh = false}) async { var response = await charactersRepository.getCustomCharactersPage( pageSize: 10, page: _currentPage.value, keyboardId: currentKeyboardInfo.id.toString(), ); if (response.characterInfos != null) { if (isRefresh) { _characterList.value = response.characterInfos!; } else { _characterList.addAll(response.characterInfos!); } if (response.count != null) { _currentListCount.value = response.count!; } } } void itemButtonClick(CharacterInfo characterInfo) async { AtmobLog.d(tag, 'characterInfo ${characterInfo.toJson()} '); try { if (characterInfo.id != null) { CharacterCustomUpdateResponse characterCustomUpdateResponse = await charactersRepository.addCustomCharacter( characterId: characterInfo.id!, keyboardId: currentKeyboardInfo.id.toString(), ); int index = characterList.indexWhere( (element) => element.id == characterCustomUpdateResponse.characterInfo.id, ); if (index != -1) { characterList[index] = characterCustomUpdateResponse.characterInfo; } ToastUtil.show("添加成功"); } } catch (error) { if (error is ServerErrorException) { ToastUtil.show(error.message); } } } }