From cb060ba77b413fce4e9ad42c65605fd517f5966f Mon Sep 17 00:00:00 2001 From: AlexisDanlos <91090088+AlexisDanlos@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:14:02 +0100 Subject: [PATCH] Favorite page --- dialer/android/gradle.properties | 2 +- .../features/favorites/favorites_page.dart | 17 ++--- .../features/favorites/favorites_state.dart | 70 +++++++++++++++++++ dialer/pubspec.yaml | 1 + 4 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 dialer/lib/features/favorites/favorites_state.dart diff --git a/dialer/android/gradle.properties b/dialer/android/gradle.properties index 157afb8..5f3aaa0 100644 --- a/dialer/android/gradle.properties +++ b/dialer/android/gradle.properties @@ -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 \ No newline at end of file +# org.gradle.java.home=/usr/lib/jvm/java-17-openjdk-amd64 \ No newline at end of file diff --git a/dialer/lib/features/favorites/favorites_page.dart b/dialer/lib/features/favorites/favorites_page.dart index 60f8e07..5318733 100644 --- a/dialer/lib/features/favorites/favorites_page.dart +++ b/dialer/lib/features/favorites/favorites_page.dart @@ -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 { - @override - Widget build(BuildContext context) { - return Scaffold( - backgroundColor: Colors.black, - appBar: AppBar( - title: const Text('Favorites'), - ), - body: Text("Hello") - ); - } -} diff --git a/dialer/lib/features/favorites/favorites_state.dart b/dialer/lib/features/favorites/favorites_state.dart new file mode 100644 index 0000000..897b3f1 --- /dev/null +++ b/dialer/lib/features/favorites/favorites_state.dart @@ -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 { + List _contacts = []; + List _favoriteContactIds = []; + bool _loading = true; + final ContactService _contactService = ContactService(); + + bool get loading => _loading; + + @override + void initState() { + super.initState(); + _loadFavorites(); + _fetchContacts(); + } + + Future _fetchContacts() async { + 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(); + 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( + appBar: AppBar( + title: Text('Contacts Favoris'), + ), + body: _loading + ? const LoadingIndicatorWidget() + : AlphabetScrollPage( + contacts: _contacts, + scrollOffset: 0.0, + ), + ); + } +} \ No newline at end of file diff --git a/dialer/pubspec.yaml b/dialer/pubspec.yaml index 94c0f54..5412f16 100644 --- a/dialer/pubspec.yaml +++ b/dialer/pubspec.yaml @@ -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: