character_custom_controller.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.value;
  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 ?? 5,
  51. hintText: StringName.customLabelHobbiesHint,
  52. clickCallback: (value) {
  53. hobbiesLabelsList.map((e) => e.name).contains(value)
  54. ? ToastUtil.show("添加失败,标签 $value 已存在")
  55. : hobbiesLabelsList.add(Hobbies(name: value));
  56. },
  57. );
  58. }
  59. // 自定义兴趣爱好
  60. void clickCharacterCustom() {
  61. AtmobLog.d(tag, "clickCharacterCustom");
  62. CustomLabelDialog.show(
  63. maxLength: currentCharacterCustomConfig?.maxCharacterWords ?? 5,
  64. hintText: StringName.customLabelCharacterHint,
  65. clickCallback: (value) {
  66. characterLabelsList.map((e) => e.name).contains(value)
  67. ? ToastUtil.show("添加失败,标签 $value 已存在")
  68. : characterLabelsList.add(CharactersList(name: value));
  69. },
  70. );
  71. }
  72. /// 返回上一页
  73. void clickBack() {
  74. AtmobLog.d(tag, "clickBack");
  75. if (currentStep.value == StepType.hobbies) {
  76. currentStep.value = StepType.home;
  77. } else if (currentStep.value == StepType.characters) {
  78. currentStep.value = StepType.hobbies;
  79. } else if (currentStep.value == StepType.inputName) {
  80. currentStep.value = StepType.characters;
  81. } else {
  82. Get.back();
  83. }
  84. }
  85. // 爱好页的下一步
  86. void clickHobbiesNext() {
  87. int min = currentCharacterCustomConfig?.minHobbyNum ?? 1;
  88. int max = currentCharacterCustomConfig?.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?.minCharacterNum ?? 1;
  106. int max = currentCharacterCustomConfig?.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. hobbiesSelectLabels: hobbiesSelectLabels,
  133. characterSelectLabels: characterSelectLabels,
  134. characterCustomName: currentNameValue.value,
  135. );
  136. }
  137. // 处理下一步
  138. void clickNextButton(StepType stepType) {
  139. if (currentCharacterCustomConfig == null) {
  140. AtmobLog.e(tag, "clickStartCustom - 当前配置为空");
  141. return;
  142. }
  143. if (stepType == StepType.hobbies) {
  144. currentStep.value = StepType.hobbies;
  145. } else if (stepType == StepType.characters) {
  146. currentStep.value = StepType.characters;
  147. } else if (stepType == StepType.inputName) {
  148. currentStep.value = StepType.inputName;
  149. } else {
  150. currentStep.value = StepType.home;
  151. }
  152. }
  153. /// 选择爱好标签
  154. void selectHobby(Hobbies hobby) {
  155. handleSelection(
  156. name: hobby,
  157. selectedList: hobbiesSelectLabels,
  158. max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
  159. errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  160. );
  161. }
  162. /// 选择性格标签
  163. void selectCharacter(CharactersList name) {
  164. handleSelection(
  165. name: name,
  166. selectedList: characterSelectLabels,
  167. max: currentCharacterCustomConfig?.maxCharacterNum ?? 3,
  168. errorMessage:
  169. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  170. );
  171. }
  172. void clickHistory() {
  173. AtmobLog.d(tag, "clickHistory");
  174. CharacterCustomListPage.start();
  175. }
  176. ///标签选择处理
  177. void handleSelection({
  178. required dynamic name,
  179. required RxList<dynamic> selectedList,
  180. required int max,
  181. required String errorMessage,
  182. }) {
  183. if (selectedList.contains(name)) {
  184. selectedList.remove(name);
  185. } else if (selectedList.length < max) {
  186. selectedList.add(name);
  187. } else {
  188. ToastUtil.show(errorMessage);
  189. }
  190. }
  191. ///清空数据
  192. void clearData() {
  193. AtmobLog.d(tag, "clearData");
  194. currentStep.value = StepType.home;
  195. hobbiesSelectLabels.clear();
  196. characterSelectLabels.clear();
  197. currentNameValue.value = "";
  198. }
  199. }