Skip to content

Commit

Permalink
Improve yield calculator integration
Browse files Browse the repository at this point in the history
  • Loading branch information
lneugebauer committed Jul 15, 2024
1 parent ee5cdfa commit 497610d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import de.lukasneugebauer.nextcloudcookbook.core.util.IoDispatcher
import de.lukasneugebauer.nextcloudcookbook.recipe.data.YieldCalculatorImpl
import de.lukasneugebauer.nextcloudcookbook.recipe.data.dto.RecipeDto
import de.lukasneugebauer.nextcloudcookbook.recipe.data.dto.RecipePreviewDto
import de.lukasneugebauer.nextcloudcookbook.recipe.data.repository.RecipeRepositoryImpl
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.YieldCalculator
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.repository.RecipeRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -85,4 +87,8 @@ object RecipeModule {
recipePreviewsStore,
recipeStore,
)

@Provides
@Singleton
fun provideYieldCalculator(): YieldCalculator = YieldCalculatorImpl()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import de.lukasneugebauer.nextcloudcookbook.core.util.Resource
import de.lukasneugebauer.nextcloudcookbook.core.util.UiText
import de.lukasneugebauer.nextcloudcookbook.core.util.asUiText
import de.lukasneugebauer.nextcloudcookbook.core.util.notZero
import de.lukasneugebauer.nextcloudcookbook.recipe.data.YieldCalculatorImpl
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.YieldCalculator
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.repository.RecipeRepository
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.state.RecipeDetailState
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -27,6 +27,7 @@ class RecipeDetailViewModel
private val preferencesManager: PreferencesManager,
private val recipeRepository: RecipeRepository,
savedStateHandle: SavedStateHandle,
private val yieldCalculator: YieldCalculator,
) : ViewModel() {
private val _state = MutableStateFlow(RecipeDetailState())
val state: StateFlow<RecipeDetailState> = _state
Expand All @@ -53,6 +54,12 @@ class RecipeDetailViewModel
val recipe = recipeResponse.value.toRecipe()
_state.value =
_state.value.copy(
calculatedIngredients =
yieldCalculator.recalculateIngredients(
recipe.ingredients,
recipe.yield,
recipe.yield,
),
currentYield = recipe.yield,
data = recipe,
loading = false,
Expand Down Expand Up @@ -96,7 +103,6 @@ class RecipeDetailViewModel

private fun recalculateYield(currentYield: Int) {
val recipe = _state.value.data ?: return
val yieldCalculator = YieldCalculatorImpl()
_state.value =
_state.value.copy(
calculatedIngredients =
Expand Down

0 comments on commit 497610d

Please sign in to comment.