Last active
June 1, 2021 02:22
-
-
Save levantAJ/47e98423cd2843e38c2708eb01b2a91f to your computer and use it in GitHub Desktop.
Observe presented UIViewController is dismissed with UIModalPresentationStyle is custom
This file contains hidden or 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
extension Reactive where Base: UIViewController { | |
var presenting: Observable<UIViewController> { | |
return sentMessage(#selector(Base.present(_:animated:completion:))) | |
.compactMap({ args -> UIViewController? in | |
return args.first as? UIViewController | |
}) | |
.do(onError: { error in | |
assertionFailure("\(error)") | |
}) | |
} | |
var dismissing: Observable<Void> { | |
return sentMessage(#selector(Base.dismiss(animated:completion:))) | |
.map { _ in Void() } | |
.do(onError: { error in | |
assertionFailure("\(error)") | |
}) | |
} | |
var presentedViewControllerDismissed: Observable<UIViewController?> { | |
return makePresentingVC().flatMap({ presentedVC -> Observable<UIViewController?> in | |
let topVC: UIViewController? = (presentedVC as? UINavigationController)?.topViewController | |
let vc: UIViewController = topVC ?? presentedVC | |
return vc.rx.dismissing | |
.map({ [weak vc] _ -> UIViewController? in | |
return vc | |
}) | |
}) | |
} | |
private func makePresentingVC() -> Observable<UIViewController> { | |
if let presentedVC: UIViewController = base.presentedViewController { | |
return Observable.merge( | |
Observable<UIViewController>.just(presentedVC), | |
presenting | |
) | |
} | |
return presenting | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use