profile_edit_controller.dart 10 KB

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