Last active
July 11, 2021 11:53
-
-
Save catalinghita8/94bcb1e57b2d405c75bbd6e96e39f7e8 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 FoodCategoriesViewModel constructor(private val repository: FoodMenuRepository) : | |
BaseViewModel< | |
FoodCategoriesContract.Event, | |
FoodCategoriesContract.State, | |
FoodCategoriesContract.Effect>() { | |
init { | |
viewModelScope.launch { getFoodCategories() } | |
} | |
override fun setInitialState() = | |
FoodCategoriesContract.State(categories = listOf(), isLoading = true) | |
override fun handleEvents(event: FoodCategoriesContract.Event) { | |
when (event) { | |
is FoodCategoriesContract.Event.CategorySelection -> { | |
setEffect { | |
FoodCategoriesContract.Effect.Navigation.ToCategoryDetails(event.categoryName) | |
} | |
} | |
} | |
} | |
private suspend fun getFoodCategories() { | |
val categories = repository.getFoodCategories() | |
setState { | |
copy(categories = categories, isLoading = false) | |
} | |
setEffect { FoodCategoriesContract.Effect.ToastDataWasLoaded } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment