mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 01:20:12 +00:00
Space flow improvements. (#4430)
* Space flow improvements - Use a state machine to select a space. - Add a SpaceFlowCoordinator for recursively presenting subspaces. - Get the spaceRoomListProxy when tapping on the space so we can indicate failures. - Fix the total room count in SpaceListScreen. * Refactor spaceRoom → spaceRoomProxy. * Add a UI test for the Spaces flow.
This commit is contained in:
@@ -22,21 +22,31 @@ class SpaceExplorerFlowCoordinator: FlowCoordinatorProtocol {
|
||||
|
||||
private let userIndicatorController: UserIndicatorControllerProtocol
|
||||
|
||||
private var spaceFlowCoordinator: SpaceFlowCoordinator?
|
||||
|
||||
enum State: StateType {
|
||||
/// The state machine hasn't started.
|
||||
case initial
|
||||
/// The root screen for this flow.
|
||||
case spaceList
|
||||
case spaceList(selectedSpaceID: String?)
|
||||
}
|
||||
|
||||
enum Event: EventType {
|
||||
/// The flow is being started.
|
||||
case start
|
||||
/// Request presentation for a particular space.
|
||||
///
|
||||
/// The space's `SpaceRoomListProxyProtocol` must be provided in the `userInfo`.
|
||||
case selectSpace
|
||||
/// The space screen has been dismissed.
|
||||
case deselectSpace
|
||||
}
|
||||
|
||||
private let stateMachine: StateMachine<State, Event>
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
|
||||
private let selectedSpaceSubject = CurrentValueSubject<String?, Never>(nil)
|
||||
|
||||
private let actionsSubject: PassthroughSubject<SpaceExplorerFlowCoordinatorAction, Never> = .init()
|
||||
var actionsPublisher: AnyPublisher<SpaceExplorerFlowCoordinatorAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
@@ -77,30 +87,79 @@ class SpaceExplorerFlowCoordinator: FlowCoordinatorProtocol {
|
||||
// MARK: - Private
|
||||
|
||||
private func configureStateMachine() {
|
||||
stateMachine.addRoutes(event: .start, transitions: [.initial => .spaceList]) { [weak self] _ in
|
||||
stateMachine.addRoutes(event: .start, transitions: [.initial => .spaceList(selectedSpaceID: nil)]) { [weak self] _ in
|
||||
self?.presentSpaceList()
|
||||
}
|
||||
|
||||
stateMachine.addRouteMapping { event, fromState, userInfo in
|
||||
guard event == .selectSpace, case .spaceList = fromState else { return nil }
|
||||
guard let spaceRoomListProxy = userInfo as? SpaceRoomListProxyProtocol else { fatalError("A space proxy must be provided.") }
|
||||
return .spaceList(selectedSpaceID: spaceRoomListProxy.spaceRoom.id)
|
||||
} handler: { [weak self] context in
|
||||
guard let self, let spaceRoomListProxy = context.userInfo as? SpaceRoomListProxyProtocol else { return }
|
||||
startSpaceFlow(spaceRoomListProxy: spaceRoomListProxy)
|
||||
}
|
||||
|
||||
stateMachine.addRouteMapping { event, fromState, _ in
|
||||
guard event == .deselectSpace, case .spaceList(.some) = fromState else { return nil }
|
||||
return .spaceList(selectedSpaceID: nil)
|
||||
} handler: { [weak self] _ in
|
||||
guard let self else { return }
|
||||
selectedSpaceSubject.send(nil)
|
||||
spaceFlowCoordinator = nil
|
||||
}
|
||||
|
||||
stateMachine.addErrorHandler { context in
|
||||
fatalError("Unexpected transition: \(context)")
|
||||
}
|
||||
}
|
||||
|
||||
private func presentSpaceList() {
|
||||
// Temporarily using the mock until the SDK is updated.
|
||||
let parameters = SpaceListScreenCoordinatorParameters(userSession: userSession, spaceServiceProxy: SpaceServiceProxyMock(.init()))
|
||||
let parameters = SpaceListScreenCoordinatorParameters(userSession: userSession,
|
||||
selectedSpaceSubject: selectedSpaceSubject.asCurrentValuePublisher(),
|
||||
userIndicatorController: userIndicatorController)
|
||||
let coordinator = SpaceListScreenCoordinator(parameters: parameters)
|
||||
coordinator.actionsPublisher.sink { [weak self] action in
|
||||
guard let self else { return }
|
||||
switch action {
|
||||
case .showSettings:
|
||||
actionsSubject.send(.showSettings)
|
||||
case .selectSpace:
|
||||
break
|
||||
coordinator.actionsPublisher
|
||||
.sink { [weak self] action in
|
||||
guard let self else { return }
|
||||
switch action {
|
||||
case .selectSpace(let spaceRoomListProxy):
|
||||
stateMachine.tryEvent(.selectSpace, userInfo: spaceRoomListProxy)
|
||||
case .showSettings:
|
||||
actionsSubject.send(.showSettings)
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
.store(in: &cancellables)
|
||||
|
||||
sidebarNavigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
}
|
||||
|
||||
private func startSpaceFlow(spaceRoomListProxy: SpaceRoomListProxyProtocol) {
|
||||
let coordinator = SpaceFlowCoordinator(spaceRoomListProxy: spaceRoomListProxy,
|
||||
spaceServiceProxy: userSession.clientProxy.spaceService,
|
||||
isChildFlow: false,
|
||||
mediaProvider: userSession.mediaProvider,
|
||||
navigationStackCoordinator: detailNavigationStackCoordinator,
|
||||
userIndicatorController: userIndicatorController)
|
||||
|
||||
coordinator.actionsPublisher
|
||||
.sink { [weak self] action in
|
||||
guard let self else { return }
|
||||
|
||||
switch action {
|
||||
case .finished:
|
||||
stateMachine.tryEvent(.deselectSpace)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
spaceFlowCoordinator = coordinator
|
||||
|
||||
if navigationSplitCoordinator.detailCoordinator !== detailNavigationStackCoordinator {
|
||||
navigationSplitCoordinator.setDetailCoordinator(detailNavigationStackCoordinator)
|
||||
}
|
||||
|
||||
coordinator.start()
|
||||
selectedSpaceSubject.send(spaceRoomListProxy.spaceRoom.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// Copyright 2025 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
import SwiftState
|
||||
|
||||
enum SpaceFlowCoordinatorAction: Equatable {
|
||||
case finished
|
||||
}
|
||||
|
||||
class SpaceFlowCoordinator: FlowCoordinatorProtocol {
|
||||
private let spaceRoomListProxy: SpaceRoomListProxyProtocol
|
||||
private let spaceServiceProxy: SpaceServiceProxyProtocol
|
||||
private let isChildFlow: Bool
|
||||
|
||||
private let mediaProvider: MediaProviderProtocol
|
||||
|
||||
private let navigationStackCoordinator: NavigationStackCoordinator
|
||||
private let userIndicatorController: UserIndicatorControllerProtocol
|
||||
|
||||
private var childSpaceFlowCoordinator: SpaceFlowCoordinator?
|
||||
|
||||
indirect enum State: StateType {
|
||||
/// The state machine hasn't started.
|
||||
case initial
|
||||
/// The root screen for this flow.
|
||||
case space
|
||||
/// A child flow is in progress.
|
||||
case presentingChild(childSpaceID: String, previousState: State)
|
||||
}
|
||||
|
||||
enum Event: EventType {
|
||||
/// The flow is being started.
|
||||
case start
|
||||
|
||||
/// Request the presentation of a child space flow.
|
||||
///
|
||||
/// The space's `SpaceRoomListProxyProtocol` must be provided in the `userInfo`.
|
||||
case startChildFlow
|
||||
/// Tidy-up the child flow after it has dismissed itself.
|
||||
case stopChildFlow
|
||||
}
|
||||
|
||||
private let stateMachine: StateMachine<State, Event>
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
|
||||
private let actionsSubject: PassthroughSubject<SpaceFlowCoordinatorAction, Never> = .init()
|
||||
var actionsPublisher: AnyPublisher<SpaceFlowCoordinatorAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(spaceRoomListProxy: SpaceRoomListProxyProtocol,
|
||||
spaceServiceProxy: SpaceServiceProxyProtocol,
|
||||
isChildFlow: Bool,
|
||||
mediaProvider: MediaProviderProtocol,
|
||||
navigationStackCoordinator: NavigationStackCoordinator,
|
||||
userIndicatorController: UserIndicatorControllerProtocol) {
|
||||
self.spaceRoomListProxy = spaceRoomListProxy
|
||||
self.spaceServiceProxy = spaceServiceProxy
|
||||
self.isChildFlow = isChildFlow
|
||||
|
||||
self.mediaProvider = mediaProvider
|
||||
|
||||
self.navigationStackCoordinator = navigationStackCoordinator
|
||||
self.userIndicatorController = userIndicatorController
|
||||
|
||||
stateMachine = .init(state: .initial)
|
||||
configureStateMachine()
|
||||
}
|
||||
|
||||
func start() {
|
||||
stateMachine.tryEvent(.start)
|
||||
}
|
||||
|
||||
func handleAppRoute(_ appRoute: AppRoute, animated: Bool) {
|
||||
// There aren't any routes to this screen yet, so clear the stacks.
|
||||
clearRoute(animated: animated)
|
||||
}
|
||||
|
||||
func clearRoute(animated: Bool) {
|
||||
switch stateMachine.state {
|
||||
case .initial:
|
||||
break
|
||||
case .space:
|
||||
if isChildFlow {
|
||||
navigationStackCoordinator.pop(animated: animated)
|
||||
} else {
|
||||
navigationStackCoordinator.setRootCoordinator(nil, animated: animated)
|
||||
}
|
||||
case .presentingChild:
|
||||
childSpaceFlowCoordinator?.clearRoute(animated: animated)
|
||||
clearRoute(animated: animated) // Re-run with the state machine back in the .space state.
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func configureStateMachine() {
|
||||
stateMachine.addRoutes(event: .start, transitions: [.initial => .space]) { [weak self] _ in
|
||||
self?.presentSpace()
|
||||
}
|
||||
|
||||
stateMachine.addRouteMapping { event, fromState, userInfo in
|
||||
guard event == .startChildFlow, case .space = fromState else { return nil }
|
||||
guard let spaceRoomListProxy = userInfo as? SpaceRoomListProxyProtocol else { fatalError("A space proxy must be provided.") }
|
||||
return .presentingChild(childSpaceID: spaceRoomListProxy.spaceRoom.id, previousState: fromState)
|
||||
} handler: { [weak self] context in
|
||||
guard let self, let spaceRoomListProxy = context.userInfo as? SpaceRoomListProxyProtocol else { return }
|
||||
startChildFlow(for: spaceRoomListProxy)
|
||||
}
|
||||
|
||||
stateMachine.addRouteMapping { event, fromState, _ in
|
||||
guard event == .stopChildFlow, case .presentingChild(_, let previousState) = fromState else { return nil }
|
||||
return previousState
|
||||
} handler: { [weak self] _ in
|
||||
guard let self else { return }
|
||||
childSpaceFlowCoordinator = nil
|
||||
}
|
||||
|
||||
stateMachine.addErrorHandler { context in
|
||||
fatalError("Unexpected transition: \(context)")
|
||||
}
|
||||
}
|
||||
|
||||
private func presentSpace() {
|
||||
let parameters = SpaceScreenCoordinatorParameters(spaceRoomListProxy: spaceRoomListProxy,
|
||||
spaceServiceProxy: spaceServiceProxy,
|
||||
mediaProvider: mediaProvider,
|
||||
userIndicatorController: userIndicatorController)
|
||||
let coordinator = SpaceScreenCoordinator(parameters: parameters)
|
||||
coordinator.actionsPublisher
|
||||
.sink { [weak self] action in
|
||||
guard let self else { return }
|
||||
switch action {
|
||||
case .selectSpace(let spaceRoomListProxy):
|
||||
stateMachine.tryEvent(.startChildFlow, userInfo: spaceRoomListProxy)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
if isChildFlow {
|
||||
navigationStackCoordinator.push(coordinator) { [weak self] in
|
||||
self?.actionsSubject.send(.finished)
|
||||
}
|
||||
} else {
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator) { [weak self] in
|
||||
self?.actionsSubject.send(.finished)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Other flows
|
||||
|
||||
private func startChildFlow(for spaceRoomListProxy: SpaceRoomListProxyProtocol) {
|
||||
let coordinator = SpaceFlowCoordinator(spaceRoomListProxy: spaceRoomListProxy,
|
||||
spaceServiceProxy: spaceServiceProxy,
|
||||
isChildFlow: true,
|
||||
mediaProvider: mediaProvider,
|
||||
navigationStackCoordinator: navigationStackCoordinator,
|
||||
userIndicatorController: userIndicatorController)
|
||||
|
||||
coordinator.actionsPublisher
|
||||
.sink { [weak self] action in
|
||||
guard let self else { return }
|
||||
|
||||
switch action {
|
||||
case .finished:
|
||||
stateMachine.tryEvent(.stopChildFlow)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
childSpaceFlowCoordinator = coordinator
|
||||
coordinator.start()
|
||||
}
|
||||
}
|
||||
@@ -271,7 +271,8 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
|
||||
.store(in: &cancellables)
|
||||
|
||||
appSettings.$spacesEnabled
|
||||
.map { $0 ? .automatic : .hidden }
|
||||
.combineLatest(userSession.clientProxy.spaceService.joinedSpacesPublisher)
|
||||
.map { $0 && !$1.isEmpty ? .automatic : .hidden }
|
||||
.weakAssign(to: \.chatsTabDetails.barVisibility, on: self)
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user