change_hobbies_controller.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:injectable/injectable.dart';
  2. import 'package:keyboard/base/base_controller.dart';
  3. import 'package:get/get.dart';
  4. import 'package:keyboard/utils/atmob_log.dart';
  5. import '../../../data/bean/custom_config_info.dart';
  6. import '../../../data/repository/config_repository.dart';
  7. import '../../../dialog/custom_label_dialog.dart';
  8. import '../../../resource/string.gen.dart';
  9. import '../../../utils/toast_util.dart';
  10. @injectable
  11. class ChangeHobbiesController extends BaseController {
  12. final String tag = 'ChangeHobbiesController';
  13. final ConfigRepository configRepository;
  14. CustomConfigInfo? get currentCharacterCustomConfig =>
  15. configRepository.characterCustomConfig.value;
  16. final RxList<Hobbies> hobbiesLabelsList = <Hobbies>[].obs;
  17. final RxList<Hobbies> hobbiesSelectLabels = <Hobbies>[].obs;
  18. ChangeHobbiesController(this.configRepository);
  19. @override
  20. void onInit() {
  21. super.onInit();
  22. initData();
  23. final List<Hobbies>? hobbies = Get.arguments["hobbies"];
  24. if (hobbies != null) {
  25. for (var hobby in hobbies) {
  26. final exists = hobbiesLabelsList.any((e) => e.name == hobby.name);
  27. if (!exists) {
  28. hobbiesLabelsList.add(hobby);
  29. }
  30. }
  31. hobbiesSelectLabels.assignAll(hobbies);
  32. AtmobLog.d(
  33. tag,
  34. "hobbiesSelectLabels: ${hobbiesSelectLabels.map((e) => e.name)}",
  35. );
  36. }
  37. }
  38. // 初始化数据
  39. void initData() {
  40. AtmobLog.d(tag, "initData");
  41. hobbiesLabelsList.value = currentCharacterCustomConfig?.hobbies ?? [];
  42. }
  43. @override
  44. void onClose() {
  45. super.onClose();
  46. }
  47. void clickBack() {
  48. Get.back();
  49. }
  50. void clickSave() {
  51. if (hobbiesSelectLabels.isEmpty) {
  52. ToastUtil.show(
  53. "至少选择${currentCharacterCustomConfig?.minHobbyNum ?? 1}个爱好",
  54. );
  55. return;
  56. }
  57. AtmobLog.d(
  58. tag,
  59. "clickSave hobbiesSelectLabels: ${hobbiesSelectLabels.toJson()}",
  60. );
  61. Get.back(result: hobbiesSelectLabels);
  62. }
  63. /// 选择爱好标签
  64. void selectHobby(Hobbies hobby) {
  65. handleSelection(
  66. name: hobby,
  67. selectedList: hobbiesSelectLabels,
  68. max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
  69. errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  70. );
  71. }
  72. ///标签选择处理
  73. void handleSelection({
  74. required dynamic name,
  75. required RxList<dynamic> selectedList,
  76. required int max,
  77. required String errorMessage,
  78. }) {
  79. if (selectedList.contains(name)) {
  80. selectedList.remove(name);
  81. } else if (selectedList.length < max) {
  82. selectedList.add(name);
  83. } else {
  84. ToastUtil.show(errorMessage);
  85. }
  86. }
  87. void clickHobbiesCustom() {
  88. AtmobLog.d(tag, "clickHobbiesCustom");
  89. CustomLabelDialog.show(
  90. title: StringName.addHobbies,
  91. maxLength: currentCharacterCustomConfig?.maxHobbyWords ?? 5,
  92. hintText: StringName.customLabelHobbiesHint,
  93. clickCallback: (value) {
  94. final isExist = hobbiesLabelsList.map((e) => e.name).contains(value);
  95. if (isExist) {
  96. ToastUtil.show("添加失败,标签 $value 已存在");
  97. return;
  98. }
  99. final newHobby = Hobbies(name: value);
  100. hobbiesLabelsList.add(newHobby);
  101. // 如果当前已选未超限,自动选中
  102. if (hobbiesSelectLabels.length <
  103. (currentCharacterCustomConfig?.maxHobbyNum ?? 3)) {
  104. hobbiesSelectLabels.add(newHobby);
  105. } else {
  106. ToastUtil.show(
  107. "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
  108. );
  109. }
  110. },
  111. );
  112. }
  113. }