From a5f0b4fcafa7d2ef6bcebc607bd4bae8ab66fc39 Mon Sep 17 00:00:00 2001 From: Greg Pardo Date: Thu, 15 Oct 2015 10:36:35 -0400 Subject: [PATCH] References #193 Allows for nil on focus (Focus can be elsewhere) --- RxCocoa/iOS/UICollectionView+Rx.swift | 30 ++++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/RxCocoa/iOS/UICollectionView+Rx.swift b/RxCocoa/iOS/UICollectionView+Rx.swift index d5ba452c..b0de1120 100644 --- a/RxCocoa/iOS/UICollectionView+Rx.swift +++ b/RxCocoa/iOS/UICollectionView+Rx.swift @@ -161,11 +161,11 @@ extension UICollectionView { /** Reactive wrapper for `delegate` message `collectionView:didUpdateFocusInContext:withAnimationCoordinator:`. */ - public var rx_nextFocusedUpdated: ControlEvent { + public var rx_nextFocusedUpdated: ControlEvent { let source = rx_delegate.observe("collectionView:didUpdateFocusInContext:withAnimationCoordinator:") - .map { a -> NSIndexPath in + .map { a -> NSIndexPath? in let context = a[1] as! UICollectionViewFocusUpdateContext - return context.nextFocusedIndexPath! as NSIndexPath + return context.nextFocusedIndexPath! } return ControlEvent(source: source) @@ -174,11 +174,11 @@ extension UICollectionView { /** Reactive wrapper for `delegate` message `collectionView:didUpdateFocusInContext:withAnimationCoordinator:`. */ - public var rx_previousFocusedUpdated: ControlEvent { + public var rx_previousFocusedUpdated: ControlEvent { let source = rx_delegate.observe("collectionView:didUpdateFocusInContext:withAnimationCoordinator:") - .map { a -> NSIndexPath in + .map { a -> NSIndexPath? in let context = a[1] as! UICollectionViewFocusUpdateContext - return context.previouslyFocusedIndexPath! as NSIndexPath + return context.previouslyFocusedIndexPath! } return ControlEvent(source: source) @@ -190,8 +190,13 @@ extension UICollectionView { It can be only used when one of the `rx_itemsWith*` methods is used to bind observable sequence. */ - public func rx_modelForNextFocusedUpdated() -> ControlEvent { - let source: Observable = rx_nextFocusedUpdated .map { indexPath in + public func rx_modelForNextFocusedUpdated() -> ControlEvent { + let source: Observable = rx_nextFocusedUpdated .map { indexPath in + + guard let indexPath = indexPath else { + return nil + } + 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)! @@ -206,8 +211,13 @@ extension UICollectionView { It can be only used when one of the `rx_itemsWith*` methods is used to bind observable sequence. */ - public func rx_modelForPreviouslyFocusedUpdated() -> ControlEvent { - let source: Observable = rx_previousFocusedUpdated .map { indexPath in + public func rx_modelForPreviouslyFocusedUpdated() -> ControlEvent { + let source: Observable = rx_previousFocusedUpdated .map { indexPath in + + guard let indexPath = indexPath else { + return nil + } + 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)!