controller.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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:clean/utils/toast_util.dart';
  6. import 'package:flutter_contacts/contact.dart';
  7. import 'package:flutter_contacts/flutter_contacts.dart';
  8. import 'package:get/get_rx/src/rx_types/rx_types.dart';
  9. import '../../../dialog/loading_dialog.dart';
  10. class ContactDuplicateController extends BaseController {
  11. // 是否为编辑状态
  12. RxBool isEdit = false.obs;
  13. // 是否全选
  14. RxBool isAllSelected = false.obs;
  15. RxMap<String, List<Contact>> contactsByPhoneNumber =
  16. <String, List<Contact>>{}.obs;
  17. var contactCount = 0;
  18. // RxMap<String, List<Contact>> contactsByPhoneNumber = <String, List<Contact>>{}.obs;
  19. // 存储选中的联系人ID
  20. final RxSet<String> selectedContacts = <String>{}.obs;
  21. @override
  22. void onInit() {
  23. // TODO: implement onInit
  24. super.onInit();
  25. loadDuplicateContacts();
  26. }
  27. Future<void> loadDuplicateContacts() async {
  28. Map<String, List<Contact>> groupedContacts = {};
  29. // 获取所有联系人
  30. List<Contact> contacts = await FlutterContacts.getContacts(
  31. withProperties: true,
  32. withPhoto: true,
  33. );
  34. // 按名字的首字母排序
  35. contacts
  36. .sort((a, b) => (a.displayName ?? '').compareTo(b.displayName ?? ''));
  37. for (var contact in contacts) {
  38. if (contact.phones.isNotEmpty) {
  39. for (var phone in contact.phones) {
  40. final phoneNumber = phone.number;
  41. if (phoneNumber.isNotEmpty) {
  42. if (!groupedContacts.containsKey(phoneNumber)) {
  43. groupedContacts[phoneNumber] = [];
  44. }
  45. groupedContacts[phoneNumber]!.add(contact);
  46. }
  47. }
  48. }
  49. }
  50. Map<String, List<Contact>> tempContacts = {};
  51. tempContacts.addAll(groupedContacts);
  52. for (var key in groupedContacts.keys) {
  53. if (groupedContacts[key]?.length == 1) {
  54. tempContacts.remove(key);
  55. }
  56. }
  57. contactsByPhoneNumber.value = tempContacts;
  58. contactCount = 0;
  59. for (var key in contactsByPhoneNumber.keys) {
  60. if (contactsByPhoneNumber[key] != null) {
  61. contactCount += contactsByPhoneNumber[key]!.length;
  62. }
  63. }
  64. }
  65. // 选择/取消选择联系人
  66. void toggleSelectContact(String phoneNumber) {
  67. // final asset = ContactState.contactList.firstWhere((contact) => contact.id == selectContact.id);
  68. final contacts = contactsByPhoneNumber[phoneNumber];
  69. if (contacts != null) {
  70. for (var contact in contacts) {
  71. if (selectedContacts.contains(contact.id)) {
  72. selectedContacts.remove(contact.id);
  73. } else {
  74. selectedContacts.add(contact.id);
  75. }
  76. }
  77. }
  78. // 更新全选状态
  79. isAllSelected.value = selectedContacts.length == contactCount;
  80. }
  81. // 全选/取消全选
  82. void toggleSelectAll() {
  83. if (isAllSelected.value) {
  84. selectedContacts.clear();
  85. } else {
  86. for (var key in contactsByPhoneNumber.keys) {
  87. if (contactsByPhoneNumber[key] != null) {
  88. for (var contact in contactsByPhoneNumber[key]!) {
  89. selectedContacts.add(contact.id);
  90. }
  91. }
  92. }
  93. }
  94. isAllSelected.value = !isAllSelected.value;
  95. }
  96. // 退出编辑模式时清空选择
  97. void exitEditMode() {
  98. isEdit.value = false;
  99. selectedContacts.clear();
  100. isAllSelected.value = false;
  101. }
  102. Future<void> mergeBtnClick(List<Contact> contacts) async {
  103. if (!userRepository.isVip()) {
  104. StorePage.start();
  105. return;
  106. }
  107. LoadingDialog.show();
  108. Contact? contactWithMostInfo;
  109. int maxInfoCount = 0;
  110. for (var contact in contacts) {
  111. final infoCount = _countContactInfo(contact);
  112. if (infoCount > maxInfoCount) {
  113. maxInfoCount = infoCount;
  114. contactWithMostInfo = contact;
  115. }
  116. }
  117. for (var contact in contacts) {
  118. if (contact.id != contactWithMostInfo?.id) {
  119. await contact.delete();
  120. }
  121. }
  122. Future.delayed(Duration(milliseconds: 300), () async {
  123. LoadingDialog.hide();
  124. ToastUtil.show("Successful");
  125. exitEditMode();
  126. await loadDuplicateContacts();
  127. });
  128. }
  129. void deleteBtnClick() {
  130. // 获取要删除的资产
  131. final contactToDelete = ContactState.contactList
  132. .where((contact) => selectedContacts.contains(contact.id))
  133. .toList();
  134. LoadingDialog.show();
  135. for (var contact in contactToDelete) {
  136. contact.delete();
  137. }
  138. Future.delayed(Duration(milliseconds: 300), () {
  139. ToastUtil.show("Successful");
  140. exitEditMode();
  141. ContactState.loadContacts();
  142. });
  143. }
  144. // 统计联系人的信息数量
  145. int _countContactInfo(Contact contact) {
  146. int count = 0;
  147. // 统计姓名
  148. if (contact.name.first.isNotEmpty || contact.name.last.isNotEmpty) {
  149. count++;
  150. }
  151. // 统计电话号码
  152. if (contact.phones.isNotEmpty) {
  153. count += contact.phones.length;
  154. }
  155. // 统计电子邮件
  156. if (contact.emails.isNotEmpty) {
  157. count += contact.emails.length;
  158. }
  159. // 统计地址
  160. if (contact.addresses.isNotEmpty) {
  161. count += contact.addresses.length;
  162. }
  163. if (contact.events.isNotEmpty) {
  164. count += contact.events.length;
  165. }
  166. if (contact.notes.isNotEmpty) {
  167. count += contact.notes.length;
  168. }
  169. if (contact.socialMedias.isNotEmpty) {
  170. count += contact.socialMedias.length;
  171. }
  172. if (contact.websites.isNotEmpty) {
  173. count += contact.websites.length;
  174. }
  175. if (contact.organizations.isNotEmpty) {
  176. count += contact.organizations.length;
  177. }
  178. // 统计图片
  179. if (contact.photo != null) {
  180. if (contact.photo!.isNotEmpty) {
  181. count++;
  182. }
  183. }
  184. return count;
  185. }
  186. }