character_custom_controller.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import 'package:flutter/cupertino.dart';
  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/bean/custom_config_info.dart';
  6. import 'package:keyboard/data/repository/config_repository.dart';
  7. import 'package:keyboard/module/character_custom/detail/character_custom_detail_page.dart';
  8. import 'package:keyboard/resource/string.gen.dart';
  9. import 'package:keyboard/utils/atmob_log.dart';
  10. import '../../dialog/custom_label_dialog.dart';
  11. import '../../utils/toast_util.dart';
  12. enum StepType {
  13. home(0),
  14. hobbies(1),
  15. characters(2),
  16. inputName(3);
  17. final int value;
  18. const StepType(this.value);
  19. }
  20. @injectable
  21. class CharacterCustomController extends BaseController {
  22. final String tag = 'CharacterCustomController';
  23. final ConfigRepository configRepository;
  24. final Rxn<CustomConfigInfo> currentCharacterCustomConfig =
  25. Rxn<CustomConfigInfo>();
  26. final RxList<Hobbies> hobbiesLabelsList = <Hobbies>[].obs;
  27. final RxList<String> hobbiesSelectLabels = <String>[].obs;
  28. final RxList<CharactersList> characterLabelsList = <CharactersList>[].obs;
  29. final RxList<String> characterSelectLabels = <String>[].obs;
  30. final Rx<StepType> currentStep = StepType.home.obs;
  31. final RxString currentNameValue = "".obs;
  32. CharacterCustomController(this.configRepository);
  33. @override
  34. void onInit() {
  35. super.onInit();
  36. AtmobLog.d(tag, "首次加载数据,触发 refreshCharacterCustomConfig()");
  37. refreshCharacterCustomConfig();
  38. }
  39. // 初始化数据
  40. // 自定义兴趣爱好
  41. void clickHobbiesCustom() {
  42. AtmobLog.d(tag, "clickHobbiesCustom");
  43. CustomLabelDialog.show(
  44. maxLength: currentCharacterCustomConfig.value?.maxHobbyWords ?? 10,
  45. hintText: StringName.customLabelHobbiesHint,
  46. clickCallback: (value) {
  47. hobbiesLabelsList.add(Hobbies(name: value));
  48. },
  49. );
  50. }
  51. // 自定义兴趣爱好
  52. void clickCharacterCustom() {
  53. AtmobLog.d(tag, "clickCharacterCustom");
  54. CustomLabelDialog.show(
  55. maxLength: currentCharacterCustomConfig.value?.maxCharacterWords ?? 10,
  56. hintText: StringName.customLabelCharacterHint,
  57. clickCallback: (value) {
  58. characterLabelsList.add(CharactersList(name: value));
  59. },
  60. );
  61. }
  62. /// 返回上一页
  63. void clickBack() {
  64. AtmobLog.d(tag, "clickBack");
  65. if (currentStep.value == StepType.hobbies) {
  66. currentStep.value = StepType.home;
  67. } else if (currentStep.value == StepType.characters) {
  68. currentStep.value = StepType.hobbies;
  69. } else if (currentStep.value == StepType.inputName) {
  70. currentStep.value = StepType.characters;
  71. } else {
  72. Get.back();
  73. }
  74. }
  75. Future<void> refreshCharacterCustomConfig() async {
  76. try {
  77. final value = await configRepository.getCharacterCustomConfig();
  78. currentCharacterCustomConfig.value = value.characterInfo;
  79. hobbiesLabelsList.value = value.characterInfo?.hobbies ?? [];
  80. characterLabelsList.value = value.characterInfo?.characters ?? [];
  81. } catch (e) {
  82. AtmobLog.e(tag, "获取定制人设配置失败: $e");
  83. }
  84. }
  85. // 爱好页的下一步
  86. void clickHobbiesNext() {
  87. int min = currentCharacterCustomConfig.value?.minHobbyNum ?? 1;
  88. int max = currentCharacterCustomConfig.value?.maxHobbyNum ?? 3;
  89. if (hobbiesSelectLabels.isEmpty) {
  90. ToastUtil.show("请选择爱好");
  91. return;
  92. }
  93. if (hobbiesSelectLabels.length < min) {
  94. ToastUtil.show("至少选择$min个爱好");
  95. return;
  96. }
  97. if (hobbiesSelectLabels.length > max) {
  98. ToastUtil.show("最多选择$max个爱好");
  99. return;
  100. }
  101. clickNextButton(StepType.characters);
  102. }
  103. // 性格页的下一步
  104. void clickCharacterNext() {
  105. int min = currentCharacterCustomConfig.value?.minCharacterNum ?? 1;
  106. int max = currentCharacterCustomConfig.value?.maxCharacterNum ?? 3;
  107. if (characterSelectLabels.isEmpty) {
  108. ToastUtil.show("请选择特质");
  109. return;
  110. }
  111. if (characterSelectLabels.length < min) {
  112. ToastUtil.show("至少选择$min个特质");
  113. return;
  114. }
  115. if (characterSelectLabels.length > max) {
  116. ToastUtil.show("最多选择$max个特质");
  117. return;
  118. }
  119. clickNextButton(StepType.inputName);
  120. }
  121. // 名字页的下一步
  122. void clickInputNameNext() {
  123. if (currentNameValue.value.isEmpty) {
  124. ToastUtil.show("请输入名字");
  125. return;
  126. }
  127. if (currentNameValue.value.length > 5) {
  128. ToastUtil.show("最多5个字哦~");
  129. return;
  130. }
  131. CharacterCustomDetailPage.start();
  132. }
  133. // 处理下一步
  134. void clickNextButton(StepType stepType) {
  135. if (currentCharacterCustomConfig.value == null) {
  136. AtmobLog.e(tag, "clickStartCustom - 当前配置为空");
  137. return;
  138. }
  139. if (stepType == StepType.hobbies) {
  140. currentStep.value = StepType.hobbies;
  141. } else if (stepType == StepType.characters) {
  142. currentStep.value = StepType.characters;
  143. } else if (stepType == StepType.inputName) {
  144. currentStep.value = StepType.inputName;
  145. } else {
  146. currentStep.value = StepType.home;
  147. }
  148. }
  149. /// 选择爱好标签
  150. void selectHobby(String name) {
  151. handleSelection(
  152. name: name,
  153. selectedList: hobbiesSelectLabels,
  154. max: currentCharacterCustomConfig.value?.maxHobbyNum ?? 3,
  155. errorMessage:
  156. "最多选择${currentCharacterCustomConfig.value?.maxHobbyNum ?? 3}个爱好",
  157. );
  158. }
  159. /// 选择性格标签
  160. void selectCharacter(String name) {
  161. handleSelection(
  162. name: name,
  163. selectedList: characterSelectLabels,
  164. max: currentCharacterCustomConfig.value?.maxCharacterNum ?? 3,
  165. errorMessage:
  166. "最多选择${currentCharacterCustomConfig.value?.maxCharacterNum ?? 3}个特质",
  167. );
  168. }
  169. ///标签选择处理
  170. void handleSelection({
  171. required String name,
  172. required RxList<String> selectedList,
  173. required int max,
  174. required String errorMessage,
  175. }) {
  176. if (selectedList.contains(name)) {
  177. selectedList.remove(name);
  178. } else if (selectedList.length < max) {
  179. selectedList.add(name);
  180. } else {
  181. ToastUtil.show(errorMessage);
  182. }
  183. }
  184. }