feat: fetch Favorite, fix fetch redundancy
All checks were successful
/ mirror (push) Successful in 4s

This commit is contained in:
Florian Griffon 2024-12-13 10:00:20 +01:00
parent 62cf3b6207
commit e6807b865b

View File

@ -2,9 +2,8 @@ import 'package:dialer/widgets/username_color_generator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
import '../contact_state.dart';
import '../../../widgets/color_darkener.dart';
import '../contact_state.dart';
import '../../../widgets/contact_service.dart';
import '../../../widgets/color_darkener.dart';
import 'add_contact_button.dart';
import 'contact_modal.dart';
import 'contact_modal.dart';
@ -13,8 +12,6 @@ import 'share_own_qr.dart';
class AlphabetScrollPage extends StatefulWidget {
final double scrollOffset;
const AlphabetScrollPage(
{super.key, required this.contacts, required this.scrollOffset});
const AlphabetScrollPage({super.key, required this.scrollOffset});
@override
@ -23,15 +20,10 @@ class AlphabetScrollPage extends StatefulWidget {
class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
late ScrollController _scrollController;
List<Contact> _contacts = []; // Local copy of contacts for updating
final ContactService _contactService = ContactService();
@override
void initState() {
super.initState();
_scrollController =
ScrollController(initialScrollOffset: widget.scrollOffset);
_contacts = widget.contacts; // Initialize with the provided contacts
_scrollController =
ScrollController(initialScrollOffset: widget.scrollOffset);
_scrollController.addListener(_onScroll);
@ -43,28 +35,20 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
}
Future<void> _refreshContacts() async {
final contactState = ContactState.of(context);
try {
// Use the fetchContacts method from ContactService
final updatedContacts = await _contactService.fetchContacts();
if (mounted) {
setState(() {
_contacts = updatedContacts;
});
}
await contactState.fetchContacts();
} catch (e) {
print('Error refreshing contacts: $e');
// Optionally show a user-friendly error message
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Failed to refresh contacts')));
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to refresh contacts')),
);
}
}
void _toggleFavorite(Contact contact) async {
try {
// Request permission first
if (await FlutterContacts.requestPermission()) {
// Fetch the full contact details with all available properties
Contact? fullContact = await FlutterContacts.getContact(contact.id,
withProperties: true,
withAccounts: true,
@ -72,7 +56,6 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
withThumbnail: true);
if (fullContact != null) {
// Update the contact
fullContact.isStarred = !fullContact.isStarred;
await FlutterContacts.updateContact(fullContact);
}
@ -82,9 +65,9 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
}
} catch (e) {
print("Error updating favorite status: $e");
// Optional: Show a user-friendly error message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to update contact favorite status')));
SnackBar(content: Text('Failed to update contact favorite status')),
);
}
}
@ -94,8 +77,12 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
final contacts = contactState.contacts;
final selfContact = contactState.selfContact;
final contactState = ContactState.of(context);
final contacts = contactState.contacts;
final selfContact = contactState.selfContact;
Map<String, List<Contact>> alphabetizedContacts = {};
for (var contact in _contacts) {
for (var contact in contacts) {
String firstLetter = contact.displayName.isNotEmpty
? contact.displayName[0].toUpperCase()
: '#';
@ -116,15 +103,12 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
// Top buttons row
Container(
color: Colors.black,
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
AddContactButton(),
QRCodeButton(
contacts: _contacts,
selfContact: ContactState.of(context).selfContact),
QRCodeButton(contacts: contacts, selfContact: selfContact),
],
),
),
@ -136,6 +120,7 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
itemBuilder: (context, index) {
String letter = alphabetKeys[index];
List<Contact> contactsForLetter = alphabetizedContacts[letter]!;
List<Contact> contactsForLetter = alphabetizedContacts[letter]!;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@ -155,7 +140,7 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
),
),
// Contact Entries
...contacts.map((contact) {
...contactsForLetter.map((contact) {
String phoneNumber = contact.phones.isNotEmpty
? contact.phones.first.number
: 'No phone number';
@ -209,9 +194,7 @@ class _AlphabetScrollPageState extends State<AlphabetScrollPage> {
return ContactModal(
contact: contact,
onEdit: () async {
// Trigger edit logic and refresh contacts
if (await FlutterContacts
.requestPermission()) {
if (await FlutterContacts.requestPermission()) {
final updatedContact =
await FlutterContacts.openExternalEdit(
contact.id);