new_user_controller.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import 'package:flutter/material.dart';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:keyboard/data/bean/keyboard_info.dart';
  4. import 'package:keyboard/data/repository/keyboard_repository.dart';
  5. import 'package:keyboard/main.dart';
  6. import 'package:keyboard/module/main/main_page.dart';
  7. import 'package:keyboard/module/new_user/result/new_user_result_page.dart';
  8. import 'package:keyboard/module/new_user/step/birthday/step_birthday_logic.dart';
  9. import 'package:keyboard/module/new_user/step/gender/step_gender_logic.dart';
  10. import 'package:keyboard/module/new_user/step/intimacy/step_intimacy_stages_logic.dart';
  11. import 'package:keyboard/module/new_user/step/nickname/step_nickname_logic.dart';
  12. import 'package:keyboard/module/new_user/step/partner/step_partner_logic.dart';
  13. import 'package:keyboard/resource/string.gen.dart';
  14. import 'package:keyboard/utils/atmob_log.dart';
  15. import 'package:intl/intl.dart';
  16. import '../../base/base_controller.dart';
  17. import '../../data/api/response/keyboard_generate_response.dart';
  18. import '../../data/bean/default_avatar_info.dart';
  19. import '../../data/consts/constants.dart';
  20. import '../../data/consts/event_report.dart';
  21. import '../../data/repository/account_repository.dart';
  22. import 'package:get/get.dart';
  23. import '../../dialog/keyboard_generating_dialog.dart';
  24. import '../../handler/event_handler.dart';
  25. import '../../utils/error_handler.dart';
  26. import '../../utils/http_handler.dart';
  27. import '../../utils/toast_util.dart';
  28. @injectable
  29. class NewUserController extends BaseController
  30. with
  31. StepGenDerLogic,
  32. StepBirthdayLogic,
  33. StepNicknameLogic,
  34. StepIntimacyStagesLogic,
  35. StepPartnerLogic {
  36. final AccountRepository accountRepository;
  37. final KeyboardRepository keyboardRepository;
  38. NewUserController(this.accountRepository, this.keyboardRepository);
  39. final RxInt _currentStep = 1.obs;
  40. int get currentStep => _currentStep.value;
  41. final RxInt _totalSteps = 5.obs;
  42. int get totalSteps => _totalSteps.value;
  43. final PageController pageController = PageController();
  44. final Rx<KeyboardInfo> _keyboardInfo = KeyboardInfo().obs;
  45. @override
  46. void onInit() {
  47. super.onInit();
  48. updateAvatarListsAndSelectFirst(currentDefaultAvatarInfo.value);
  49. ever<DefaultAvatarInfo?>(currentDefaultAvatarInfo, (info) {
  50. updateAvatarListsAndSelectFirst(info);
  51. });
  52. }
  53. @override
  54. void onReady() {
  55. super.onReady();
  56. if (currentStep == 1){
  57. EventHandler.report(EventId.event_01003);
  58. }
  59. ever(_currentStep, (_) {
  60. if (currentStep == 1) {
  61. EventHandler.report(EventId.event_01003);
  62. } else if (currentStep == 2) {
  63. EventHandler.report(EventId.event_01007);
  64. } else if (currentStep == 3) {
  65. EventHandler.report(EventId.event_01009);
  66. } else if (currentStep == 4) {
  67. EventHandler.report(EventId.event_01011);
  68. }
  69. });
  70. }
  71. void clickBack() {
  72. if (currentStep > 1) {
  73. pageController.previousPage(
  74. duration: const Duration(milliseconds: 300),
  75. curve: Curves.easeOut,
  76. );
  77. _currentStep.value--;
  78. } else {
  79. Get.back();
  80. }
  81. }
  82. Future<void> clickNextButton() async {
  83. if (!isCurrentStepValid) {
  84. ToastUtil.show(currentStepValidationError ?? "请完成当前步骤");
  85. return;
  86. }
  87. if(currentStep == 1) {
  88. EventHandler.report(EventId.event_01006);
  89. } else if (currentStep == 2) {
  90. EventHandler.report(EventId.event_01008);
  91. } else if (currentStep == 3) {
  92. EventHandler.report(EventId.event_01010);
  93. } else if (currentStep == 4) {
  94. EventHandler.report(EventId.event_01012);
  95. }
  96. if (currentStep == totalSteps) {
  97. EventHandler.report(EventId.event_01013);
  98. _generateKeyboard();
  99. } else {
  100. await _settingUseInfo();
  101. }
  102. }
  103. void clickUseGeneralMode() {
  104. EventHandler.report(EventId.event_01014);
  105. MainPage.start();
  106. }
  107. void clickSkip() {
  108. EventHandler.report(EventId.event_01015);
  109. MainPage.start();
  110. }
  111. /// 当前步骤是否完成验证
  112. bool get isCurrentStepValid {
  113. switch (currentStep) {
  114. case 1: // 性别
  115. return currentGender != null;
  116. case 2: // 生日
  117. return currentBirthday != null;
  118. case 3: // 昵称
  119. return nickname.trim().isNotEmpty;
  120. case 4: // 亲密阶段
  121. return currentIntimacyMedian > 0;
  122. case 5: // 伴侣信息
  123. return partnerName.value.trim().isNotEmpty &&
  124. currentPartnerBirthday != null &&
  125. partnerAvatarUrl.isNotEmpty &&
  126. partnerGender != null;
  127. default:
  128. return false;
  129. }
  130. }
  131. /// 获取当前步骤未完成提示
  132. String? get currentStepValidationError {
  133. switch (currentStep) {
  134. case 1:
  135. return currentGender == null ? "请选择性别" : null;
  136. case 2:
  137. return currentBirthday == null ? "请选择生日" : null;
  138. case 3:
  139. if (nickname.trim().isEmpty) return "请输入昵称";
  140. if (nickname.value.length > 5) return "昵称最多5个字";
  141. if (nickname.contains(' ')) return "昵称不能包含空格";
  142. return null;
  143. case 4:
  144. return currentIntimacyMedian == 0 ? "请选择亲密阶段" : null;
  145. case 5:
  146. if (partnerName.value.isEmpty) return "请填写TA姓名";
  147. if (partnerGender == null) return "请选择TA性别";
  148. if (currentPartnerBirthday == null) return "请填写TA生日";
  149. if (partnerAvatarUrl.isEmpty) return "请选择TA头像";
  150. return null;
  151. default:
  152. return null;
  153. }
  154. }
  155. Future<void> _settingUseInfo() async {
  156. try {
  157. await accountRepository.setUserInfo(
  158. name: nickname.value,
  159. birthday:
  160. currentBirthday != null
  161. ? DateFormat('yyyy-MM-dd').format(currentBirthday!)
  162. : null,
  163. gender: currentGender,
  164. imageUrl: null,
  165. );
  166. pageController.nextPage(
  167. duration: const Duration(milliseconds: 300),
  168. curve: Curves.easeIn,
  169. );
  170. _currentStep.value++;
  171. accountRepository.refreshUserInfo();
  172. } catch (error) {
  173. if (error is ServerErrorException) {
  174. ToastUtil.show(error.message);
  175. } else {
  176. ErrorHandler.toastError(error);
  177. }
  178. }
  179. }
  180. Future<void> _generateKeyboard() async {
  181. KeyboardGeneratingDialog.show(text: StringName.newUserKeyboardGenerating);
  182. try {
  183. KeyboardGenerateResponse keyboardGenerateResponse =
  184. await keyboardRepository.getKeyboardGenerate(
  185. name: partnerName.value,
  186. gender: partnerGender!,
  187. imageUrl: partnerAvatarUrl,
  188. birthday: DateFormat('yyyy-MM-dd').format(currentPartnerBirthday!),
  189. intimacy: currentIntimacyMedian,
  190. );
  191. if (keyboardGenerateResponse.keyboardInfo == null) {
  192. ToastUtil.show("生成失败");
  193. KeyboardGeneratingDialog.hide();
  194. return;
  195. }
  196. _keyboardInfo.value = keyboardGenerateResponse.keyboardInfo!;
  197. keyboardRepository.refreshData();
  198. Future.delayed(const Duration(seconds: 3), () {
  199. KeyboardGeneratingDialog.hide();
  200. NewUserResultPage.start(
  201. newUserKeyboardInfo: _keyboardInfo.value,
  202. );
  203. });
  204. } catch (error) {
  205. KeyboardGeneratingDialog.hide();
  206. if (error is ServerErrorException) {
  207. ToastUtil.show(error.message);
  208. AtmobLog.d(tag, '_generateKeyboard error: ${error.message}');
  209. } else {
  210. AtmobLog.d(tag, '_generateKeyboard error: $error');
  211. }
  212. }
  213. }
  214. }