character_custom_detail_controller.dart 12 KB

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