Skip to content

Instantly share code, notes, and snippets.

@catalinghita8
Last active July 11, 2021 11:53
Show Gist options
  • Save catalinghita8/94bcb1e57b2d405c75bbd6e96e39f7e8 to your computer and use it in GitHub Desktop.
Save catalinghita8/94bcb1e57b2d405c75bbd6e96e39f7e8 to your computer and use it in GitHub Desktop.
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