From f9bb6862de1658a9206799db07f85b51c4080052 Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Thu, 27 May 2021 11:51:54 +0700 Subject: [PATCH 1/4] Update dependencies --- Cartfile.resolved | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 3208360..5b18ec1 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,3 +1,3 @@ -github "ReactiveX/RxSwift" "5.0.1" -github "RxSwiftCommunity/RxDataSources" "4.0.1" -github "rechsteiner/Parchment" "v1.7.0" +github "ReactiveX/RxSwift" "6.1.0" +github "RxSwiftCommunity/RxDataSources" "5.0.1" +github "rechsteiner/Parchment" "v3.0.1" From e83e071d32c2f94e22e910cfd79d800fd4e71b8e Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Thu, 27 May 2021 14:02:33 +0700 Subject: [PATCH 2/4] Fix library --- .../Parchment+Rx.swift | 8 +- .../RxParchmentDataSourceType.swift | 2 +- .../RxParchmentReactiveArrayDataSource.swift | 123 +++++++++--------- .../RxHelper/ParchmentDelegateProxy.swift | 10 +- ...xPagingViewControllerDataSourceProxy.swift | 88 +++++++------ 5 files changed, 114 insertions(+), 117 deletions(-) diff --git a/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift b/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift index 441ac84..29021cd 100644 --- a/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift +++ b/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift @@ -9,10 +9,10 @@ import RxSwift import Parchment import RxCocoa -public extension Reactive where Base: PagingViewController { +public extension Reactive where Base: PagingViewController { func items (_ source: Source) - -> (_ pagingFactory: @escaping (PagingViewController, Int, Sequence.Element) -> (UIViewController, String)) + -> (_ pagingFactory: @escaping (PagingViewController, Int, Sequence.Element) -> (UIViewController, String)) -> Disposable where Source.Element == Sequence { return { pagingFactory in @@ -37,12 +37,12 @@ public extension Reactive where Base: PagingViewController { } } } -extension Reactive where Base: PagingViewController { +extension Reactive where Base: PagingViewController { /** Reactive wrapper for `dataSource`. For more information take a look at `DelegateProxyType` protocol documentation. */ - public var dataSource: DelegateProxy, PagingViewControllerDataSource> { + public var dataSource: DelegateProxy { return RxPagingViewControllerDataSourceProxy.proxy(for: base) } diff --git a/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift b/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift index 46a256d..84f68f8 100644 --- a/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift +++ b/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift @@ -20,7 +20,7 @@ public protocol RxParchmentDataSourceType { /// /// - parameter pageViewController: Bound picker view. /// - parameter observedEvent: Event - func pagingViewController(_ pageViewController: PagingViewController, observedEvent: Event) + func pagingViewController(_ pageViewController: PagingViewController, observedEvent: Event) } #endif diff --git a/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift b/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift index 0367746..6bbc36a 100644 --- a/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift +++ b/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift @@ -13,64 +13,59 @@ import Parchment import RxCocoa // objc monkey business -class _RxParchmentReactiveArrayDataSource - : NSObject -, PagingViewControllerDataSource { - func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return 0 - } - - func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return _numberOfViewControllers(in: pagingViewController) - } - - func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return UIViewController() - } - func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return _pagingViewController(pagingViewController, viewControllerForIndex: index) - } - - func _pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return PagingIndexItem(index: 0, title: "") as! T - } - func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return _pagingViewController(pagingViewController, pagingItemForIndex: index) - } - +class _RxParchmentReactiveArrayDataSource: NSObject, PagingViewControllerDataSource { + + func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return 0 + } + + func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return _numberOfViewControllers(in: pagingViewController) + } + + func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController { + return UIViewController() + } + + func pagingViewController(_ pagingViewController: PagingViewController, viewControllerAt index: Int) -> UIViewController { + return _pagingViewController(pagingViewController, viewControllerForIndex: index) + } + + func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem { + return PagingIndexItem(index: 0, title: "") + } } - class RxParchmentReactiveArrayDataSourceSequenceWrapper - : RxParchmentReactiveArrayDataSource -, RxParchmentDataSourceType { - +: RxParchmentReactiveArrayDataSource + , RxParchmentDataSourceType { + typealias Element = Sequence - + override init(pagingFactory: @escaping PagingFactory) { super.init(pagingFactory: pagingFactory) } - - func pagingViewController(_ pagingViewController : PagingViewController, observedEvent: Event) where T : PagingItem, T : Comparable, T : Hashable { - Binder(self) { pagingViewControllerDataSource, sectionModels in - let sections = Array(sectionModels) - pagingViewControllerDataSource.pagingViewController(pagingViewController as! PagingViewController, observedElements: sections) - }.on(observedEvent) - } + + func pagingViewController(_ pagingViewController : PagingViewController, observedEvent: Event) { + Binder(self) { pagingViewControllerDataSource, sectionModels in + let sections = Array(sectionModels) + pagingViewControllerDataSource.pagingViewController(pagingViewController, observedElements: sections) + }.on(observedEvent) + } } // Please take a look at `DelegateProxyType.swift` class RxParchmentReactiveArrayDataSource - : _RxParchmentReactiveArrayDataSource - , SectionedViewDataSourceType { - typealias PagingFactory = (PagingViewController, Int, Element) -> (controller: UIViewController, title: String) - +: _RxParchmentReactiveArrayDataSource + , SectionedViewDataSourceType { + typealias PagingFactory = (PagingViewController, Int, Element) -> (controller: UIViewController, title: String) + var itemModels: [Element]? - + func modelAtIndex(_ index: Int) -> Element? { return itemModels?[index] } - + func model(at indexPath: IndexPath) throws -> Any { precondition(indexPath.section == 0) guard let item = itemModels?[indexPath.item] else { @@ -78,31 +73,31 @@ class RxParchmentReactiveArrayDataSource } return item } - + let pagingFactory: PagingFactory - + init(pagingFactory: @escaping PagingFactory) { self.pagingFactory = pagingFactory } - - override func _pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - let title = pagingFactory(pagingViewController as! PagingViewController, index, itemModels![index]).title - return PagingIndexItem(index: index, title: title) as! T - } - override func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return pagingFactory(pagingViewController as! PagingViewController, index, itemModels![index]).controller - } - override func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return itemModels?.count ?? 0 - } - - - // reactive - func pagingViewController(_ pagingViewController: PagingViewController, observedElements: [Element]) { - self.itemModels = observedElements - - pagingViewController.reloadData() - } + + override func pagingViewController(_ pagingViewController: PagingViewController, pagingItemAt index: Int) -> PagingItem { + let title = pagingFactory(pagingViewController, index, itemModels![index]).title + return PagingIndexItem(index: index, title: title) + } + + override func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController { + return pagingFactory(pagingViewController, index, itemModels![index]).controller + } + override func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return itemModels?.count ?? 0 + } + + // reactive + func pagingViewController(_ pagingViewController: PagingViewController, observedElements: [Element]) { + self.itemModels = observedElements + + pagingViewController.reloadData() + } } #endif diff --git a/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift b/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift index 675c0b2..ee96d51 100644 --- a/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift +++ b/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift @@ -14,7 +14,7 @@ import RxCocoa extension ObservableType { func subscribeProxyDataSource(ofObject object: DelegateProxy.ParentObject, dataSource: DelegateProxy.Delegate, retainDataSource: Bool, binding: @escaping (DelegateProxy, Event) -> Void) -> Disposable - where DelegateProxy.ParentObject: PagingViewController { + where DelegateProxy.ParentObject: PagingViewController { let proxy = DelegateProxy.proxy(for: object) @@ -28,17 +28,17 @@ extension ObservableType { object.view.layoutIfNeeded() let subscription = self.asObservable() - .observeOn(MainScheduler()) - .catchError { error in + .observe(on: MainScheduler()) + .catch { error in bindingErrorToInterface(error) return Observable.empty() } // source can never end, otherwise it would release the subscriber, and deallocate the data source .concat(Observable.never()) - .takeUntil(object.rx.deallocated) + .take(until: object.rx.deallocated) .subscribe { [weak object] (event: RxSwift.Event) in - if let object = object { + if object != nil { // TODO: Enable assert again to prevent Proxy changed // Temporary comment out this to by pass `pod lib lint` // assert(proxy === DelegateProxy.currentDelegate(for: object), "Proxy changed from the time it was first set.\nOriginal: \(proxy)\nExisting: \(String(describing: DelegateProxy.currentDelegate(for: object)))") diff --git a/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift b/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift index bec970c..2dffc3c 100644 --- a/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift +++ b/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift @@ -24,62 +24,64 @@ extension PagingViewController: HasDataSource{ private let pagingViewDataSourceNotSet = PagingViewDataSourceNotSet() private final class PagingViewDataSourceNotSet - : NSObject - , PagingViewControllerDataSource { - - func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return PagingIndexItem(index: index, title: "") as! T - } - func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - rxAbstractMethod(message: dataSourceNotSet) - } - - func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return 0 - } +: NSObject + , PagingViewControllerDataSource { + func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem { + PagingIndexItem(index: index, title: "") + } + + func pagingViewController(_ pagingViewController: PagingViewController, viewControllerAt index: Int) -> UIViewController { + rxAbstractMethod(message: dataSourceNotSet) + } + + func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return 0 + } } /// For more information take a look at `DelegateProxyType`. open class RxPagingViewControllerDataSourceProxy - : DelegateProxy, PagingViewControllerDataSource> - , DelegateProxyType -, PagingViewControllerDataSource { - - +: DelegateProxy + , DelegateProxyType + , PagingViewControllerDataSource { + public func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController ?? PagingViewController(), pagingItemAt: index) + } + /// Typed parent object. - public weak private(set) var pagingViewController: PagingViewController? - + public weak private(set) var pagingViewController: PagingViewController? + /// - parameter tableView: Parent object for delegate proxy. - public init(pagingViewController: PagingViewController) { + public init(pagingViewController: PagingViewController) { self.pagingViewController = pagingViewController super.init(parentObject: pagingViewController, delegateProxy: RxPagingViewControllerDataSourceProxy.self) } - + // Register known implementations public static func registerKnownImplementations() { self.register { RxPagingViewControllerDataSourceProxy(pagingViewController: $0) } } - + private weak var _requiredMethodsDataSource: PagingViewControllerDataSource? = pagingViewDataSourceNotSet - + // MARK: delegate - - public func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).numberOfViewControllers(in: pagingViewController) - } - - public func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, viewControllerForIndex: index) - } - - public func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, pagingItemForIndex: index) - } - - /// For more information take a look at `DelegateProxyType`. - open override func setForwardToDelegate(_ forwardToDelegate: PagingViewControllerDataSource?, retainDelegate: Bool) { - _requiredMethodsDataSource = forwardToDelegate ?? pagingViewDataSourceNotSet - super.setForwardToDelegate(forwardToDelegate, retainDelegate: retainDelegate) - } - + + public func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).numberOfViewControllers(in: pagingViewController) + } + + public func pagingViewController(_ pagingViewController: PagingViewController, viewControllerAt index: Int) -> UIViewController { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, viewControllerAt: index) + } + + public func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, pagingItemAt: index) as! T + } + + /// For more information take a look at `DelegateProxyType`. + open override func setForwardToDelegate(_ forwardToDelegate: PagingViewControllerDataSource?, retainDelegate: Bool) { + _requiredMethodsDataSource = forwardToDelegate ?? pagingViewDataSourceNotSet + super.setForwardToDelegate(forwardToDelegate, retainDelegate: retainDelegate) + } + } #endif From 2f70ad6338587ece8a51ee52be8e9d49e215f9c6 Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Thu, 27 May 2021 11:51:54 +0700 Subject: [PATCH 3/4] Update dependencies --- Cartfile.resolved | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 30e3fd6..5b18ec1 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,3 +1,3 @@ github "ReactiveX/RxSwift" "6.1.0" github "RxSwiftCommunity/RxDataSources" "5.0.1" -github "rechsteiner/Parchment" "v1.7.0" +github "rechsteiner/Parchment" "v3.0.1" From 0a40b07fb5e904528066bfc382d0d637ec7af0cd Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Thu, 27 May 2021 14:02:33 +0700 Subject: [PATCH 4/4] Fix library --- .../Parchment+Rx.swift | 8 +- .../RxParchmentDataSourceType.swift | 2 +- .../RxParchmentReactiveArrayDataSource.swift | 123 +++++++++--------- .../RxHelper/ParchmentDelegateProxy.swift | 10 +- ...xPagingViewControllerDataSourceProxy.swift | 88 +++++++------ 5 files changed, 114 insertions(+), 117 deletions(-) diff --git a/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift b/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift index 441ac84..29021cd 100644 --- a/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift +++ b/RxParchment/Sources/PagingViewController+Rx/Parchment+Rx.swift @@ -9,10 +9,10 @@ import RxSwift import Parchment import RxCocoa -public extension Reactive where Base: PagingViewController { +public extension Reactive where Base: PagingViewController { func items (_ source: Source) - -> (_ pagingFactory: @escaping (PagingViewController, Int, Sequence.Element) -> (UIViewController, String)) + -> (_ pagingFactory: @escaping (PagingViewController, Int, Sequence.Element) -> (UIViewController, String)) -> Disposable where Source.Element == Sequence { return { pagingFactory in @@ -37,12 +37,12 @@ public extension Reactive where Base: PagingViewController { } } } -extension Reactive where Base: PagingViewController { +extension Reactive where Base: PagingViewController { /** Reactive wrapper for `dataSource`. For more information take a look at `DelegateProxyType` protocol documentation. */ - public var dataSource: DelegateProxy, PagingViewControllerDataSource> { + public var dataSource: DelegateProxy { return RxPagingViewControllerDataSourceProxy.proxy(for: base) } diff --git a/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift b/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift index 46a256d..84f68f8 100644 --- a/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift +++ b/RxParchment/Sources/PagingViewController+Rx/RxParchmentDataSourceType.swift @@ -20,7 +20,7 @@ public protocol RxParchmentDataSourceType { /// /// - parameter pageViewController: Bound picker view. /// - parameter observedEvent: Event - func pagingViewController(_ pageViewController: PagingViewController, observedEvent: Event) + func pagingViewController(_ pageViewController: PagingViewController, observedEvent: Event) } #endif diff --git a/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift b/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift index 0367746..6bbc36a 100644 --- a/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift +++ b/RxParchment/Sources/PagingViewController+Rx/RxParchmentReactiveArrayDataSource.swift @@ -13,64 +13,59 @@ import Parchment import RxCocoa // objc monkey business -class _RxParchmentReactiveArrayDataSource - : NSObject -, PagingViewControllerDataSource { - func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return 0 - } - - func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return _numberOfViewControllers(in: pagingViewController) - } - - func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return UIViewController() - } - func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return _pagingViewController(pagingViewController, viewControllerForIndex: index) - } - - func _pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return PagingIndexItem(index: 0, title: "") as! T - } - func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return _pagingViewController(pagingViewController, pagingItemForIndex: index) - } - +class _RxParchmentReactiveArrayDataSource: NSObject, PagingViewControllerDataSource { + + func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return 0 + } + + func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return _numberOfViewControllers(in: pagingViewController) + } + + func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController { + return UIViewController() + } + + func pagingViewController(_ pagingViewController: PagingViewController, viewControllerAt index: Int) -> UIViewController { + return _pagingViewController(pagingViewController, viewControllerForIndex: index) + } + + func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem { + return PagingIndexItem(index: 0, title: "") + } } - class RxParchmentReactiveArrayDataSourceSequenceWrapper - : RxParchmentReactiveArrayDataSource -, RxParchmentDataSourceType { - +: RxParchmentReactiveArrayDataSource + , RxParchmentDataSourceType { + typealias Element = Sequence - + override init(pagingFactory: @escaping PagingFactory) { super.init(pagingFactory: pagingFactory) } - - func pagingViewController(_ pagingViewController : PagingViewController, observedEvent: Event) where T : PagingItem, T : Comparable, T : Hashable { - Binder(self) { pagingViewControllerDataSource, sectionModels in - let sections = Array(sectionModels) - pagingViewControllerDataSource.pagingViewController(pagingViewController as! PagingViewController, observedElements: sections) - }.on(observedEvent) - } + + func pagingViewController(_ pagingViewController : PagingViewController, observedEvent: Event) { + Binder(self) { pagingViewControllerDataSource, sectionModels in + let sections = Array(sectionModels) + pagingViewControllerDataSource.pagingViewController(pagingViewController, observedElements: sections) + }.on(observedEvent) + } } // Please take a look at `DelegateProxyType.swift` class RxParchmentReactiveArrayDataSource - : _RxParchmentReactiveArrayDataSource - , SectionedViewDataSourceType { - typealias PagingFactory = (PagingViewController, Int, Element) -> (controller: UIViewController, title: String) - +: _RxParchmentReactiveArrayDataSource + , SectionedViewDataSourceType { + typealias PagingFactory = (PagingViewController, Int, Element) -> (controller: UIViewController, title: String) + var itemModels: [Element]? - + func modelAtIndex(_ index: Int) -> Element? { return itemModels?[index] } - + func model(at indexPath: IndexPath) throws -> Any { precondition(indexPath.section == 0) guard let item = itemModels?[indexPath.item] else { @@ -78,31 +73,31 @@ class RxParchmentReactiveArrayDataSource } return item } - + let pagingFactory: PagingFactory - + init(pagingFactory: @escaping PagingFactory) { self.pagingFactory = pagingFactory } - - override func _pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - let title = pagingFactory(pagingViewController as! PagingViewController, index, itemModels![index]).title - return PagingIndexItem(index: index, title: title) as! T - } - override func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return pagingFactory(pagingViewController as! PagingViewController, index, itemModels![index]).controller - } - override func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return itemModels?.count ?? 0 - } - - - // reactive - func pagingViewController(_ pagingViewController: PagingViewController, observedElements: [Element]) { - self.itemModels = observedElements - - pagingViewController.reloadData() - } + + override func pagingViewController(_ pagingViewController: PagingViewController, pagingItemAt index: Int) -> PagingItem { + let title = pagingFactory(pagingViewController, index, itemModels![index]).title + return PagingIndexItem(index: index, title: title) + } + + override func _pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController { + return pagingFactory(pagingViewController, index, itemModels![index]).controller + } + override func _numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return itemModels?.count ?? 0 + } + + // reactive + func pagingViewController(_ pagingViewController: PagingViewController, observedElements: [Element]) { + self.itemModels = observedElements + + pagingViewController.reloadData() + } } #endif diff --git a/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift b/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift index 675c0b2..ee96d51 100644 --- a/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift +++ b/RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift @@ -14,7 +14,7 @@ import RxCocoa extension ObservableType { func subscribeProxyDataSource(ofObject object: DelegateProxy.ParentObject, dataSource: DelegateProxy.Delegate, retainDataSource: Bool, binding: @escaping (DelegateProxy, Event) -> Void) -> Disposable - where DelegateProxy.ParentObject: PagingViewController { + where DelegateProxy.ParentObject: PagingViewController { let proxy = DelegateProxy.proxy(for: object) @@ -28,17 +28,17 @@ extension ObservableType { object.view.layoutIfNeeded() let subscription = self.asObservable() - .observeOn(MainScheduler()) - .catchError { error in + .observe(on: MainScheduler()) + .catch { error in bindingErrorToInterface(error) return Observable.empty() } // source can never end, otherwise it would release the subscriber, and deallocate the data source .concat(Observable.never()) - .takeUntil(object.rx.deallocated) + .take(until: object.rx.deallocated) .subscribe { [weak object] (event: RxSwift.Event) in - if let object = object { + if object != nil { // TODO: Enable assert again to prevent Proxy changed // Temporary comment out this to by pass `pod lib lint` // assert(proxy === DelegateProxy.currentDelegate(for: object), "Proxy changed from the time it was first set.\nOriginal: \(proxy)\nExisting: \(String(describing: DelegateProxy.currentDelegate(for: object)))") diff --git a/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift b/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift index bec970c..2dffc3c 100644 --- a/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift +++ b/RxParchment/Sources/RxProxies/RxPagingViewControllerDataSourceProxy.swift @@ -24,62 +24,64 @@ extension PagingViewController: HasDataSource{ private let pagingViewDataSourceNotSet = PagingViewDataSourceNotSet() private final class PagingViewDataSourceNotSet - : NSObject - , PagingViewControllerDataSource { - - func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return PagingIndexItem(index: index, title: "") as! T - } - func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - rxAbstractMethod(message: dataSourceNotSet) - } - - func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return 0 - } +: NSObject + , PagingViewControllerDataSource { + func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem { + PagingIndexItem(index: index, title: "") + } + + func pagingViewController(_ pagingViewController: PagingViewController, viewControllerAt index: Int) -> UIViewController { + rxAbstractMethod(message: dataSourceNotSet) + } + + func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return 0 + } } /// For more information take a look at `DelegateProxyType`. open class RxPagingViewControllerDataSourceProxy - : DelegateProxy, PagingViewControllerDataSource> - , DelegateProxyType -, PagingViewControllerDataSource { - - +: DelegateProxy + , DelegateProxyType + , PagingViewControllerDataSource { + public func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController ?? PagingViewController(), pagingItemAt: index) + } + /// Typed parent object. - public weak private(set) var pagingViewController: PagingViewController? - + public weak private(set) var pagingViewController: PagingViewController? + /// - parameter tableView: Parent object for delegate proxy. - public init(pagingViewController: PagingViewController) { + public init(pagingViewController: PagingViewController) { self.pagingViewController = pagingViewController super.init(parentObject: pagingViewController, delegateProxy: RxPagingViewControllerDataSourceProxy.self) } - + // Register known implementations public static func registerKnownImplementations() { self.register { RxPagingViewControllerDataSourceProxy(pagingViewController: $0) } } - + private weak var _requiredMethodsDataSource: PagingViewControllerDataSource? = pagingViewDataSourceNotSet - + // MARK: delegate - - public func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int where T : PagingItem, T : Comparable, T : Hashable { - return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).numberOfViewControllers(in: pagingViewController) - } - - public func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable { - return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, viewControllerForIndex: index) - } - - public func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { - return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, pagingItemForIndex: index) - } - - /// For more information take a look at `DelegateProxyType`. - open override func setForwardToDelegate(_ forwardToDelegate: PagingViewControllerDataSource?, retainDelegate: Bool) { - _requiredMethodsDataSource = forwardToDelegate ?? pagingViewDataSourceNotSet - super.setForwardToDelegate(forwardToDelegate, retainDelegate: retainDelegate) - } - + + public func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).numberOfViewControllers(in: pagingViewController) + } + + public func pagingViewController(_ pagingViewController: PagingViewController, viewControllerAt index: Int) -> UIViewController { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, viewControllerAt: index) + } + + public func pagingViewController(_ pagingViewController: PagingViewController, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable { + return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, pagingItemAt: index) as! T + } + + /// For more information take a look at `DelegateProxyType`. + open override func setForwardToDelegate(_ forwardToDelegate: PagingViewControllerDataSource?, retainDelegate: Bool) { + _requiredMethodsDataSource = forwardToDelegate ?? pagingViewDataSourceNotSet + super.setForwardToDelegate(forwardToDelegate, retainDelegate: retainDelegate) + } + } #endif