2015-04-08 01:28:38 +03:00
//
// U I C o l l e c t i o n V i e w + R x . s w i f t
// R x C o c o a
//
// C r e a t e d b y K r u n o s l a v Z a h e r o n 4 / 2 / 1 5 .
// C o p y r i g h t ( c ) 2 0 1 5 K r u n o s l a v Z a h e r . A l l r i g h t s r e s e r v e d .
//
import Foundation
2015-08-01 16:13:26 +03:00
#if ! RX_NO_MODULE
2015-04-10 02:52:51 +03:00
import RxSwift
2015-08-01 16:13:26 +03:00
#endif
2015-05-17 20:07:24 +03:00
import UIKit
2015-04-08 01:28:38 +03:00
2015-07-03 21:57:04 +03:00
extension UICollectionView {
2015-04-08 01:28:38 +03:00
2015-07-03 21:57:04 +03:00
// f a c t o r i e s
2015-04-08 01:28:38 +03:00
2015-07-05 12:34:31 +03:00
override func rx_createDelegateProxy ( ) -> RxScrollViewDelegateProxy {
return RxCollectionViewDelegateProxy ( parentObject : self )
2015-04-08 01:28:38 +03:00
}
2015-06-29 01:54:05 +03:00
2015-07-05 12:34:31 +03:00
// p r o x i e s
public var rx_dataSource : DelegateProxy {
get {
return proxyForObject ( self ) as RxCollectionViewDataSourceProxy
}
2015-04-08 01:28:38 +03:00
}
2015-07-05 19:40:06 +03:00
// F o r m o r e d e t a i l e d e x p l a n a t i o n s , t a k e a l o o k a t ` D e l e g a t e P r o x y T y p e . s w i f t `
public func rx_setDataSource ( dataSource : UICollectionViewDataSource )
-> Disposable {
let proxy : RxCollectionViewDataSourceProxy = proxyForObject ( self )
2015-07-06 09:35:19 +03:00
return installDelegate ( proxy , delegate : dataSource , retainDelegate : false , onProxyForObject : self )
2015-07-05 19:40:06 +03:00
}
2015-07-03 21:57:04 +03:00
// d a t a s o u r c e
2015-04-08 01:28:38 +03:00
2015-07-03 21:57:04 +03:00
// R e g i s t e r s r e a c t i v e d a t a s o u r c e w i t h c o l l e c t i o n v i e w .
// D i f f e r e n c e b e t w e e n r e a c t i v e d a t a s o u r c e a n d U I C o l l e c t i o n V i e w D a t a S o u r c e i s t h a t r e a c t i v e
// h a s a d d i t i o n a l m e t h o d :
//
// ` ` `
// f u n c c o l l e c t i o n V i e w ( c o l l e c t i o n V i e w : U I C o l l e c t i o n V i e w , o b s e r v e d E v e n t : E v e n t < E l e m e n t > ) - > V o i d
// ` ` `
//
// I f y o u w a n t t o r e g i s t e r n o n r e a c t i v e d a t a s o u r c e , p l e a s e u s e ` r x _ s e t D a t a S o u r c e ` m e t h o d
2015-07-04 14:22:05 +03:00
public func rx_subscribeWithReactiveDataSource < DataSource : protocol < RxCollectionViewDataSourceType , UICollectionViewDataSource > >
2015-07-03 21:57:04 +03:00
( dataSource : DataSource )
-> Observable < DataSource . Element > -> Disposable {
2015-07-06 09:35:19 +03:00
return setProxyDataSourceForObject ( self , dataSource : dataSource , retainDataSource : false ) { ( _ : RxCollectionViewDataSourceProxy , event ) -> Void in
2015-07-05 19:40:06 +03:00
dataSource . collectionView ( self , observedEvent : event )
}
2015-04-08 01:28:38 +03:00
}
2015-07-05 19:40:06 +03:00
2015-07-03 21:57:04 +03:00
// ` r e l o a d D a t a ` - i t e m s s u b s c r i p t i o n m e t h o d s ( i t ' s a s s u m e d t h a t t h e r e i s o n e s e c t i o n , a n d i t i s t y p e d ` V o i d ` )
2015-04-08 01:28:38 +03:00
2015-07-03 21:57:04 +03:00
public func rx_subscribeItemsTo < Item >
2015-07-05 19:40:06 +03:00
( cellFactory : ( UICollectionView , Int , Item ) -> UICollectionViewCell )
2015-07-03 21:57:04 +03:00
-> Observable < [ Item ] > -> Disposable {
return { source in
let dataSource = RxCollectionViewReactiveArrayDataSource < Item > ( cellFactory : cellFactory )
return self . rx_subscribeWithReactiveDataSource ( dataSource ) ( source )
2015-04-08 01:28:38 +03:00
}
2015-07-03 21:57:04 +03:00
}
public func rx_subscribeItemsToWithCellIdentifier < Item , Cell : UICollectionViewCell >
2015-07-05 19:40:06 +03:00
( cellIdentifier : String , configureCell : ( Int , Item , Cell ) -> Void )
2015-07-03 21:57:04 +03:00
-> Observable < [ Item ] > -> Disposable {
return { source in
2015-07-05 19:40:06 +03:00
let dataSource = RxCollectionViewReactiveArrayDataSource < Item > { ( cv , i , item ) in
let indexPath = NSIndexPath ( forItem : i , inSection : 0 )
2015-07-03 21:57:04 +03:00
let cell = cv . dequeueReusableCellWithReuseIdentifier ( cellIdentifier , forIndexPath : indexPath ) as ! Cell
2015-07-05 19:40:06 +03:00
configureCell ( i , item , cell )
2015-07-03 21:57:04 +03:00
return cell
2015-04-08 01:28:38 +03:00
}
2015-07-03 21:57:04 +03:00
return self . rx_subscribeWithReactiveDataSource ( dataSource ) ( source )
2015-05-01 23:04:41 +03:00
}
2015-04-08 01:28:38 +03:00
}
2015-07-03 21:57:04 +03:00
// e v e n t s
2015-07-05 12:34:31 +03:00
public var rx_itemSelected : Observable < NSIndexPath > {
2015-07-05 19:40:06 +03:00
return rx_delegate . observe ( " collectionView:didSelectItemAtIndexPath: " )
>- map { a in
return a [ 1 ] as ! NSIndexPath
}
2015-07-03 21:57:04 +03:00
}
// t y p e d e v e n t s
2015-07-05 12:34:31 +03:00
public func rx_modelSelected < T > ( ) -> Observable < T > {
return rx_itemSelected >- map { indexPath in
2015-07-06 09:35:19 +03:00
let dataSource : RxCollectionViewReactiveArrayDataSource < T > = castOrFatalError ( self . rx_dataSource . forwardToDelegate ( ) , message : " This method only works in case one of the `rx_subscribeItemsTo` methods was used. " )
2015-04-08 01:28:38 +03:00
2015-07-03 21:57:04 +03:00
return dataSource . modelAtIndex ( indexPath . item ) !
2015-04-08 01:28:38 +03:00
}
}
2015-07-05 19:40:06 +03:00
}
// d e p r e c a t e d
extension UICollectionView {
2015-07-06 09:35:19 +03:00
@ available ( * , deprecated = 1.7 , message = " Replaced by `rx_subscribeItemsToWithCellIdentifier` " )
2015-07-05 19:40:06 +03:00
public func rx_subscribeItemsWithIdentifierTo < E , Cell where E : AnyObject , Cell : UICollectionViewCell >
( cellIdentifier : String , configureCell : ( UICollectionView , NSIndexPath , E , Cell ) -> Void )
( source : Observable < [ E ] > )
-> Disposable {
let l = rx_subscribeItemsToWithCellIdentifier ( cellIdentifier ) { ( i : Int , e : E , cell : Cell ) in
return configureCell ( self , NSIndexPath ( forItem : i , inSection : 0 ) , e , cell )
}
return l ( source )
}
2015-04-08 01:28:38 +03:00
2015-07-06 09:35:19 +03:00
@ available ( * , deprecated = 1.7 , message = " Replaced by `rx_itemSelected` " )
2015-07-05 19:40:06 +03:00
public func rx_itemTap ( ) -> Observable < ( UICollectionView , Int ) > {
return rx_itemSelected
>- map { i in
return ( self , i . item )
}
2015-04-08 01:28:38 +03:00
}
2015-07-06 09:35:19 +03:00
@ available ( * , deprecated = 1.7 , message = " Replaced by `rx_modelSelected` " )
2015-07-05 19:40:06 +03:00
public func rx_elementTap < E > ( ) -> Observable < E > {
return rx_modelSelected ( )
2015-04-08 01:28:38 +03:00
}
2015-07-06 09:35:19 +03:00
}