From e66fd5eba6fb2beeb0bd8fbed2cdc0ecb5840e2f Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Tue, 27 Oct 2026 18:14:45 +0100 Subject: [PATCH] End the web call when its account's screens go away Signing out, a soft logout or clearing the cache replace the session's screens, which stops the tab coordinator. Its stop() only tore down its tabs, so a call screen in its overlay (and anything in its sheet or full-screen cover) was never stopped: the web call stayed ongoing and, with the missed-while-busy rule, every later call showed as missed until the app restarted. stop() now also tears down its sheet, full-screen cover and overlay, as the stack and split coordinators already do for what they present. --- .../Navigation/NavigationTabCoordinator.swift | 6 ++-- .../Sources/Mocks/JoinedRoomProxyMock.swift | 1 + .../NavigationSplitCoordinatorTests.swift | 30 +++++++++++++++++++ .../NavigationStackCoordinatorTests.swift | 30 +++++++++++++++++++ .../NavigationTabCoordinatorTests.swift | 30 +++++++++++++++++++ .../UserSessionFlowCoordinatorTests.swift | 22 ++++++++++++-- 6 files changed, 114 insertions(+), 5 deletions(-) diff --git a/ElementX/Sources/Application/Navigation/NavigationTabCoordinator.swift b/ElementX/Sources/Application/Navigation/NavigationTabCoordinator.swift index 208d60ebb..625d4e9f7 100644 --- a/ElementX/Sources/Application/Navigation/NavigationTabCoordinator.swift +++ b/ElementX/Sources/Application/Navigation/NavigationTabCoordinator.swift @@ -273,9 +273,11 @@ import SwiftUI // MARK: - CoordinatorProtocol - /// No idea if this is particuarly needed for the TabView but we do this for the NavigationStackCoordinator and NavigationSplitCoordinator so it - /// doesn't seem to harm to also do it here. func stop() { + sheetModule?.tearDown() + fullScreenCoverModule?.tearDown() + overlayModule?.tearDown() + tabModules.forEach { $0.module.tearDown() } } diff --git a/ElementX/Sources/Mocks/JoinedRoomProxyMock.swift b/ElementX/Sources/Mocks/JoinedRoomProxyMock.swift index 01235223a..38361fab9 100644 --- a/ElementX/Sources/Mocks/JoinedRoomProxyMock.swift +++ b/ElementX/Sources/Mocks/JoinedRoomProxyMock.swift @@ -116,6 +116,7 @@ extension JoinedRoomProxyMock { unbanUserReturnValue = .success(()) let widgetDriver = ElementCallWidgetDriverMock() + widgetDriver.widgetID = UUID().uuidString widgetDriver.messagePublisher = .init() widgetDriver.actions = PassthroughSubject().eraseToAnyPublisher() diff --git a/UnitTests/Sources/NavigationSplitCoordinatorTests.swift b/UnitTests/Sources/NavigationSplitCoordinatorTests.swift index 8f4d971cc..21868e341 100644 --- a/UnitTests/Sources/NavigationSplitCoordinatorTests.swift +++ b/UnitTests/Sources/NavigationSplitCoordinatorTests.swift @@ -171,6 +171,28 @@ struct NavigationSplitCoordinatorTests { } } + // MARK: - Stopping + + @Test + func stoppingTearsDownTheSidebarDetailAndEverythingPresentedOverThem() { + let sidebarCoordinator = StopTrackingCoordinator() + let detailCoordinator = StopTrackingCoordinator() + let sheetCoordinator = StopTrackingCoordinator() + let fullScreenCoverCoordinator = StopTrackingCoordinator() + + navigationSplitCoordinator.setSidebarCoordinator(sidebarCoordinator) + navigationSplitCoordinator.setDetailCoordinator(detailCoordinator) + navigationSplitCoordinator.setSheetCoordinator(sheetCoordinator) + navigationSplitCoordinator.setFullScreenCoverCoordinator(fullScreenCoverCoordinator) + + navigationSplitCoordinator.stop() + + #expect(sidebarCoordinator.didStop) + #expect(detailCoordinator.didStop) + #expect(sheetCoordinator.didStop) + #expect(fullScreenCoverCoordinator.didStop) + } + // MARK: - Advanced @Test @@ -348,3 +370,11 @@ private func assertCoordinatorsEqual(_ lhs: CoordinatorProtocol?, _ rhs: Coordin private class SomeTestCoordinator: CoordinatorProtocol { let id = UUID() } + +private class StopTrackingCoordinator: CoordinatorProtocol { + private(set) var didStop = false + + func stop() { + didStop = true + } +} diff --git a/UnitTests/Sources/NavigationStackCoordinatorTests.swift b/UnitTests/Sources/NavigationStackCoordinatorTests.swift index e75882dcc..ea066e452 100644 --- a/UnitTests/Sources/NavigationStackCoordinatorTests.swift +++ b/UnitTests/Sources/NavigationStackCoordinatorTests.swift @@ -203,6 +203,28 @@ struct NavigationStackCoordinatorTests { } } + // MARK: - Stopping + + @Test + func stoppingTearsDownTheStackAndEverythingPresentedOverIt() { + let rootCoordinator = StopTrackingCoordinator() + let pushedCoordinator = StopTrackingCoordinator() + let sheetCoordinator = StopTrackingCoordinator() + let fullScreenCoverCoordinator = StopTrackingCoordinator() + + navigationStackCoordinator.setRootCoordinator(rootCoordinator) + navigationStackCoordinator.push(pushedCoordinator) + navigationStackCoordinator.setSheetCoordinator(sheetCoordinator) + navigationStackCoordinator.setFullScreenCoverCoordinator(fullScreenCoverCoordinator) + + navigationStackCoordinator.stop() + + #expect(rootCoordinator.didStop) + #expect(pushedCoordinator.didStop) + #expect(sheetCoordinator.didStop) + #expect(fullScreenCoverCoordinator.didStop) + } + // MARK: - Private private func assertCoordinatorsEqual(_ lhs: CoordinatorProtocol?, _ rhs: CoordinatorProtocol?) { @@ -219,3 +241,11 @@ struct NavigationStackCoordinatorTests { private class SomeTestCoordinator: CoordinatorProtocol { let id = UUID() } + +private class StopTrackingCoordinator: CoordinatorProtocol { + private(set) var didStop = false + + func stop() { + didStop = true + } +} diff --git a/UnitTests/Sources/NavigationTabCoordinatorTests.swift b/UnitTests/Sources/NavigationTabCoordinatorTests.swift index ed9590a2b..81b1b3182 100644 --- a/UnitTests/Sources/NavigationTabCoordinatorTests.swift +++ b/UnitTests/Sources/NavigationTabCoordinatorTests.swift @@ -181,6 +181,28 @@ struct NavigationTabCoordinatorTests { } } + // MARK: - Stopping + + @Test + mutating func stoppingTearsDownTheTabsAndEverythingPresentedOverThem() { + let tabCoordinator = StopTrackingCoordinator() + let sheetCoordinator = StopTrackingCoordinator() + let fullScreenCoverCoordinator = StopTrackingCoordinator() + let overlayCoordinator = StopTrackingCoordinator() + + navigationTabCoordinator.setTabs([.init(coordinator: tabCoordinator, details: .init(tag: .tab, title: "Tab", icon: \.help, selectedIcon: \.helpSolid))]) + navigationTabCoordinator.setSheetCoordinator(sheetCoordinator) + navigationTabCoordinator.setFullScreenCoverCoordinator(fullScreenCoverCoordinator) + navigationTabCoordinator.setOverlayCoordinator(overlayCoordinator) + + navigationTabCoordinator.stop() + + #expect(tabCoordinator.didStop) + #expect(sheetCoordinator.didStop) + #expect(fullScreenCoverCoordinator.didStop) + #expect(overlayCoordinator.didStop) + } + // MARK: - Private private func assertCoordinatorsEqual(_ lhs: CoordinatorProtocol?, _ rhs: CoordinatorProtocol?) { @@ -208,3 +230,11 @@ struct NavigationTabCoordinatorTests { private class SomeTestCoordinator: CoordinatorProtocol { let id = UUID() } + +private class StopTrackingCoordinator: CoordinatorProtocol { + private(set) var didStop = false + + func stop() { + didStop = true + } +} diff --git a/UnitTests/Sources/UserSessionFlowCoordinatorTests.swift b/UnitTests/Sources/UserSessionFlowCoordinatorTests.swift index 3a0a23651..fb45ebdfb 100644 --- a/UnitTests/Sources/UserSessionFlowCoordinatorTests.swift +++ b/UnitTests/Sources/UserSessionFlowCoordinatorTests.swift @@ -8,7 +8,7 @@ import Combine @testable import ElementX -import Foundation +import SwiftUI import Testing @MainActor @@ -20,6 +20,7 @@ struct UserSessionFlowCoordinatorTests { private let networkReachabilitySubject: CurrentValueSubject = .init(.reachable) private let homeserverReachabilitySubject: CurrentValueSubject = .init(.reachable) + private let elementCallService = ElementCallServiceMock(.init()) private var tabCoordinator: NavigationTabCoordinator? { rootCoordinator?.rootCoordinator as? NavigationTabCoordinator } @@ -39,13 +40,14 @@ struct UserSessionFlowCoordinatorTests { init() async throws { rootCoordinator = NavigationRootCoordinator() - let clientProxy = ClientProxyMock(.init(userID: "hi@bob", roomSummaryProvider: RoomSummaryProviderMock(.init(state: .loaded(.mockRooms))))) + let clientProxy = ClientProxyMock(.init(userID: "hi@bob", deviceID: "call-device-id", roomSummaryProvider: RoomSummaryProviderMock(.init(state: .loaded(.mockRooms))))) clientProxy.homeserverReachabilityPublisher = homeserverReachabilitySubject.asCurrentValuePublisher() let networkMonitor = NetworkMonitorMock(.init()) networkMonitor.reachabilityPublisher = networkReachabilitySubject.asCurrentValuePublisher() let appMediator = AppMediatorMock(.init()) appMediator.networkMonitor = networkMonitor + (appMediator.windowManager as? WindowManagerMock)?.mainWindow = UIWindow() userIndicatorController = UserIndicatorControllerMock() let userSession = UserSessionMock(.init(clientProxy: clientProxy)) @@ -53,7 +55,7 @@ struct UserSessionFlowCoordinatorTests { let flowParameters = CommonFlowParameters(userSession: userSession, availableSessionsPublisher: .init([.init(userSession: userSession)]), bugReportService: BugReportServiceMock(.init()), - elementCallService: ElementCallServiceMock(.init()), + elementCallService: elementCallService, timelineControllerFactory: TimelineControllerFactoryMock(.init()), emojiProvider: EmojiProvider(userSettings: userSession.userSettings), linkMetadataProvider: LinkMetadataProvider(), @@ -84,6 +86,20 @@ struct UserSessionFlowCoordinatorTests { #expect((tabCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is SettingsScreenCoordinator) } + @Test + func replacingTheSessionScreensEndsTheWebViewCall() async { + // E.g. clearing the cache or signing out during a call: the call screen goes with the screens. + await waitForConfirmation { confirmation in + elementCallService.setupCallSessionRoomIDRoomDisplayNameIsVideoClosure = { _, _, _ in confirmation() } + userSessionFlowCoordinator.handleAppRoute(.call(roomID: "1", isVoiceCall: false), animated: false) + } + + rootCoordinator.setRootCoordinator(nil) + + #expect(elementCallService.tearDownCallSessionRoomIDReceivedRoomID == "1", + "A call left ongoing would make every later call count as busy") + } + @Test mutating func roomPresentation() async throws { try await process(route: .room(roomID: "1", via: []), expectedChatsState: .roomList(detailState: .room(roomID: "1")))