callNotifications and various fix related to calls (#49)

Reviewed-on: #49
Co-authored-by: florian <florian.griffon@epitech.eu>
Co-committed-by: florian <florian.griffon@epitech.eu>
This commit is contained in:
florian 2025-04-17 12:26:32 +00:00 committed by Florian Griffon
parent 540b330f0d
commit c76826ba62

View File

@ -2,7 +2,10 @@ 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';
<<<<<<< HEAD
import '../services/contact_service.dart'; import '../services/contact_service.dart';
=======
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
class CallService { class CallService {
static const MethodChannel _channel = MethodChannel('call_service'); static const MethodChannel _channel = MethodChannel('call_service');
@ -10,16 +13,25 @@ class CallService {
static String? currentDisplayName; static String? currentDisplayName;
static Uint8List? currentThumbnail; static Uint8List? currentThumbnail;
static bool _isCallPageVisible = false; static bool _isCallPageVisible = false;
<<<<<<< HEAD
static String? _currentCallState; static String? _currentCallState;
static Map<String, dynamic>? _pendingCall; static Map<String, dynamic>? _pendingCall;
static bool wasPhoneLocked = false; static bool wasPhoneLocked = false;
final ContactService _contactService = ContactService(); final ContactService _contactService = ContactService();
=======
static Map<String, dynamic>? _pendingCall;
static bool wasPhoneLocked = false;
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
CallService() { CallService() {
_channel.setMethodCallHandler((call) async { _channel.setMethodCallHandler((call) async {
<<<<<<< HEAD
print('CallService: Received method ${call.method} with args ${call.arguments}'); print('CallService: Received method ${call.method} with args ${call.arguments}');
=======
print('CallService: Handling method call: ${call.method}');
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
switch (call.method) { switch (call.method) {
case "callAdded": case "callAdded":
final phoneNumber = call.arguments["callId"] as String; final phoneNumber = call.arguments["callId"] as String;
@ -27,13 +39,35 @@ 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');
<<<<<<< HEAD
_handleCallState(state); _handleCallState(state);
=======
if (state == "ringing") {
_handleIncomingCall(phoneNumber);
} else {
_navigateToCallPage();
}
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
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; wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked'); print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked');
<<<<<<< HEAD
_handleCallState(state); _handleCallState(state);
=======
if (state == "disconnected" || state == "disconnecting") {
_closeCallPage();
if (wasPhoneLocked) {
_channel.invokeMethod("callEndedFromFlutter");
}
} else if (state == "active" || state == "dialing") {
_navigateToCallPage();
} else if (state == "ringing") {
final phoneNumber = call.arguments["callId"] as String;
_handleIncomingCall(phoneNumber.replaceFirst('tel:', ''));
}
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
break; break;
case "callEnded": case "callEnded":
case "callRemoved": case "callRemoved":
@ -56,10 +90,18 @@ class CallService {
print('CallService: Incoming call from notification: $phoneNumber, wasPhoneLocked: $wasPhoneLocked'); print('CallService: Incoming call from notification: $phoneNumber, wasPhoneLocked: $wasPhoneLocked');
_handleIncomingCall(phoneNumber); _handleIncomingCall(phoneNumber);
break; break;
case "incomingCallFromNotification":
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;
} }
}); });
} }
<<<<<<< HEAD
Future<void> _fetchContactInfo(String phoneNumber) async { Future<void> _fetchContactInfo(String phoneNumber) async {
try { try {
final contacts = await _contactService.fetchContacts(); final contacts = await _contactService.fetchContacts();
@ -86,6 +128,8 @@ class CallService {
return number.replaceAll(RegExp(r'[\s\-\(\)]'), ''); return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
} }
=======
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
void _handleIncomingCall(String phoneNumber) { void _handleIncomingCall(String phoneNumber) {
final context = navigatorKey.currentContext; final context = navigatorKey.currentContext;
if (context == null) { if (context == null) {
@ -112,6 +156,7 @@ class CallService {
} }
} }
<<<<<<< HEAD
void _handleCallState(String state) { void _handleCallState(String state) {
final context = navigatorKey.currentContext; final context = navigatorKey.currentContext;
if (context == null) { if (context == null) {
@ -145,6 +190,20 @@ class CallService {
Navigator.pop(context); Navigator.pop(context);
} }
Navigator.pushReplacement( Navigator.pushReplacement(
=======
void _navigateToCallPage() {
final context = navigatorKey.currentContext;
if (context == null) {
print('CallService: Cannot navigate to CallPage, context is null');
return;
}
if (_isCallPageVisible) {
print('CallService: CallPage already visible, skipping navigation');
return;
}
print('CallService: Navigating to CallPage');
Navigator.push(
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
context, context,
MaterialPageRoute( MaterialPageRoute(
settings: const RouteSettings(name: '/call'), settings: const RouteSettings(name: '/call'),
@ -157,14 +216,19 @@ 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;
} }
void _navigateToIncomingCallPage(BuildContext context) { void _navigateToIncomingCallPage(BuildContext context) {
<<<<<<< HEAD
final currentRoute = ModalRoute.of(context)?.settings.name; final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigating to IncomingCallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute'); print('CallService: Navigating to IncomingCallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
if (_isCallPageVisible && currentRoute == '/incoming_call') { if (_isCallPageVisible && currentRoute == '/incoming_call') {
=======
if (_isCallPageVisible) {
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
print('CallService: IncomingCallPage already visible, skipping navigation'); print('CallService: IncomingCallPage already visible, skipping navigation');
return; return;
} }
@ -185,6 +249,7 @@ 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;
} }
@ -193,23 +258,35 @@ class CallService {
final context = navigatorKey.currentContext; final context = navigatorKey.currentContext;
if (context == null) { if (context == null) {
print('CallService: Cannot close page, context is null'); print('CallService: Cannot close page, context is null');
<<<<<<< HEAD
return; 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');
=======
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
return; return;
} }
print('CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible');
if (Navigator.canPop(context)) { if (Navigator.canPop(context)) {
<<<<<<< HEAD
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);
_isCallPageVisible = false; _isCallPageVisible = false;
} else { } else {
print('CallService: Cannot pop, no routes to pop'); print('CallService: Cannot pop, no routes to pop');
=======
print('CallService: Popping call page');
Navigator.pop(context);
_isCallPageVisible = false;
} else {
print('CallService: No page to pop');
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
} }
} }
Future<void> makeGsmCall( Future<Map<String, dynamic>> makeGsmCall(
BuildContext context, { BuildContext context, {
required String phoneNumber, required String phoneNumber,
String? displayName, String? displayName,
@ -225,40 +302,47 @@ 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');
if (result["status"] != "calling") { final resultMap = Map<String, dynamic>.from(result as Map);
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;
} }
<<<<<<< HEAD
_handleCallState("dialing"); _handleCallState("dialing");
=======
return resultMap;
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
} 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")),
); );
rethrow; return {"status": "error", "message": e.toString()};
} }
} }
Future<void> hangUpCall(BuildContext context) async { Future<Map<String, dynamic>> 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');
if (result["status"] != "ended") { final resultMap = Map<String, dynamic>.from(result as Map);
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(); _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")),
); );
rethrow; return {"status": "error", "message": e.toString()};
} }
} }
} }