custom_character_add_controller.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:easy_refresh/easy_refresh.dart';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:keyboard/base/base_controller.dart';
  4. import 'package:get/get.dart';
  5. import 'package:keyboard/data/repository/characters_repository.dart';
  6. import '../../data/api/response/character_custom_update_response.dart';
  7. import '../../data/bean/character_info.dart';
  8. import '../../data/bean/keyboard_info.dart';
  9. import '../../data/repository/keyboard_repository.dart';
  10. import '../../utils/atmob_log.dart';
  11. import '../../utils/http_handler.dart';
  12. import '../../utils/toast_util.dart';
  13. @injectable
  14. class CustomCharacterAddController extends BaseController {
  15. final String tag = "CustomCharacterAddController";
  16. final CharactersRepository charactersRepository;
  17. final KeyboardRepository keyboardRepository;
  18. final RxInt _currentPage = 1.obs;
  19. final RxInt _currentListCount = 0.obs;
  20. final RxList<CharacterInfo> _characterList = <CharacterInfo>[].obs;
  21. List<CharacterInfo> get characterList => _characterList;
  22. KeyboardInfo currentKeyboardInfo;
  23. late EasyRefreshController refreshController;
  24. @factoryMethod
  25. CustomCharacterAddController(
  26. this.charactersRepository,
  27. this.keyboardRepository, {
  28. @factoryParam required this.currentKeyboardInfo,
  29. });
  30. @override
  31. void onInit() {
  32. super.onInit();
  33. refreshController = EasyRefreshController(
  34. controlFinishLoad: true,
  35. controlFinishRefresh: true,
  36. );
  37. refreshData();
  38. }
  39. @override
  40. void onReady() {
  41. super.onReady();
  42. AtmobLog.d(tag, "onReady");
  43. }
  44. // 下拉刷新
  45. Future<void> refreshData() async {
  46. _currentPage.value = 1;
  47. await getCurrentCharacterListInfo(isRefresh: true);
  48. refreshController.finishRefresh();
  49. refreshController.resetFooter(); // 允许加载更多
  50. }
  51. // 上拉加载更多
  52. Future<void> loadMoreData() async {
  53. if (characterList.length >= _currentListCount.value) {
  54. refreshController.finishLoad(IndicatorResult.noMore);
  55. return;
  56. }
  57. _currentPage.value++;
  58. await getCurrentCharacterListInfo(isRefresh: false);
  59. refreshController.finishLoad(IndicatorResult.success);
  60. }
  61. @override
  62. void onClose() {
  63. super.onClose();
  64. refreshController.dispose();
  65. }
  66. // 获取角色列表
  67. Future<void> getCurrentCharacterListInfo({bool isRefresh = false}) async {
  68. var response = await charactersRepository.getCustomCharactersPage(
  69. pageSize: 10,
  70. page: _currentPage.value,
  71. keyboardId: currentKeyboardInfo.id.toString(),
  72. );
  73. if (response.characterInfos != null) {
  74. if (isRefresh) {
  75. _characterList.value = response.characterInfos!;
  76. } else {
  77. _characterList.addAll(response.characterInfos!);
  78. }
  79. if (response.count != null) {
  80. _currentListCount.value = response.count!;
  81. }
  82. }
  83. }
  84. void itemButtonClick(CharacterInfo characterInfo) async {
  85. AtmobLog.d(tag, 'characterInfo ${characterInfo.toJson()} ');
  86. try {
  87. if (characterInfo.id != null) {
  88. CharacterCustomUpdateResponse characterCustomUpdateResponse =
  89. await charactersRepository.addCustomCharacter(
  90. characterId: characterInfo.id!,
  91. keyboardId: currentKeyboardInfo.id.toString(),
  92. );
  93. int index = characterList.indexWhere(
  94. (element) =>
  95. element.id == characterCustomUpdateResponse.characterInfo.id,
  96. );
  97. if (index != -1) {
  98. characterList[index] = characterCustomUpdateResponse.characterInfo;
  99. }
  100. ToastUtil.show("添加成功");
  101. }
  102. } catch (error) {
  103. if (error is ServerErrorException) {
  104. ToastUtil.show(error.message);
  105. }
  106. }
  107. }
  108. }