fix: no screen stacking when making calls
All checks were successful
/ mirror (push) Successful in 5s
/ build (push) Successful in 10m21s
/ build-stealth (push) Successful in 10m21s

This commit is contained in:
Florian Griffon 2025-04-08 22:06:43 +03:00
parent 63e5632f9a
commit 6190991780

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';
@ -11,6 +10,7 @@ class CallService {
static String? currentDisplayName;
static Uint8List? currentThumbnail;
static bool _isCallPageVisible = false;
static String? _currentCallState;
final ContactService _contactService = ContactService();
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
@ -31,22 +31,12 @@ class CallService {
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
await _fetchContactInfo(currentPhoneNumber!);
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
if (state == "ringing") {
_navigateToIncomingCallPage(context);
} else {
_navigateToCallPage(context);
}
_handleCallState(context, state);
break;
case "callStateChanged":
final state = call.arguments["state"] as String;
print('CallService: State changed to $state');
if (state == "disconnected" || state == "disconnecting") {
_closeCallPage(context);
} else if (state == "active" || state == "dialing") {
_navigateToCallPage(context);
} else if (state == "ringing") {
_navigateToIncomingCallPage(context);
}
_handleCallState(context, state);
break;
case "callEnded":
case "callRemoved":
@ -55,6 +45,7 @@ class CallService {
currentPhoneNumber = null;
currentDisplayName = null;
currentThumbnail = null;
_currentCallState = null;
break;
}
});
@ -73,7 +64,6 @@ class CallService {
}
}
}
// If no match found, use phone number as fallback
currentDisplayName = phoneNumber;
currentThumbnail = null;
} catch (e) {
@ -87,12 +77,33 @@ class CallService {
return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
}
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") {
_navigateToIncomingCallPage(context);
}
}
void _navigateToCallPage(BuildContext context) {
if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/call') {
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;
}
print('CallService: Navigating to CallPage');
if (_isCallPageVisible && currentRoute == '/incoming_call') {
print('CallService: Replacing IncomingCallPage with CallPage');
Navigator.pop(context);
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
@ -104,17 +115,23 @@ class CallService {
),
),
).then((_) {
print('CallService: CallPage popped');
_isCallPageVisible = false;
});
_isCallPageVisible = true;
}
void _navigateToIncomingCallPage(BuildContext context) {
if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/incoming_call') {
final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigating to IncomingCallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
if (_isCallPageVisible && currentRoute == '/incoming_call') {
print('CallService: IncomingCallPage already visible, skipping navigation');
return;
}
print('CallService: Navigating to IncomingCallPage');
if (_isCallPageVisible && currentRoute == '/call') {
print('CallService: CallPage visible, not showing IncomingCallPage');
return;
}
Navigator.push(
context,
MaterialPageRoute(
@ -126,20 +143,24 @@ class CallService {
),
),
).then((_) {
print('CallService: IncomingCallPage popped');
_isCallPageVisible = false;
});
_isCallPageVisible = true;
}
void _closeCallPage(BuildContext context) {
print('CallService: Attempting to close call page. Visible: $_isCallPageVisible');
if (!_isCallPageVisible) {
print('CallService: CallPage not visible, skipping pop');
return;
}
if (Navigator.canPop(context)) {
print('CallService: Popping CallPage');
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');
}
}
@ -151,7 +172,6 @@ class CallService {
}) async {
try {
currentPhoneNumber = phoneNumber;
// Use provided displayName and thumbnail if available, otherwise fetch
currentDisplayName = displayName ?? phoneNumber;
currentThumbnail = thumbnail;
if (displayName == null || thumbnail == null) {
@ -166,7 +186,7 @@ class CallService {
);
return;
}
_navigateToCallPage(context);
_handleCallState(context, "dialing");
} catch (e) {
print("CallService: Error making call: $e");
ScaffoldMessenger.of(context).showSnackBar(
@ -185,6 +205,8 @@ class CallService {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Failed to end call")),
);
} else {
_closeCallPage(context);
}
} catch (e) {
print("CallService: Error hanging up call: $e");