diff --git a/dialer/lib/features/history/history_page.dart b/dialer/lib/features/history/history_page.dart index 642b942..6054173 100644 --- a/dialer/lib/features/history/history_page.dart +++ b/dialer/lib/features/history/history_page.dart @@ -7,6 +7,7 @@ import 'package:url_launcher/url_launcher.dart'; import 'package:dialer/features/contacts/contact_state.dart'; import 'package:dialer/widgets/username_color_generator.dart'; import '../../services/block_service.dart'; +import '../contacts/widgets/contact_modal.dart'; class History { final Contact contact; @@ -47,6 +48,45 @@ class _HistoryPageState extends State } } + Future _refreshContacts() async { + final contactState = ContactState.of(context); + try { + // Refresh contacts or fetch them again + await contactState.fetchContacts(); + } catch (e) { + print('Error refreshing contacts: $e'); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Failed to refresh contacts')), + ); + } + } + + void _toggleFavorite(Contact contact) async { + try { + // Ensure you have the necessary permissions to fetch contact details + if (await FlutterContacts.requestPermission()) { + 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); + } + await _refreshContacts(); // Refresh the contact list + } 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 contact favorite status')), + ); + } + } + Future _buildHistories() async { final contactState = ContactState.of(context); if (contactState.loading) { @@ -213,11 +253,53 @@ class _HistoryPageState extends State return Column( children: [ ListTile( - leading: ObfuscatedAvatar( - imageBytes: contact.thumbnail, - radius: 25, - backgroundColor: avatarColor, - fallbackInitial: contact.displayName, + leading: GestureDetector( + onTap: () { + // When the profile picture is tapped, show the ContactModal + showModalBottomSheet( + context: context, + isScrollControlled: true, + backgroundColor: Colors.transparent, + builder: (context) { + return ContactModal( + contact: contact, + onEdit: () async { + if (await FlutterContacts.requestPermission()) { + final updatedContact = + await FlutterContacts.openExternalEdit( + contact.id); + if (updatedContact != null) { + await _refreshContacts(); + Navigator.of(context).pop(); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + '${contact.displayName} updated successfully!'), + ), + ); + } else { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('Edit canceled or failed.'), + ), + ); + } + } + }, + onToggleFavorite: () { + _toggleFavorite(contact); + }, + isFavorite: contact.isStarred, + ); + }, + ); + }, + child: ObfuscatedAvatar( + imageBytes: contact.thumbnail, + radius: 25, + backgroundColor: avatarColor, + fallbackInitial: contact.displayName, + ), ), title: Text( _obfuscateService.obfuscateData(contact.displayName), @@ -327,9 +409,8 @@ class _HistoryPageState extends State if (phoneNumber == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( - content: - Text('Contact has no phone number'), - ), + content: + Text('Contact has no phone number')), ); return; } @@ -352,10 +433,8 @@ class _HistoryPageState extends State icon: Icon( isBlocked ? Icons.lock_open : Icons.block, color: Colors.white), - label: Text( - isBlocked ? 'Unblock' : 'Block', - style: const TextStyle(color: Colors.white), - ), + label: Text(isBlocked ? 'Unblock' : 'Block', + style: const TextStyle(color: Colors.white)), ), ], );