| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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 '../../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<CharacterInfo> _characterList = <CharacterInfo>[].obs;
- List<CharacterInfo> 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<void> refreshData() async {
- _currentPage.value = 1;
- await getCurrentCharacterListInfo(isRefresh: true);
- refreshController.finishRefresh();
- refreshController.resetFooter(); // 允许加载更多
- }
- // 上拉加载更多
- Future<void> 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<void> 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);
- }
- }
- }
- }
|