2019-04-04 14:51:28 +00:00
|
|
|
/*
|
2021-07-09 08:08:42 +00:00
|
|
|
Copyright 2021 New Vector Ltd
|
2019-04-04 14:51:28 +00:00
|
|
|
|
|
|
|
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 Foundation
|
|
|
|
|
|
|
|
@objc protocol FlowTemplateCoordinatorBridgePresenterDelegate {
|
2019-04-04 19:06:46 +00:00
|
|
|
func flowTemplateCoordinatorBridgePresenterDelegateDidComplete(_ coordinatorBridgePresenter: FlowTemplateCoordinatorBridgePresenter)
|
2019-04-04 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FlowTemplateCoordinatorBridgePresenter enables to start FlowTemplateCoordinator from a view controller.
|
|
|
|
/// This bridge is used while waiting for global usage of coordinator pattern.
|
2021-09-06 16:13:24 +00:00
|
|
|
/// **WARNING**: This class breaks the Coordinator abstraction and it has been introduced for **Objective-C compatibility only** (mainly for integration in legacy view controllers). Each bridge should be removed once the underlying Coordinator has been integrated by another Coordinator.
|
2019-04-04 14:51:28 +00:00
|
|
|
@objcMembers
|
|
|
|
final class FlowTemplateCoordinatorBridgePresenter: NSObject {
|
|
|
|
|
2021-09-06 16:13:24 +00:00
|
|
|
// MARK: - Constants
|
|
|
|
|
|
|
|
private enum NavigationType {
|
|
|
|
case present
|
|
|
|
case push
|
|
|
|
}
|
|
|
|
|
2019-04-04 14:51:28 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let session: MXSession
|
2021-09-06 16:13:24 +00:00
|
|
|
private var navigationType: NavigationType = .present
|
2019-04-04 14:51:28 +00:00
|
|
|
private var coordinator: FlowTemplateCoordinator?
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
weak var delegate: FlowTemplateCoordinatorBridgePresenterDelegate?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(session: MXSession) {
|
|
|
|
self.session = session
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
// NOTE: Default value feature is not compatible with Objective-C.
|
|
|
|
// func present(from viewController: UIViewController, animated: Bool) {
|
2019-04-05 07:34:48 +00:00
|
|
|
// self.present(from: viewController, animated: animated)
|
2019-04-04 14:51:28 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
func present(from viewController: UIViewController, animated: Bool) {
|
2021-09-06 16:13:24 +00:00
|
|
|
|
|
|
|
let flowTemplateCoordinatorParameters = FlowTemplateCoordinatorParameters(session: self.session)
|
|
|
|
|
|
|
|
let flowTemplateCoordinator = FlowTemplateCoordinator(parameters: flowTemplateCoordinatorParameters)
|
2019-04-04 14:51:28 +00:00
|
|
|
flowTemplateCoordinator.delegate = self
|
2021-05-11 22:09:28 +00:00
|
|
|
let presentable = flowTemplateCoordinator.toPresentable()
|
|
|
|
presentable.presentationController?.delegate = self
|
|
|
|
viewController.present(presentable, animated: animated, completion: nil)
|
2019-04-04 14:51:28 +00:00
|
|
|
flowTemplateCoordinator.start()
|
|
|
|
|
|
|
|
self.coordinator = flowTemplateCoordinator
|
2021-09-06 16:13:24 +00:00
|
|
|
self.navigationType = .present
|
|
|
|
}
|
|
|
|
|
|
|
|
func push(from navigationController: UINavigationController, animated: Bool) {
|
|
|
|
|
|
|
|
let navigationRouter = NavigationRouter(navigationController: navigationController)
|
|
|
|
|
|
|
|
let flowTemplateCoordinatorParameters = FlowTemplateCoordinatorParameters(session: self.session, navigationRouter: navigationRouter)
|
|
|
|
|
|
|
|
let flowTemplateCoordinator = FlowTemplateCoordinator(parameters: flowTemplateCoordinatorParameters)
|
|
|
|
flowTemplateCoordinator.delegate = self
|
|
|
|
flowTemplateCoordinator.start() // Will trigger the view controller push
|
|
|
|
|
|
|
|
self.coordinator = flowTemplateCoordinator
|
|
|
|
self.navigationType = .push
|
2019-04-04 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 06:57:58 +00:00
|
|
|
func dismiss(animated: Bool, completion: (() -> Void)?) {
|
2019-04-04 14:51:28 +00:00
|
|
|
guard let coordinator = self.coordinator else {
|
|
|
|
return
|
2019-04-24 06:57:58 +00:00
|
|
|
}
|
2021-09-06 16:13:24 +00:00
|
|
|
|
|
|
|
switch navigationType {
|
|
|
|
case .present:
|
|
|
|
// Dismiss modal
|
|
|
|
coordinator.toPresentable().dismiss(animated: animated) {
|
|
|
|
self.coordinator = nil
|
|
|
|
|
|
|
|
if let completion = completion {
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .push:
|
|
|
|
// Pop view controller from UINavigationController
|
|
|
|
guard let navigationController = coordinator.toPresentable() as? UINavigationController else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
navigationController.popViewController(animated: animated)
|
2019-04-04 14:51:28 +00:00
|
|
|
self.coordinator = nil
|
2019-04-24 06:57:58 +00:00
|
|
|
|
|
|
|
if let completion = completion {
|
|
|
|
completion()
|
|
|
|
}
|
2019-04-04 14:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - FlowTemplateCoordinatorDelegate
|
|
|
|
extension FlowTemplateCoordinatorBridgePresenter: FlowTemplateCoordinatorDelegate {
|
2021-09-06 16:21:21 +00:00
|
|
|
func flowTemplateCoordinatorDidComplete(_ coordinator: FlowTemplateCoordinatorProtocol) {
|
2019-04-04 14:51:28 +00:00
|
|
|
self.delegate?.flowTemplateCoordinatorBridgePresenterDelegateDidComplete(self)
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 22:09:28 +00:00
|
|
|
|
|
|
|
// MARK: - UIAdaptivePresentationControllerDelegate
|
|
|
|
|
|
|
|
extension FlowTemplateCoordinatorBridgePresenter: UIAdaptivePresentationControllerDelegate {
|
|
|
|
|
|
|
|
func flowTemplateCoordinatorDidComplete(_ presentationController: UIPresentationController) {
|
|
|
|
self.delegate?.flowTemplateCoordinatorBridgePresenterDelegateDidComplete(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|