crowd_labels.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'dart:collection';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vnoteapp/architecture/defines.dart';
  5. import 'package:vnoteapp/managers/interfaces/label.dart';
  6. /// 人群分类标签控制器
  7. class CrowdLabelsController extends FControllerBase {
  8. final state = CrowdLablesState();
  9. final _manager = Get.find<ILabelManager>();
  10. @override
  11. void onReady() {
  12. super.onReady();
  13. _loadOptions();
  14. }
  15. void onNormalSelectedChanged(List<String> codes) {
  16. state.selectedNormalCodes = codes;
  17. }
  18. void onDiseaseSelectedChanged(List<String> codes) {
  19. state.selectedDiseaseCodes = codes;
  20. }
  21. void onSpecialCareSelectedChanged(List<String> codes) {
  22. state.selectedSpecialCareCodes = codes;
  23. }
  24. void onItemCheckChanged(String code) {
  25. bool result = false;
  26. result = _updateNormalChecked(code);
  27. if (result) return;
  28. result = _updateDiseaseChecked(code);
  29. if (result) return;
  30. result = _updateSpecialCareChecked(code);
  31. }
  32. bool _updateNormalChecked(code) {
  33. final item = state.normalOptions.firstWhereOrNull((e) => e.code == code);
  34. if (item == null) return false;
  35. final arr = state.selectedNormalCodes.toList();
  36. if (!arr.contains(code)) {
  37. if (arr.contains(code) == false) {
  38. arr.add(code);
  39. }
  40. } else {
  41. arr.remove(code);
  42. }
  43. state.selectedNormalCodes = arr;
  44. return true;
  45. }
  46. bool _updateDiseaseChecked(code) {
  47. final item = state.diseaseOptions.firstWhereOrNull((e) => e.code == code);
  48. if (item == null) return false;
  49. final arr = state.selectedDiseaseCodes.toList();
  50. if (!arr.contains(code)) {
  51. if (arr.contains(code) == false) {
  52. arr.add(code);
  53. }
  54. } else {
  55. arr.remove(code);
  56. }
  57. state.selectedDiseaseCodes = arr;
  58. return true;
  59. }
  60. bool _updateSpecialCareChecked(code) {
  61. final item =
  62. state.specialCareOptions.firstWhereOrNull((e) => e.code == code);
  63. if (item == null) return false;
  64. final arr = state.selectedSpecialCareCodes.toList();
  65. if (!arr.contains(code)) {
  66. if (arr.contains(code) == false) {
  67. arr.add(code);
  68. }
  69. } else {
  70. arr.remove(code);
  71. }
  72. state.selectedSpecialCareCodes = arr;
  73. return true;
  74. }
  75. Future<void> _loadOptions() async {
  76. // TODO: Future.await()
  77. state.normalOptions = await _manager.getNormalCrowdLabels();
  78. // TODO:
  79. state.diseaseOptions = (await _manager.getDiseaseCrowdLabels())
  80. ..removeLast();
  81. // state.diseaseOptions = await _manager.getDiseaseCrowdLabels();
  82. state.specialCareOptions = await _manager.getSpecialCareCrowdLabels();
  83. }
  84. }
  85. /// 人群分类标签状态
  86. class CrowdLablesState {
  87. final RxList<LabelDTO> _normalOptions = RxList<LabelDTO>();
  88. final RxList<LabelDTO> _diseaseOptions = RxList<LabelDTO>();
  89. final RxList<LabelDTO> _specialCareOptions = RxList<LabelDTO>();
  90. final RxList<String> _selectedNormalCodes = RxList<String>();
  91. final RxList<String> _selectedDiseaseCodes = RxList<String>();
  92. final RxList<String> _selectedSpecialCareCodes = RxList<String>();
  93. /// 一般人群分类选项集合
  94. List<LabelDTO> get normalOptions =>
  95. UnmodifiableListView(_normalOptions.toList());
  96. set normalOptions(List<LabelDTO> val) => _normalOptions.value = val;
  97. /// 疾病人群分类选项集合
  98. List<LabelDTO> get diseaseOptions =>
  99. UnmodifiableListView(_diseaseOptions.toList());
  100. set diseaseOptions(List<LabelDTO> val) => _diseaseOptions.value = val;
  101. /// 特殊照顾人群分类选项集合
  102. List<LabelDTO> get specialCareOptions =>
  103. UnmodifiableListView(_specialCareOptions.toList());
  104. set specialCareOptions(List<LabelDTO> val) => _specialCareOptions.value = val;
  105. /// 一般人群分类 已选择Code集合
  106. List<String> get selectedNormalCodes =>
  107. UnmodifiableListView(_selectedNormalCodes.toList());
  108. set selectedNormalCodes(List<String> val) => _selectedNormalCodes.value = val;
  109. /// 疾病人群分类 已选择Code集合
  110. List<String> get selectedDiseaseCodes =>
  111. UnmodifiableListView(_selectedDiseaseCodes.toList());
  112. set selectedDiseaseCodes(List<String> val) =>
  113. _selectedDiseaseCodes.value = val;
  114. /// 特殊照顾人群分类 已选择Code集合
  115. List<String> get selectedSpecialCareCodes =>
  116. UnmodifiableListView(_selectedSpecialCareCodes.toList());
  117. set selectedSpecialCareCodes(List<String> val) =>
  118. _selectedSpecialCareCodes.value = val;
  119. /// 所有 已选择Code集合
  120. List<String> get selectedCodes {
  121. final array = <String>[];
  122. array.addAll(_selectedNormalCodes);
  123. array.addAll(_selectedDiseaseCodes);
  124. array.addAll(_selectedSpecialCareCodes);
  125. return array;
  126. }
  127. /// 所有 已选择标签名称集合
  128. List<String> get selectedNames {
  129. final array = <String>[];
  130. for (var code in _selectedNormalCodes) {
  131. array.add(_getNameByCode(_normalOptions, code)!);
  132. }
  133. for (var code in _selectedDiseaseCodes) {
  134. array.add(_getNameByCode(_diseaseOptions, code)!);
  135. }
  136. for (var code in _selectedSpecialCareCodes) {
  137. array.add(_getNameByCode(_specialCareOptions, code)!);
  138. }
  139. return array;
  140. }
  141. static String? _getNameByCode(List<LabelDTO> source, String code) {
  142. final dto = source.firstWhereOrNull((e) => e.code == code);
  143. return dto?.labelName;
  144. }
  145. }