profile_edit_controller.dart 8.7 KB

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