feat: favorite page
All checks were successful
/ mirror (push) Successful in 4s

This commit is contained in:
Florian Griffon 2024-12-15 03:25:44 +01:00
parent e6807b865b
commit c46a43aa4e
5 changed files with 65 additions and 35 deletions

View File

@ -17,8 +17,10 @@ class _ContactPageState extends State<ContactPage> {
return Scaffold(
body: contactState.loading
? const LoadingIndicatorWidget()
// : ContactListWidget(contacts: contactState.contacts),
: AlphabetScrollPage(scrollOffset: contactState.scrollOffset),
: AlphabetScrollPage(
scrollOffset: contactState.scrollOffset,
contacts: contactState.contacts, // Use all contacts here
),
);
}
}

View File

@ -19,12 +19,15 @@ class ContactState extends StatefulWidget {
class _ContactStateState extends State<ContactState> {
final ContactService _contactService = ContactService();
List<Contact> _contacts = [];
List<Contact> _allContacts = [];
List<Contact> _favoriteContacts = [];
bool _loading = true;
double _scrollOffset = 0.0;
Contact? _selfContact = Contact();
List<Contact> get contacts => _contacts;
// Getters for all contacts and favorites
List<Contact> get contacts => _allContacts;
List<Contact> get favoriteContacts => _favoriteContacts;
bool get loading => _loading;
double get scrollOffset => _scrollOffset;
Contact? get selfContact => _selfContact;
@ -32,9 +35,7 @@ class _ContactStateState extends State<ContactState> {
@override
void initState() {
super.initState();
fetchContacts();
// Add listener for contact changes
fetchContacts(); // Fetch all contacts by default
FlutterContacts.addListener(_onContactChange);
}
@ -42,20 +43,33 @@ class _ContactStateState extends State<ContactState> {
@override
void dispose() {
// Remove listener
FlutterContacts.removeListener(_onContactChange);
super.dispose();
}
Future<void> fetchContacts({bool onlyStarred = false}) async {
List<Contact> contacts = onlyStarred
? await _contactService.fetchFavoriteContacts()
: await _contactService.fetchContacts();
// Fetch all contacts
Future<void> fetchContacts() async {
setState(() => _loading = true);
try {
List<Contact> contacts = await _contactService.fetchContacts();
_processContacts(contacts);
} finally {
setState(() => _loading = false);
}
}
debugPrint(
"Fetched ${contacts.length} ${onlyStarred ? 'favorite' : ''} contacts");
// Fetch only favorite contacts
Future<void> fetchFavoriteContacts() async {
setState(() => _loading = true);
try {
List<Contact> contacts = await _contactService.fetchFavoriteContacts();
setState(() => _favoriteContacts = contacts);
} finally {
setState(() => _loading = false);
}
}
// Find selfContact before filtering
void _processContacts(List<Contact> contacts) {
_selfContact = contacts.firstWhere(
(contact) => contact.displayName.toLowerCase() == "user",
orElse: () => Contact(),
@ -70,8 +84,9 @@ class _ContactStateState extends State<ContactState> {
contacts.sort((a, b) => a.displayName.compareTo(b.displayName));
setState(() {
_contacts = contacts;
_loading = false;
_allContacts = contacts;
_favoriteContacts =
contacts.where((contact) => contact.isStarred).toList();
_selfContact = _selfContact;
});
}
@ -96,6 +111,7 @@ class _ContactStateState extends State<ContactState> {
}
}
class _InheritedContactState extends InheritedWidget {
final _ContactStateState data;

View File

@ -11,8 +11,13 @@ import 'share_own_qr.dart';
class AlphabetScrollPage extends StatefulWidget {
final double scrollOffset;
final List<Contact> contacts;
const AlphabetScrollPage({super.key, required this.scrollOffset});
const AlphabetScrollPage({
super.key,
required this.scrollOffset,
required this.contacts,
});
@override
_AlphabetScrollPageState createState() => _AlphabetScrollPageState();
@ -24,8 +29,7 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
@override
void initState() {
super.initState();
_scrollController =
ScrollController(initialScrollOffset: widget.scrollOffset);
_scrollController = ScrollController(initialScrollOffset: widget.scrollOffset);
_scrollController.addListener(_onScroll);
}
@ -73,9 +77,8 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
@override
Widget build(BuildContext context) {
final contactState = ContactState.of(context);
final contacts = contactState.contacts;
final selfContact = contactState.selfContact;
final contacts = widget.contacts;
final selfContact = ContactState.of(context).selfContact;
final contactState = ContactState.of(context);
final contacts = contactState.contacts;

View File

@ -1,23 +1,32 @@
import 'package:dialer/features/contacts/contact_state.dart';
import 'package:dialer/features/contacts/widgets/alphabet_scroll_page.dart';
import 'package:flutter/material.dart';
import 'package:dialer/widgets/loading_indicator.dart';
class FavoritePage extends StatefulWidget {
const FavoritePage({super.key});
class FavoritesPage extends StatefulWidget {
const FavoritesPage({super.key});
@override
_FavoritePageState createState() => _FavoritePageState();
_FavoritesPageState createState() => _FavoritesPageState();
}
class _FavoritePageState extends State<FavoritePage> {
class _FavoritesPageState extends State<FavoritesPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final contactState = ContactState.of(context);
return Scaffold(
backgroundColor: Colors.black,
body: Center( // Center the text within the body
child: Text(
"Hello",
style: TextStyle(color: Colors.white), // Change text color for visibility
),
),
body: contactState.loading
? const LoadingIndicatorWidget()
: AlphabetScrollPage(
scrollOffset: contactState.scrollOffset,
contacts:
contactState.favoriteContacts, // Use only favorites here
),
);
}
}

View File

@ -140,7 +140,7 @@ class _MyHomePageState extends State<MyHomePage>
TabBarView(
controller: _tabController,
children: const [
FavoritePage(),
FavoritesPage(),
HistoryPage(),
ContactPage(),
SettingsPage(), // Add your SettingsPage here