Add toggle favorite functionality and simplify favorite contact filtering
This commit is contained in:
parent
3a4a3816a8
commit
a8e64ff169
@ -12,4 +12,9 @@ class ContactService {
|
||||
Future<void> addNewContact(Contact contact) async {
|
||||
await FlutterContacts.insertContact(contact);
|
||||
}
|
||||
|
||||
Future<void> toogleFavorite(Contact contact) async {
|
||||
contact.isStarred = !contact.isStarred;
|
||||
await FlutterContacts.updateContact(contact);
|
||||
}
|
||||
}
|
@ -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<FavoritePage> {
|
||||
List<Contact> _contacts = [];
|
||||
List<String> _favoriteContactIds = [];
|
||||
bool _loading = true;
|
||||
final ContactService _contactService = ContactService();
|
||||
|
||||
@ -17,7 +15,6 @@ class FavoriteContactListState extends State<FavoritePage> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadFavorites();
|
||||
_fetchContacts();
|
||||
}
|
||||
|
||||
@ -25,34 +22,13 @@ class FavoriteContactListState extends State<FavoritePage> {
|
||||
List<Contact> 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<void> _loadFavorites() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
_favoriteContactIds = prefs.getStringList('favoriteContacts') ?? [];
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _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(
|
||||
|
Loading…
Reference in New Issue
Block a user