Implemented the SheetRouter router in the Coordination library.
This commit is contained in:
parent
0fb3fcfee0
commit
558bedb57e
103
Libraries/Coordination/Kit/Sources/Routers/SheetRouter.swift
Normal file
103
Libraries/Coordination/Kit/Sources/Routers/SheetRouter.swift
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
//
|
||||||
|
// SheetRouter.swift
|
||||||
|
// ReviewsCoordinationKit
|
||||||
|
//
|
||||||
|
// Created by Javier Cicchelli on 21/03/2024.
|
||||||
|
// Copyright © 2024 Röck+Cöde. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import ReviewsUIKit
|
||||||
|
import UIKit
|
||||||
|
|
||||||
|
public class SheetRouter: BaseNavigationRouter {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
public unowned let parentViewController: UIViewController
|
||||||
|
|
||||||
|
// MARK: Initialisers
|
||||||
|
public init(parentViewController: UIViewController) {
|
||||||
|
self.parentViewController = parentViewController
|
||||||
|
|
||||||
|
super.init(navigationController: .init())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Router
|
||||||
|
extension SheetRouter: Router {
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
public func present(
|
||||||
|
_ viewController: UIViewController,
|
||||||
|
animated: Bool,
|
||||||
|
onDismiss: Router.OnDismissClosure?
|
||||||
|
) {
|
||||||
|
onDismissForViewController[viewController] = onDismiss
|
||||||
|
|
||||||
|
if navigationController.viewControllers.isEmpty {
|
||||||
|
presentModally(
|
||||||
|
viewController,
|
||||||
|
animated: animated
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
navigationController.pushViewController(
|
||||||
|
viewController,
|
||||||
|
animated: animated
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func dismiss(animated: Bool) {
|
||||||
|
guard let firstViewController = navigationController.viewControllers.first else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
performOnDismiss(for: firstViewController)
|
||||||
|
|
||||||
|
parentViewController.dismiss(animated: animated)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Helpers
|
||||||
|
private extension SheetRouter {
|
||||||
|
|
||||||
|
// MARK: Actions
|
||||||
|
@objc func onCancelPressed() {
|
||||||
|
guard let firstViewController = navigationController.viewControllers.first else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dismiss(animated: true)
|
||||||
|
|
||||||
|
performOnDismiss(for: firstViewController)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
func presentModally(
|
||||||
|
_ viewController: UIViewController,
|
||||||
|
animated: Bool
|
||||||
|
) {
|
||||||
|
viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(
|
||||||
|
image: .Icon.close,
|
||||||
|
style: .plain,
|
||||||
|
target: self,
|
||||||
|
action: #selector(onCancelPressed)
|
||||||
|
)
|
||||||
|
|
||||||
|
if #available(iOS 15.0, *) {
|
||||||
|
navigationController.sheetPresentationController?.detents = [.medium(), .large()]
|
||||||
|
}
|
||||||
|
|
||||||
|
navigationController.setViewControllers(
|
||||||
|
[viewController],
|
||||||
|
animated: false
|
||||||
|
)
|
||||||
|
|
||||||
|
parentViewController.present(
|
||||||
|
navigationController,
|
||||||
|
animated: animated
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -25,6 +25,7 @@ let package = Package(
|
|||||||
name: .Target.coordination.kit,
|
name: .Target.coordination.kit,
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.byName(name: .Target.foundation.kit),
|
.byName(name: .Target.foundation.kit),
|
||||||
|
.byName(name: .Target.ui.kit),
|
||||||
],
|
],
|
||||||
path: "Coordination/Kit"
|
path: "Coordination/Kit"
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user