profile_edit_controller.dart 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. if (result == 1 && _boyAvatars.isNotEmpty) {
  147. _avatarUrl.value = _boyAvatars.first;
  148. } else if (_girlAvatars.isNotEmpty) {
  149. _avatarUrl.value = _girlAvatars.first;
  150. }
  151. }
  152. void clickBirthday() async {
  153. AtmobLog.d(tag, 'clickBirthday');
  154. final result = await ChangeBirthdayPage.start(
  155. birthday: _currentBirthday.value,
  156. );
  157. if (result != null) {
  158. AtmobLog.d(tag, 'clickBirthday result: $result');
  159. _currentBirthday.value = result;
  160. }
  161. }
  162. void clickRelationship() {
  163. AtmobLog.d(tag, "clickRelationship");
  164. }
  165. void clickGoKeyboardManage() async {
  166. var result = await KeyboardManagePage.start(
  167. customKeyboardInfo: _currentCustomKeyboardInfo.value,
  168. );
  169. if (result != null && result is List<CharacterInfo>) {
  170. AtmobLog.d(tag, 'clickGoKeyboardManage result: $result');
  171. _currentCustomKeyboardCharacterList.assignAll(result);
  172. }
  173. }
  174. String get genderText {
  175. if (_currentGender.value == 1) return '男';
  176. if (_currentGender.value == 2) return '女';
  177. return '请选择';
  178. }
  179. AssetGenImage? get genderImage {
  180. if (_currentGender.value == 1)
  181. return Assets.images.iconCharacterCustomDetailMale;
  182. if (_currentGender.value == 2)
  183. return Assets.images.iconCharacterCustomDetailFemale;
  184. return null;
  185. }
  186. bool _validateForm() {
  187. if (_currentNickname.value?.isEmpty ?? true) {
  188. ToastUtil.show("请输入昵称");
  189. return false;
  190. }
  191. if (_currentGender.value == null) {
  192. ToastUtil.show("请选择性别");
  193. return false;
  194. }
  195. if (_currentBirthday.value == null) {
  196. ToastUtil.show("请选择生日");
  197. return false;
  198. }
  199. if (_avatarUrl.value.isEmpty) {
  200. ToastUtil.show("请选择头像");
  201. return false;
  202. }
  203. if (isEditMode && _currentCustomKeyboardInfo.value.id == null) {
  204. ToastUtil.show("请先选择键盘");
  205. return false;
  206. }
  207. return true;
  208. }
  209. Future<void> _updateKeyboard() async {
  210. await keyboardRepository.updateKeyboardInfo(
  211. keyboardId: _currentCustomKeyboardInfo.value.id!,
  212. name: _currentNickname.value,
  213. imageUrl: _avatarUrl.value,
  214. birthday: _currentBirthday.value,
  215. gender: _currentGender.value,
  216. intimacy: _currentCustomIntimacy.value,
  217. );
  218. }
  219. Future<void> _generateKeyboard() async {
  220. await keyboardRepository.getKeyboardGenerate(
  221. name: _currentNickname.value!,
  222. gender: _currentGender.value!,
  223. imageUrl: _avatarUrl.value,
  224. birthday: _currentBirthday.value!,
  225. intimacy: _currentCustomIntimacy.value,
  226. );
  227. }
  228. void _handleError(dynamic error) {
  229. if (error is ServerErrorException) {
  230. ErrorHandler.toastError(error,message: error.message);
  231. AtmobLog.d(tag, 'clickSaveButton error: ${error.message}');
  232. } else {
  233. AtmobLog.d(tag, 'clickSaveButton error1: $error');
  234. }
  235. }
  236. // 获取当前键盘人设列表
  237. void getKeyboardCharacterList({required String keyboardId}) {
  238. keyboardRepository.getKeyboardCharacterList(keyboardId: keyboardId).then((
  239. keyboardCharacterListResponse,
  240. ) {
  241. AtmobLog.i(
  242. tag,
  243. 'keyboardCharacterListResponse: ${keyboardCharacterListResponse.characterInfos.toString()}',
  244. );
  245. _currentCustomKeyboardCharacterList.assignAll(
  246. keyboardCharacterListResponse.characterInfos,
  247. );
  248. });
  249. }
  250. }