Skip to content

Commit

Permalink
feat: get flavors from resource json
Browse files Browse the repository at this point in the history
  • Loading branch information
aslansari committed May 17, 2024
1 parent 961cb9f commit be4f6a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.aslansari.spiritvisor.home

import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
Expand All @@ -10,6 +11,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewmodel.compose.viewModel
import com.aslansari.spiritvisor.cocktail.component.CreditText

Expand All @@ -35,6 +37,13 @@ internal fun HomeScreen(
uiState: HomeUiState,
modifier: Modifier = Modifier,
) {
if (uiState.loading) {
Dialog(onDismissRequest = { /*TODO*/ }) {
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}
}
Box(modifier = modifier.fillMaxSize()) {
Column(
modifier = Modifier.fillMaxSize(),
Expand All @@ -49,15 +58,9 @@ internal fun HomeScreen(
verticalArrangement = Arrangement.spacedBy(16.dp),
maxItemsInEachRow = 3,
) {
FlavorCategoryButton("Sour", onClick = { onClick("Sour") })
FlavorCategoryButton("Sweet", onClick = { onClick("Sweet") })
FlavorCategoryButton("Salty", onClick = { onClick("Salty") })
FlavorCategoryButton("Spicy", onClick = { onClick("Spicy") })
FlavorCategoryButton("Bitter", onClick = { onClick("Bitter") })
FlavorCategoryButton("Herbal", onClick = { onClick("Herbal") })
FlavorCategoryButton("Fruity", onClick = { onClick("Fruity") })
FlavorCategoryButton("Smoky", onClick = { onClick("Smoky") })
FlavorCategoryButton("Umami", onClick = { onClick("Umami") })
uiState.flavors.forEach { flavor ->
FlavorCategoryButton(flavor, onClick = { onClick(flavor) })
}
}
}
CreditText(modifier = Modifier.align(Alignment.BottomCenter))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package com.aslansari.spiritvisor.home

import androidx.lifecycle.viewModelScope
import com.aslansari.spiritvisor.BaseViewModel
import com.aslansari.spiritvisor.UIState
import com.aslansari.spiritvisor.cocktail.CocktailDTO
import com.aslansari.spiritvisor.cocktail.CocktailService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class HomeViewModel : BaseViewModel<HomeUiState>() {
val cocktailService = CocktailService()
var cocktailsByFlavor: Map<String, List<CocktailDTO>> = emptyMap()
override fun createInitialState(): HomeUiState = HomeUiState()
init {
viewModelScope.launch(Dispatchers.Default) {
setState { copy(loading = true) }
cocktailsByFlavor = cocktailService.fetchCocktailsByFlavor().flavors
setState { copy(flavors = cocktailsByFlavor.keys.toList()) }
setState { copy(loading = false) }
}
}
}

data class HomeUiState(
val title: String = "Home"
val loading: Boolean = false,
val flavors: List<String> = emptyList(),
) : UIState

0 comments on commit be4f6a5

Please sign in to comment.