Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply ScrollView Delegate Methods Rx Extension In ASTableNode, ASCollectionNode #13

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,64 @@ extension Reactive where Base: ASCollectionNode {
-> Disposable {
return RxASCollectionDelegateProxy.installForwardDelegate(delegate, retainDelegate: false, onProxyForObject: self.base)
}

/// Reactive wrapper for `contentOffset`.
public var contentOffset: ControlProperty<CGPoint> {
let proxy = RxASCollectionDelegateProxy.proxy(for: base)

let bindingObserver = Binder(self.base) { collectionNode, contentOffset in
collectionNode.contentOffset = contentOffset
}

return ControlProperty(values: proxy.contentOffsetBehaviorSubject, valueSink: bindingObserver)
}

/// Reactive wrapper for delegate method `scrollViewDidScroll`
public var didScroll: ControlEvent<Void> {
let source = RxASCollectionDelegateProxy.proxy(for: base).contentOffsetPublishSubject
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewWillBeginDecelerating`
public var willBeginDecelerating: ControlEvent<Void> {
let source = delegate.methodInvoked(#selector(ASCollectionDelegate.scrollViewWillBeginDecelerating(_:))).map { _ in }
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewDidEndDecelerating`
public var didEndDecelerating: ControlEvent<Void> {
let source = delegate.methodInvoked(#selector(ASCollectionDelegate.scrollViewDidEndDecelerating(_:))).map { _ in }
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewWillBeginDragging`
public var willBeginDragging: ControlEvent<Void> {
let source = delegate.methodInvoked(#selector(ASCollectionDelegate.scrollViewWillBeginDragging(_:))).map { _ in }
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)`
public var willEndDragging: ControlEvent<WillEndDraggingEvent> {
let source = delegate.methodInvoked(#selector(ASCollectionDelegate.scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)))
.map { value -> WillEndDraggingEvent in
let velocity = try castOrThrow(CGPoint.self, value[1])
let targetContentOffsetValue = try castOrThrow(NSValue.self, value[2])

guard let rawPointer = targetContentOffsetValue.pointerValue else { throw RxCocoaError.unknown }
let typedPointer = rawPointer.bindMemory(to: CGPoint.self, capacity: MemoryLayout<CGPoint>.size)

return (velocity, typedPointer)
}
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewDidEndDragging(_:willDecelerate:)`
public var didEndDragging: ControlEvent<Bool> {
let source = delegate.methodInvoked(#selector(ASCollectionDelegate.scrollViewDidEndDragging(_:willDecelerate:))).map { value -> Bool in
return try castOrThrow(Bool.self, value[1])
}
return ControlEvent(events: source)
}

/// Reactive wrapper for `delegate` message `collectionNode(_:didSelectItemAtIndexPath:)`.
public var itemSelected: ControlEvent<IndexPath> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,56 @@ open class RxASCollectionDelegateProxy
public static func registerKnownImplementations() {
self.register { RxASCollectionDelegateProxy(collectionNode: $0) }
}

fileprivate var _contentOffsetBehaviorSubject: BehaviorSubject<CGPoint>?
fileprivate var _contentOffsetPublishSubject: PublishSubject<()>?

/// Optimized version used for observing content offset changes.
internal var contentOffsetBehaviorSubject: BehaviorSubject<CGPoint> {
if let subject = _contentOffsetBehaviorSubject {
return subject
}

let subject = BehaviorSubject<CGPoint>(value: self.collectionNode?.contentOffset ?? CGPoint.zero)
_contentOffsetBehaviorSubject = subject

return subject
}

/// Optimized version used for observing content offset changes.
internal var contentOffsetPublishSubject: PublishSubject<()> {
if let subject = _contentOffsetPublishSubject {
return subject
}

let subject = PublishSubject<()>()
_contentOffsetPublishSubject = subject

return subject
}

// MARK: delegate methods

/// For more information take a look at `DelegateProxyType`.
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let subject = _contentOffsetBehaviorSubject {
subject.on(.next(scrollView.contentOffset))
}
if let subject = _contentOffsetPublishSubject {
subject.on(.next(()))
}
self._forwardToDelegate?.scrollViewDidScroll?(scrollView)
}

deinit {
if let subject = _contentOffsetBehaviorSubject {
subject.on(.completed)
}

if let subject = _contentOffsetPublishSubject {
subject.on(.completed)
}
}
}

#endif
58 changes: 57 additions & 1 deletion RxDataSources-Texture/Classes/ASTableNode/ASTableNode+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,63 @@ extension Reactive where Base: ASTableNode {
return RxASTableDelegateProxy.installForwardDelegate(delegate, retainDelegate: false, onProxyForObject: self.base)
}

// events
/// Reactive wrapper for `contentOffset`.
public var contentOffset: ControlProperty<CGPoint> {
let proxy = RxASTableDelegateProxy.proxy(for: base)

let bindingObserver = Binder(self.base) { tableNode, contentOffset in
tableNode.contentOffset = contentOffset
}

return ControlProperty(values: proxy.contentOffsetBehaviorSubject, valueSink: bindingObserver)
}

/// Reactive wrapper for delegate method `scrollViewDidScroll`
public var didScroll: ControlEvent<Void> {
let source = RxASTableDelegateProxy.proxy(for: base).contentOffsetPublishSubject
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewWillBeginDecelerating`
public var willBeginDecelerating: ControlEvent<Void> {
let source = delegate.methodInvoked(#selector(ASTableDelegate.scrollViewWillBeginDecelerating(_:))).map { _ in }
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewDidEndDecelerating`
public var didEndDecelerating: ControlEvent<Void> {
let source = delegate.methodInvoked(#selector(ASTableDelegate.scrollViewDidEndDecelerating(_:))).map { _ in }
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewWillBeginDragging`
public var willBeginDragging: ControlEvent<Void> {
let source = delegate.methodInvoked(#selector(ASTableDelegate.scrollViewWillBeginDragging(_:))).map { _ in }
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)`
public var willEndDragging: ControlEvent<WillEndDraggingEvent> {
let source = delegate.methodInvoked(#selector(ASTableDelegate.scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)))
.map { value -> WillEndDraggingEvent in
let velocity = try castOrThrow(CGPoint.self, value[1])
let targetContentOffsetValue = try castOrThrow(NSValue.self, value[2])

guard let rawPointer = targetContentOffsetValue.pointerValue else { throw RxCocoaError.unknown }
let typedPointer = rawPointer.bindMemory(to: CGPoint.self, capacity: MemoryLayout<CGPoint>.size)

return (velocity, typedPointer)
}
return ControlEvent(events: source)
}

/// Reactive wrapper for delegate method `scrollViewDidEndDragging(_:willDecelerate:)`
public var didEndDragging: ControlEvent<Bool> {
let source = delegate.methodInvoked(#selector(ASTableDelegate.scrollViewDidEndDragging(_:willDecelerate:))).map { value -> Bool in
return try castOrThrow(Bool.self, value[1])
}
return ControlEvent(events: source)
}

/**
Reactive wrapper for `delegate` message `tableNode:didSelectRowAtIndexPath:`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,56 @@ open class RxASTableDelegateProxy
public static func registerKnownImplementations() {
self.register { RxASTableDelegateProxy(tableNode: $0) }
}

fileprivate var _contentOffsetBehaviorSubject: BehaviorSubject<CGPoint>?
fileprivate var _contentOffsetPublishSubject: PublishSubject<()>?

/// Optimized version used for observing content offset changes.
internal var contentOffsetBehaviorSubject: BehaviorSubject<CGPoint> {
if let subject = _contentOffsetBehaviorSubject {
return subject
}

let subject = BehaviorSubject<CGPoint>(value: self.tableNode?.contentOffset ?? CGPoint.zero)
_contentOffsetBehaviorSubject = subject

return subject
}

/// Optimized version used for observing content offset changes.
internal var contentOffsetPublishSubject: PublishSubject<()> {
if let subject = _contentOffsetPublishSubject {
return subject
}

let subject = PublishSubject<()>()
_contentOffsetPublishSubject = subject

return subject
}

// MARK: delegate methods

/// For more information take a look at `DelegateProxyType`.
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let subject = _contentOffsetBehaviorSubject {
subject.on(.next(scrollView.contentOffset))
}
if let subject = _contentOffsetPublishSubject {
subject.on(.next(()))
}
self._forwardToDelegate?.scrollViewDidScroll?(scrollView)
}

deinit {
if let subject = _contentOffsetBehaviorSubject {
subject.on(.completed)
}

if let subject = _contentOffsetPublishSubject {
subject.on(.completed)
}
}
}

#endif