Files
bundesmessenger-ios/Riot/Modules/CreateRoom/CreateRoomCoordinator.swift
T
Gil Eluard 408527e106 SP2: Adding Rooms to Spaces #523o
- update after review
2022-02-15 22:23:12 +01:00

118 lines
4.4 KiB
Swift

// File created from FlowTemplate
// $ createRootCoordinator.sh CreateRoom CreateRoom EnterNewRoomDetails
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
@objcMembers
final class CreateRoomCoordinator: CreateRoomCoordinatorType {
// MARK: - Properties
// MARK: Private
private let navigationRouter: NavigationRouterType
private let tabRouter: TabbedRouterType
private let session: MXSession
private(set) var parentSpace: MXSpace?
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
weak var delegate: CreateRoomCoordinatorDelegate?
// MARK: - Setup
init(session: MXSession, parentSpace: MXSpace?) {
self.navigationRouter = NavigationRouter(navigationController: RiotNavigationController())
let segmentedController = RiotSegmentedController.instantiate()
segmentedController.title = VectorL10n.spacesAddRoom
self.tabRouter = SegmentedRouter(segmentedController: segmentedController)
self.session = session
self.parentSpace = parentSpace
}
// MARK: - Public methods
func start() {
let createRoomCoordinator = self.createEnterNewRoomDetailsCoordinator()
createRoomCoordinator.start()
self.add(childCoordinator: createRoomCoordinator)
if let parentSpace = self.parentSpace, #available(iOS 14, *) {
let roomSelectionCoordinator = self.createRoomSelectorCoordinator(parentSpace: parentSpace)
roomSelectionCoordinator.completion = { [weak self] result in
guard let self = self else {
return
}
switch result {
case .done(let selectedItemIds):
self.delegate?.createRoomCoordinator(self, didAddRoomsWithIds: selectedItemIds)
default:
self.delegate?.createRoomCoordinatorDidCancel(self)
}
}
roomSelectionCoordinator.start()
self.add(childCoordinator: roomSelectionCoordinator)
self.tabRouter.tabs = [
TabbedRouterTab(title: VectorL10n.newWord, icon: nil, module: createRoomCoordinator),
TabbedRouterTab(title: VectorL10n.existing, icon: nil, module: roomSelectionCoordinator)
]
self.navigationRouter.setRootModule(self.tabRouter)
} else {
self.navigationRouter.setRootModule(createRoomCoordinator)
}
}
func toPresentable() -> UIViewController {
return self.navigationRouter.toPresentable()
}
// MARK: - Private methods
private func createEnterNewRoomDetailsCoordinator() -> EnterNewRoomDetailsCoordinator {
let coordinator = EnterNewRoomDetailsCoordinator(session: self.session, parentSpace: self.parentSpace)
coordinator.delegate = self
return coordinator
}
@available(iOS 14.0, *)
private func createRoomSelectorCoordinator(parentSpace: MXSpace) -> MatrixItemChooserCoordinator {
let paramaters = MatrixItemChooserCoordinatorParameters(session: self.session, viewProvider: AddRoomSelectorViewProvider(), itemsProcessor: AddRoomItemsProcessor(parentSpace: parentSpace))
let coordinator = MatrixItemChooserCoordinator(parameters: paramaters)
return coordinator
}
}
// MARK: - EnterNewRoomDetailsCoordinatorDelegate
extension CreateRoomCoordinator: EnterNewRoomDetailsCoordinatorDelegate {
func enterNewRoomDetailsCoordinator(_ coordinator: EnterNewRoomDetailsCoordinatorType, didCreateNewRoom room: MXRoom) {
self.delegate?.createRoomCoordinator(self, didCreateNewRoom: room)
}
func enterNewRoomDetailsCoordinatorDidCancel(_ coordinator: EnterNewRoomDetailsCoordinatorType) {
self.delegate?.createRoomCoordinatorDidCancel(self)
}
}