character_custom_detail_controller.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import 'package:injectable/injectable.dart';
  2. import 'package:keyboard/base/base_controller.dart';
  3. import 'package:keyboard/data/bean/character_info.dart';
  4. import 'package:keyboard/data/bean/custom_config_info.dart';
  5. import 'package:keyboard/data/repository/characters_repository.dart';
  6. import 'package:get/get.dart';
  7. import 'package:keyboard/data/repository/config_repository.dart';
  8. import 'package:keyboard/module/store/store_page.dart';
  9. import 'package:keyboard/utils/atmob_log.dart';
  10. import '../../../resource/assets.gen.dart';
  11. import '../../../utils/age_zodiac_sign_util.dart';
  12. import '../../../utils/error_handler.dart';
  13. import '../../../utils/http_handler.dart';
  14. import '../../../utils/toast_util.dart';
  15. import '../../change/birthday/change_birthday_page.dart';
  16. import '../../change/character/change_character_page.dart';
  17. import '../../change/gender/change_gender_page.dart';
  18. import '../../change/hobbies/change_hobbies_page.dart';
  19. import '../../change/nickname/change_nickname_page.dart';
  20. // 定制人设详情页
  21. enum CharacterCustomEditMode {
  22. add, // 新增
  23. edit, // 编辑
  24. }
  25. @injectable
  26. class CharacterCustomDetailController extends BaseController {
  27. final String tag = 'CharacterCustomDetailController';
  28. final CharactersRepository charactersRepository;
  29. final ConfigRepository configRepository;
  30. CustomConfigInfo? get currentCharacterCustomConfig =>
  31. configRepository.characterCustomConfig.value;
  32. final RxList<Hobbies> _hobbiesSelectLabels = <Hobbies>[].obs;
  33. RxList<Hobbies> get hobbiesSelectLabels => _hobbiesSelectLabels;
  34. final RxList<CharactersList> _characterSelectLabels = <CharactersList>[].obs;
  35. RxList<CharactersList> get characterSelectLabels => _characterSelectLabels;
  36. final Rx<CharacterInfo> _currentCharacterInfo = CharacterInfo().obs;
  37. final RxString _currentNickname = "".obs;
  38. String get currentNickname => _currentNickname.value;
  39. final RxnInt _currentGender = RxnInt(null);
  40. final RxnString _currentBirthday = RxnString(null);
  41. String? get currentBirthday =>
  42. AgeZodiacSignUtil.formatBirthdayFromString(_currentBirthday.value);
  43. final RxString _avatarUrl = "".obs;
  44. String get avatarUrl => _avatarUrl.value;
  45. final List<String> _boyAvatars = [];
  46. final List<String> _girlAvatars = [];
  47. late final CharacterCustomEditMode mode;
  48. bool get isEditMode => mode == CharacterCustomEditMode.edit;
  49. bool get isAddMode => mode == CharacterCustomEditMode.add;
  50. final RxList<Hobbies> hobbiesLabelsList = <Hobbies>[].obs;
  51. final RxList<CharactersList> characterLabelsList = <CharactersList>[].obs;
  52. CharacterCustomDetailController(
  53. this.charactersRepository,
  54. this.configRepository,
  55. );
  56. @override
  57. void onInit() {
  58. super.onInit();
  59. initData();
  60. _getArgs();
  61. }
  62. void initData() {
  63. AtmobLog.d(tag, "initData");
  64. _boyAvatars.addAll(currentCharacterCustomConfig?.boyAvatars ?? []);
  65. _girlAvatars.addAll(currentCharacterCustomConfig?.girlAvatars ?? []);
  66. hobbiesLabelsList.value = currentCharacterCustomConfig?.hobbies ?? [];
  67. characterLabelsList.value = currentCharacterCustomConfig?.characters ?? [];
  68. if (_currentGender.value == 1) {
  69. _avatarUrl.value = _boyAvatars[0];
  70. } else {
  71. _avatarUrl.value = _girlAvatars[0];
  72. }
  73. }
  74. void _getArgs() {
  75. final arguments = Get.arguments as Map<String, dynamic>?;
  76. if (arguments?['currentCharacterInfo'] == null) {
  77. AtmobLog.i(tag, '没有传递 currentCharacterInfo 参数');
  78. } else {
  79. mode = CharacterCustomEditMode.edit;
  80. _currentCharacterInfo.value = arguments?['currentCharacterInfo'];
  81. List<String>? hobbies = _currentCharacterInfo.value.hobbies;
  82. print("hobbies: $hobbies");
  83. if (hobbies != null) {
  84. for (var hobby in hobbies) {
  85. final exists = hobbiesLabelsList.any((e) => e.name == hobby);
  86. if (!exists) {
  87. hobbiesLabelsList.add(Hobbies(name: hobby));
  88. }
  89. final selectedHobby = hobbiesLabelsList.firstWhere(
  90. (e) => e.name == hobby,
  91. orElse: () => Hobbies(name: hobby),
  92. );
  93. if (selectedHobby.name != null) {
  94. _hobbiesSelectLabels.add(selectedHobby);
  95. }
  96. }
  97. }
  98. List<String>? characters = _currentCharacterInfo.value.characters;
  99. if (characters != null) {
  100. for (var character in characters) {
  101. final exists = characterLabelsList.any((e) => e.name == character);
  102. if (!exists) {
  103. characterLabelsList.add(CharactersList(name: character));
  104. }
  105. final selectedCharacter = characterLabelsList.firstWhere(
  106. (e) => e.name == character,
  107. orElse: () => CharactersList(name: character),
  108. );
  109. if (selectedCharacter.name != null) {
  110. _characterSelectLabels.add(selectedCharacter);
  111. }
  112. }
  113. }
  114. _avatarUrl.value =
  115. _currentCharacterInfo.value.imageUrl ?? _avatarUrl.value;
  116. _currentNickname.value = _currentCharacterInfo.value.name ?? '';
  117. _currentGender.value = _currentCharacterInfo.value.gender;
  118. _currentBirthday.value = _currentCharacterInfo.value.birthday;
  119. return;
  120. }
  121. mode = CharacterCustomEditMode.add;
  122. if (arguments?['hobbiesSelectLabels'] == null) {
  123. AtmobLog.i(tag, '没有传递 hobbiesSelectLabels 参数');
  124. } else {
  125. _hobbiesSelectLabels.assignAll(arguments?['hobbiesSelectLabels'] ?? []);
  126. AtmobLog.i(tag, "hobbiesSelectLabels: $hobbiesSelectLabels");
  127. }
  128. if (arguments?['characterSelectLabels'] == null) {
  129. AtmobLog.i(tag, '没有传递 characterSelectLabels 参数');
  130. } else {
  131. _characterSelectLabels.assignAll(
  132. arguments?['characterSelectLabels'] ?? [],
  133. );
  134. AtmobLog.i(tag, "characterSelectLabels: $characterSelectLabels");
  135. }
  136. if (arguments?['characterCustomName'] == null) {
  137. AtmobLog.i(tag, ' 没有传递 characterCustomName 参数');
  138. } else {
  139. _currentNickname(arguments?['characterCustomName'] ?? '');
  140. AtmobLog.i(tag, "characterCustomName: $currentNickname");
  141. }
  142. }
  143. void nextAvatar() {
  144. AtmobLog.d(tag, "nextAvatar");
  145. if (_currentGender.value == 1) {
  146. int currentIndex = _boyAvatars.indexOf(_avatarUrl.value);
  147. _avatarUrl.value = _boyAvatars[(currentIndex + 1) % _boyAvatars.length];
  148. } else {
  149. int currentIndex = _girlAvatars.indexOf(_avatarUrl.value);
  150. _avatarUrl.value = _girlAvatars[(currentIndex + 1) % _girlAvatars.length];
  151. }
  152. }
  153. @override
  154. void onReady() {
  155. super.onReady();
  156. }
  157. @override
  158. void onClose() {
  159. super.onClose();
  160. }
  161. void clickBack() {
  162. Get.back();
  163. }
  164. void clickNickname() async {
  165. AtmobLog.d(tag, 'clickNickname');
  166. final result = await ChangeNicknamePage.start(
  167. nickName: _currentNickname.value,
  168. );
  169. if (result != null) {
  170. _currentNickname.value = result;
  171. }
  172. }
  173. void clickUnlockButton() {
  174. AtmobLog.d(tag, "点击解锁按钮,生成专属人设");
  175. if (isEditMode) {
  176. updateCharacterCustom();
  177. } else {
  178. generateCharacterCustom();
  179. }
  180. }
  181. void clickGender() async {
  182. AtmobLog.d(tag, 'clickGender');
  183. final result = await ChangeGenderPage.start(gender: _currentGender.value);
  184. if (result != null) {
  185. _currentGender.value = result;
  186. }
  187. }
  188. String get genderText {
  189. if (_currentGender.value == 1) return '男';
  190. if (_currentGender.value == 2) return '女';
  191. return '请选择';
  192. }
  193. AssetGenImage? get genderImage {
  194. if (_currentGender.value == 1) {
  195. return Assets.images.iconCharacterCustomDetailMale;
  196. }
  197. if (_currentGender.value == 2) {
  198. return Assets.images.iconCharacterCustomDetailFemale;
  199. }
  200. return null;
  201. }
  202. void clickBirthday() async {
  203. AtmobLog.d(tag, 'clickBirthday');
  204. final result = await ChangeBirthdayPage.start(
  205. birthday: _currentBirthday.value,
  206. );
  207. if (result != null) {
  208. AtmobLog.d(tag, 'clickBirthday result: $result');
  209. _currentBirthday.value = result;
  210. }
  211. }
  212. void clickHobbies() async {
  213. AtmobLog.d(tag, 'clickHobbies');
  214. var result = await ChangeHobbiesPage.start(hobbies: _hobbiesSelectLabels);
  215. if (result is List<Hobbies>) {
  216. _hobbiesSelectLabels.assignAll(result);
  217. }
  218. }
  219. void clickCharacter() async {
  220. AtmobLog.d(tag, 'clickCharacter');
  221. var result = await ChangeCharacterPage.start(
  222. characters: _characterSelectLabels,
  223. );
  224. if (result is List<CharactersList>) {
  225. _characterSelectLabels.assignAll(result);
  226. }
  227. }
  228. // 生成定制人设
  229. Future<void> generateCharacterCustom() async {
  230. try {
  231. await charactersRepository.generateCharacterCustom(
  232. name: _currentNickname.value,
  233. gender: _currentGender.value,
  234. hobbies:
  235. _hobbiesSelectLabels
  236. .map((hobby) => hobby.name)
  237. .whereType<String>()
  238. .toList(),
  239. characters:
  240. _characterSelectLabels
  241. .map((character) => character.name)
  242. .whereType<String>()
  243. .toList(),
  244. birthday: _currentBirthday.value,
  245. imageUrl: _avatarUrl.value,
  246. );
  247. } catch (error) {
  248. if (error is ServerErrorException && error.code == 1005) {
  249. ToastUtil.show('请开通会员解锁权益~');
  250. StorePage.start();
  251. }
  252. if (error is ServerErrorException) {
  253. ErrorHandler.toastError(error);
  254. }
  255. }
  256. }
  257. // 更新专属人设
  258. Future<void> updateCharacterCustom() async {
  259. try {
  260. if (_currentCharacterInfo.value.id == null) {
  261. ToastUtil.show("当前人设为空");
  262. return;
  263. }
  264. await charactersRepository.characterCustomUpdate(
  265. id: _currentCharacterInfo.value.id!,
  266. name: _currentNickname.value,
  267. gender: 1,
  268. hobbies:
  269. _hobbiesSelectLabels
  270. .map((hobby) => hobby.name)
  271. .whereType<String>()
  272. .toList(),
  273. characters:
  274. _characterSelectLabels
  275. .map((character) => character.name)
  276. .whereType<String>()
  277. .toList(),
  278. birthday: _currentBirthday.value,
  279. imgUrl: _avatarUrl.value,
  280. );
  281. ToastUtil.show("更新成功");
  282. _currentCharacterInfo.value.name = _currentNickname.value;
  283. _currentCharacterInfo.value.birthday = _currentBirthday.value;
  284. _currentCharacterInfo.value.gender = _currentGender.value;
  285. _currentCharacterInfo.value.hobbies =
  286. _hobbiesSelectLabels.map((e) => e.name!).toList();
  287. _currentCharacterInfo.value.characters =
  288. _characterSelectLabels.map((e) => e.name!).toList();
  289. Get.back(result: _currentCharacterInfo.value);
  290. } catch (error) {
  291. if (error is ServerErrorException && error.code == 1005) {
  292. ToastUtil.show('请开通会员解锁权益~');
  293. StorePage.start();
  294. }
  295. if (error is ServerErrorException) {
  296. ErrorHandler.toastError(error);
  297. }
  298. }
  299. }
  300. }