-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience methods for presenting BottomSheet
- Loading branch information
1 parent
9e58dcd
commit 88e5a21
Showing
3 changed files
with
104 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Sources/BottomSheet/Core/Extensions/UIViewController+Convenience.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// UIViewController+Convenience.swift | ||
// BottomSheetDemo | ||
// | ||
// Created by Mikhail Maslo on 15.08.2022. | ||
// Copyright © 2022 Joom. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public final class DefaultBottomSheetPresentationControllerFactory: BottomSheetPresentationControllerFactory { | ||
// MARK: - Nested types | ||
|
||
public typealias DismissalHandlerProvider = () -> BottomSheetModalDismissalHandler | ||
|
||
// MARK: - Public properties | ||
|
||
private let dismissalHandlerProvider: DismissalHandlerProvider | ||
|
||
// MARK: - Init | ||
|
||
public init(dismissalHandlerProvider: @escaping DismissalHandlerProvider) { | ||
self.dismissalHandlerProvider = dismissalHandlerProvider | ||
} | ||
|
||
// MARK: - BottomSheetPresentationControllerFactory | ||
|
||
public func makeBottomSheetPresentationController( | ||
presentedViewController: UIViewController, | ||
presentingViewController: UIViewController? | ||
) -> BottomSheetPresentationController { | ||
BottomSheetPresentationController( | ||
presentedViewController: presentedViewController, | ||
presentingViewController: presentingViewController, | ||
dismissalHandler: dismissalHandlerProvider() | ||
) | ||
} | ||
} | ||
|
||
public final class DefaultBottomSheetModalDismissalHandler: BottomSheetModalDismissalHandler { | ||
// MARK: - Private properties | ||
|
||
private weak var presentingViewController: UIViewController? | ||
private let dismissCompletion: (() -> Void)? | ||
|
||
// MARK: - Init | ||
|
||
init( | ||
presentingViewController: UIViewController?, | ||
dismissCompletion: (() -> Void)? | ||
) { | ||
self.presentingViewController = presentingViewController | ||
self.dismissCompletion = dismissCompletion | ||
} | ||
|
||
// MARK: - BottomSheetModalDismissalHandler | ||
|
||
public let canBeDismissed: Bool = true | ||
|
||
public func performDismissal(animated: Bool) { | ||
presentingViewController?.presentedViewController?.dismiss(animated: animated, completion: dismissCompletion) | ||
} | ||
} | ||
|
||
public extension UIViewController { | ||
private(set) var bottomSheetTransitionDelegate: UIViewControllerTransitioningDelegate? { | ||
get { objc_getAssociatedObject(self, &Self.bottomSheetTransitionDelegateKey) as? UIViewControllerTransitioningDelegate } | ||
set { objc_setAssociatedObject(self, &Self.bottomSheetTransitionDelegateKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } | ||
} | ||
|
||
private static var bottomSheetTransitionDelegateKey: UInt8 = 0 | ||
|
||
func presentBottomSheet(viewController: UIViewController) { | ||
weak var presentingViewController = self | ||
weak var currentBottomSheetTransitionDelegate: UIViewControllerTransitioningDelegate? | ||
let presentationControllerFactory = DefaultBottomSheetPresentationControllerFactory { | ||
DefaultBottomSheetModalDismissalHandler(presentingViewController: presentingViewController) { | ||
if currentBottomSheetTransitionDelegate === presentingViewController?.bottomSheetTransitionDelegate { | ||
presentingViewController?.bottomSheetTransitionDelegate = nil | ||
} | ||
} | ||
} | ||
bottomSheetTransitionDelegate = BottomSheetTransitioningDelegate( | ||
presentationControllerFactory: presentationControllerFactory | ||
) | ||
currentBottomSheetTransitionDelegate = bottomSheetTransitionDelegate | ||
viewController.transitioningDelegate = bottomSheetTransitionDelegate | ||
viewController.modalPresentationStyle = .custom | ||
present(viewController, animated: true, completion: nil) | ||
} | ||
} |