Skip to content

Commit

Permalink
Merge pull request #2 from ntnthien/feat/xcode12
Browse files Browse the repository at this point in the history
Feat/xcode12
  • Loading branch information
luiyen authored May 27, 2021
2 parents 2aa85eb + e38887e commit 5d7d292
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import RxSwift
import Parchment
import RxCocoa

public extension Reactive where Base: PagingViewController<PagingIndexItem> {
public extension Reactive where Base: PagingViewController {
func items<Sequence: Swift.Sequence, Source: ObservableType>
(_ source: Source)
-> (_ pagingFactory: @escaping (PagingViewController<PagingIndexItem>, Int, Sequence.Element) -> (UIViewController, String))
-> (_ pagingFactory: @escaping (PagingViewController, Int, Sequence.Element) -> (UIViewController, String))
-> Disposable
where Source.Element == Sequence {
return { pagingFactory in
Expand All @@ -37,12 +37,12 @@ public extension Reactive where Base: PagingViewController<PagingIndexItem> {
}
}
}
extension Reactive where Base: PagingViewController<PagingIndexItem> {
extension Reactive where Base: PagingViewController {
/**
Reactive wrapper for `dataSource`.
For more information take a look at `DelegateProxyType` protocol documentation.
*/
public var dataSource: DelegateProxy<PagingViewController<PagingIndexItem>, PagingViewControllerDataSource> {
public var dataSource: DelegateProxy<PagingViewController, PagingViewControllerDataSource> {
return RxPagingViewControllerDataSourceProxy.proxy(for: base)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public protocol RxParchmentDataSourceType {
///
/// - parameter pageViewController: Bound picker view.
/// - parameter observedEvent: Event
func pagingViewController<T>(_ pageViewController: PagingViewController<T>, observedEvent: Event<Element>)
func pagingViewController(_ pageViewController: PagingViewController, observedEvent: Event<Element>)
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,96 +13,91 @@ import Parchment
import RxCocoa

// objc monkey business
class _RxParchmentReactiveArrayDataSource
: NSObject
, PagingViewControllerDataSource {
func _numberOfViewControllers<T>(in pagingViewController: PagingViewController<T>) -> Int where T : PagingItem, T : Comparable, T : Hashable {
return 0
}

func numberOfViewControllers<T>(in pagingViewController: PagingViewController<T>) -> Int where T : PagingItem, T : Comparable, T : Hashable {
return _numberOfViewControllers(in: pagingViewController)
}

func _pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable {
return UIViewController()
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable {
return _pagingViewController(pagingViewController, viewControllerForIndex: index)
}

func _pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable {
return PagingIndexItem(index: 0, title: "") as! T
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, 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<Sequence: Swift.Sequence>
: RxParchmentReactiveArrayDataSource<Sequence.Element>
, RxParchmentDataSourceType {

: RxParchmentReactiveArrayDataSource<Sequence.Element>
, RxParchmentDataSourceType {
typealias Element = Sequence

override init(pagingFactory: @escaping PagingFactory) {
super.init(pagingFactory: pagingFactory)
}

func pagingViewController<T>(_ pagingViewController : PagingViewController<T>, observedEvent: Event<Sequence>) where T : PagingItem, T : Comparable, T : Hashable {
Binder(self) { pagingViewControllerDataSource, sectionModels in
let sections = Array(sectionModels)
pagingViewControllerDataSource.pagingViewController(pagingViewController as! PagingViewController<PagingIndexItem>, observedElements: sections)
}.on(observedEvent)
}
func pagingViewController(_ pagingViewController : PagingViewController, observedEvent: Event<Sequence>) {
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<Element>
: _RxParchmentReactiveArrayDataSource
, SectionedViewDataSourceType {
typealias PagingFactory = (PagingViewController<PagingIndexItem>, 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 {
throw RxCocoaError.itemsNotYetBound(object: self)
}
return item
}

let pagingFactory: PagingFactory

init(pagingFactory: @escaping PagingFactory) {
self.pagingFactory = pagingFactory
}

override func _pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable {
let title = pagingFactory(pagingViewController as! PagingViewController<PagingIndexItem>, index, itemModels![index]).title
return PagingIndexItem(index: index, title: title) as! T
}
override func _pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable {
return pagingFactory(pagingViewController as! PagingViewController<PagingIndexItem>, index, itemModels![index]).controller
}
override func _numberOfViewControllers<T>(in pagingViewController: PagingViewController<T>) -> Int where T : PagingItem, T : Comparable, T : Hashable {
return itemModels?.count ?? 0
}


// reactive
func pagingViewController(_ pagingViewController: PagingViewController<PagingIndexItem>, 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
10 changes: 5 additions & 5 deletions RxParchment/Sources/RxHelper/ParchmentDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import RxCocoa
extension ObservableType {
func subscribeProxyDataSource<DelegateProxy: DelegateProxyType>(ofObject object: DelegateProxy.ParentObject, dataSource: DelegateProxy.Delegate, retainDataSource: Bool, binding: @escaping (DelegateProxy, Event<Element>) -> Void)
-> Disposable
where DelegateProxy.ParentObject: PagingViewController<PagingIndexItem> {
where DelegateProxy.ParentObject: PagingViewController {

let proxy = DelegateProxy.proxy(for: object)

Expand All @@ -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<Element>) 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)))")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,64 @@ extension PagingViewController: HasDataSource{
private let pagingViewDataSourceNotSet = PagingViewDataSourceNotSet()

private final class PagingViewDataSourceNotSet
: NSObject
, PagingViewControllerDataSource {

func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemForIndex index: Int) -> T where T : PagingItem, T : Comparable, T : Hashable {
return PagingIndexItem(index: index, title: "") as! T
}
func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable {
rxAbstractMethod(message: dataSourceNotSet)
}

func numberOfViewControllers<T>(in pagingViewController: PagingViewController<T>) -> 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<PagingViewController<PagingIndexItem>, PagingViewControllerDataSource>
, DelegateProxyType
, PagingViewControllerDataSource {


: DelegateProxy<PagingViewController, PagingViewControllerDataSource>
, 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<PagingIndexItem>?

public weak private(set) var pagingViewController: PagingViewController?
/// - parameter tableView: Parent object for delegate proxy.
public init(pagingViewController: PagingViewController<PagingIndexItem>) {
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<T>(in pagingViewController: PagingViewController<T>) -> Int where T : PagingItem, T : Comparable, T : Hashable {
return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).numberOfViewControllers(in: pagingViewController)
}

public func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController where T : PagingItem, T : Comparable, T : Hashable {
return (_requiredMethodsDataSource ?? pagingViewDataSourceNotSet).pagingViewController(pagingViewController, viewControllerForIndex: index)
}

public func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, 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<T>(_ 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

0 comments on commit 5d7d292

Please sign in to comment.