Skip to content

Instantly share code, notes, and snippets.

@hassanvfx
Created October 4, 2019 21:56
Show Gist options
  • Save hassanvfx/1a46655c4d6718cdcb15dcc2b01fd242 to your computer and use it in GitHub Desktop.
Save hassanvfx/1a46655c4d6718cdcb15dcc2b01fd242 to your computer and use it in GitHub Desktop.
extension MoviesCollectionView{
func setupRx(){
setupRxCollectionView()
setupRxDrivers()
}
func setupRxCollectionView(){
let dataSubject = PublishSubject<[MoviesSection]>()
let dataSource = RxCollectionViewSectionedAnimatedDataSource<MoviesSection>(
configureCell:{ dataSource, tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(withReuseIdentifier: self.cellName(), for: indexPath) as! MovieViewCell
cell.setupWithMovie(item)
return cell
})
dataSource.canMoveItemAtIndexPath = { dataSource, indexPath in
return true
}
dataSubject
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
self.dataSubject = dataSubject
self.dataSource = dataSource
}
func setupRxDrivers(){
movies
.driver
.throttle(.milliseconds(500), latest: true)
.drive(onNext:{ [weak self] movies in
guard let this = self else { return }
this.captureSearchString()
this.isLoading.accept(false)
this.dataSubject.onNext(this.dataFiltered())
}).disposed(by: disposeBag)
isLoading
.drive(refreshControl.rx.isRefreshing)
.disposed(by: disposeBag)
currentPage
.drive(onNext:{ [weak self] currentPage in
self?.loadMovies()
})
.disposed(by: disposeBag)
searchString
.drive( onNext:{ [weak self] searchString in
guard let this = self else { return }
this.dataSubject.onNext(this.dataFiltered())
})
.disposed(by: disposeBag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment