From 7ff9418e066a237b9697666a30c2dd5db15e0642 Mon Sep 17 00:00:00 2001 From: AlexisDanlos <91090088+AlexisDanlos@users.noreply.github.com> Date: Thu, 6 Mar 2025 15:44:03 +0100 Subject: [PATCH] bug fix: enhance contact fetching with permission handling --- .../lib/features/contacts/contact_state.dart | 24 +++++++++++++-- dialer/lib/features/history/history_page.dart | 29 +++++++++---------- dialer/lib/services/contact_service.dart | 12 ++++++-- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/dialer/lib/features/contacts/contact_state.dart b/dialer/lib/features/contacts/contact_state.dart index 58e7574..fb544c4 100644 --- a/dialer/lib/features/contacts/contact_state.dart +++ b/dialer/lib/features/contacts/contact_state.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; +import 'package:permission_handler/permission_handler.dart'; import '../../services/contact_service.dart'; class ContactState extends StatefulWidget { @@ -24,6 +25,7 @@ class _ContactStateState extends State { bool _loading = true; double _scrollOffset = 0.0; Contact? _selfContact = Contact(); + bool _permissionRequestInProgress = false; // Getters for all contacts and favorites List get contacts => _allContacts; @@ -35,8 +37,26 @@ class _ContactStateState extends State { @override void initState() { super.initState(); - fetchContacts(); // Fetch all contacts by default - FlutterContacts.addListener(_onContactChange); + _initializeContacts(); + } + + Future _initializeContacts() async { + try { + final status = await Permission.contacts.status; + if (status.isGranted) { + await fetchContacts(); + } else { + final result = await Permission.contacts.request(); + if (result.isGranted) { + await fetchContacts(); + } else { + setState(() => _loading = false); + } + } + } catch (e) { + debugPrint('Error initializing contacts: $e'); + setState(() => _loading = false); + } } void _onContactChange() => fetchContacts(); diff --git a/dialer/lib/features/history/history_page.dart b/dialer/lib/features/history/history_page.dart index 117d1e8..9f500d1 100644 --- a/dialer/lib/features/history/history_page.dart +++ b/dialer/lib/features/history/history_page.dart @@ -68,25 +68,24 @@ class _HistoryPageState extends State void _toggleFavorite(Contact contact) async { try { - if (await FlutterContacts.requestPermission()) { - Contact? fullContact = await FlutterContacts.getContact(contact.id, - withProperties: true, - withAccounts: true, - withPhoto: true, - withThumbnail: true); + Contact? fullContact = await FlutterContacts.getContact(contact.id, + withProperties: true, + withAccounts: true, + withPhoto: true, + withThumbnail: true); - if (fullContact != null) { - fullContact.isStarred = !fullContact.isStarred; - await FlutterContacts.updateContact(fullContact); - } + if (fullContact != null) { + fullContact.isStarred = !fullContact.isStarred; + await FlutterContacts.updateContact(fullContact); await _refreshContacts(); - } else { - print("Could not fetch contact details"); } } catch (e) { - print("Error updating favorite status: $e"); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Failed to update favorite status'))); + debugPrint("Error updating favorite status: $e"); + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Failed to update contact favorite status')), + ); + } } } diff --git a/dialer/lib/services/contact_service.dart b/dialer/lib/services/contact_service.dart index ef00185..8fa12a4 100644 --- a/dialer/lib/services/contact_service.dart +++ b/dialer/lib/services/contact_service.dart @@ -1,19 +1,27 @@ import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; import 'package:qr_flutter/qr_flutter.dart'; +import 'package:permission_handler/permission_handler.dart'; // Service to manage contact-related operations class ContactService { Future> fetchContacts() async { - if (await FlutterContacts.requestPermission()) { + final hasPermission = await Permission.contacts.status; + if (!hasPermission.isGranted) { + return []; + } + + try { return await FlutterContacts.getContacts( withProperties: true, withThumbnail: true, withAccounts: true, withGroups: true, withPhoto: true); + } catch (e) { + debugPrint('Error fetching contacts: $e'); + return []; } - return []; } Future> fetchFavoriteContacts() async {