controller.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'package:clean/base/base_controller.dart';
  2. import 'package:clean/data/repositories/user_repository.dart';
  3. import 'package:clean/module/contact/contact_state.dart';
  4. import 'package:clean/module/store/store_view.dart';
  5. import 'package:flutter/widgets.dart';
  6. import 'package:flutter_contacts/contact.dart';
  7. import 'package:get/get.dart';
  8. import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
  9. import '../../../utils/toast_util.dart';
  10. class ContactInCompleteController extends BaseController {
  11. // 存储不符合要求的联系人
  12. final RxList<Contact> filteredContacts = <Contact>[].obs;
  13. // 存储联系人按首字母分组
  14. final RxMap<String, List<Contact>> groupedContacts =
  15. <String, List<Contact>>{}.obs;
  16. // 存储联系人首字母
  17. final RxList<String> initials = <String>[].obs;
  18. // 是否全选
  19. final RxBool isAllSelected = false.obs;
  20. // 存储选中的联系人
  21. final RxSet<String> selectedContacts = <String>{}.obs;
  22. final ItemScrollController itemScrollController = ItemScrollController();
  23. final ItemPositionsListener itemPositionsListener =
  24. ItemPositionsListener.create();
  25. // isEdit
  26. final RxBool isEdit = false.obs;
  27. @override
  28. void onInit()async {
  29. super.onInit();
  30. }
  31. @override
  32. void onReady() {
  33. // TODO: implement onReady
  34. super.onReady();
  35. filterContacts();
  36. groupContacts();
  37. }
  38. // 过滤联系人
  39. void filterContacts() {
  40. filteredContacts.value = ContactState.contactList.where((contact) {
  41. final hasValidPhone =
  42. contact.phones.any((p) => isValidPhoneNumber(p.number ?? ''));
  43. return contact.phones.isEmpty || !hasValidPhone;
  44. }).toList();
  45. }
  46. bool isValidPhoneNumber(String phone) {
  47. // 如果电话号码为空,则返回false
  48. if (phone.isEmpty) return false;
  49. // 如果电话号码不是数字、空格、+号,则返回false
  50. return RegExp(r'^\+?[0-9 -]+$').hasMatch(phone);
  51. }
  52. Future<void> deleteBtnClick() async {
  53. if (!userRepository.isVip()) {
  54. StorePage.start();
  55. return;
  56. }
  57. if (selectedContacts.isEmpty) {
  58. ToastUtil.show("Please select a contact");
  59. return;
  60. }
  61. print("deleteBtnClick selectedContacts: $selectedContacts");
  62. final contactToDelete = ContactState.contactList
  63. .where((contact) => selectedContacts.contains(contact.id))
  64. .toList();
  65. for (var contact in contactToDelete) {
  66. await contact.delete(); // 删除操作必须 `await`
  67. }
  68. ToastUtil.show("Successful");
  69. exitEditMode();
  70. await ContactState.loadContacts(); // 确保联系人列表更新
  71. filterContacts(); // 重新筛选联系人
  72. groupContacts(); // 重新分组
  73. debugPrint("filteredContacts.length: ${filteredContacts.length}");
  74. }
  75. // 全选/取消全选
  76. void toggleSelectAll() {
  77. if (isAllSelected.value) {
  78. selectedContacts.clear();
  79. } else {
  80. selectedContacts
  81. .addAll(filteredContacts.map((contact) => contact.id));
  82. }
  83. isAllSelected.value = !isAllSelected.value;
  84. }
  85. // 退出编辑模式时清空选择
  86. void exitEditMode() {
  87. isEdit.value = false;
  88. selectedContacts.clear();
  89. isAllSelected.value = false;
  90. }
  91. // 滚动到指定首字母
  92. void groupContacts() {
  93. final Map<String, List<Contact>> map = {};
  94. for (var contact in filteredContacts) {
  95. final initial = contact.displayName.isNotEmpty == true
  96. ? contact.displayName[0].toUpperCase()
  97. : '#';
  98. map.putIfAbsent(initial, () => []).add(contact);
  99. }
  100. final sortedKeys = map.keys.toList()..sort();
  101. groupedContacts.value =
  102. Map.fromEntries(sortedKeys.map((key) => MapEntry(key, map[key]!)));
  103. initials.value = sortedKeys;
  104. }
  105. void scrollToInitial(String initial) {
  106. int index = ContactState.initials.indexOf(initial);
  107. if (index != -1) {
  108. final positions = itemPositionsListener.itemPositions.value;
  109. final isVisible = positions.any((position) => position.index == index);
  110. if (!isVisible) {
  111. itemScrollController.scrollTo(
  112. index: index,
  113. duration: Duration(milliseconds: 300),
  114. );
  115. }
  116. }
  117. }
  118. void toggleSelectContact(Contact selectContact) {
  119. print("selectContact: ${selectContact.displayName}");
  120. if (selectedContacts.contains(selectContact.id)) {
  121. selectedContacts.remove(selectContact.id);
  122. } else {
  123. selectedContacts.add(selectContact.id);
  124. }
  125. isAllSelected.value = selectedContacts.length == filteredContacts.length;
  126. }
  127. }