diff --git a/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinator.swift b/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinator.swift index 7f9d612260..86c87cded7 100644 --- a/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinator.swift +++ b/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinator.swift @@ -171,8 +171,16 @@ class UserSessionFlowCoordinator: CoordinatorProtocol { let coordinator = RoomScreenCoordinator(parameters: parameters) detailStackScreenCoordinator.setRootCoordinator(coordinator) - splitScreenCoordinator.setDetailCoordinator(detailStackScreenCoordinator) { [weak self] in - self?.stateMachine.processEvent(.deselectRoom) + splitScreenCoordinator.setDetailCoordinator(detailStackScreenCoordinator) { [weak self, roomIdentifier] in + guard let self else { return } + + // Move the state machine to no room selected if the room currently being dimissed + // is the same as the one selected in the state machine. + // This generally happens when popping the room screen while in a compact layout + if case let .roomList(selectedRoomId) = self.stateMachine.state, selectedRoomId == roomIdentifier { + self.stateMachine.processEvent(.deselectRoom) + self.detailStackScreenCoordinator.setRootCoordinator(nil) + } } } } diff --git a/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinatorStateMachine.swift b/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinatorStateMachine.swift index fae3ef7ac3..12d2ad5e8c 100644 --- a/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinatorStateMachine.swift +++ b/ElementX/Sources/Services/UserSession/UserSessionFlowCoordinatorStateMachine.swift @@ -65,6 +65,10 @@ class UserSessionFlowCoordinatorStateMachine { private let stateMachine: StateMachine + var state: UserSessionFlowCoordinatorStateMachine.State { + stateMachine.state + } + init() { stateMachine = StateMachine(state: .initial) configure()