Skip to content

Commit

Permalink
feat: add suggest another button
Browse files Browse the repository at this point in the history
  • Loading branch information
aslansari committed May 15, 2024
1 parent 1e3c2be commit a8026a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal fun CocktailRoute(

CocktailScreen(
onBackClick = navigateBack,
onSuggestAnotherClick = viewModel::suggestAnother,
uiState = uiState
)
}
Expand All @@ -40,6 +41,7 @@ internal fun CocktailRoute(
internal fun CocktailScreen(
uiState: CocktailUIState,
onBackClick: () -> Unit = {},
onSuggestAnotherClick: () -> Unit = {},
modifier: Modifier = Modifier,
) {
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Expand All @@ -64,8 +66,13 @@ internal fun CocktailScreen(
onClick = {},
)
Spacer(Modifier.size(12.dp))
Button(onClick = onBackClick) {
Text("Go Back")
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) {
OutlinedButton(onClick = onBackClick) {
Text("Change Flavor")
}
OutlinedButton(onClick = onSuggestAnotherClick) {
Text("Suggest Another")
}
}
}
CreditText(modifier = Modifier.align(Alignment.BottomCenter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,45 @@ import spiritvisor.composeapp.generated.resources.Res
class CocktailViewModel : BaseViewModel<CocktailUIState>() {

val cocktailService = CocktailService()
var cocktailsByFlavor: Map<String, List<CocktailDTO>> = emptyMap()
var args: CocktailArgs? = null

override fun createInitialState(): CocktailUIState = CocktailUIState()

init {
viewModelScope.launch(Dispatchers.Default) {
cocktailsByFlavor = cocktailService.fetchCocktailsByFlavor().flavors
}
}

fun updateArgs(args: CocktailArgs) {
setState { copy(loading = true) }
this.args = args
setState { copy(category = args.category) }
viewModelScope.launch(Dispatchers.Default) {
// val title = cocktailService.fetchCocktail(args.category)
val cocktailsByFlavor = cocktailService.fetchCocktailsByFlavor()
val cocktails = cocktailsByFlavor.flavors[args.category]
if (!cocktails.isNullOrEmpty()) {
val randomIndex = cocktails.indices.random()
val cocktail = cocktails[randomIndex]
setState {
copy(
title = cocktail.title,
cocktailImageUrl = cocktail.image,
loading = false
)
}
} else {
// setState { copy(title = title, loading = false) }
setState { copy(title = "Not Found", loading = false) }
selectCocktailByFlavor(args.category)
}
}

fun selectCocktailByFlavor(flavor: String) {
val cocktails = cocktailsByFlavor[flavor]
if (!cocktails.isNullOrEmpty()) {
val randomIndex = cocktails.indices.random()
val cocktail = cocktails[randomIndex]
setState {
copy(
title = cocktail.title,
cocktailImageUrl = cocktail.image,
)
}
} else {
setState { copy(title = "Not Found", loading = false) }
}
}

fun suggestAnother() {
viewModelScope.launch(Dispatchers.Default) {
args?.let {
selectCocktailByFlavor(it.category)
}
}
}
Expand Down

0 comments on commit a8026a8

Please sign in to comment.