fix: no screen stacking when making calls

This commit is contained in:
Florian Griffon 2025-04-08 22:06:43 +03:00
parent 7cb5993382
commit fa6e538c24

View File

@ -1,4 +1,3 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../features/call/call_page.dart';
@ -24,6 +23,7 @@ class CallService {
static String? currentDisplayName;
static Uint8List? currentThumbnail;
static bool _isCallPageVisible = false;
<<<<<<< HEAD
<<<<<<< HEAD
static String? _currentCallState;
=======
@ -34,6 +34,8 @@ class CallService {
static Uint8List? currentThumbnail;
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
static bool _isCallPageVisible = false;
=======
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
<<<<<<< HEAD
final ContactService _contactService = ContactService(); // Instantiate ContactService
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
@ -47,6 +49,9 @@ class CallService {
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
=======
=======
static String? _currentCallState;
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
final ContactService _contactService = ContactService();
>>>>>>> acbccaa (feat: give parameters to callPage to avoid fetching when possible)
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
@ -74,9 +79,12 @@ class CallService {
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
await _fetchContactInfo(currentPhoneNumber!);
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
<<<<<<< HEAD
<<<<<<< HEAD
_handleCallState(state);
=======
=======
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
if (state == "ringing") {
_handleIncomingCall(phoneNumber);
} else {
@ -102,7 +110,18 @@ class CallService {
final phoneNumber = call.arguments["callId"] as String;
_handleIncomingCall(phoneNumber.replaceFirst('tel:', ''));
}
<<<<<<< HEAD
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
=======
_handleCallState(context, state);
break;
case "callStateChanged":
final state = call.arguments["state"] as String;
print('CallService: State changed to $state');
_handleCallState(context, state);
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
break;
case "callEnded":
case "callRemoved":
@ -115,10 +134,14 @@ class CallService {
currentPhoneNumber = null;
currentDisplayName = null;
currentThumbnail = null;
<<<<<<< HEAD
<<<<<<< HEAD
_currentCallState = null;
=======
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
=======
_currentCallState = null;
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
break;
case "incomingCallFromNotification":
final phoneNumber = call.arguments["phoneNumber"] as String;
@ -170,11 +193,14 @@ class CallService {
}
}
}
<<<<<<< HEAD
<<<<<<< HEAD
currentDisplayName = phoneNumber;
currentThumbnail = null;
=======
// If no match found, use phone number as fallback
=======
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
currentDisplayName = phoneNumber;
currentThumbnail = null;
} catch (e) {
@ -188,6 +214,7 @@ class CallService {
return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
}
<<<<<<< HEAD
Future<void> _fetchContactInfo(String phoneNumber) async {
if (currentDisplayName != null && currentThumbnail != null) return; // Already set
try {
@ -228,10 +255,25 @@ class CallService {
_pendingCall = {"phoneNumber": phoneNumber};
Future.delayed(Duration(milliseconds: 500), () => _checkPendingCall());
} else {
=======
void _handleCallState(BuildContext context, String state) {
if (_currentCallState == state) {
print('CallService: State $state already handled, skipping');
return;
}
_currentCallState = state;
if (state == "disconnected" || state == "disconnecting") {
_closeCallPage(context);
} else if (state == "active" || state == "dialing") {
_navigateToCallPage(context);
} else if (state == "ringing") {
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
_navigateToIncomingCallPage(context);
}
}
<<<<<<< HEAD
void _checkPendingCall() {
if (_pendingCall != null) {
final context = navigatorKey.currentContext;
@ -294,7 +336,24 @@ class CallService {
}
print('CallService: Navigating to CallPage');
Navigator.push(
<<<<<<< HEAD
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
=======
void _navigateToCallPage(BuildContext context) {
final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigating to CallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
if (_isCallPageVisible && currentRoute == '/call') {
print('CallService: CallPage already visible, skipping navigation');
return;
}
if (_isCallPageVisible && currentRoute == '/incoming_call') {
print('CallService: Replacing IncomingCallPage with CallPage');
Navigator.pop(context);
}
Navigator.pushReplacement(
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
context,
MaterialPageRoute(
settings: const RouteSettings(name: '/call'),
@ -313,6 +372,7 @@ class CallService {
}
void _navigateToIncomingCallPage(BuildContext context) {
<<<<<<< HEAD
<<<<<<< HEAD
final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigating to IncomingCallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
@ -320,6 +380,14 @@ class CallService {
=======
if (_isCallPageVisible) {
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
if (_isCallPageVisible) {
=======
final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigating to IncomingCallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
if (_isCallPageVisible && currentRoute == '/incoming_call') {
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
print('CallService: IncomingCallPage already visible, skipping navigation');
return;
}
@ -345,10 +413,12 @@ class CallService {
_isCallPageVisible = true;
}
<<<<<<< HEAD
void _closeCallPage() {
final context = navigatorKey.currentContext;
if (context == null) {
print('CallService: Cannot close page, context is null');
<<<<<<< HEAD
<<<<<<< HEAD
return;
}
@ -357,10 +427,19 @@ class CallService {
print('CallService: CallPage not visible, skipping pop');
=======
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
=======
void _closeCallPage(BuildContext context) {
print('CallService: Attempting to close call page. Visible: $_isCallPageVisible');
if (!_isCallPageVisible) {
print('CallService: CallPage not visible, skipping pop');
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
return;
}
print('CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible');
if (Navigator.canPop(context)) {
<<<<<<< HEAD
<<<<<<< HEAD
print('CallService: Popping CallPage. Current Route: ${ModalRoute.of(context)?.settings.name}');
Navigator.pop(context);
@ -368,12 +447,24 @@ class CallService {
} else {
print('CallService: Cannot pop, no routes to pop');
=======
=======
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
print('CallService: Popping call page');
Navigator.pop(context);
_isCallPageVisible = false;
} else {
print('CallService: No page to pop');
<<<<<<< HEAD
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
=======
print('CallService: Popping CallPage. Current Route: ${ModalRoute.of(context)?.settings.name}');
Navigator.pop(context);
_isCallPageVisible = false;
} else {
print('CallService: Cannot pop, no routes to pop');
>>>>>>> f491fb6 (fix: no screen stacking when making calls)
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
}
}
@ -387,9 +478,12 @@ class CallService {
currentPhoneNumber = phoneNumber;
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
// Use provided displayName and thumbnail if available, otherwise fetch
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
=======
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
currentDisplayName = displayName ?? phoneNumber;
currentThumbnail = thumbnail;
if (displayName == null || thumbnail == null) {
@ -414,6 +508,7 @@ class CallService {
}
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
_handleCallState("dialing");
=======
@ -427,6 +522,9 @@ class CallService {
=======
_navigateToCallPage(context);
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
=======
_handleCallState(context, "dialing");
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
} catch (e) {
print("CallService: Error making call: $e");
ScaffoldMessenger.of(context).showSnackBar(
@ -447,7 +545,11 @@ class CallService {
SnackBar(content: Text("Failed to end call")),
);
} else {
<<<<<<< HEAD
_closeCallPage();
=======
_closeCallPage(context);
>>>>>>> 9e76148 (fix: no screen stacking when making calls)
}
return resultMap;
} catch (e) {