character_custom_controller.dart 6.1 KB

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