character_custom_detail_controller.dart 4.2 KB

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