diff --git a/dialer/lib/features/contacts/contact_state.dart b/dialer/lib/features/contacts/contact_state.dart index fb544c4..6c104c0 100644 --- a/dialer/lib/features/contacts/contact_state.dart +++ b/dialer/lib/features/contacts/contact_state.dart @@ -116,6 +116,38 @@ class _ContactStateState extends State { await fetchContacts(); } + // Add this new method to update a single contact in state without reloading all contacts + Future updateContactInState(Contact updatedContact) async { + setState(() { + // Find and update in the all contacts list + final allIndex = _allContacts.indexWhere((c) => c.id == updatedContact.id); + if (allIndex != -1) { + _allContacts[allIndex] = updatedContact; + } + + // Update the favorites list based on the star status + if (updatedContact.isStarred) { + // Add to favorites if not already there + if (!_favoriteContacts.any((c) => c.id == updatedContact.id)) { + _favoriteContacts.add(updatedContact); + } else { + // If already in favorites, update it + final favIndex = _favoriteContacts.indexWhere((c) => c.id == updatedContact.id); + if (favIndex != -1) { + _favoriteContacts[favIndex] = updatedContact; + } + } + } else { + // Remove from favorites if it's there + _favoriteContacts.removeWhere((c) => c.id == updatedContact.id); + } + + // Re-sort both lists to maintain alphabetical order + _allContacts.sort((a, b) => a.displayName.compareTo(b.displayName)); + _favoriteContacts.sort((a, b) => a.displayName.compareTo(b.displayName)); + }); + } + void setScrollOffset(double offset) { setState(() { _scrollOffset = offset; diff --git a/dialer/lib/features/contacts/widgets/alphabet_scroll_page.dart b/dialer/lib/features/contacts/widgets/alphabet_scroll_page.dart index 822c36e..c3f9418 100644 --- a/dialer/lib/features/contacts/widgets/alphabet_scroll_page.dart +++ b/dialer/lib/features/contacts/widgets/alphabet_scroll_page.dart @@ -52,20 +52,42 @@ class _AlphabetScrollPageState extends State { void _toggleFavorite(Contact contact) async { try { - if (await FlutterContacts.requestPermission()) { - Contact? fullContact = await FlutterContacts.getContact(contact.id, - withProperties: true, - withAccounts: true, - withPhoto: true, - withThumbnail: true); + // Check permission only once + if (!await FlutterContacts.requestPermission()) { + print("Could not get contact permission"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Contact permission not granted')), + ); + return; + } + + Contact? fullContact = await FlutterContacts.getContact(contact.id, + withProperties: true, + withAccounts: true, + withPhoto: true, + withThumbnail: true); - if (fullContact != null) { - fullContact.isStarred = !fullContact.isStarred; - await FlutterContacts.updateContact(fullContact); - } - await _refreshContacts(); - } else { - print("Could not fetch contact details"); + if (fullContact != null) { + // Toggle the favorite status + fullContact.isStarred = !fullContact.isStarred; + // Update in database + await FlutterContacts.updateContact(fullContact); + + // Update the UI immediately - we need to update the ContactState + final contactState = ContactState.of(context); + await contactState.updateContactInState(fullContact); + + // Show feedback to the user + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + fullContact.isStarred + ? '${fullContact.displayName} added to favorites' + : '${fullContact.displayName} removed from favorites' + ), + duration: Duration(seconds: 1), + ), + ); } } catch (e) { print("Error updating favorite status: $e"); diff --git a/dialer/lib/features/home/home_page.dart b/dialer/lib/features/home/home_page.dart index 65adaa8..d2e8ff3 100644 --- a/dialer/lib/features/home/home_page.dart +++ b/dialer/lib/features/home/home_page.dart @@ -25,7 +25,7 @@ class _MyHomePageState extends State void initState() { super.initState(); // Set the TabController length to 4 - _tabController = TabController(length: 4, vsync: this, initialIndex: 1); + _tabController = TabController(length: 4, vsync: this, initialIndex: 2); _tabController.addListener(_handleTabIndex); _fetchContacts(); } @@ -68,23 +68,42 @@ class _MyHomePageState extends State void _toggleFavorite(Contact contact) async { try { - if (await FlutterContacts.requestPermission()) { - Contact? fullContact = await FlutterContacts.getContact(contact.id, - withProperties: true, - withAccounts: true, - withPhoto: true, - withThumbnail: true); - - if (fullContact != null) { - fullContact.isStarred = !fullContact.isStarred; - await FlutterContacts.updateContact(fullContact); - setState(() { - // Updating the contact list after toggling the favorite - _fetchContacts(); - }); - } - } else { - print("Could not fetch contact details"); + // Check permission only once at the beginning + if (!await FlutterContacts.requestPermission()) { + print("Could not get contact permission"); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Contact permission not granted')), + ); + return; + } + + // Get the full contact with all properties + Contact? fullContact = await FlutterContacts.getContact(contact.id, + withProperties: true, + withAccounts: true, + withPhoto: true, + withThumbnail: true); + + if (fullContact != null) { + // Toggle the starred status + fullContact.isStarred = !fullContact.isStarred; + // Update in the database + await FlutterContacts.updateContact(fullContact); + + // Update UI immediately with the new state + setState(() { + // Find and update the contact in our local list + final index = _allContacts.indexWhere((c) => c.id == contact.id); + if (index != -1) { + _allContacts[index] = fullContact; + } + + // Update contact suggestions if needed + final suggestionIndex = _contactSuggestions.indexWhere((c) => c.id == contact.id); + if (suggestionIndex != -1) { + _contactSuggestions[suggestionIndex] = fullContact; + } + }); } } catch (e) { print("Error updating favorite status: $e");