favorite-page (#12)
All checks were successful
/ mirror (push) Successful in 4s

Feat: favorite page
Reviewed-on: #12
Co-authored-by: Florian Griffon <florian.griffon@epitech.eu>
Co-committed-by: Florian Griffon <florian.griffon@epitech.eu>
This commit is contained in:
Florian Griffon 2024-12-15 18:42:23 +00:00 committed by stcb
parent 21b6b0a29a
commit 09fa0a0216
7 changed files with 86 additions and 74 deletions

View File

@ -22,9 +22,11 @@ class _CompositionPageState extends State<CompositionPage> {
} }
Future<void> _fetchContacts() async { Future<void> _fetchContacts() async {
_allContacts = await _contactService.fetchContacts(); if (await FlutterContacts.requestPermission()) {
_filteredContacts = _allContacts; _allContacts = await _contactService.fetchContacts();
setState(() {}); _filteredContacts = _allContacts;
setState(() {});
}
} }
void _filterContacts() { void _filterContacts() {
@ -79,9 +81,9 @@ class _CompositionPageState extends State<CompositionPage> {
// Top half: Display contacts matching dialed number // Top half: Display contacts matching dialed number
Expanded( Expanded(
flex: 2, flex: 2,
child: Container( child:
padding: const EdgeInsets.only( Container(
top: 42.0, left: 16.0, right: 16.0, bottom: 16.0), padding: const EdgeInsets.only(top: 42.0, left: 16.0, right: 16.0, bottom: 16.0),
color: Colors.black, color: Colors.black,
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
@ -93,14 +95,12 @@ class _CompositionPageState extends State<CompositionPage> {
return ListTile( return ListTile(
title: Text( title: Text(
contact.displayName, contact.displayName,
style: style: const TextStyle(color: Colors.white),
const TextStyle(color: Colors.white),
), ),
subtitle: contact.phones.isNotEmpty subtitle: contact.phones.isNotEmpty
? Text( ? Text(
contact.phones.first.number, contact.phones.first.number,
style: const TextStyle( style: const TextStyle(color: Colors.grey),
color: Colors.grey),
) )
: null, : null,
trailing: Row( trailing: Row(
@ -108,22 +108,16 @@ class _CompositionPageState extends State<CompositionPage> {
children: [ children: [
// Call button // Call button
IconButton( IconButton(
icon: Icon(Icons.phone, icon: Icon(Icons.phone, color: Colors.green[300], size: 20),
color: Colors.green[300],
size: 20),
onPressed: () { onPressed: () {
print( print('Calling ${contact.displayName}');
'Calling ${contact.displayName}');
}, },
), ),
// Text button // Text button
IconButton( IconButton(
icon: Icon(Icons.message, icon: Icon(Icons.message, color: Colors.blue[300], size: 20),
color: Colors.blue[300],
size: 20),
onPressed: () { onPressed: () {
print( print('Texting ${contact.displayName}');
'Texting ${contact.displayName}');
}, },
), ),
], ],
@ -133,12 +127,7 @@ class _CompositionPageState extends State<CompositionPage> {
}, },
); );
}).toList() }).toList()
: [ : [Center(child: Text('No contacts found', style: TextStyle(color: Colors.white)))],
Center(
child: Text('No contacts found',
style:
TextStyle(color: Colors.white)))
],
), ),
), ),
], ],
@ -163,16 +152,14 @@ class _CompositionPageState extends State<CompositionPage> {
alignment: Alignment.center, alignment: Alignment.center,
child: Text( child: Text(
dialedNumber, dialedNumber,
style: const TextStyle( style: const TextStyle(fontSize: 24, color: Colors.white),
fontSize: 24, color: Colors.white),
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
), ),
), ),
IconButton( IconButton(
onPressed: _onClearPress, onPressed: _onClearPress,
icon: const Icon(Icons.backspace, icon: const Icon(Icons.backspace, color: Colors.white),
color: Colors.white),
), ),
], ],
), ),
@ -185,8 +172,7 @@ class _CompositionPageState extends State<CompositionPage> {
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
Row( Row(
mainAxisAlignment: mainAxisAlignment: MainAxisAlignment.spaceEvenly,
MainAxisAlignment.spaceEvenly,
children: [ children: [
_buildDialButton('1'), _buildDialButton('1'),
_buildDialButton('2'), _buildDialButton('2'),
@ -194,8 +180,7 @@ class _CompositionPageState extends State<CompositionPage> {
], ],
), ),
Row( Row(
mainAxisAlignment: mainAxisAlignment: MainAxisAlignment.spaceEvenly,
MainAxisAlignment.spaceEvenly,
children: [ children: [
_buildDialButton('4'), _buildDialButton('4'),
_buildDialButton('5'), _buildDialButton('5'),
@ -203,8 +188,7 @@ class _CompositionPageState extends State<CompositionPage> {
], ],
), ),
Row( Row(
mainAxisAlignment: mainAxisAlignment: MainAxisAlignment.spaceEvenly,
MainAxisAlignment.spaceEvenly,
children: [ children: [
_buildDialButton('7'), _buildDialButton('7'),
_buildDialButton('8'), _buildDialButton('8'),
@ -212,8 +196,7 @@ class _CompositionPageState extends State<CompositionPage> {
], ],
), ),
Row( Row(
mainAxisAlignment: mainAxisAlignment: MainAxisAlignment.spaceEvenly,
MainAxisAlignment.spaceEvenly,
children: [ children: [
_buildDialButton('*'), _buildDialButton('*'),
_buildDialButton('0'), _buildDialButton('0'),

View File

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

View File

@ -9,8 +9,13 @@ import 'share_own_qr.dart';
class AlphabetScrollPage extends StatefulWidget { class AlphabetScrollPage extends StatefulWidget {
final double scrollOffset; 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 @override
_AlphabetScrollPageState createState() => _AlphabetScrollPageState(); _AlphabetScrollPageState createState() => _AlphabetScrollPageState();
@ -22,8 +27,7 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_scrollController = _scrollController = ScrollController(initialScrollOffset: widget.scrollOffset);
ScrollController(initialScrollOffset: widget.scrollOffset);
_scrollController.addListener(_onScroll); _scrollController.addListener(_onScroll);
} }
@ -71,9 +75,8 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final contactState = ContactState.of(context); final contacts = widget.contacts;
final contacts = contactState.contacts; final selfContact = ContactState.of(context).selfContact;
final selfContact = contactState.selfContact;
Map<String, List<Contact>> alphabetizedContacts = {}; Map<String, List<Contact>> alphabetizedContacts = {};
for (var contact in contacts) { for (var contact in contacts) {

View File

@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart'; import 'package:flutter_contacts/flutter_contacts.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:dialer/widgets/username_color_generator.dart'; import 'package:dialer/widgets/username_color_generator.dart';
import 'package:flutter/material.dart';
class ContactModal extends StatelessWidget { class ContactModal extends StatelessWidget {
final Contact contact; final Contact contact;

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

View File

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