diff --git a/dialer/lib/features/contacts/widgets/contact_modal.dart b/dialer/lib/features/contacts/widgets/contact_modal.dart index 198f614..a73f7e6 100644 --- a/dialer/lib/features/contacts/widgets/contact_modal.dart +++ b/dialer/lib/features/contacts/widgets/contact_modal.dart @@ -267,8 +267,12 @@ class _ContactModalState extends State { ), onTap: () async { if (widget.contact.phones.isNotEmpty) { - await _callService.makeGsmCall(context, - phoneNumber: phoneNumber); + await _callService.makeGsmCall( + context, + phoneNumber: phoneNumber, + displayName: widget.contact.displayName, + thumbnail: widget.contact.thumbnail, + ); } }, ), diff --git a/dialer/lib/features/history/history_page.dart b/dialer/lib/features/history/history_page.dart index 3c5b1f1..9906908 100644 --- a/dialer/lib/features/history/history_page.dart +++ b/dialer/lib/features/history/history_page.dart @@ -425,7 +425,12 @@ class _HistoryPageState extends State icon: const Icon(Icons.phone, color: Colors.green), onPressed: () async { if (contact.phones.isNotEmpty) { - _callService.makeGsmCall(context, phoneNumber: contact.phones.first.number); + await _callService.makeGsmCall( + context, + phoneNumber: contact.phones.first.number, + displayName: contact.displayName, + thumbnail: contact.thumbnail, + ); } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( diff --git a/dialer/lib/services/call_service.dart b/dialer/lib/services/call_service.dart index c7e41a6..c7e8ad2 100644 --- a/dialer/lib/services/call_service.dart +++ b/dialer/lib/services/call_service.dart @@ -1,16 +1,17 @@ +import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../features/call/call_page.dart'; import '../features/call/incoming_call_page.dart'; -import '../services/contact_service.dart'; // Import your ContactService +import '../services/contact_service.dart'; class CallService { static const MethodChannel _channel = MethodChannel('call_service'); static String? currentPhoneNumber; - static String? currentDisplayName; // Store display name - static Uint8List? currentThumbnail; // Store thumbnail + static String? currentDisplayName; + static Uint8List? currentThumbnail; static bool _isCallPageVisible = false; - final ContactService _contactService = ContactService(); // Instantiate ContactService + final ContactService _contactService = ContactService(); static final GlobalKey navigatorKey = GlobalKey(); @@ -28,7 +29,6 @@ class CallService { final phoneNumber = call.arguments["callId"] as String; final state = call.arguments["state"] as String; currentPhoneNumber = phoneNumber.replaceFirst('tel:', ''); - // Fetch contact info using ContactService if not already set await _fetchContactInfo(currentPhoneNumber!); print('CallService: Call added, number: $currentPhoneNumber, state: $state'); if (state == "ringing") { @@ -61,12 +61,12 @@ class CallService { } Future _fetchContactInfo(String phoneNumber) async { - if (currentDisplayName != null && currentThumbnail != null) return; // Already set try { - final contacts = await _contactService.fetchContacts(); // Use ContactService + final contacts = await _contactService.fetchContacts(); + final normalizedPhoneNumber = _normalizePhoneNumber(phoneNumber); for (var contact in contacts) { for (var phone in contact.phones) { - if (_normalizePhoneNumber(phone.number) == _normalizePhoneNumber(phoneNumber)) { + if (_normalizePhoneNumber(phone.number) == normalizedPhoneNumber) { currentDisplayName = contact.displayName; currentThumbnail = contact.thumbnail; return; @@ -74,8 +74,8 @@ class CallService { } } // If no match found, use phone number as fallback - currentDisplayName ??= phoneNumber; - currentThumbnail ??= null; + currentDisplayName = phoneNumber; + currentThumbnail = null; } catch (e) { print('CallService: Error fetching contact info: $e'); currentDisplayName = phoneNumber; @@ -151,8 +151,12 @@ class CallService { }) async { try { currentPhoneNumber = phoneNumber; - currentDisplayName = displayName ?? phoneNumber; // Use provided or fetch later - currentThumbnail = thumbnail; // Use provided or fetch later + // Use provided displayName and thumbnail if available, otherwise fetch + currentDisplayName = displayName ?? phoneNumber; + currentThumbnail = thumbnail; + if (displayName == null || thumbnail == null) { + await _fetchContactInfo(phoneNumber); + } print('CallService: Making GSM call to $phoneNumber'); final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber}); print('CallService: makeGsmCall result: $result'); @@ -162,9 +166,7 @@ class CallService { ); return; } - // Fetch contact info if not provided - await _fetchContactInfo(phoneNumber); - _navigateToCallPage(context); // Navigate immediately after call initiation + _navigateToCallPage(context); } catch (e) { print("CallService: Error making call: $e"); ScaffoldMessenger.of(context).showSnackBar(