diff --git a/dialer/lib/domain/services/call_service.dart b/dialer/lib/domain/services/call_service.dart index e6669f3..1113afd 100644 --- a/dialer/lib/domain/services/call_service.dart +++ b/dialer/lib/domain/services/call_service.dart @@ -101,18 +101,15 @@ class CallService { print('CallService: _manualHangupFlag: $_manualHangupFlag'); print('CallService: _isCallPageVisible: $_isCallPageVisible'); - // Close call page if no pending SIM switch OR if it was a manual hangup - if (_pendingSimSwitch == null || _manualHangupFlag) { - print('CallService: Condition met, calling _closeCallPage()'); - _closeCallPage(); - // Only reset manual hangup flag after successful page close - if (_manualHangupFlag) { - print( - 'CallService: Resetting manual hangup flag after page close'); - _manualHangupFlag = false; - } - } else { - print('CallService: NOT closing call page - condition not met'); + // Always close call page on disconnection - SIM switching should not prevent this + print('CallService: Calling _closeCallPage() on call disconnection'); + _closeCallPage(); + + // Reset manual hangup flag after successful page close + if (_manualHangupFlag) { + print( + 'CallService: Resetting manual hangup flag after page close'); + _manualHangupFlag = false; } if (wasPhoneLocked) { await _channel.invokeMethod("callEndedFromFlutter"); @@ -169,18 +166,15 @@ class CallService { print('CallService: _manualHangupFlag: $_manualHangupFlag'); print('CallService: _isCallPageVisible: $_isCallPageVisible'); - // Only close call page if there's no pending SIM switch OR if it was a manual hangup during SIM switch - if (_pendingSimSwitch == null || _manualHangupFlag) { - print('CallService: Condition met, calling _closeCallPage()'); - _closeCallPage(); - // Reset manual hangup flag after closing page - if (_manualHangupFlag) { - print( - 'CallService: Resetting manual hangup flag after callEnded'); - _manualHangupFlag = false; - } - } else { - print('CallService: NOT closing call page - condition not met'); + // Always close call page when call ends - SIM switching should not prevent this + print('CallService: Calling _closeCallPage() on call ended/removed'); + _closeCallPage(); + + // Reset manual hangup flag after closing page + if (_manualHangupFlag) { + print( + 'CallService: Resetting manual hangup flag after callEnded'); + _manualHangupFlag = false; } if (wasPhoneLocked) { await _channel.invokeMethod("callEndedFromFlutter"); @@ -490,13 +484,22 @@ class CallService { print( 'CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible, _pendingSimSwitch: ${_pendingSimSwitch != null}, _manualHangupFlag: $_manualHangupFlag'); - if (Navigator.canPop(context)) { - print('CallService: Popping call page'); - Navigator.pop(context); - _isCallPageVisible = false; - } else { - print('CallService: No page to pop, setting _isCallPageVisible to false'); + + // Use popUntil to ensure we go back to the home page + try { + Navigator.popUntil(context, (route) => route.isFirst); _isCallPageVisible = false; + print('CallService: Used popUntil to return to home page'); + } catch (e) { + print('CallService: Error with popUntil, trying regular pop: $e'); + if (Navigator.canPop(context)) { + Navigator.pop(context); + _isCallPageVisible = false; + print('CallService: Used regular pop as fallback'); + } else { + print('CallService: No page to pop, setting _isCallPageVisible to false'); + _isCallPageVisible = false; + } } _activeCallNumber = null; } diff --git a/dialer/lib/presentation/features/call/call_page.dart b/dialer/lib/presentation/features/call/call_page.dart index 5791afc..aee2690 100644 --- a/dialer/lib/presentation/features/call/call_page.dart +++ b/dialer/lib/presentation/features/call/call_page.dart @@ -319,7 +319,7 @@ class _CallPageState extends State { print( 'CallPage: Initiating manual hangUp - canceling any pending SIM switch'); - // Immediately mark call as inactive to allow page navigation + // Immediately mark call as inactive to prevent further interactions setState(() { _isCallActive = false; _callStatus = "Ending Call..."; @@ -330,6 +330,12 @@ class _CallPageState extends State { final result = await _callService.hangUpCall(context); print('CallPage: Hang up result: $result'); + + // If the page is still visible after hangup, try to close it + if (mounted && ModalRoute.of(context)?.isCurrent == true) { + print('CallPage: Still visible after hangup, navigating back'); + Navigator.of(context).popUntil((route) => route.isFirst); + } } catch (e) { print('CallPage: Error hanging up: $e'); if (mounted) { @@ -367,6 +373,17 @@ class _CallPageState extends State { print( 'CallPage: Building UI, _callStatus: $_callStatus, route: ${ModalRoute.of(context)?.settings.name ?? "unknown"}'); + + // If call is disconnected and we're not actively navigating, force navigation + if ((_callStatus == "Call Ended" || !_isCallActive) && mounted) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted && ModalRoute.of(context)?.isCurrent == true) { + print('CallPage: Call ended, forcing navigation back to home'); + Navigator.of(context).popUntil((route) => route.isFirst); + } + }); + } + return PopScope( canPop: true, // Always allow popping - CallService manages when it's appropriate