Skip to content

Commit

Permalink
refactor IngredientCategory, add FirestoreIngredientCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Weaver authored and Cody Weaver committed Mar 1, 2024
1 parent 33be8df commit 6db41c4
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -25,7 +27,6 @@ import androidx.compose.ui.window.Popup
import androidx.hilt.navigation.compose.hiltViewModel
import com.android.pocketalchemy.R
import com.android.pocketalchemy.model.IngredientCategory
import com.android.pocketalchemy.model.displayLabelRes
import com.android.pocketalchemy.ui.common.PaTopAppBar

/**
Expand Down Expand Up @@ -55,28 +56,29 @@ fun SelectIngredientCategory(
.fillMaxSize(1f)
.padding(scaffoldPadding)
) {
val selectedCategory by selectIngredientViewModel.selectedCategory.collectAsState()
var showSelectIngredient by remember { mutableStateOf(false) }

LazyColumn(
modifier = Modifier.padding(8.dp),
) {
items(IngredientCategory.entries) {
IngredientCategoryCard(category = it) { categoryName ->
selectIngredientViewModel.updateCategory(categoryName)
IngredientCategoryCard(category = it) { category ->
selectIngredientViewModel.updateCategory(category)
showSelectIngredient = true
}
}
}

if (selectedCategory != null) {
if (showSelectIngredient) {
SelectIngredient(selectIngredientViewModel) {
selectIngredientViewModel.updateCategory(null)
showSelectIngredient = false
}
}

BackHandler(
enabled = (selectedCategory != null)
enabled = (showSelectIngredient)
) {
selectIngredientViewModel.updateCategory(null)
showSelectIngredient = false
}
}
}
Expand All @@ -93,10 +95,10 @@ fun SelectIngredientCategory(
@Composable
fun IngredientCategoryCard(
category: IngredientCategory,
onClickCategoryCard: (String) -> Unit,
onClickCategoryCard: (IngredientCategory) -> Unit,
) {
Card(
onClick = { onClickCategoryCard(category.categoryName) },
onClick = { onClickCategoryCard(category) },
elevation = CardDefaults.cardElevation(4.dp),
modifier = Modifier
.fillMaxWidth(1f)
Expand All @@ -109,7 +111,7 @@ fun IngredientCategoryCard(
.padding(8.dp)
) {
Text(
text = stringResource(id = category.displayLabelRes()),
text = stringResource(id = category.labelResId),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth(1f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.android.pocketalchemy.model.ALL_CATEGORIES_NAME
import com.android.pocketalchemy.firebase.FirestoreIngredientCategory
import com.android.pocketalchemy.model.Ingredient
import com.android.pocketalchemy.model.IngredientCategory
import com.android.pocketalchemy.paging.IngredientPagingSource
import com.android.pocketalchemy.paging.PageSizes.INGREDIENT_PAGE_SIZE
import com.android.pocketalchemy.paging.PageSizes.INGREDIENT_PREFETCH_DISTANCE
Expand All @@ -30,6 +31,7 @@ class SelectIngredientViewModel @Inject constructor(
private val ingredientRepository: IngredientRepository,
): ViewModel() {
private val defaultDispatcher = Dispatchers.Default
private var _selectedCategory: IngredientCategory = IngredientCategory.ALL

private val defaultQuery
get() = ingredientRepository.getIngredientCollectionRef()
Expand All @@ -45,12 +47,6 @@ class SelectIngredientViewModel @Inject constructor(
val ingredientPagingState: StateFlow<Flow<PagingData<Ingredient>>>
get () = _ingredientPagingState.asStateFlow()

private val _selectedCategory: MutableStateFlow<String?>
= MutableStateFlow(null)

val selectedCategory: StateFlow<String?>
get() = _selectedCategory.asStateFlow()

/**
* Updates paging flow with new query
* @param query new query for paging data
Expand Down Expand Up @@ -81,19 +77,16 @@ class SelectIngredientViewModel @Inject constructor(
viewModelScope.launch(
defaultDispatcher
) {
val categoryName = selectedCategory.value
val category = _selectedCategory
var query = defaultQuery
val firestoreCategory = FirestoreIngredientCategory.getCategoryName(category)

// set category name
when (categoryName) {
ALL_CATEGORIES_NAME,
null -> {} // no specific category selected
else -> {
query = query.whereEqualTo(
Ingredient.CATEGORY_KEY,
categoryName
)
}
if (firestoreCategory != null) {
query = query.whereEqualTo(
Ingredient.CATEGORY_KEY,
firestoreCategory
)
}

keyword?.let { keyword ->
Expand All @@ -111,12 +104,10 @@ class SelectIngredientViewModel @Inject constructor(

/**
* Updates selected category and sets query
* @param categoryName
* @param category
*/
fun updateCategory(categoryName: String?) {
_selectedCategory.update {
categoryName
}
fun updateCategory(category: IngredientCategory) {
_selectedCategory = category
setQuery()
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
package com.android.pocketalchemy.editrecipe.selectingredient

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.android.pocketalchemy.R
import com.android.pocketalchemy.model.Ingredient
import com.android.pocketalchemy.model.MeasureUnitDefaults


/**
Expand All @@ -25,13 +52,129 @@ fun SelectIngredientQuantityDialog(
onAddIngredient: (Ingredient, Float) -> Unit,
) {
Dialog(onDismissRequest = onDismiss) {
Box(modifier = Modifier.fillMaxSize(.5f)) {
Text(
text = ingredient.fancyDescription,
modifier = Modifier.align(Alignment.Center)
)
var surfaceSize by remember { mutableStateOf(IntSize.Zero) }

Surface(
modifier = Modifier
.fillMaxWidth(0.75f)
.fillMaxHeight(0.4f)
.onGloballyPositioned {
surfaceSize = it.size
},
color = MaterialTheme.colorScheme.primaryContainer,
) {

Box(
modifier = Modifier
.fillMaxSize(1f)
.padding(8.dp)
) {
var quantity by remember { mutableFloatStateOf(0.0f) }
var unitMenuExpanded by remember { mutableStateOf(false) }
var unit by remember { mutableStateOf(MeasureUnitDefaults.defaultUnit) }
var buttonSize by remember { mutableStateOf(IntSize.Zero) }

Text(
text = ingredient.fancyDescription,
modifier = Modifier.align(Alignment.TopCenter),
textAlign = TextAlign.Center
)

/* TODO: Add quantity selection UI */
Column(
modifier = Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth(1f)
.padding(8.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
QuantityField(
quantity = quantity,
onValueChange = {
val newQuantity = it.toFloatOrNull()
if (newQuantity != null) {
quantity = newQuantity
}
},
)

Button(
onClick = { unitMenuExpanded = !unitMenuExpanded },
modifier = Modifier
.fillMaxWidth(0.8f)
.padding(8.dp)
.onGloballyPositioned {
buttonSize = it.size
}
) {
Text(
text = stringResource(id = unit.labelResId),
style = MaterialTheme.typography.bodyLarge,
maxLines = 1
)

//val unitMenuOffset = (100)
//Log.d("Test", "$surfaceSize")

DropdownMenu(
expanded = unitMenuExpanded,
onDismissRequest = { unitMenuExpanded = false },
//offset = DpOffset(x = unitMenuOffset.dp, y=0.dp)
) {
DropdownMenuItem(
text = { /*TODO*/ },
onClick = { /*TODO*/ },
)
}
}
}
}
}

}
}

@Composable
private fun QuantityField(
quantity: Float,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
) {
TextField(
value = quantity.toString(),
onValueChange = { onValueChange(it) },
modifier = modifier.fillMaxWidth(0.5f),
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Decimal
),
singleLine = true,
label = {
Text(
stringResource(id = R.string.quantity_field_label),
style = MaterialTheme.typography.bodyMedium,
maxLines = 1,
textAlign = TextAlign.Center
)
},
textStyle = MaterialTheme.typography.bodyLarge
)
}

@Preview
@Composable
private fun PreviewDialog(
ingredient: Ingredient = Ingredient(description = "Preview Ingredient Description"),
onDismiss: () -> Unit = {},
onAddIngredient: (Ingredient, Float) -> Unit = { _: Ingredient, _: Float -> },
) {
Surface(
modifier = Modifier
.height(750.dp)
.width(300.dp)
) {
SelectIngredientQuantityDialog(
ingredient = ingredient,
onDismiss = onDismiss,
onAddIngredient = onAddIngredient
)
}
}
Loading

0 comments on commit 6db41c4

Please sign in to comment.