profile_edit_controller.dart 9.4 KB

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