Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DetailViewModel(
- private val getRecipeById: GetRecipeById,
- private val getRecipeSummary: GetRecipeSummary,
- private val toggleRecipeFavorite: ToggleRecipeFavorite,
- override val uiDispatcher: CoroutineDispatcher,
- private val recipeId: Int) : ScopedViewModel(uiDispatcher) {
- data class UiModel(val recipeId: Int, val title: String, val summary: String, var favorite: Boolean, val image: String)
- private lateinit var recipe : Recipe
- private val _model = MutableLiveData<UiModel>()
- val model: LiveData<UiModel>
- get() {
- if (_model.value == null) getSummary(recipeId)
- return _model
- }
- private fun getSummary(id: Int) {
- launch {
- recipe = getRecipeById.invoke(id)
- recipe.let {
- _model.value = getRecipeSummary.invoke(id)?.let { it1 ->
- UiModel(
- recipe.id,
- recipe.title,
- it1.summary,
- recipe.favorite,
- recipe.image
- )
- }
- }
- }
- }
- fun onFavoriteClicked() {
- launch {
- _model.value?.let {
- _model.value = UiModel(it.recipeId, it.title, it.summary, !it.favorite, it.image)
- toggleRecipeFavorite.invoke(recipe.id, !it.favorite)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement