Created
September 1, 2021 11:58
-
-
Save danielt1263/5595aaca09e699419fcb7814dde1453a to your computer and use it in GitHub Desktop.
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
// | |
// UIPageController+Rx.swift | |
// | |
// Created by Daniel Tartaglia on 7/11/21. | |
// Copyright © 2021 Daniel Tartaglia. MIT License. | |
// | |
import RxCocoa | |
import RxSwift | |
import UIKit | |
extension UIPageViewController: HasDelegate { } | |
class UIPageViewControllerDelegateProxy: DelegateProxy<UIPageViewController, UIPageViewControllerDelegate>, DelegateProxyType, UIPageViewControllerDelegate { | |
init(parentObject: UIPageViewController) { | |
super.init( | |
parentObject: parentObject, | |
delegateProxy: UIPageViewControllerDelegateProxy.self | |
) | |
} | |
public static func registerKnownImplementations() { | |
self.register { UIPageViewControllerDelegateProxy(parentObject: $0) } | |
} | |
func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor orientation: UIInterfaceOrientation) -> UIPageViewController.SpineLocation { | |
spineLocationForRelay.value[orientation] ?? .none | |
} | |
func pageViewControllerSupportedInterfaceOrientations(_ pageViewController: UIPageViewController) -> UIInterfaceOrientationMask { | |
supportedInterfaceOrientations.value | |
} | |
func pageViewControllerPreferredInterfaceOrientationForPresentation(_ pageViewController: UIPageViewController) -> UIInterfaceOrientation { | |
preferredInterfaceOrientationForPresentation.value | |
} | |
fileprivate let supportedInterfaceOrientations = | |
BehaviorRelay<UIInterfaceOrientationMask>(value: .allButUpsideDown) | |
fileprivate let spineLocationForRelay = BehaviorRelay<[UIInterfaceOrientation: UIPageViewController.SpineLocation]>(value: [:]) | |
fileprivate let preferredInterfaceOrientationForPresentation = BehaviorRelay<UIInterfaceOrientation>(value: UIInterfaceOrientation.portraitUpsideDown) | |
} | |
extension Reactive where Base: UIPageViewController { | |
var delegate: UIPageViewControllerDelegateProxy { | |
return UIPageViewControllerDelegateProxy.proxy(for: base) | |
} | |
var willTransitionTo: Observable<[UIViewController]> { | |
delegate.methodInvoked(#selector(UIPageViewControllerDelegate.pageViewController(_:willTransitionTo:))) | |
.map { $0[1] as! [UIViewController] } | |
} | |
var didFinishAnimating: Observable<(finished: Bool, previousViewControllers: [UIViewController], completed: Bool)> { | |
delegate.methodInvoked(#selector(UIPageViewControllerDelegate.pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:))) | |
.map { ($0[1] as! Bool, $0[2] as! [UIViewController], $0[3] as! Bool) } | |
} | |
var spineLocationFor: Binder<[UIInterfaceOrientation: UIPageViewController.SpineLocation]> { | |
Binder(delegate) { del, value in | |
del.spineLocationForRelay.accept(value) | |
} | |
} | |
var supportedInterfaceOrientations: Binder<UIInterfaceOrientationMask> { | |
Binder(delegate) { del, value in | |
del.supportedInterfaceOrientations.accept(value) | |
} | |
} | |
var preferredInterfaceOrientationForPresentation: Binder<UIInterfaceOrientation> { | |
Binder(delegate) { del, value in | |
del.preferredInterfaceOrientationForPresentation.accept(value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment