WIP: callPageV2 #52
@ -11,13 +11,15 @@ class CallService {
|
||||
static Uint8List? currentThumbnail;
|
||||
static bool _isCallPageVisible = false;
|
||||
static String? _currentCallState;
|
||||
static Map<String, dynamic>? _pendingCall;
|
||||
static bool wasPhoneLocked = false;
|
||||
final ContactService _contactService = ContactService();
|
||||
|
||||
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
CallService() {
|
||||
_channel.setMethodCallHandler((call) async {
|
||||
print('CallService: Handling method call: ${call.method}');
|
||||
print('CallService: Received method ${call.method} with args ${call.arguments}');
|
||||
switch (call.method) {
|
||||
case "callAdded":
|
||||
final phoneNumber = call.arguments["callId"] as String;
|
||||
@ -25,12 +27,13 @@ class CallService {
|
||||
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
||||
await _fetchContactInfo(currentPhoneNumber!);
|
||||
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
||||
_handleCallState(context, state);
|
||||
_handleCallState(state);
|
||||
break;
|
||||
case "callStateChanged":
|
||||
final state = call.arguments["state"] as String;
|
||||
print('CallService: State changed to $state');
|
||||
_handleCallState(context, state);
|
||||
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
|
||||
print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked');
|
||||
_handleCallState(state);
|
||||
break;
|
||||
case "callEnded":
|
||||
case "callRemoved":
|
||||
@ -45,6 +48,14 @@ class CallService {
|
||||
currentThumbnail = null;
|
||||
_currentCallState = null;
|
||||
break;
|
||||
case "incomingCallFromNotification":
|
||||
final phoneNumber = call.arguments["phoneNumber"] as String;
|
||||
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
|
||||
currentPhoneNumber = phoneNumber;
|
||||
await _fetchContactInfo(currentPhoneNumber!);
|
||||
print('CallService: Incoming call from notification: $phoneNumber, wasPhoneLocked: $wasPhoneLocked');
|
||||
_handleIncomingCall(phoneNumber);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -75,7 +86,38 @@ class CallService {
|
||||
return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
|
||||
}
|
||||
|
||||
void _handleCallState(BuildContext context, String state) {
|
||||
void _handleIncomingCall(String phoneNumber) {
|
||||
final context = navigatorKey.currentContext;
|
||||
if (context == null) {
|
||||
print('CallService: Context is null, queuing incoming call: $phoneNumber');
|
||||
_pendingCall = {"phoneNumber": phoneNumber};
|
||||
Future.delayed(Duration(milliseconds: 500), () => _checkPendingCall());
|
||||
} else {
|
||||
_navigateToIncomingCallPage(context);
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _handleCallState(String state) {
|
||||
final context = navigatorKey.currentContext;
|
||||
if (context == null) {
|
||||
print('CallService: Navigator context is null, cannot navigate');
|
||||
return;
|
||||
}
|
||||
if (_currentCallState == state) {
|
||||
print('CallService: State $state already handled, skipping');
|
||||
return;
|
||||
@ -83,7 +125,7 @@ class CallService {
|
||||
_currentCallState = state;
|
||||
|
||||
if (state == "disconnected" || state == "disconnecting") {
|
||||
_closeCallPage(context);
|
||||
_closeCallPage();
|
||||
} else if (state == "active" || state == "dialing") {
|
||||
_navigateToCallPage(context);
|
||||
} else if (state == "ringing") {
|
||||
@ -115,7 +157,6 @@ class CallService {
|
||||
).then((_) {
|
||||
print('CallService: CallPage popped');
|
||||
_isCallPageVisible = false;
|
||||
print('CallService: CallPage popped, _isCallPageVisible set to false');
|
||||
});
|
||||
_isCallPageVisible = true;
|
||||
}
|
||||
@ -144,18 +185,21 @@ class CallService {
|
||||
).then((_) {
|
||||
print('CallService: IncomingCallPage popped');
|
||||
_isCallPageVisible = false;
|
||||
print('CallService: IncomingCallPage popped, _isCallPageVisible set to false');
|
||||
});
|
||||
_isCallPageVisible = true;
|
||||
}
|
||||
|
||||
void _closeCallPage(BuildContext context) {
|
||||
void _closeCallPage() {
|
||||
final context = navigatorKey.currentContext;
|
||||
if (context == null) {
|
||||
print('CallService: Cannot close page, context is null');
|
||||
return;
|
||||
}
|
||||
print('CallService: Attempting to close call page. Visible: $_isCallPageVisible');
|
||||
if (!_isCallPageVisible) {
|
||||
print('CallService: CallPage not visible, skipping pop');
|
||||
return;
|
||||
}
|
||||
print('CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible');
|
||||
if (Navigator.canPop(context)) {
|
||||
print('CallService: Popping CallPage. Current Route: ${ModalRoute.of(context)?.settings.name}');
|
||||
Navigator.pop(context);
|
||||
@ -165,7 +209,7 @@ class CallService {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> makeGsmCall(
|
||||
Future<void> makeGsmCall(
|
||||
BuildContext context, {
|
||||
required String phoneNumber,
|
||||
String? displayName,
|
||||
@ -181,43 +225,40 @@ class CallService {
|
||||
print('CallService: Making GSM call to $phoneNumber');
|
||||
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
|
||||
print('CallService: makeGsmCall result: $result');
|
||||
final resultMap = Map<String, dynamic>.from(result as Map);
|
||||
if (resultMap["status"] != "calling") {
|
||||
if (result["status"] != "calling") {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Failed to initiate call")),
|
||||
);
|
||||
return;
|
||||
}
|
||||
_handleCallState(context, "dialing");
|
||||
_handleCallState("dialing");
|
||||
} catch (e) {
|
||||
print("CallService: Error making call: $e");
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
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 {
|
||||
print('CallService: Hanging up call');
|
||||
final result = await _channel.invokeMethod('hangUpCall');
|
||||
print('CallService: hangUpCall result: $result');
|
||||
final resultMap = Map<String, dynamic>.from(result as Map);
|
||||
if (resultMap["status"] != "ended") {
|
||||
if (result["status"] != "ended") {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Failed to end call")),
|
||||
);
|
||||
} else {
|
||||
_closeCallPage(context);
|
||||
_closeCallPage();
|
||||
}
|
||||
return resultMap;
|
||||
} catch (e) {
|
||||
print("CallService: Error hanging up call: $e");
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Error hanging up call: $e")),
|
||||
);
|
||||
return {"status": "error", "message": e.toString()};
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user