Skip to content

Instantly share code, notes, and snippets.

@plusmobileapps
Last active December 11, 2018 07:40
Show Gist options
  • Save plusmobileapps/aeaa304a946918955274b58bc129259c to your computer and use it in GitHub Desktop.
Save plusmobileapps/aeaa304a946918955274b58bc129259c to your computer and use it in GitHub Desktop.
class MainViewModel (private val stateReducer: StateReducer,
private val countryRepository: CountryRepository
) : ViewModel(), MainView {
private val bigCards: LiveData<List<Card>> = Transformations.map(countryRepository.getAll()) { countries ->
return@map countries.map { country ->
Card(country.id!!, country.name, country.imageUrl, country.description)
}
}
private val topCarousel: LiveData<List<CarouselItem>> = Transformations.map(countryRepository.getAll()) { countries ->
return@map countries.map { country ->
CarouselItem(country.id!!, country.name, country.description)
}
}
private val bottomCarousel: LiveData<List<CarouselItem>> = Transformations.map(countryRepository.getAll()) { countries ->
return@map countries.map { country ->
CarouselItem(country.id!!, country.name, country.description)
}
}
private val mediator = MediatorLiveData<List<DataWrapper>>().apply {
addSource(topCarousel) { stateReducer.onTopCarouselLoaded(it) }
addSource(bigCards) { stateReducer.onCardsLoaded(it) }
addSource(bottomCarousel) { stateReducer.onBottomCarouselLoaded(it) }
addSource(stateReducer.getData()) { t: List<DataWrapper>? -> value = t }
}
fun getData(): LiveData<List<DataWrapper>> = mediator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment