diff --git a/dialer/lib/features/contacts/contact_service.dart b/dialer/lib/features/contacts/contact_service.dart index 85e4cf1..105f815 100644 --- a/dialer/lib/features/contacts/contact_service.dart +++ b/dialer/lib/features/contacts/contact_service.dart @@ -12,4 +12,9 @@ class ContactService { Future addNewContact(Contact contact) async { await FlutterContacts.insertContact(contact); } + + Future toogleFavorite(Contact contact) async { + contact.isStarred = !contact.isStarred; + await FlutterContacts.updateContact(contact); + } } \ No newline at end of file diff --git a/dialer/lib/features/favorites/favorites_state.dart b/dialer/lib/features/favorites/favorites_state.dart index 897b3f1..d4acc8f 100644 --- a/dialer/lib/features/favorites/favorites_state.dart +++ b/dialer/lib/features/favorites/favorites_state.dart @@ -2,13 +2,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; import 'package:dialer/features/contacts/contact_service.dart'; import 'package:dialer/widgets/loading_indicator.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'package:dialer/features/contacts/widgets/alphabet_scroll_page.dart'; import 'package:dialer/features/favorites/favorites_page.dart'; class FavoriteContactListState extends State { List _contacts = []; - List _favoriteContactIds = []; bool _loading = true; final ContactService _contactService = ContactService(); @@ -17,7 +15,6 @@ class FavoriteContactListState extends State { @override void initState() { super.initState(); - _loadFavorites(); _fetchContacts(); } @@ -25,34 +22,13 @@ class FavoriteContactListState extends State { List contacts = await _contactService.fetchContacts(); contacts = contacts.where((contact) => contact.phones.isNotEmpty).toList(); contacts.sort((a, b) => a.displayName.compareTo(b.displayName)); - contacts = contacts.where((contact) => _favoriteContactIds.contains(contact.id)).toList(); + contacts = contacts.where((contact) => contact.isStarred).toList(); setState(() { _contacts = contacts; _loading = false; }); } - Future _loadFavorites() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - setState(() { - _favoriteContactIds = prefs.getStringList('favoriteContacts') ?? []; - }); - } - - Future _toggleFavorite(Contact contact) async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - setState(() { - if (_favoriteContactIds.contains(contact.id)) { - print("Removing ${contact.displayName} from favorites"); - _favoriteContactIds.remove(contact.id); - } else { - print("Adding ${contact.displayName} to favorites"); - _favoriteContactIds.add(contact.id); - } - prefs.setStringList('favoriteContacts', _favoriteContactIds); - }); - } - @override Widget build(BuildContext context) { return Scaffold(