character_custom_detail_controller.dart 12 KB

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