WIP: callPageV2 #52
@ -2,124 +2,115 @@ 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 '../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;
|
||||||
static Map<String, dynamic>? _pendingCall;
|
final ContactService _contactService = ContactService(); // Instantiate ContactService
|
||||||
static bool wasPhoneLocked = false;
|
|
||||||
|
|
||||||
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
|
||||||
CallService() {
|
CallService() {
|
||||||
_channel.setMethodCallHandler((call) async {
|
_channel.setMethodCallHandler((call) async {
|
||||||
print('CallService: Handling method call: ${call.method}');
|
final context = navigatorKey.currentContext;
|
||||||
|
print('CallService: Received method ${call.method} with args ${call.arguments}');
|
||||||
|
if (context == null) {
|
||||||
|
print('CallService: Navigator context is null, cannot navigate');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (call.method) {
|
switch (call.method) {
|
||||||
case "callAdded":
|
case "callAdded":
|
||||||
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") {
|
||||||
_handleIncomingCall(phoneNumber);
|
_navigateToIncomingCallPage(context);
|
||||||
} else {
|
} else {
|
||||||
_navigateToCallPage();
|
_navigateToCallPage(context);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "callStateChanged":
|
case "callStateChanged":
|
||||||
final state = call.arguments["state"] as String;
|
final state = call.arguments["state"] as String;
|
||||||
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
|
print('CallService: State changed to $state');
|
||||||
print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked');
|
|
||||||
if (state == "disconnected" || state == "disconnecting") {
|
if (state == "disconnected" || state == "disconnecting") {
|
||||||
_closeCallPage();
|
_closeCallPage(context);
|
||||||
if (wasPhoneLocked) {
|
|
||||||
_channel.invokeMethod("callEndedFromFlutter");
|
|
||||||
}
|
|
||||||
} else if (state == "active" || state == "dialing") {
|
} else if (state == "active" || state == "dialing") {
|
||||||
_navigateToCallPage();
|
_navigateToCallPage(context);
|
||||||
} else if (state == "ringing") {
|
} else if (state == "ringing") {
|
||||||
final phoneNumber = call.arguments["callId"] as String;
|
_navigateToIncomingCallPage(context);
|
||||||
_handleIncomingCall(phoneNumber.replaceFirst('tel:', ''));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "callEnded":
|
case "callEnded":
|
||||||
case "callRemoved":
|
case "callRemoved":
|
||||||
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
|
print('CallService: Call ended/removed');
|
||||||
print('CallService: Call ended/removed, wasPhoneLocked: $wasPhoneLocked');
|
_closeCallPage(context);
|
||||||
_closeCallPage();
|
|
||||||
if (wasPhoneLocked) {
|
|
||||||
_channel.invokeMethod("callEndedFromFlutter");
|
|
||||||
}
|
|
||||||
currentPhoneNumber = null;
|
currentPhoneNumber = null;
|
||||||
break;
|
currentDisplayName = null;
|
||||||
case "incomingCallFromNotification":
|
currentThumbnail = null;
|
||||||
final phoneNumber = call.arguments["phoneNumber"] as String;
|
|
||||||
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
|
|
||||||
currentPhoneNumber = phoneNumber;
|
|
||||||
print('CallService: Incoming call from notification: $phoneNumber, wasPhoneLocked: $wasPhoneLocked');
|
|
||||||
_handleIncomingCall(phoneNumber);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleIncomingCall(String phoneNumber) {
|
Future<void> _fetchContactInfo(String phoneNumber) async {
|
||||||
final context = navigatorKey.currentContext;
|
if (currentDisplayName != null && currentThumbnail != null) return; // Already set
|
||||||
if (context == null) {
|
try {
|
||||||
print('CallService: Context is null, queuing incoming call: $phoneNumber');
|
final contacts = await _contactService.fetchContacts(); // Use ContactService
|
||||||
_pendingCall = {"phoneNumber": phoneNumber};
|
for (var contact in contacts) {
|
||||||
Future.delayed(Duration(milliseconds: 500), () => _checkPendingCall());
|
for (var phone in contact.phones) {
|
||||||
} else {
|
if (_normalizePhoneNumber(phone.number) == _normalizePhoneNumber(phoneNumber)) {
|
||||||
_navigateToIncomingCallPage(context);
|
currentDisplayName = contact.displayName;
|
||||||
}
|
currentThumbnail = contact.thumbnail;
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
void _checkPendingCall() {
|
}
|
||||||
if (_pendingCall != null) {
|
|
||||||
final context = navigatorKey.currentContext;
|
|
||||||
if (context != null) {
|
|
||||||
print('CallService: Processing queued call: ${_pendingCall!["phoneNumber"]}');
|
|
||||||
currentPhoneNumber = _pendingCall!["phoneNumber"];
|
|
||||||
_navigateToIncomingCallPage(context);
|
|
||||||
_pendingCall = null;
|
|
||||||
} else {
|
|
||||||
print('CallService: Context still null, retrying...');
|
|
||||||
Future.delayed(Duration(milliseconds: 500), () => _checkPendingCall());
|
|
||||||
}
|
}
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _navigateToCallPage() {
|
String _normalizePhoneNumber(String number) {
|
||||||
final context = navigatorKey.currentContext;
|
return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
|
||||||
if (context == null) {
|
}
|
||||||
print('CallService: Cannot navigate to CallPage, context is null');
|
|
||||||
return;
|
void _navigateToCallPage(BuildContext context) {
|
||||||
}
|
if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/call') {
|
||||||
if (_isCallPageVisible) {
|
|
||||||
print('CallService: CallPage already visible, skipping navigation');
|
print('CallService: CallPage already visible, skipping navigation');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
print('CallService: Navigating to CallPage');
|
print('CallService: Navigating to CallPage');
|
||||||
Navigator.push(
|
Navigator.pushReplacement(
|
||||||
context,
|
context,
|
||||||
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((_) {
|
||||||
_isCallPageVisible = false;
|
_isCallPageVisible = false;
|
||||||
print('CallService: CallPage popped, _isCallPageVisible set to false');
|
|
||||||
});
|
});
|
||||||
_isCallPageVisible = true;
|
_isCallPageVisible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _navigateToIncomingCallPage(BuildContext context) {
|
void _navigateToIncomingCallPage(BuildContext context) {
|
||||||
if (_isCallPageVisible) {
|
if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/incoming_call') {
|
||||||
print('CallService: IncomingCallPage already visible, skipping navigation');
|
print('CallService: IncomingCallPage already visible, skipping navigation');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -129,35 +120,30 @@ 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((_) {
|
||||||
_isCallPageVisible = false;
|
_isCallPageVisible = false;
|
||||||
print('CallService: IncomingCallPage popped, _isCallPageVisible set to false');
|
|
||||||
});
|
});
|
||||||
_isCallPageVisible = true;
|
_isCallPageVisible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _closeCallPage() {
|
void _closeCallPage(BuildContext context) {
|
||||||
final context = navigatorKey.currentContext;
|
if (!_isCallPageVisible) {
|
||||||
if (context == null) {
|
print('CallService: CallPage not visible, skipping pop');
|
||||||
print('CallService: Cannot close page, context is null');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
print('CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible');
|
|
||||||
if (Navigator.canPop(context)) {
|
if (Navigator.canPop(context)) {
|
||||||
print('CallService: Popping call page');
|
print('CallService: Popping CallPage');
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
_isCallPageVisible = false;
|
_isCallPageVisible = false;
|
||||||
} else {
|
|
||||||
print('CallService: No page to pop');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> makeGsmCall(
|
Future<void> makeGsmCall(
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
required String phoneNumber,
|
required String phoneNumber,
|
||||||
String? displayName,
|
String? displayName,
|
||||||
@ -165,43 +151,45 @@ 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');
|
||||||
final resultMap = Map<String, dynamic>.from(result as Map);
|
if (result["status"] != "calling") {
|
||||||
if (resultMap["status"] != "calling") {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text("Failed to initiate call")),
|
SnackBar(content: Text("Failed to initiate call")),
|
||||||
);
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return resultMap;
|
// 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(
|
||||||
SnackBar(content: Text("Error making call: $e")),
|
SnackBar(content: Text("Error making call: $e")),
|
||||||
);
|
);
|
||||||
return {"status": "error", "message": e.toString()};
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> hangUpCall(BuildContext context) async {
|
Future<void> hangUpCall(BuildContext context) async {
|
||||||
try {
|
try {
|
||||||
print('CallService: Hanging up call');
|
print('CallService: Hanging up call');
|
||||||
final result = await _channel.invokeMethod('hangUpCall');
|
final result = await _channel.invokeMethod('hangUpCall');
|
||||||
print('CallService: hangUpCall result: $result');
|
print('CallService: hangUpCall result: $result');
|
||||||
final resultMap = Map<String, dynamic>.from(result as Map);
|
if (result["status"] != "ended") {
|
||||||
if (resultMap["status"] != "ended") {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text("Failed to end call")),
|
SnackBar(content: Text("Failed to end call")),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return resultMap;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("CallService: Error hanging up call: $e");
|
print("CallService: Error hanging up call: $e");
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text("Error hanging up call: $e")),
|
SnackBar(content: Text("Error hanging up call: $e")),
|
||||||
);
|
);
|
||||||
return {"status": "error", "message": e.toString()};
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user