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