Skip to content

Commit

Permalink
Add documentation, add NumberUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Weaver authored and Cody Weaver committed Mar 5, 2024
1 parent be621e2 commit bf14c81
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fun EditRecipeScreen(
recipe = recipe,
onUpdate = {
editRecipeViewModel.updateUiState(
recipe = recipe.copy(title = it)
newRecipe = recipe.copy(title = it)
)
}
)
Expand All @@ -100,7 +100,7 @@ fun EditRecipeScreen(
recipe = recipe,
onUpdate = {
editRecipeViewModel.updateUiState(
recipe = recipe.copy(description = it)
newRecipe = recipe.copy(description = it)
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.android.pocketalchemy.model.RecipeIngredient
import com.android.pocketalchemy.repository.AuthRepository
import com.android.pocketalchemy.repository.RecipeIngredientRepository
import com.android.pocketalchemy.repository.RecipeRepository
import com.android.pocketalchemy.util.plus
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -91,37 +92,18 @@ class EditRecipeViewModel @Inject constructor(
}
}

/**
* Updates the state of the recipe and its ingredients being
* edited. Has no effect if both parameters are null.
* @param recipe new recipe to update UI state with
* @param ingredients new ingredients to update UI state with
* TODO: Refactor into two distinct functions
*/
fun updateUiState(
recipe: Recipe? = null,
ingredients: List<RecipeIngredient>? = null,
newRecipe: Recipe? = null,
newIngredients: List<RecipeIngredient>? = null,
) {
viewModelScope.launch {
if (recipe != null && ingredients != null) {
_editRecipeUiState.update {
it.copy(
recipe = recipe,
ingredients = ingredients
)
}
} else {
recipe?.let { newRecipe ->
_editRecipeUiState.update {
it.copy(recipe = newRecipe)
}
}
viewModelScope.launch(
defaultDispatcher
) {
_editRecipeUiState.update {
val recipe = newRecipe ?: it.recipe
val ingredients = newIngredients ?: it.ingredients

ingredients?.let { newIngredients ->
_editRecipeUiState.update {
it.copy(ingredients = newIngredients)
}
}
it.copy(recipe = recipe, ingredients = ingredients)
}
}
}
Expand Down Expand Up @@ -174,15 +156,15 @@ class EditRecipeViewModel @Inject constructor(
recipeIngredients.mapIndexed { i: Int, ingredient: RecipeIngredient ->
if (i == ingredientIndex) {
ingredient.copy(
gramWeight = ingredient.gramWeight + recipeIngredient.gramWeight
value = ingredient.value + recipeIngredient.value
)
} else {
ingredient
}
}
}
updateUiState(
ingredients = newRecipeIngredients
newIngredients = newRecipeIngredients
)
}
}
Expand All @@ -196,7 +178,7 @@ class EditRecipeViewModel @Inject constructor(
viewModelScope.launch {
val recipeIngredients = _editRecipeUiState.value.ingredients
updateUiState(
ingredients = recipeIngredients.filter {
newIngredients = recipeIngredients.filter {
it.ingredientId != recipeIngredient.id
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ fun SelectIngredientQuantityDialog(
}
}

/**
* Displays nutritional information
* for a given ingredient and quantity.
* @param quantity
* @param ingredient
*/
@Composable
private fun NutritionInfo(
quantity: String,
Expand All @@ -142,6 +148,13 @@ private fun NutritionInfo(
}
}


/**
* Button with dropdown menu for selecting a unit of measure
* for an ingredient which is represented by [MeasureUnit].
* @param currentUnit unit currently selected
* @param onSelectUnit updates current unit
*/
@Composable
private fun UnitSelectionButton(
currentUnit: MeasureUnit,
Expand Down Expand Up @@ -219,6 +232,8 @@ private fun QuantityField(
)
}


/** SelectQuantityDialog Preview */
@Preview
@Composable
private fun PreviewDialog(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.android.pocketalchemy.model

import com.android.pocketalchemy.util.MeasureUnit
import com.android.pocketalchemy.util.MeasureUnitDefaults
import com.google.errorprone.annotations.Keep
import com.google.firebase.firestore.DocumentId
import com.google.firebase.firestore.IgnoreExtraProperties
Expand All @@ -20,8 +22,9 @@ data class RecipeIngredient(
val recipeId: String = "",
val ingredientId: String = "",
val description: String = "",
val gramWeight: Float = 0f
) {
val unit: MeasureUnit = MeasureUnitDefaults.defaultUnit,
val value: Number = 0.0,
) {
/**
* Shortened recipe description for recipe view
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package com.android.pocketalchemy.util

import com.android.pocketalchemy.R


/**
* Units of measurement for ingredients.
*/
enum class MeasureUnit(val labelResId: Int) {
Kilograms(R.string.kilogram_unit_label),
Grams(R.string.gram_unit_label),
Milligrams(R.string.milligram_unit_label),
}

/**
* Defaults for units of measures and their quantities.
*/
object MeasureUnitDefaults {
val defaultUnit = MeasureUnit.Grams
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/android/pocketalchemy/util/NumberUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.android.pocketalchemy.util


/**
* operator function for addition of two Number
* instances. Uses float addition under the hood.
* @param other
* @return The result of adding the receiver and
* [other].
*
*
* NOTE:
* From my research it seems that Floats, i.e. (32-bit Floating-Point Numbers)
* will perform more consistently across all devices. It seems that some
* mobile devices can perform 32-bit operations at much higher rates
* compared to 64-bit floating-point operations, especially lower end hardware.
* #TODO: Research this claim more.
*/
operator fun Number.plus(other: Number): Number {
return this.toFloat() + other.toFloat()
}

0 comments on commit bf14c81

Please sign in to comment.