Last active
December 11, 2018 07:40
-
-
Save plusmobileapps/aeaa304a946918955274b58bc129259c to your computer and use it in GitHub Desktop.
This file contains 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 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