character_custom_detail_controller.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'package:injectable/injectable.dart';
  2. import 'package:keyboard/base/base_controller.dart';
  3. import 'package:keyboard/data/bean/custom_config_info.dart';
  4. import 'package:keyboard/data/repository/characters_repository.dart';
  5. import 'package:get/get.dart';
  6. import 'package:keyboard/data/repository/config_repository.dart';
  7. import 'package:keyboard/module/store/store_page.dart';
  8. import 'package:keyboard/utils/atmob_log.dart';
  9. import '../../../utils/http_handler.dart';
  10. import '../../../utils/toast_util.dart';
  11. // 定制人设详情页
  12. @injectable
  13. class CharacterCustomDetailController extends BaseController {
  14. final String tag = 'CharacterCustomDetailController';
  15. final CharactersRepository charactersRepository;
  16. final ConfigRepository configRepository;
  17. CustomConfigInfo? get currentCharacterCustomConfig =>
  18. configRepository.characterCustomConfig;
  19. final RxList<Hobbies> _hobbiesSelectLabels = <Hobbies>[].obs;
  20. List<Hobbies> get hobbiesSelectLabels => _hobbiesSelectLabels.toList();
  21. final RxList<CharactersList> _characterSelectLabels = <CharactersList>[].obs;
  22. List<CharactersList> get characterSelectLabels =>
  23. _characterSelectLabels.toList();
  24. final RxString _characterCustomName = "".obs;
  25. String get characterCustomName => _characterCustomName.value;
  26. final RxInt _gender = 1.obs;
  27. final RxString _birthday = "".obs;
  28. final RxString _avatarUrl = "".obs;
  29. String get avatarUrl => _avatarUrl.value;
  30. final List<String> _boyAvatars = [];
  31. final List<String> _girlAvatars = [];
  32. CharacterCustomDetailController(
  33. this.charactersRepository,
  34. this.configRepository,
  35. );
  36. @override
  37. void onInit() {
  38. _getArgs();
  39. super.onInit();
  40. initData();
  41. }
  42. void initData() {
  43. AtmobLog.d(tag, "initData");
  44. _boyAvatars.addAll(currentCharacterCustomConfig?.boyAvatars ?? []);
  45. _girlAvatars.addAll(currentCharacterCustomConfig?.girlAvatars ?? []);
  46. if (_gender.value == 1) {
  47. _avatarUrl.value = _boyAvatars[0];
  48. } else {
  49. _avatarUrl.value = _girlAvatars[0];
  50. }
  51. }
  52. void nextAvatar() {
  53. AtmobLog.d(tag, "nextAvatar");
  54. if (_gender.value == 1) {
  55. int currentIndex = _boyAvatars.indexOf(_avatarUrl.value);
  56. _avatarUrl.value = _boyAvatars[(currentIndex + 1) % _boyAvatars.length];
  57. } else {
  58. int currentIndex = _girlAvatars.indexOf(_avatarUrl.value);
  59. _avatarUrl.value = _girlAvatars[(currentIndex + 1) % _girlAvatars.length];
  60. }
  61. }
  62. void _getArgs() {
  63. final arguments = Get.arguments as Map<String, dynamic>?;
  64. if (arguments?['hobbiesSelectLabels'] == null) {
  65. AtmobLog.i(tag, '没有传递 hobbiesSelectLabels 参数');
  66. } else {
  67. _hobbiesSelectLabels.assignAll(arguments?['hobbiesSelectLabels'] ?? []);
  68. AtmobLog.i(tag, "hobbiesSelectLabels: $hobbiesSelectLabels");
  69. }
  70. if (arguments?['characterSelectLabels'] == null) {
  71. AtmobLog.i(tag, '没有传递 characterSelectLabels 参数');
  72. } else {
  73. _characterSelectLabels.assignAll(
  74. arguments?['characterSelectLabels'] ?? [],
  75. );
  76. AtmobLog.i(tag, "characterSelectLabels: $characterSelectLabels");
  77. }
  78. if (arguments?['characterCustomName'] == null) {
  79. AtmobLog.i(tag, '警告: 没有传递 characterCustomName 参数');
  80. } else {
  81. _characterCustomName(arguments?['characterCustomName'] ?? '');
  82. AtmobLog.i(tag, "characterCustomName: $characterCustomName");
  83. }
  84. }
  85. @override
  86. void onReady() {
  87. super.onReady();
  88. }
  89. @override
  90. void onClose() {
  91. super.onClose();
  92. }
  93. void clickBack() {
  94. Get.back();
  95. }
  96. void clickUnlockButton() {
  97. AtmobLog.d(tag, "点击解锁按钮,生成专属人设");
  98. generateCharacterCustom();
  99. }
  100. // 生成定制人设
  101. Future<void> generateCharacterCustom() async {
  102. try {
  103. await charactersRepository.generateCharacterCustom(
  104. name: _characterCustomName.value,
  105. gender: 1,
  106. hobbies:
  107. _hobbiesSelectLabels
  108. .map((hobby) => hobby.name)
  109. .whereType<String>()
  110. .toList(),
  111. characters:
  112. _characterSelectLabels
  113. .map((character) => character.name)
  114. .whereType<String>()
  115. .toList(),
  116. birthday: _birthday.value,
  117. imageUrl: _avatarUrl.value,
  118. );
  119. } catch (error) {
  120. if (error is ServerErrorException && error.code == 1005) {
  121. ToastUtil.show('请开通会员解锁权益~');
  122. StorePage.start();
  123. }
  124. if (error is ServerErrorException) {
  125. ToastUtil.show(error.message);
  126. }
  127. }
  128. }
  129. }