character_custom_controller.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // 是否从定制历史跳过来的
  35. final RxBool isSkipFromHistory = false.obs;
  36. @override
  37. void onInit() {
  38. super.onInit();
  39. initData();
  40. AtmobLog.d(tag, "首次加载数据,触发 refreshCharacterCustomConfig()");
  41. }
  42. // 初始化数据
  43. void initData() {
  44. AtmobLog.d(tag, "initData");
  45. hobbiesLabelsList.value = currentCharacterCustomConfig?.hobbies ?? [];
  46. characterLabelsList.value = currentCharacterCustomConfig?.characters ?? [];
  47. }
  48. // 自定义兴趣爱好
  49. void clickHobbiesCustom() {
  50. AtmobLog.d(tag, "clickHobbiesCustom");
  51. CustomLabelDialog.show(
  52. maxLength: currentCharacterCustomConfig?.maxHobbyWords ?? 5,
  53. hintText: StringName.customLabelHobbiesHint,
  54. clickCallback: (value) {
  55. hobbiesLabelsList.map((e) => e.name).contains(value)
  56. ? ToastUtil.show("添加失败,标签 $value 已存在")
  57. : hobbiesLabelsList.add(Hobbies(name: value));
  58. },
  59. );
  60. }
  61. // 自定义兴趣爱好
  62. void clickCharacterCustom() {
  63. AtmobLog.d(tag, "clickCharacterCustom");
  64. CustomLabelDialog.show(
  65. maxLength: currentCharacterCustomConfig?.maxCharacterWords ?? 5,
  66. hintText: StringName.customLabelCharacterHint,
  67. clickCallback: (value) {
  68. characterLabelsList.map((e) => e.name).contains(value)
  69. ? ToastUtil.show("添加失败,标签 $value 已存在")
  70. : characterLabelsList.add(CharactersList(name: value));
  71. },
  72. );
  73. }
  74. /// 返回上一页
  75. void clickBack() {
  76. AtmobLog.d(tag, "clickBack");
  77. if (currentStep.value == StepType.hobbies) {
  78. currentStep.value = StepType.home;
  79. } else if (currentStep.value == StepType.characters) {
  80. currentStep.value = StepType.hobbies;
  81. } else if (currentStep.value == StepType.inputName) {
  82. currentStep.value = StepType.characters;
  83. } else {
  84. if (isSkipFromHistory.value) {
  85. // 跳到定制历史
  86. clickHistory();
  87. isSkipFromHistory.value = false;
  88. return;
  89. }
  90. Get.back();
  91. }
  92. }
  93. // 爱好页的下一步
  94. void clickHobbiesNext() {
  95. int min = currentCharacterCustomConfig?.minHobbyNum ?? 1;
  96. int max = currentCharacterCustomConfig?.maxHobbyNum ?? 3;
  97. if (hobbiesSelectLabels.isEmpty) {
  98. ToastUtil.show("请选择爱好");
  99. return;
  100. }
  101. if (hobbiesSelectLabels.length < min) {
  102. ToastUtil.show("至少选择$min个爱好");
  103. return;
  104. }
  105. if (hobbiesSelectLabels.length > max) {
  106. ToastUtil.show("最多选择$max个爱好");
  107. return;
  108. }
  109. clickNextButton(StepType.characters);
  110. }
  111. // 性格页的下一步
  112. void clickCharacterNext() {
  113. int min = currentCharacterCustomConfig?.minCharacterNum ?? 1;
  114. int max = currentCharacterCustomConfig?.maxCharacterNum ?? 3;
  115. if (characterSelectLabels.isEmpty) {
  116. ToastUtil.show("请选择特质");
  117. return;
  118. }
  119. if (characterSelectLabels.length < min) {
  120. ToastUtil.show("至少选择$min个特质");
  121. return;
  122. }
  123. if (characterSelectLabels.length > max) {
  124. ToastUtil.show("最多选择$max个特质");
  125. return;
  126. }
  127. clickNextButton(StepType.inputName);
  128. }
  129. // 名字页的下一步
  130. Future<void> clickInputNameNext() async {
  131. if (currentNameValue.value.isEmpty) {
  132. ToastUtil.show("请输入名字");
  133. return;
  134. }
  135. if (currentNameValue.value.length > 5) {
  136. ToastUtil.show("最多5个字哦~");
  137. return;
  138. }
  139. await CharacterCustomDetailPage.start(
  140. hobbiesSelectLabels: hobbiesSelectLabels,
  141. characterSelectLabels: characterSelectLabels,
  142. characterCustomName: currentNameValue.value,
  143. );
  144. if (isSkipFromHistory.value) {
  145. // 跳到定制历史
  146. clickHistory();
  147. isSkipFromHistory.value = false;
  148. }
  149. }
  150. // 处理下一步
  151. void clickNextButton(StepType stepType) {
  152. if (currentCharacterCustomConfig == null) {
  153. AtmobLog.e(tag, "clickStartCustom - 当前配置为空");
  154. return;
  155. }
  156. if (stepType == StepType.hobbies) {
  157. currentStep.value = StepType.hobbies;
  158. } else if (stepType == StepType.characters) {
  159. currentStep.value = StepType.characters;
  160. } else if (stepType == StepType.inputName) {
  161. currentStep.value = StepType.inputName;
  162. } else {
  163. currentStep.value = StepType.home;
  164. }
  165. }
  166. /// 选择爱好标签
  167. void selectHobby(Hobbies hobby) {
  168. handleSelection(
  169. name: hobby,
  170. selectedList: hobbiesSelectLabels,
  171. max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
  172. errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  173. );
  174. }
  175. /// 选择性格标签
  176. void selectCharacter(CharactersList name) {
  177. handleSelection(
  178. name: name,
  179. selectedList: characterSelectLabels,
  180. max: currentCharacterCustomConfig?.maxCharacterNum ?? 3,
  181. errorMessage:
  182. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  183. );
  184. }
  185. Future<void> clickHistory() async {
  186. AtmobLog.d(tag, "clickHistory");
  187. var result = await CharacterCustomListPage.start();
  188. if (result != null) {
  189. AtmobLog.d(tag, "clickHistory");
  190. currentStep.value = result;
  191. isSkipFromHistory.value = true;
  192. }
  193. }
  194. ///标签选择处理
  195. void handleSelection({
  196. required dynamic name,
  197. required RxList<dynamic> selectedList,
  198. required int max,
  199. required String errorMessage,
  200. }) {
  201. if (selectedList.contains(name)) {
  202. selectedList.remove(name);
  203. } else if (selectedList.length < max) {
  204. selectedList.add(name);
  205. } else {
  206. ToastUtil.show(errorMessage);
  207. }
  208. }
  209. ///清空数据
  210. void clearData() {
  211. AtmobLog.d(tag, "clearData");
  212. currentStep.value = StepType.home;
  213. hobbiesSelectLabels.clear();
  214. characterSelectLabels.clear();
  215. currentNameValue.value = "";
  216. }
  217. }