| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- 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<Hobbies> _hobbiesSelectLabels = <Hobbies>[].obs;
- List<Hobbies> get hobbiesSelectLabels => _hobbiesSelectLabels.toList();
- final RxList<CharactersList> _characterSelectLabels = <CharactersList>[].obs;
- List<CharactersList> 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<String> _boyAvatars = [];
- final List<String> _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<String, dynamic>?;
- 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<void> generateCharacterCustom() async {
- try {
- await charactersRepository.generateCharacterCustom(
- name: _characterCustomName.value,
- gender: 1,
- hobbies:
- _hobbiesSelectLabels
- .map((hobby) => hobby.name)
- .whereType<String>()
- .toList(),
- characters:
- _characterSelectLabels
- .map((character) => character.name)
- .whereType<String>()
- .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);
- }
- }
- }
- }
|