character_custom_controller.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. final isExist = hobbiesLabelsList.map((e) => e.name).contains(value);
  56. if (isExist) {
  57. ToastUtil.show("添加失败,标签 $value 已存在");
  58. return;
  59. }
  60. final newHobby = Hobbies(name: value);
  61. hobbiesLabelsList.add(newHobby);
  62. ToastUtil.show("添加成功,标签 $value");
  63. // 如果当前已选未超限,自动选中
  64. if (hobbiesSelectLabels.length <
  65. (currentCharacterCustomConfig?.maxHobbyNum ?? 3)) {
  66. hobbiesSelectLabels.add(newHobby);
  67. } else {
  68. ToastUtil.show(
  69. "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  70. );
  71. }
  72. },
  73. );
  74. }
  75. // 自定义兴趣爱好
  76. void clickCharacterCustom() {
  77. AtmobLog.d(tag, "clickCharacterCustom");
  78. CustomLabelDialog.show(
  79. maxLength: currentCharacterCustomConfig?.maxCharacterWords ?? 5,
  80. hintText: StringName.customLabelCharacterHint,
  81. clickCallback: (value) {
  82. var isExist = characterLabelsList
  83. .map((e) => e.name)
  84. .contains(value);
  85. if (isExist) {
  86. ToastUtil.show("添加失败,标签 $value 已存在");
  87. return;
  88. }
  89. characterLabelsList.add(CharactersList(name: value));
  90. ToastUtil.show("添加成功,标签 $value");
  91. if (characterSelectLabels.length <
  92. (currentCharacterCustomConfig?.maxCharacterNum ?? 3)) {
  93. characterSelectLabels.add(CharactersList(name: value));
  94. } else {
  95. ToastUtil.show(
  96. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  97. );
  98. }
  99. },
  100. );
  101. }
  102. /// 返回上一页
  103. void clickBack() {
  104. AtmobLog.d(tag, "clickBack");
  105. if (currentStep.value == StepType.hobbies) {
  106. currentStep.value = StepType.home;
  107. } else if (currentStep.value == StepType.characters) {
  108. currentStep.value = StepType.hobbies;
  109. } else if (currentStep.value == StepType.inputName) {
  110. currentStep.value = StepType.characters;
  111. } else {
  112. if (isSkipFromHistory.value) {
  113. // 跳到定制历史
  114. clickHistory();
  115. isSkipFromHistory.value = false;
  116. return;
  117. }
  118. Get.back();
  119. }
  120. }
  121. // 爱好页的下一步
  122. void clickHobbiesNext() {
  123. int min = currentCharacterCustomConfig?.minHobbyNum ?? 1;
  124. int max = currentCharacterCustomConfig?.maxHobbyNum ?? 3;
  125. if (hobbiesSelectLabels.isEmpty) {
  126. ToastUtil.show("请选择爱好");
  127. return;
  128. }
  129. if (hobbiesSelectLabels.length < min) {
  130. ToastUtil.show("至少选择$min个爱好");
  131. return;
  132. }
  133. if (hobbiesSelectLabels.length > max) {
  134. ToastUtil.show("最多选择$max个爱好");
  135. return;
  136. }
  137. clickNextButton(StepType.characters);
  138. }
  139. // 性格页的下一步
  140. void clickCharacterNext() {
  141. int min = currentCharacterCustomConfig?.minCharacterNum ?? 1;
  142. int max = currentCharacterCustomConfig?.maxCharacterNum ?? 3;
  143. if (characterSelectLabels.isEmpty) {
  144. ToastUtil.show("请选择特质");
  145. return;
  146. }
  147. if (characterSelectLabels.length < min) {
  148. ToastUtil.show("至少选择$min个特质");
  149. return;
  150. }
  151. if (characterSelectLabels.length > max) {
  152. ToastUtil.show("最多选择$max个特质");
  153. return;
  154. }
  155. clickNextButton(StepType.inputName);
  156. }
  157. // 名字页的下一步
  158. Future<void> clickInputNameNext() async {
  159. if (currentNameValue.value.isEmpty) {
  160. ToastUtil.show("请输入名字");
  161. return;
  162. }
  163. if (currentNameValue.value.length > 5) {
  164. ToastUtil.show("最多5个字哦~");
  165. return;
  166. }
  167. await CharacterCustomDetailPage.start(
  168. hobbiesSelectLabels: hobbiesSelectLabels,
  169. characterSelectLabels: characterSelectLabels,
  170. characterCustomName: currentNameValue.value,
  171. );
  172. if (isSkipFromHistory.value) {
  173. // 跳到定制历史
  174. clickHistory();
  175. isSkipFromHistory.value = false;
  176. }
  177. }
  178. // 处理下一步
  179. void clickNextButton(StepType stepType) {
  180. if (currentCharacterCustomConfig == null) {
  181. AtmobLog.e(tag, "clickStartCustom - 当前配置为空");
  182. return;
  183. }
  184. if (stepType == StepType.hobbies) {
  185. currentStep.value = StepType.hobbies;
  186. } else if (stepType == StepType.characters) {
  187. currentStep.value = StepType.characters;
  188. } else if (stepType == StepType.inputName) {
  189. currentStep.value = StepType.inputName;
  190. } else {
  191. currentStep.value = StepType.home;
  192. }
  193. }
  194. /// 选择爱好标签
  195. void selectHobby(Hobbies hobby) {
  196. handleSelection(
  197. name: hobby,
  198. selectedList: hobbiesSelectLabels,
  199. max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
  200. errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  201. );
  202. }
  203. /// 选择性格标签
  204. void selectCharacter(CharactersList name) {
  205. handleSelection(
  206. name: name,
  207. selectedList: characterSelectLabels,
  208. max: currentCharacterCustomConfig?.maxCharacterNum ?? 3,
  209. errorMessage:
  210. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  211. );
  212. }
  213. Future<void> clickHistory() async {
  214. AtmobLog.d(tag, "clickHistory");
  215. var result = await CharacterCustomListPage.start();
  216. if (result != null) {
  217. AtmobLog.d(tag, "clickHistory");
  218. currentStep.value = result;
  219. isSkipFromHistory.value = true;
  220. }
  221. }
  222. ///标签选择处理
  223. void handleSelection({
  224. required dynamic name,
  225. required RxList<dynamic> selectedList,
  226. required int max,
  227. required String errorMessage,
  228. }) {
  229. if (selectedList.contains(name)) {
  230. selectedList.remove(name);
  231. } else if (selectedList.length < max) {
  232. selectedList.add(name);
  233. } else {
  234. ToastUtil.show(errorMessage);
  235. }
  236. }
  237. ///清空数据
  238. void clearData() {
  239. AtmobLog.d(tag, "clearData");
  240. currentStep.value = StepType.home;
  241. hobbiesSelectLabels.clear();
  242. characterSelectLabels.clear();
  243. currentNameValue.value = "";
  244. CharacterCustomListPage.start();
  245. }
  246. }