character_custom_controller.dart 8.6 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. // Get.back();
  118. // return;
  119. // }
  120. Get.back();
  121. }
  122. }
  123. // 爱好页的下一步
  124. void clickHobbiesNext() {
  125. int min = currentCharacterCustomConfig?.minHobbyNum ?? 1;
  126. int max = currentCharacterCustomConfig?.maxHobbyNum ?? 3;
  127. if (hobbiesSelectLabels.isEmpty) {
  128. ToastUtil.show("请选择爱好");
  129. return;
  130. }
  131. if (hobbiesSelectLabels.length < min) {
  132. ToastUtil.show("至少选择$min个爱好");
  133. return;
  134. }
  135. if (hobbiesSelectLabels.length > max) {
  136. ToastUtil.show("最多选择$max个爱好");
  137. return;
  138. }
  139. clickNextButton(StepType.characters);
  140. }
  141. // 性格页的下一步
  142. void clickCharacterNext() {
  143. int min = currentCharacterCustomConfig?.minCharacterNum ?? 1;
  144. int max = currentCharacterCustomConfig?.maxCharacterNum ?? 3;
  145. if (characterSelectLabels.isEmpty) {
  146. ToastUtil.show("请选择特质");
  147. return;
  148. }
  149. if (characterSelectLabels.length < min) {
  150. ToastUtil.show("至少选择$min个特质");
  151. return;
  152. }
  153. if (characterSelectLabels.length > max) {
  154. ToastUtil.show("最多选择$max个特质");
  155. return;
  156. }
  157. clickNextButton(StepType.inputName);
  158. }
  159. // 名字页的下一步
  160. Future<void> clickInputNameNext() async {
  161. if (currentNameValue.value.isEmpty) {
  162. ToastUtil.show("请输入名字");
  163. return;
  164. }
  165. if (currentNameValue.value.length > 5) {
  166. ToastUtil.show("最多5个字哦~");
  167. return;
  168. }
  169. EventHandler.report(EventId.event_12004);
  170. await CharacterCustomDetailPage.start(
  171. hobbiesSelectLabels: hobbiesSelectLabels,
  172. characterSelectLabels: characterSelectLabels,
  173. characterCustomName: currentNameValue.value,
  174. );
  175. // if (isSkipFromHistory.value) {
  176. // // 跳到定制历史
  177. // clickHistory();
  178. //
  179. // }
  180. }
  181. // 处理下一步
  182. void clickNextButton(StepType stepType) {
  183. if (currentCharacterCustomConfig == null) {
  184. AtmobLog.e(tag, "clickStartCustom - 当前配置为空");
  185. return;
  186. }
  187. if (stepType == StepType.hobbies) {
  188. EventHandler.report(EventId.event_12001);
  189. currentStep.value = StepType.hobbies;
  190. } else if (stepType == StepType.characters) {
  191. EventHandler.report(EventId.event_12002);
  192. currentStep.value = StepType.characters;
  193. } else if (stepType == StepType.inputName) {
  194. EventHandler.report(EventId.event_12003);
  195. currentStep.value = StepType.inputName;
  196. } else {
  197. currentStep.value = StepType.home;
  198. }
  199. }
  200. /// 选择爱好标签
  201. void selectHobby(Hobbies hobby) {
  202. handleSelection(
  203. name: hobby,
  204. selectedList: hobbiesSelectLabels,
  205. max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
  206. errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  207. );
  208. }
  209. /// 选择性格标签
  210. void selectCharacter(CharactersList name) {
  211. handleSelection(
  212. name: name,
  213. selectedList: characterSelectLabels,
  214. max: currentCharacterCustomConfig?.maxCharacterNum ?? 3,
  215. errorMessage:
  216. "最多选择${currentCharacterCustomConfig?.maxCharacterNum ?? 3}个特质",
  217. );
  218. }
  219. Future<void> clickHistory() async {
  220. AtmobLog.d(tag, "clickHistory");
  221. if (isSkipFromHistory.value) {
  222. isSkipFromHistory.value = false;
  223. }else {
  224. EventHandler.report(EventId.event_12007);
  225. }
  226. var result = await CharacterCustomListPage.start();
  227. if (result != null) {
  228. AtmobLog.d(tag, "clickHistory");
  229. currentStep.value = result;
  230. isSkipFromHistory.value = true;
  231. }
  232. }
  233. ///标签选择处理
  234. void handleSelection({
  235. required dynamic name,
  236. required RxList<dynamic> selectedList,
  237. required int max,
  238. required String errorMessage,
  239. }) {
  240. if (selectedList.contains(name)) {
  241. selectedList.remove(name);
  242. } else if (selectedList.length < max) {
  243. selectedList.add(name);
  244. } else {
  245. ToastUtil.show(errorMessage);
  246. }
  247. }
  248. ///清空数据
  249. void clearData() {
  250. AtmobLog.d(tag, "clearData");
  251. currentStep.value = StepType.home;
  252. hobbiesSelectLabels.clear();
  253. characterSelectLabels.clear();
  254. currentNameValue.value = "";
  255. CharacterCustomListPage.start();
  256. }
  257. }