feat: fetch contact in makeGsmCall to show it in call

This commit is contained in:
Florian Griffon 2025-04-08 21:39:39 +03:00
parent e6b19c63e1
commit ba2c1b049f

View File

@ -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<String, dynamic>? _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<void> _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<void> _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<void> _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(