Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MovieHunt blog part4. list view model
class MovieListViewModel : BaseViewModel() {
private val movieRepo: MovieRepo by inject()
private val movieCategoryEvent = BehaviorSubject.create<MovieCategory>()
private val fetchDataSource: Observable<Listing<MovieModel>> = movieCategoryEvent
.map { movieRepo.fetchMovieList(it) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.cache()
val movieList: Observable<PagedList<MovieModel>>
get() = fetchDataSource.flatMap { it.pagedList }
val refreshState: Observable<NetworkState>
get() = fetchDataSource.flatMap{ it.refreshState }
val networkState: Observable<NetworkState>
get() = fetchDataSource.flatMap { it.loadMoreState }
fun fetchMovieList(category: MovieCategory) {
movieCategoryEvent.onNext(category)
}
fun refresh() {
fetchDataSource
.map { it.refresh }
.doOnNext { it.invoke() }
.subscribe()
.disposeOnCleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.