Favorite page

This commit is contained in:
AlexisDanlos 2024-10-28 19:14:02 +01:00
parent 74d9eda464
commit cb060ba77b
4 changed files with 76 additions and 14 deletions

View File

@ -1,4 +1,4 @@
org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError
android.useAndroidX=true
android.enableJetifier=true
org.gradle.java.home=/usr/lib/jvm/java-17-openjdk-amd64
# org.gradle.java.home=/usr/lib/jvm/java-17-openjdk-amd64

View File

@ -1,21 +1,12 @@
import 'package:flutter/material.dart';
// import 'dart:developer';
import 'package:dialer/features/favorites/favorites_state.dart';
class FavoritePage extends StatefulWidget {
const FavoritePage({super.key});
@override
_FavoritePageState createState() => _FavoritePageState();
FavoriteContactListState createState() => FavoriteContactListState();
}
class _FavoritePageState extends State<FavoritePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: const Text('Favorites'),
),
body: Text("Hello")
);
}
}

View File

@ -0,0 +1,70 @@
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();
bool get loading => _loading;
@override
void initState() {
super.initState();
_loadFavorites();
_fetchContacts();
}
Future<void> _fetchContacts() async {
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();
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(
appBar: AppBar(
title: Text('Contacts Favoris'),
),
body: _loading
? const LoadingIndicatorWidget()
: AlphabetScrollPage(
contacts: _contacts,
scrollOffset: 0.0,
),
);
}
}

View File

@ -38,6 +38,7 @@ dependencies:
flutter_contacts: ^1.1.9+2
permission_handler: ^10.2.0 # For handling permissions
cached_network_image: ^3.2.3 # For caching contact images
shared_preferences: ^2.3.2 # For storing user preferences
dev_dependencies:
flutter_test: