Skip to content

Commit

Permalink
Improve prep time field behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
lneugebauer committed Aug 17, 2023
1 parent ae9444a commit 4e6288f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import de.lukasneugebauer.nextcloudcookbook.category.domain.model.Category
import de.lukasneugebauer.nextcloudcookbook.core.util.UiText
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.model.Recipe

typealias DurationComponents = Pair<String, String>

sealed interface RecipeCreateEditState {
object Loading : RecipeCreateEditState
data class Success(
val recipe: Recipe,
val prepTime: DurationComponents,
val categories: List<Category> = emptyList(),
) : RecipeCreateEditState
data class Updated(val recipeId: Int) : RecipeCreateEditState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ import de.lukasneugebauer.nextcloudcookbook.core.presentation.components.Gap
import de.lukasneugebauer.nextcloudcookbook.core.presentation.ui.theme.NcBlue700
import de.lukasneugebauer.nextcloudcookbook.core.presentation.ui.theme.NextcloudCookbookTheme
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.model.Recipe
import de.lukasneugebauer.nextcloudcookbook.recipe.domain.state.DurationComponents
import java.time.Duration

@Composable
fun CreateEditRecipeForm(
recipe: Recipe,
prepTime: DurationComponents,
categories: List<Category>,
@StringRes title: Int,
onNavIconClick: () -> Unit,
onNameChanged: (name: String) -> Unit,
onDescriptionChanged: (description: String) -> Unit,
onUrlChanged: (url: String) -> Unit,
onImageOriginChanged: (imageUrl: String) -> Unit,
onPrepTimeChanged: (time: String) -> Unit,
onPrepTimeChanged: (hours: String, minutes: String) -> Unit,
onCookTimeChanged: (time: String) -> Unit,
onTotalTimeChanged: (time: String) -> Unit,
onCategoryChanged: (category: String) -> Unit,
Expand Down Expand Up @@ -134,9 +136,9 @@ fun CreateEditRecipeForm(
textFieldColors = textFieldColors,
)
PrepTime(
recipe = recipe,
focusManager = focusManager,
onPrepTimeChange = onPrepTimeChanged,
prepTime = prepTime,
textFieldColors = textFieldColors,
)
CookTime(
Expand Down Expand Up @@ -321,23 +323,19 @@ private fun ImageOrigin(

@Composable
private fun PrepTime(
recipe: Recipe,
focusManager: FocusManager,
onPrepTimeChange: (time: String) -> Unit,
onPrepTimeChange: (hours: String, minutes: String) -> Unit,
prepTime: DurationComponents,
textFieldColors: TextFieldColors,
) {
TimeTextField(
hours = recipe.prepTime?.toHoursPart()?.toString() ?: "",
minutes = recipe.prepTime?.toMinutesPart()?.toString() ?: "",
hours = prepTime.first,
minutes = prepTime.second,
onHoursChange = {
val hours = it.ifBlank { "0" }
val minutes = recipe.prepTime?.toMinutesPart() ?: 0
onPrepTimeChange.invoke("PT${hours}H${minutes}M0S")
onPrepTimeChange.invoke(it, prepTime.second)
},
onMinutesChange = {
val hours = recipe.prepTime?.toHoursPart()?.toString() ?: 0
val minutes = it.ifBlank { "0" }
onPrepTimeChange.invoke("PT${hours}H${minutes}M0S")
onPrepTimeChange.invoke(prepTime.first, it)
},
label = R.string.recipe_prep_time,
modifier = Modifier
Expand Down Expand Up @@ -705,14 +703,15 @@ private fun CreateEditRecipeFormPreview() {
NextcloudCookbookTheme {
CreateEditRecipeForm(
recipe = recipe,
prepTime = DurationComponents("1", "50"),
categories = categories,
title = R.string.recipe_new,
onNavIconClick = {},
onNameChanged = {},
onDescriptionChanged = {},
onUrlChanged = {},
onImageOriginChanged = {},
onPrepTimeChanged = {},
onPrepTimeChanged = {_, _ -> },
onCookTimeChanged = {},
onTotalTimeChanged = {},
onCategoryChanged = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ fun RecipeCreateScreen(
is RecipeCreateEditState.Loading -> Loader()
is RecipeCreateEditState.Success -> {
val recipe = (uiState as RecipeCreateEditState.Success).recipe
val prepTime = (uiState as RecipeCreateEditState.Success).prepTime
val categories = (uiState as RecipeCreateEditState.Success).categories

CreateEditRecipeForm(
recipe = recipe,
prepTime = prepTime,
categories = categories,
title = R.string.recipe_new,
onNavIconClick = { navigator.popBackStack() },
Expand All @@ -45,8 +47,8 @@ fun RecipeCreateScreen(
onImageOriginChanged = { newImageUrl ->
viewModel.changeImageOrigin(newImageUrl)
},
onPrepTimeChanged = { newPrepTime ->
viewModel.changePrepTime(newPrepTime)
onPrepTimeChanged = { hours, minutes ->
viewModel.changePrepTime(hours, minutes)
},
onCookTimeChanged = { newCookTime ->
viewModel.changeCookTime(newCookTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RecipeCreateViewModel @Inject constructor(
_uiState.value.ifSuccess {
_uiState.update { RecipeCreateEditState.Loading }
viewModelScope.launch {
val result = recipeRepository.createRecipe(recipe)
val result = recipeRepository.createRecipe(recipeDto)
_uiState.update {
if (result is Resource.Success && result.data != null) {
val recipeId = result.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ fun RecipeEditScreen(
is RecipeCreateEditState.Loading -> Loader()
is RecipeCreateEditState.Success -> {
val recipe = (uiState as RecipeCreateEditState.Success).recipe
val prepTime = (uiState as RecipeCreateEditState.Success).prepTime
val categories = (uiState as RecipeCreateEditState.Success).categories

CreateEditRecipeForm(
recipe = recipe,
prepTime = prepTime,
categories = categories,
title = R.string.recipe_edit,
onNavIconClick = { navigator.popBackStack() },
Expand All @@ -46,8 +48,8 @@ fun RecipeEditScreen(
onImageOriginChanged = { newImageUrl ->
viewModel.changeImageOrigin(newImageUrl)
},
onPrepTimeChanged = { newPrepTime ->
viewModel.changePrepTime(newPrepTime)
onPrepTimeChanged = { hours, minutes ->
viewModel.changePrepTime(hours, minutes)
},
onCookTimeChanged = { newCookTime ->
viewModel.changeCookTime(newCookTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class RecipeEditViewModel @Inject constructor(
_uiState.update { RecipeCreateEditState.Loading }
viewModelScope.launch {
_uiState.update {
when (val result = recipeRepository.updateRecipe(recipe)) {
when (val result = recipeRepository.updateRecipe(recipeDto)) {
is Resource.Error -> RecipeCreateEditState.Error(
result.message ?: UiText.StringResource(R.string.error_unknown),
)
is Resource.Success -> RecipeCreateEditState.Updated(recipe.id)
is Resource.Success -> RecipeCreateEditState.Updated(recipeDto.id)
}
}
}
Expand Down
Loading

0 comments on commit 4e6288f

Please sign in to comment.