feat: merge call_service with callNotifications changes
This commit is contained in:
parent
24fef24c6d
commit
540b330f0d
@ -11,13 +11,15 @@ class CallService {
|
|||||||
static Uint8List? currentThumbnail;
|
static Uint8List? currentThumbnail;
|
||||||
static bool _isCallPageVisible = false;
|
static bool _isCallPageVisible = false;
|
||||||
static String? _currentCallState;
|
static String? _currentCallState;
|
||||||
|
static Map<String, dynamic>? _pendingCall;
|
||||||
|
static bool wasPhoneLocked = false;
|
||||||
final ContactService _contactService = ContactService();
|
final ContactService _contactService = ContactService();
|
||||||
|
|
||||||
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}');
|
print('CallService: Received method ${call.method} with args ${call.arguments}');
|
||||||
switch (call.method) {
|
switch (call.method) {
|
||||||
case "callAdded":
|
case "callAdded":
|
||||||
final phoneNumber = call.arguments["callId"] as String;
|
final phoneNumber = call.arguments["callId"] as String;
|
||||||
@ -25,12 +27,13 @@ class CallService {
|
|||||||
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
||||||
await _fetchContactInfo(currentPhoneNumber!);
|
await _fetchContactInfo(currentPhoneNumber!);
|
||||||
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
||||||
_handleCallState(context, state);
|
_handleCallState(state);
|
||||||
break;
|
break;
|
||||||
case "callStateChanged":
|
case "callStateChanged":
|
||||||
final state = call.arguments["state"] as String;
|
final state = call.arguments["state"] as String;
|
||||||
print('CallService: State changed to $state');
|
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
|
||||||
_handleCallState(context, state);
|
print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked');
|
||||||
|
_handleCallState(state);
|
||||||
break;
|
break;
|
||||||
case "callEnded":
|
case "callEnded":
|
||||||
case "callRemoved":
|
case "callRemoved":
|
||||||
@ -45,6 +48,14 @@ class CallService {
|
|||||||
currentThumbnail = null;
|
currentThumbnail = null;
|
||||||
_currentCallState = null;
|
_currentCallState = null;
|
||||||
break;
|
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\-\(\)]'), '');
|
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) {
|
if (_currentCallState == state) {
|
||||||
print('CallService: State $state already handled, skipping');
|
print('CallService: State $state already handled, skipping');
|
||||||
return;
|
return;
|
||||||
@ -83,7 +125,7 @@ class CallService {
|
|||||||
_currentCallState = state;
|
_currentCallState = state;
|
||||||
|
|
||||||
if (state == "disconnected" || state == "disconnecting") {
|
if (state == "disconnected" || state == "disconnecting") {
|
||||||
_closeCallPage(context);
|
_closeCallPage();
|
||||||
} else if (state == "active" || state == "dialing") {
|
} else if (state == "active" || state == "dialing") {
|
||||||
_navigateToCallPage(context);
|
_navigateToCallPage(context);
|
||||||
} else if (state == "ringing") {
|
} else if (state == "ringing") {
|
||||||
@ -115,7 +157,6 @@ class CallService {
|
|||||||
).then((_) {
|
).then((_) {
|
||||||
print('CallService: CallPage popped');
|
print('CallService: CallPage popped');
|
||||||
_isCallPageVisible = false;
|
_isCallPageVisible = false;
|
||||||
print('CallService: CallPage popped, _isCallPageVisible set to false');
|
|
||||||
});
|
});
|
||||||
_isCallPageVisible = true;
|
_isCallPageVisible = true;
|
||||||
}
|
}
|
||||||
@ -144,18 +185,21 @@ class CallService {
|
|||||||
).then((_) {
|
).then((_) {
|
||||||
print('CallService: IncomingCallPage popped');
|
print('CallService: IncomingCallPage popped');
|
||||||
_isCallPageVisible = false;
|
_isCallPageVisible = false;
|
||||||
print('CallService: IncomingCallPage popped, _isCallPageVisible set to false');
|
|
||||||
});
|
});
|
||||||
_isCallPageVisible = true;
|
_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');
|
print('CallService: Attempting to close call page. Visible: $_isCallPageVisible');
|
||||||
if (!_isCallPageVisible) {
|
if (!_isCallPageVisible) {
|
||||||
print('CallService: CallPage not visible, skipping pop');
|
print('CallService: CallPage not visible, skipping pop');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
print('CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible');
|
|
||||||
if (Navigator.canPop(context)) {
|
if (Navigator.canPop(context)) {
|
||||||
print('CallService: Popping CallPage. Current Route: ${ModalRoute.of(context)?.settings.name}');
|
print('CallService: Popping CallPage. Current Route: ${ModalRoute.of(context)?.settings.name}');
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
@ -165,7 +209,7 @@ class CallService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> makeGsmCall(
|
Future<void> makeGsmCall(
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
required String phoneNumber,
|
required String phoneNumber,
|
||||||
String? displayName,
|
String? displayName,
|
||||||
@ -181,43 +225,40 @@ class CallService {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
_handleCallState(context, "dialing");
|
_handleCallState("dialing");
|
||||||
} 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")),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
_closeCallPage(context);
|
_closeCallPage();
|
||||||
}
|
}
|
||||||
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