import 'package:injectable/injectable.dart'; import 'package:keyboard/base/base_controller.dart'; import 'package:keyboard/data/bean/custom_config_info.dart'; import 'package:keyboard/data/repository/characters_repository.dart'; import 'package:get/get.dart'; import 'package:keyboard/data/repository/config_repository.dart'; import 'package:keyboard/module/store/store_page.dart'; import 'package:keyboard/utils/atmob_log.dart'; import '../../../utils/http_handler.dart'; import '../../../utils/toast_util.dart'; // 定制人设详情页 @injectable class CharacterCustomDetailController extends BaseController { final String tag = 'CharacterCustomDetailController'; final CharactersRepository charactersRepository; final ConfigRepository configRepository; CustomConfigInfo? get currentCharacterCustomConfig => configRepository.characterCustomConfig; final RxList _hobbiesSelectLabels = [].obs; List get hobbiesSelectLabels => _hobbiesSelectLabels.toList(); final RxList _characterSelectLabels = [].obs; List get characterSelectLabels => _characterSelectLabels.toList(); final RxString _characterCustomName = "".obs; String get characterCustomName => _characterCustomName.value; final RxInt _gender = 1.obs; final RxString _birthday = "".obs; final RxString _avatarUrl = "".obs; String get avatarUrl => _avatarUrl.value; final List _boyAvatars = []; final List _girlAvatars = []; CharacterCustomDetailController( this.charactersRepository, this.configRepository, ); @override void onInit() { _getArgs(); super.onInit(); initData(); } void initData() { AtmobLog.d(tag, "initData"); _boyAvatars.addAll(currentCharacterCustomConfig?.boyAvatars ?? []); _girlAvatars.addAll(currentCharacterCustomConfig?.girlAvatars ?? []); if (_gender.value == 1) { _avatarUrl.value = _boyAvatars[0]; } else { _avatarUrl.value = _girlAvatars[0]; } } void nextAvatar() { AtmobLog.d(tag, "nextAvatar"); if (_gender.value == 1) { int currentIndex = _boyAvatars.indexOf(_avatarUrl.value); _avatarUrl.value = _boyAvatars[(currentIndex + 1) % _boyAvatars.length]; } else { int currentIndex = _girlAvatars.indexOf(_avatarUrl.value); _avatarUrl.value = _girlAvatars[(currentIndex + 1) % _girlAvatars.length]; } } void _getArgs() { final arguments = Get.arguments as Map?; if (arguments?['hobbiesSelectLabels'] == null) { AtmobLog.i(tag, '没有传递 hobbiesSelectLabels 参数'); } else { _hobbiesSelectLabels.assignAll(arguments?['hobbiesSelectLabels'] ?? []); AtmobLog.i(tag, "hobbiesSelectLabels: $hobbiesSelectLabels"); } if (arguments?['characterSelectLabels'] == null) { AtmobLog.i(tag, '没有传递 characterSelectLabels 参数'); } else { _characterSelectLabels.assignAll( arguments?['characterSelectLabels'] ?? [], ); AtmobLog.i(tag, "characterSelectLabels: $characterSelectLabels"); } if (arguments?['characterCustomName'] == null) { AtmobLog.i(tag, '警告: 没有传递 characterCustomName 参数'); } else { _characterCustomName(arguments?['characterCustomName'] ?? ''); AtmobLog.i(tag, "characterCustomName: $characterCustomName"); } } @override void onReady() { super.onReady(); } @override void onClose() { super.onClose(); } void clickBack() { Get.back(); } void clickUnlockButton() { AtmobLog.d(tag, "点击解锁按钮,生成专属人设"); generateCharacterCustom(); } // 生成定制人设 Future generateCharacterCustom() async { try { await charactersRepository.generateCharacterCustom( name: _characterCustomName.value, gender: 1, hobbies: _hobbiesSelectLabels .map((hobby) => hobby.name) .whereType() .toList(), characters: _characterSelectLabels .map((character) => character.name) .whereType() .toList(), birthday: _birthday.value, imageUrl: _avatarUrl.value, ); } catch (error) { if (error is ServerErrorException && error.code == 1005) { ToastUtil.show('请开通会员解锁权益~'); StorePage.start(); } if (error is ServerErrorException) { ToastUtil.show(error.message); } } } }