feat: fetch contact in makeGsmCall to show it in call
This commit is contained in:
parent
22941f78d0
commit
20d8c9643f
@ -1,12 +1,16 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import '../features/call/call_page.dart';
|
import '../features/call/call_page.dart';
|
||||||
import '../features/call/incoming_call_page.dart'; // Import the new page
|
import '../features/call/incoming_call_page.dart';
|
||||||
|
import '../services/contact_service.dart'; // Import your ContactService
|
||||||
|
|
||||||
class CallService {
|
class CallService {
|
||||||
static const MethodChannel _channel = MethodChannel('call_service');
|
static const MethodChannel _channel = MethodChannel('call_service');
|
||||||
static String? currentPhoneNumber;
|
static String? currentPhoneNumber;
|
||||||
|
static String? currentDisplayName; // Store display name
|
||||||
|
static Uint8List? currentThumbnail; // Store thumbnail
|
||||||
static bool _isCallPageVisible = false;
|
static bool _isCallPageVisible = false;
|
||||||
|
final ContactService _contactService = ContactService(); // Instantiate ContactService
|
||||||
|
|
||||||
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
|
||||||
@ -24,6 +28,8 @@ class CallService {
|
|||||||
final phoneNumber = call.arguments["callId"] as String;
|
final phoneNumber = call.arguments["callId"] as String;
|
||||||
final state = call.arguments["state"] as String;
|
final state = call.arguments["state"] as String;
|
||||||
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
||||||
|
// Fetch contact info using ContactService if not already set
|
||||||
|
await _fetchContactInfo(currentPhoneNumber!);
|
||||||
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
||||||
if (state == "ringing") {
|
if (state == "ringing") {
|
||||||
_navigateToIncomingCallPage(context);
|
_navigateToIncomingCallPage(context);
|
||||||
@ -47,11 +53,40 @@ class CallService {
|
|||||||
print('CallService: Call ended/removed');
|
print('CallService: Call ended/removed');
|
||||||
_closeCallPage(context);
|
_closeCallPage(context);
|
||||||
currentPhoneNumber = null;
|
currentPhoneNumber = null;
|
||||||
|
currentDisplayName = null;
|
||||||
|
currentThumbnail = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
} catch (e) {
|
||||||
|
print('CallService: Error fetching contact info: $e');
|
||||||
|
currentDisplayName = phoneNumber;
|
||||||
|
currentThumbnail = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String _normalizePhoneNumber(String number) {
|
||||||
|
return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
|
||||||
|
}
|
||||||
|
|
||||||
void _navigateToCallPage(BuildContext context) {
|
void _navigateToCallPage(BuildContext context) {
|
||||||
if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/call') {
|
if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/call') {
|
||||||
print('CallService: CallPage already visible, skipping navigation');
|
print('CallService: CallPage already visible, skipping navigation');
|
||||||
@ -63,9 +98,9 @@ class CallService {
|
|||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
settings: const RouteSettings(name: '/call'),
|
settings: const RouteSettings(name: '/call'),
|
||||||
builder: (context) => CallPage(
|
builder: (context) => CallPage(
|
||||||
displayName: currentPhoneNumber!,
|
displayName: currentDisplayName ?? currentPhoneNumber!,
|
||||||
phoneNumber: currentPhoneNumber!,
|
phoneNumber: currentPhoneNumber!,
|
||||||
thumbnail: null,
|
thumbnail: currentThumbnail,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
).then((_) {
|
).then((_) {
|
||||||
@ -85,9 +120,9 @@ class CallService {
|
|||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
settings: const RouteSettings(name: '/incoming_call'),
|
settings: const RouteSettings(name: '/incoming_call'),
|
||||||
builder: (context) => IncomingCallPage(
|
builder: (context) => IncomingCallPage(
|
||||||
displayName: currentPhoneNumber!,
|
displayName: currentDisplayName ?? currentPhoneNumber!,
|
||||||
phoneNumber: currentPhoneNumber!,
|
phoneNumber: currentPhoneNumber!,
|
||||||
thumbnail: null,
|
thumbnail: currentThumbnail,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
).then((_) {
|
).then((_) {
|
||||||
@ -116,6 +151,8 @@ class CallService {
|
|||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
currentPhoneNumber = phoneNumber;
|
currentPhoneNumber = phoneNumber;
|
||||||
|
currentDisplayName = displayName ?? phoneNumber; // Use provided or fetch later
|
||||||
|
currentThumbnail = thumbnail; // Use provided or fetch later
|
||||||
print('CallService: Making GSM call to $phoneNumber');
|
print('CallService: Making GSM call to $phoneNumber');
|
||||||
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
|
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
|
||||||
print('CallService: makeGsmCall result: $result');
|
print('CallService: makeGsmCall result: $result');
|
||||||
@ -123,7 +160,11 @@ class CallService {
|
|||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text("Failed to initiate call")),
|
SnackBar(content: Text("Failed to initiate call")),
|
||||||
);
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
// Fetch contact info if not provided
|
||||||
|
await _fetchContactInfo(phoneNumber);
|
||||||
|
_navigateToCallPage(context); // Navigate immediately after call initiation
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("CallService: Error making call: $e");
|
print("CallService: Error making call: $e");
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
Loading…
Reference in New Issue
Block a user