custom_character_add_controller.dart 3.7 KB

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