From 0534aa983a8c3ef84e64128df29ce9eba9bc8bec Mon Sep 17 00:00:00 2001 From: Greg Pardo Date: Sun, 18 Oct 2015 10:23:53 -0700 Subject: [PATCH] #193 Only implement essential functions as per kzaher's suggestions --- RxCocoa/iOS/UICollectionView+Rx.swift | 85 +++++---------------------- RxCocoa/iOS/UITableView+Rx.swift | 33 +++++++++++ 2 files changed, 48 insertions(+), 70 deletions(-) diff --git a/RxCocoa/iOS/UICollectionView+Rx.swift b/RxCocoa/iOS/UICollectionView+Rx.swift index 8c554c5f..2bd20fd1 100644 --- a/RxCocoa/iOS/UICollectionView+Rx.swift +++ b/RxCocoa/iOS/UICollectionView+Rx.swift @@ -144,28 +144,25 @@ extension UICollectionView { */ public func rx_modelSelected() -> ControlEvent { let source: Observable = rx_itemSelected .map { indexPath in - return self.rx_modelAtIndexPath(indexPath).asObservable() - }.switchLatest() + let dataSource: RxCollectionViewReactiveArrayDataSource = castOrFatalError(self.rx_dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx_itemsWith*` methods was used.") + + return dataSource.modelAtIndex(indexPath.item)! + } return ControlEvent(source: source) } - public func rx_modelAtIndexPath(indexPath: NSIndexPath?) -> ControlEvent { - let source: Observable = create { observer in - if let indexPath = indexPath { - - let dataSource: RxCollectionViewReactiveArrayDataSource = castOrFatalError(self.rx_dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx_itemsWith*` methods was used.") - - if let model = dataSource.modelAtIndex(indexPath.item) { - observer.on(.Next(model)) - } - - } - - return AnonymousDisposable { } + /** + Syncronous helper method for retrieving a model at indexPath through a reactive data source + */ + public func rx_modelAtIndexPath(indexPath: NSIndexPath) throws -> T { + let dataSource: RxCollectionViewReactiveArrayDataSource = castOrFatalError(self.rx_dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx_itemsWith*` methods was used.") + + guard let element = dataSource.modelAtIndex(indexPath.item) else { + throw rxError(.InvalidOperation, "Items not set yet.") } - return ControlEvent(source: source) + return element } } #endif @@ -174,15 +171,13 @@ extension UICollectionView { extension UICollectionView { - public typealias ContextAndAnimationCoordinator = (context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator) - /** Reactive wrapper for `delegate` message `collectionView:didUpdateFocusInContext:withAnimationCoordinator:`. */ - public var rx_didUpdateFocusInContextWithAnimationCoordinator: ControlEvent { + public var rx_didUpdateFocusInContextWithAnimationCoordinator: ControlEvent<(context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator)> { let source = rx_delegate.observe("collectionView:didUpdateFocusInContext:withAnimationCoordinator:") - .map { a -> ContextAndAnimationCoordinator in + .map { a -> (context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator) in let context = a[1] as! UIFocusUpdateContext let animationCoordinator = a[2] as! UIFocusAnimationCoordinator return (context: context, animationCoordinator: animationCoordinator) @@ -190,55 +185,5 @@ extension UICollectionView { return ControlEvent(source: source) } - - /** - Reactive wrapper for UICollectionViewFocusUpdateContext's nextFocusedIndexPath - */ - public var rx_nextFocusedUpdated: ControlEvent { - let source = rx_didUpdateFocusInContextWithAnimationCoordinator - .map { (c: ContextAndAnimationCoordinator) -> NSIndexPath? in - let context = c.context as! UICollectionViewFocusUpdateContext - return context.nextFocusedIndexPath - } - - return ControlEvent(source: source) - } - - /** - Reactive wrapper for UICollectionViewFocusUpdateContext's previouslyFocusedIndexPath - */ - public var rx_previousFocusedUpdated: ControlEvent { - let source = rx_didUpdateFocusInContextWithAnimationCoordinator - .map { (c: ContextAndAnimationCoordinator) -> NSIndexPath? in - let context = c.context as! UICollectionViewFocusUpdateContext - return context.previouslyFocusedIndexPath - } - - return ControlEvent(source: source) - } - - /** - Reactive wrapper for the next focused model - */ - public func rx_modelForNextFocusedUpdated() -> ControlEvent { - let source: Observable = rx_nextFocusedUpdated.map { indexPath in - return self.rx_modelAtIndexPath(indexPath).asObservable() - - }.switchLatest() - - return ControlEvent(source: source) - } - - /** - Reactive wrapper for the previously focused model - */ - public func rx_modelForPreviouslyFocusedUpdated() -> ControlEvent { - let source: Observable = rx_nextFocusedUpdated.map { indexPath in - return self.rx_modelAtIndexPath(indexPath).asObservable() - }.switchLatest() - - return ControlEvent(source: source) - } } - #endif diff --git a/RxCocoa/iOS/UITableView+Rx.swift b/RxCocoa/iOS/UITableView+Rx.swift index 97ca683f..28d59935 100644 --- a/RxCocoa/iOS/UITableView+Rx.swift +++ b/RxCocoa/iOS/UITableView+Rx.swift @@ -196,6 +196,39 @@ extension UITableView { return ControlEvent(source: source) } + /** + Syncronous helper method for retrieving a model at indexPath through a reactive data source + */ + public func rx_modelAtIndexPath(indexPath: NSIndexPath) throws -> T { + let dataSource: RxCollectionViewReactiveArrayDataSource = castOrFatalError(self.rx_dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx_subscribeItemsTo*` methods was used.") + + guard let element = dataSource.modelAtIndex(indexPath.item) else { + throw rxError(.InvalidOperation, "Items not set yet.") + } + + return element + } } #endif + +#if os(tvOS) + + extension UITableView { + + /** + Reactive wrapper for `delegate` message `tableView:didUpdateFocusInContext:withAnimationCoordinator:`. + */ + public var rx_didUpdateFocusInContextWithAnimationCoordinator: ControlEvent<(context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator)> { + + let source = rx_delegate.observe("tableView:didUpdateFocusInContext:withAnimationCoordinator:") + .map { a -> (context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator) in + let context = a[1] as! UIFocusUpdateContext + let animationCoordinator = a[2] as! UIFocusAnimationCoordinator + return (context: context, animationCoordinator: animationCoordinator) + } + + return ControlEvent(source: source) + } + } +#endif