character_custom_controller.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/handler/event_handler.dart';
  8. import 'package:keyboard/module/character_custom/detail/character_custom_detail_page.dart';
  9. import 'package:keyboard/module/character_custom/list/character_custom_list_page.dart';
  10. import 'package:keyboard/resource/string.gen.dart';
  11. import 'package:keyboard/utils/atmob_log.dart';
  12. import '../../data/consts/event_report.dart';
  13. import '../../dialog/custom_label_dialog.dart';
  14. import '../../utils/toast_util.dart';
  15. enum StepType {
  16. home(0),
  17. hobbies(1),
  18. characters(2),
  19. inputName(3);
  20. final int value;
  21. const StepType(this.value);
  22. }
  23. @injectable
  24. class CharacterCustomController extends BaseController {
  25. final String tag = 'CharacterCustomController';
  26. final ConfigRepository configRepository;
  27. CustomConfigInfo? get currentCharacterCustomConfig =>
  28. configRepository.characterCustomConfig.value;
  29. final RxList<Hobbies> hobbiesLabelsList = <Hobbies>[].obs;
  30. final RxList<Hobbies> hobbiesSelectLabels = <Hobbies>[].obs;
  31. final RxList<CharactersList> characterLabelsList = <CharactersList>[].obs;
  32. final RxList<CharactersList> characterSelectLabels = <CharactersList>[].obs;
  33. final Rx<StepType> currentStep = StepType.home.obs;
  34. final RxString currentNameValue = "".obs;
  35. CharacterCustomController(this.configRepository);
  36. // 是否从定制历史跳过来的
  37. final RxBool isSkipFromHistory = false.obs;
  38. @override
  39. void onInit() {
  40. super.onInit();
  41. initData();
  42. AtmobLog.d(tag, "首次加载数据,触发 refreshCharacterCustomConfig()");
  43. }
  44. // 初始化数据
  45. void initData() {
  46. AtmobLog.d(tag, "initData");
  47. hobbiesLabelsList.value = currentCharacterCustomConfig?.hobbies ?? [];
  48. characterLabelsList.value = currentCharacterCustomConfig?.characters ?? [];
  49. }
  50. // 自定义兴趣爱好
  51. void clickHobbiesCustom() {
  52. AtmobLog.d(tag, "clickHobbiesCustom");
  53. CustomLabelDialog.show(
  54. maxLength: currentCharacterCustomConfig?.maxHobbyWords ?? 5,
  55. hintText: StringName.customLabelHobbiesHint,
  56. clickCallback: (value) {
  57. final isExist = hobbiesLabelsList.map((e) => e.name).contains(value);
  58. if (isExist) {
  59. ToastUtil.show("添加失败,标签 $value 已存在");
  60. return;
  61. }
  62. final newHobby = Hobbies(name: value);
  63. hobbiesLabelsList.add(newHobby);
  64. ToastUtil.show("添加成功,标签 $value");
  65. // 如果当前已选未超限,自动选中
  66. if (hobbiesSelectLabels.length <
  67. (currentCharacterCustomConfig?.maxHobbyNum ?? 3)) {
  68. hobbiesSelectLabels.add(newHobby);
  69. } else {
  70. ToastUtil.show(
  71. "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  72. );
  73. }
  74. },
  75. );
  76. }
  77. // 自定义兴趣爱好
  78. void clickCharacterCustom() {
  79. AtmobLog.d(tag, "clickCharacterCustom");
  80. CustomLabelDialog.show(
  81. maxLength: currentCharacterCustomConfig?.maxCharacterWords ?? 5,
  82. hintText: StringName.customLabelCharacterHint,
  83. clickCallback: (value) {
  84. var isExist = characterLabelsList
  85. .map((e) => e.name)
  86. .contains(value);
  87. if (isExist) {
  88. ToastUtil.show("添加失败,标签 $value 已存在");
  89. return;
  90. }
  91. characterLabelsList.add(CharactersList(name: value));
  92. ToastUtil.show("添加成功,标签 $value");
  93. if (characterSelectLabels.length <
  94. (currentCharacterCustomConfig?.maxCharacterNum ?? 3)) {
  95. characterSelectLabels.add(CharactersList(name: value));
  96. } else {
  97. ToastUtil.show(
  98. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  99. );
  100. }
  101. },
  102. );
  103. }
  104. /// 返回上一页
  105. void clickBack() {
  106. AtmobLog.d(tag, "clickBack");
  107. if (currentStep.value == StepType.hobbies) {
  108. currentStep.value = StepType.home;
  109. } else if (currentStep.value == StepType.characters) {
  110. currentStep.value = StepType.hobbies;
  111. } else if (currentStep.value == StepType.inputName) {
  112. currentStep.value = StepType.characters;
  113. } else {
  114. if (isSkipFromHistory.value) {
  115. // 跳到定制历史
  116. clickHistory();
  117. return;
  118. }
  119. Get.back();
  120. }
  121. }
  122. // 爱好页的下一步
  123. void clickHobbiesNext() {
  124. int min = currentCharacterCustomConfig?.minHobbyNum ?? 1;
  125. int max = currentCharacterCustomConfig?.maxHobbyNum ?? 3;
  126. if (hobbiesSelectLabels.isEmpty) {
  127. ToastUtil.show("请选择爱好");
  128. return;
  129. }
  130. if (hobbiesSelectLabels.length < min) {
  131. ToastUtil.show("至少选择$min个爱好");
  132. return;
  133. }
  134. if (hobbiesSelectLabels.length > max) {
  135. ToastUtil.show("最多选择$max个爱好");
  136. return;
  137. }
  138. clickNextButton(StepType.characters);
  139. }
  140. // 性格页的下一步
  141. void clickCharacterNext() {
  142. int min = currentCharacterCustomConfig?.minCharacterNum ?? 1;
  143. int max = currentCharacterCustomConfig?.maxCharacterNum ?? 3;
  144. if (characterSelectLabels.isEmpty) {
  145. ToastUtil.show("请选择特质");
  146. return;
  147. }
  148. if (characterSelectLabels.length < min) {
  149. ToastUtil.show("至少选择$min个特质");
  150. return;
  151. }
  152. if (characterSelectLabels.length > max) {
  153. ToastUtil.show("最多选择$max个特质");
  154. return;
  155. }
  156. clickNextButton(StepType.inputName);
  157. }
  158. // 名字页的下一步
  159. Future<void> clickInputNameNext() async {
  160. if (currentNameValue.value.isEmpty) {
  161. ToastUtil.show("请输入名字");
  162. return;
  163. }
  164. if (currentNameValue.value.length > 5) {
  165. ToastUtil.show("最多5个字哦~");
  166. return;
  167. }
  168. EventHandler.report(EventId.event_12004);
  169. await CharacterCustomDetailPage.start(
  170. hobbiesSelectLabels: hobbiesSelectLabels,
  171. characterSelectLabels: characterSelectLabels,
  172. characterCustomName: currentNameValue.value,
  173. );
  174. if (isSkipFromHistory.value) {
  175. // 跳到定制历史
  176. clickHistory();
  177. }
  178. }
  179. // 处理下一步
  180. void clickNextButton(StepType stepType) {
  181. if (currentCharacterCustomConfig == null) {
  182. AtmobLog.e(tag, "clickStartCustom - 当前配置为空");
  183. return;
  184. }
  185. if (stepType == StepType.hobbies) {
  186. EventHandler.report(EventId.event_12001);
  187. currentStep.value = StepType.hobbies;
  188. } else if (stepType == StepType.characters) {
  189. EventHandler.report(EventId.event_12002);
  190. currentStep.value = StepType.characters;
  191. } else if (stepType == StepType.inputName) {
  192. EventHandler.report(EventId.event_12003);
  193. currentStep.value = StepType.inputName;
  194. } else {
  195. currentStep.value = StepType.home;
  196. }
  197. }
  198. /// 选择爱好标签
  199. void selectHobby(Hobbies hobby) {
  200. handleSelection(
  201. name: hobby,
  202. selectedList: hobbiesSelectLabels,
  203. max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
  204. errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  205. );
  206. }
  207. /// 选择性格标签
  208. void selectCharacter(CharactersList name) {
  209. handleSelection(
  210. name: name,
  211. selectedList: characterSelectLabels,
  212. max: currentCharacterCustomConfig?.maxCharacterNum ?? 3,
  213. errorMessage:
  214. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  215. );
  216. }
  217. Future<void> clickHistory() async {
  218. AtmobLog.d(tag, "clickHistory");
  219. if (isSkipFromHistory.value) {
  220. isSkipFromHistory.value = false;
  221. }else {
  222. EventHandler.report(EventId.event_12007);
  223. }
  224. var result = await CharacterCustomListPage.start();
  225. if (result != null) {
  226. AtmobLog.d(tag, "clickHistory");
  227. currentStep.value = result;
  228. isSkipFromHistory.value = true;
  229. }
  230. }
  231. ///标签选择处理
  232. void handleSelection({
  233. required dynamic name,
  234. required RxList<dynamic> selectedList,
  235. required int max,
  236. required String errorMessage,
  237. }) {
  238. if (selectedList.contains(name)) {
  239. selectedList.remove(name);
  240. } else if (selectedList.length < max) {
  241. selectedList.add(name);
  242. } else {
  243. ToastUtil.show(errorMessage);
  244. }
  245. }
  246. ///清空数据
  247. void clearData() {
  248. AtmobLog.d(tag, "clearData");
  249. currentStep.value = StepType.home;
  250. hobbiesSelectLabels.clear();
  251. characterSelectLabels.clear();
  252. currentNameValue.value = "";
  253. CharacterCustomListPage.start();
  254. }
  255. }