Skip to content

Instantly share code, notes, and snippets.

@enginebai
Created November 2, 2020 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginebai/05eee464b18f79c1b5e661e5a7c98220 to your computer and use it in GitHub Desktop.
Save enginebai/05eee464b18f79c1b5e661e5a7c98220 to your computer and use it in GitHub Desktop.
MovieHunt blog part4. detail view model
class MovieDetailViewModel : BaseViewModel() {
private val movieRepo: MovieRepo by inject()
private val _movieDetail = MutableLiveData<MovieModel>()
val posterUrl: LiveData<String> = Transformations.map(_movieDetail) { it.getPosterUrl() }
val title: LiveData<String> = Transformations.map(_movieDetail) { it.displayTitle() }
val rating: LiveData<Float> = Transformations.map(_movieDetail) { it.display5StarsRating() }
val voteCount: LiveData<String> = Transformations.map(_movieDetail) { it.displayVoteCount() }
val duration: LiveData<String> = Transformations.map(_movieDetail) { it.displayDuration() }
val releaseDate: LiveData<String> = Transformations.map(_movieDetail) { it.displayReleaseDate() }
fun fetchMovieDetail(id: String) {
movieRepo.fetchMovieDetail(id)
.subscribeOn(Schedulers.io())
.doOnSuccess { _movieDetail.postValue(it) }
.subscribe()
.disposeOnCleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment