From c6a88e237b920547239fe595fb9e95ad5418c5b1 Mon Sep 17 00:00:00 2001 From: Florian Griffon Date: Tue, 8 Apr 2025 21:39:39 +0300 Subject: [PATCH] feat: fetch contact in makeGsmCall to show it in call --- dialer/lib/services/call_service.dart | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/dialer/lib/services/call_service.dart b/dialer/lib/services/call_service.dart index 43d34e4..9233a85 100644 --- a/dialer/lib/services/call_service.dart +++ b/dialer/lib/services/call_service.dart @@ -3,18 +3,29 @@ import 'package:flutter/services.dart'; import '../features/call/call_page.dart'; import '../features/call/incoming_call_page.dart'; <<<<<<< HEAD +<<<<<<< HEAD import '../services/contact_service.dart'; ======= >>>>>>> ec1779b (callNotifications and various fix related to calls (#49)) +======= +import '../services/contact_service.dart'; // Import your ContactService +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) class CallService { static const MethodChannel _channel = MethodChannel('call_service'); static String? currentPhoneNumber; +<<<<<<< HEAD static String? currentDisplayName; static Uint8List? currentThumbnail; static bool _isCallPageVisible = false; <<<<<<< HEAD static String? _currentCallState; +======= + static String? currentDisplayName; // Store display name + static Uint8List? currentThumbnail; // Store thumbnail + static bool _isCallPageVisible = false; + final ContactService _contactService = ContactService(); // Instantiate ContactService +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) static Map? _pendingCall; static bool wasPhoneLocked = false; final ContactService _contactService = ContactService(); @@ -37,6 +48,10 @@ class CallService { final phoneNumber = call.arguments["callId"] as String; final state = call.arguments["state"] as String; currentPhoneNumber = phoneNumber.replaceFirst('tel:', ''); +<<<<<<< HEAD +======= + // Fetch contact info using ContactService if not already set +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) await _fetchContactInfo(currentPhoneNumber!); print('CallService: Call added, number: $currentPhoneNumber, state: $state'); <<<<<<< HEAD @@ -80,13 +95,19 @@ class CallService { currentPhoneNumber = null; currentDisplayName = null; currentThumbnail = null; +<<<<<<< HEAD _currentCallState = null; +======= +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) break; case "incomingCallFromNotification": final phoneNumber = call.arguments["phoneNumber"] as String; wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false; currentPhoneNumber = phoneNumber; +<<<<<<< HEAD await _fetchContactInfo(currentPhoneNumber!); +======= +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) print('CallService: Incoming call from notification: $phoneNumber, wasPhoneLocked: $wasPhoneLocked'); _handleIncomingCall(phoneNumber); break; @@ -101,6 +122,7 @@ class CallService { }); } +<<<<<<< HEAD <<<<<<< HEAD Future _fetchContactInfo(String phoneNumber) async { try { @@ -109,14 +131,28 @@ class CallService { for (var contact in contacts) { for (var phone in contact.phones) { if (_normalizePhoneNumber(phone.number) == normalizedPhoneNumber) { +======= + Future _fetchContactInfo(String phoneNumber) async { + if (currentDisplayName != null && currentThumbnail != null) return; // Already set + try { + final contacts = await _contactService.fetchContacts(); // Use ContactService + for (var contact in contacts) { + for (var phone in contact.phones) { + if (_normalizePhoneNumber(phone.number) == _normalizePhoneNumber(phoneNumber)) { +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) currentDisplayName = contact.displayName; currentThumbnail = contact.thumbnail; return; } } } +<<<<<<< HEAD currentDisplayName = phoneNumber; currentThumbnail = null; +======= + // If no match found, use phone number as fallback + currentDisplayName ??= phoneNumber; + currentThumbnail ??= null; } catch (e) { print('CallService: Error fetching contact info: $e'); currentDisplayName = phoneNumber; @@ -128,8 +164,39 @@ class CallService { return number.replaceAll(RegExp(r'[\s\-\(\)]'), ''); } + Future _fetchContactInfo(String phoneNumber) async { + if (currentDisplayName != null && currentThumbnail != null) return; // Already set + try { + final contacts = await _contactService.fetchContacts(); // Use ContactService + for (var contact in contacts) { + for (var phone in contact.phones) { + if (_normalizePhoneNumber(phone.number) == _normalizePhoneNumber(phoneNumber)) { + currentDisplayName = contact.displayName; + currentThumbnail = contact.thumbnail; + return; + } + } + } + // If no match found, use phone number as fallback + currentDisplayName ??= phoneNumber; + currentThumbnail ??= null; +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) + } catch (e) { + print('CallService: Error fetching contact info: $e'); + currentDisplayName = phoneNumber; + currentThumbnail = null; + } + } + + String _normalizePhoneNumber(String number) { + return number.replaceAll(RegExp(r'[\s\-\(\)]'), ''); + } + +<<<<<<< HEAD ======= >>>>>>> ec1779b (callNotifications and various fix related to calls (#49)) +======= +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) void _handleIncomingCall(String phoneNumber) { final context = navigatorKey.currentContext; if (context == null) { @@ -294,11 +361,16 @@ class CallService { }) async { try { currentPhoneNumber = phoneNumber; +<<<<<<< HEAD currentDisplayName = displayName ?? phoneNumber; currentThumbnail = thumbnail; if (displayName == null || thumbnail == null) { await _fetchContactInfo(phoneNumber); } +======= + currentDisplayName = displayName ?? phoneNumber; // Use provided or fetch later + currentThumbnail = thumbnail; // Use provided or fetch later +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) print('CallService: Making GSM call to $phoneNumber'); final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber}); print('CallService: makeGsmCall result: $result'); @@ -309,11 +381,17 @@ class CallService { ); return; } +<<<<<<< HEAD <<<<<<< HEAD _handleCallState("dialing"); ======= return resultMap; >>>>>>> ec1779b (callNotifications and various fix related to calls (#49)) +======= + // Fetch contact info if not provided + await _fetchContactInfo(phoneNumber); + _navigateToCallPage(context); // Navigate immediately after call initiation +>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call) } catch (e) { print("CallService: Error making call: $e"); ScaffoldMessenger.of(context).showSnackBar(