Created
November 2, 2020 22:53
-
-
Save enginebai/80ecf8263b1e0417f9f6557c8d26ecf0 to your computer and use it in GitHub Desktop.
MovieHunt blog part4. list view model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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