character_custom_detail_controller.dart 12 KB

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