profile_edit_controller.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import 'package:get/get.dart';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:keyboard/base/base_controller.dart';
  4. import 'package:keyboard/data/api/request/keyboard_delete_request.dart';
  5. import 'package:keyboard/data/bean/default_avatar_info.dart';
  6. import 'package:keyboard/data/bean/keyboard_info.dart';
  7. import 'package:keyboard/data/repository/keyboard_repository.dart';
  8. import 'package:keyboard/dialog/loading_dialog.dart';
  9. import 'package:keyboard/module/change/gender/change_gender_page.dart';
  10. import 'package:keyboard/utils/age_zodiac_sign_util.dart';
  11. import 'package:keyboard/utils/atmob_log.dart';
  12. import 'package:keyboard/utils/toast_util.dart';
  13. import '../../../data/bean/character_info.dart';
  14. import '../../../data/consts/event_report.dart';
  15. import '../../../data/repository/config_repository.dart';
  16. import '../../../handler/event_handler.dart';
  17. import '../../../resource/assets.gen.dart';
  18. import '../../../utils/error_handler.dart';
  19. import '../../../utils/http_handler.dart';
  20. import '../../change/birthday/change_birthday_page.dart';
  21. import '../../change/nickname/change_nickname_page.dart';
  22. import '../../keyboard_manage/keyboard_manage_page.dart';
  23. import '../dialog/delete_profile_confirm_dialog.dart';
  24. enum ProfileEditMode {
  25. add, // 新增
  26. edit, // 编辑
  27. }
  28. @injectable
  29. class ProfileEditController extends BaseController {
  30. final tag = "ProfileEditController";
  31. final ConfigRepository configRepository;
  32. final KeyboardRepository keyboardRepository;
  33. final RxnInt _currentGender = RxnInt(null);
  34. final RxString _avatarUrl = "".obs;
  35. String get avatarUrl => _avatarUrl.value;
  36. Rxn<DefaultAvatarInfo> get currentDefaultAvatarInfo =>
  37. configRepository.defaultAvatarInfo;
  38. final Rxn<ProfileEditMode> mode = Rxn<ProfileEditMode>();
  39. bool get isEditMode => mode.value == ProfileEditMode.edit;
  40. bool get isAddMode => mode.value == ProfileEditMode.add;
  41. final Rx<KeyboardInfo> _currentCustomKeyboardInfo = KeyboardInfo().obs;
  42. Rx<KeyboardInfo> get currentCustomKeyboardInfo => _currentCustomKeyboardInfo;
  43. final RxnString _currentBirthday = RxnString(null);
  44. String? get currentBirthday =>
  45. AgeZodiacSignUtil.formatBirthdayFromString(_currentBirthday.value);
  46. // 当前昵称
  47. final RxnString _currentNickname = RxnString(null);
  48. String? get currentNickname => _currentNickname.value;
  49. // 当前自定义键盘亲密度
  50. final RxInt _currentCustomIntimacy = 30.obs;
  51. int get currentCustomIntimacy => _currentCustomIntimacy.value;
  52. // 当前定制亲密度是否有变化
  53. final RxBool _customIntimacyChanged = false.obs;
  54. bool get customIntimacyChanged => _customIntimacyChanged.value;
  55. final RxList<String> _boyAvatars = <String>[].obs;
  56. final RxList<String> _girlAvatars = <String>[].obs;
  57. // 当前定制人设列表
  58. final RxList<CharacterInfo> _currentCustomKeyboardCharacterList = RxList();
  59. RxList<CharacterInfo> get currentCustomKeyboardCharacterList =>
  60. _currentCustomKeyboardCharacterList;
  61. ProfileEditController(this.configRepository, this.keyboardRepository);
  62. @override
  63. void onInit() {
  64. super.onInit();
  65. final KeyboardInfo? keyboardInfo = Get.arguments?["keyboardInfo"];
  66. if (keyboardInfo != null) {
  67. mode.value = ProfileEditMode.edit;
  68. _currentCustomKeyboardInfo.value = keyboardInfo;
  69. _currentNickname.value = keyboardInfo.name;
  70. _currentGender.value = keyboardInfo.gender;
  71. _currentCustomIntimacy.value = keyboardInfo.intimacy ?? 30;
  72. _currentBirthday.value = keyboardInfo.birthday;
  73. _avatarUrl.value = keyboardInfo.imageUrl ?? "";
  74. getKeyboardCharacterList(keyboardId: keyboardInfo.id!);
  75. } else {
  76. mode.value = ProfileEditMode.add;
  77. _currentGender.value = null;
  78. _currentCustomIntimacy.value = 30;
  79. _avatarUrl.value = "";
  80. }
  81. ever<DefaultAvatarInfo?>(currentDefaultAvatarInfo, (info) {
  82. updateAvatarListsAndSelectFirst(info);
  83. });
  84. }
  85. @override
  86. void onReady() {
  87. super.onReady();
  88. updateAvatarListsAndSelectFirst(currentDefaultAvatarInfo.value);
  89. }
  90. void updateAvatarListsAndSelectFirst(DefaultAvatarInfo? info) {
  91. _boyAvatars.assignAll(info?.maleAvatars ?? []);
  92. _girlAvatars.assignAll(info?.femaleAvatars ?? []);
  93. if (_currentGender.value == 1 && _boyAvatars.isNotEmpty) {
  94. _avatarUrl.value = _boyAvatars[0];
  95. } else if (_girlAvatars.isNotEmpty) {
  96. _avatarUrl.value = _girlAvatars[0];
  97. }
  98. }
  99. clickSaveButton() async {
  100. EventHandler.report(EventId.event_06003);
  101. AtmobLog.d(tag, 'clickSaveButton');
  102. if (!_validateForm()) return;
  103. AtmobLog.d(tag, 'generateKeyboard');
  104. try {
  105. LoadingDialog.show(isEditMode ? "更新键盘数据中" : "生成键盘中");
  106. if (isEditMode) {
  107. await _updateKeyboard();
  108. ToastUtil.show("更新键盘成功");
  109. } else {
  110. await _generateKeyboard();
  111. ToastUtil.show("生成键盘成功");
  112. }
  113. Get.back(result: true);
  114. } catch (error) {
  115. _handleError(error);
  116. } finally {
  117. LoadingDialog.hide();
  118. }
  119. }
  120. void nextAvatar() {
  121. List<String> avatars =
  122. _currentGender.value == 1 ? _boyAvatars : _girlAvatars;
  123. if (avatars.isEmpty) return;
  124. int currentIndex = avatars.indexOf(_avatarUrl.value);
  125. _avatarUrl.value = avatars[(currentIndex + 1) % avatars.length];
  126. AtmobLog.d(tag, 'nextAvatar: ${_avatarUrl.value}');
  127. }
  128. // 更新亲密度
  129. void updateIntimacy(int intimacy) {
  130. _currentCustomIntimacy.value = intimacy;
  131. }
  132. clickBack() {
  133. AtmobLog.d(tag, 'clickBackButton');
  134. Get.back();
  135. }
  136. /// 删除档案
  137. clickDelete() {
  138. DeleteProfileConfirmDialog.show(_currentNickname.value ?? "", () async {
  139. var keyboardId = _currentCustomKeyboardInfo.value.id ?? "";
  140. // 发起删除
  141. await keyboardRepository.keyboardDelete(
  142. KeyboardDeleteRequest(keyboardId: keyboardId),
  143. );
  144. Get.back(result: true);
  145. });
  146. }
  147. void clickNickname() async {
  148. AtmobLog.d(tag, 'clickNickname');
  149. final result = await ChangeNicknamePage.start(
  150. nickName: _currentNickname.value,
  151. );
  152. if (result != null) {
  153. _currentNickname.value = result;
  154. }
  155. }
  156. void clickGender() async {
  157. AtmobLog.d(tag, 'clickGender');
  158. final result = await ChangeGenderPage.start(gender: _currentGender.value);
  159. if (result != null) {
  160. _currentGender.value = result;
  161. }
  162. if (result == 1 && _boyAvatars.isNotEmpty) {
  163. _avatarUrl.value = _boyAvatars.first;
  164. } else if (_girlAvatars.isNotEmpty) {
  165. _avatarUrl.value = _girlAvatars.first;
  166. }
  167. }
  168. void clickBirthday() async {
  169. AtmobLog.d(tag, 'clickBirthday');
  170. final result = await ChangeBirthdayPage.start(
  171. birthday: _currentBirthday.value,
  172. );
  173. if (result != null) {
  174. AtmobLog.d(tag, 'clickBirthday result: $result');
  175. _currentBirthday.value = result;
  176. }
  177. }
  178. void clickRelationship() {
  179. AtmobLog.d(tag, "clickRelationship");
  180. }
  181. void clickGoKeyboardManage() async {
  182. var result = await KeyboardManagePage.start(
  183. customKeyboardInfo: _currentCustomKeyboardInfo.value,
  184. );
  185. if (result != null && result is List<CharacterInfo>) {
  186. AtmobLog.d(tag, 'clickGoKeyboardManage result: $result');
  187. _currentCustomKeyboardCharacterList.assignAll(result);
  188. }
  189. }
  190. String get genderText {
  191. if (_currentGender.value == 1) return '男';
  192. if (_currentGender.value == 2) return '女';
  193. return '请选择';
  194. }
  195. AssetGenImage? get genderImage {
  196. if (_currentGender.value == 1)
  197. return Assets.images.iconCharacterCustomDetailMale;
  198. if (_currentGender.value == 2)
  199. return Assets.images.iconCharacterCustomDetailFemale;
  200. return null;
  201. }
  202. bool _validateForm() {
  203. if (_currentNickname.value?.isEmpty ?? true) {
  204. ToastUtil.show("请输入昵称");
  205. return false;
  206. }
  207. if (_currentGender.value == null) {
  208. ToastUtil.show("请选择性别");
  209. return false;
  210. }
  211. if (_currentBirthday.value == null) {
  212. ToastUtil.show("请选择生日");
  213. return false;
  214. }
  215. if (_avatarUrl.value.isEmpty) {
  216. ToastUtil.show("请选择头像");
  217. return false;
  218. }
  219. if (isEditMode && _currentCustomKeyboardInfo.value.id == null) {
  220. ToastUtil.show("请先选择键盘");
  221. return false;
  222. }
  223. return true;
  224. }
  225. Future<void> _updateKeyboard() async {
  226. await keyboardRepository.updateKeyboardInfo(
  227. keyboardId: _currentCustomKeyboardInfo.value.id!,
  228. name: _currentNickname.value,
  229. imageUrl: _avatarUrl.value,
  230. birthday: _currentBirthday.value,
  231. gender: _currentGender.value,
  232. intimacy: _currentCustomIntimacy.value,
  233. );
  234. }
  235. Future<void> _generateKeyboard() async {
  236. await keyboardRepository.getKeyboardGenerate(
  237. name: _currentNickname.value!,
  238. gender: _currentGender.value!,
  239. imageUrl: _avatarUrl.value,
  240. birthday: _currentBirthday.value!,
  241. intimacy: _currentCustomIntimacy.value,
  242. );
  243. }
  244. void _handleError(dynamic error) {
  245. if (error is ServerErrorException) {
  246. ErrorHandler.toastError(error, message: error.message);
  247. AtmobLog.d(tag, 'clickSaveButton error: ${error.message}');
  248. } else {
  249. AtmobLog.d(tag, 'clickSaveButton error1: $error');
  250. }
  251. }
  252. // 获取当前键盘人设列表
  253. void getKeyboardCharacterList({required String keyboardId}) {
  254. keyboardRepository.getKeyboardCharacterList(keyboardId: keyboardId).then((
  255. keyboardCharacterListResponse,
  256. ) {
  257. AtmobLog.i(
  258. tag,
  259. 'keyboardCharacterListResponse: ${keyboardCharacterListResponse.characterInfos.toString()}',
  260. );
  261. _currentCustomKeyboardCharacterList.assignAll(
  262. keyboardCharacterListResponse.characterInfos,
  263. );
  264. });
  265. }
  266. }