diff --git a/dialer/lib/features/contacts/contact_state.dart b/dialer/lib/features/contacts/contact_state.dart index 82e21c2..b6cf003 100644 --- a/dialer/lib/features/contacts/contact_state.dart +++ b/dialer/lib/features/contacts/contact_state.dart @@ -102,6 +102,25 @@ class _ContactStateState extends State { }); } + bool doesContactExist(Contact contact) { + // Example: consider it "existing" if there's a matching phone number + for (final existing in _allContacts) { + if (existing.toVCard() == contact.toVCard()) { + return true; + } + // for (final phone in existing.phones) { + // for (final newPhone in contact.phones) { + // // Simple exact match; you can do more advanced logic + // if (phone.normalizedNumber == newPhone.normalizedNumber) { + // return true; + // } + // } + // } We might switch to finer and smarter logic later, ex: remove trailing spaces, capitals + } + return false; + } + + @override Widget build(BuildContext context) { return _InheritedContactState( diff --git a/dialer/lib/features/contacts/widgets/add_contact_button.dart b/dialer/lib/features/contacts/widgets/add_contact_button.dart index e370061..399ab48 100644 --- a/dialer/lib/features/contacts/widgets/add_contact_button.dart +++ b/dialer/lib/features/contacts/widgets/add_contact_button.dart @@ -3,38 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; class AddContactButton extends StatelessWidget { - const AddContactButton({Key? key}) : super(key: key); - - - /// Create the contact from vCard, insert it, and open the system Contacts app in edit mode. - Future createContactFromVCardAndOpenEditor(String vCardString) async { - // Request permission first - final granted = await FlutterContacts.requestPermission(readonly: false); - if (!granted) { - debugPrint('Contacts permission denied'); - return; - } - - // Create a contact from vCard data - final newContact = Contact.fromVCard(vCardString); - - // Insert it into the device's contacts - await newContact.insert(); - - // Open the system Contacts app with this contact in edit mode - final edited = await FlutterContacts.openExternalEdit(newContact.id); - if (edited == null) { - debugPrint(" ____ Could NOT edit contact !"); - } - } - - Future createNewContactBlank() async { - // For a blank contact, we can rely on flutter_contacts to do an external insert: - // But if we specifically want the system's default "insert" flow, we can do: - // `await FlutterContacts.openExternalInsert();` - // or rely on your old Android-intent approach. - await FlutterContacts.openExternalInsert(); - } + const AddContactButton({super.key}); @override Widget build(BuildContext context) { @@ -62,9 +31,9 @@ class AddContactButton extends StatelessWidget { ), ); - // If we got a vCard string back if (vCardString != null && vCardString is String) { - await createContactFromVCardAndOpenEditor(vCardString); + await FlutterContacts.openExternalInsert(Contact + .fromVCard(vCardString)); } }, child: const Text( @@ -76,7 +45,7 @@ class AddContactButton extends StatelessWidget { onPressed: () async { Navigator.of(context).pop(); // Create a blank contact entry - await createNewContactBlank(); + await FlutterContacts.openExternalInsert(); }, child: const Text( "Create new contact",