From 78af2aa75250989310c18a292d78364a540f0e9f Mon Sep 17 00:00:00 2001 From: Alexandre G Pereira Date: Sat, 13 Jan 2024 15:52:20 -0300 Subject: [PATCH] Add image background edit field (#225) --- .../registration/ui/form/MonsterHeaderForm.kt | 13 +++++++++++++ .../src/main/res/values-pt-rBR/strings.xml | 1 + .../android/src/main/res/values/strings.xml | 1 + .../domain/NormalizeMonsterUseCase.kt | 17 +++++++++++++++-- .../alexandregpereira/hunter/ui/util/Color.kt | 2 +- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/feature/monster-registration/android/src/main/kotlin/br/alexandregpereira/hunter/monster/registration/ui/form/MonsterHeaderForm.kt b/feature/monster-registration/android/src/main/kotlin/br/alexandregpereira/hunter/monster/registration/ui/form/MonsterHeaderForm.kt index 3b3e1d45..85f1b25e 100644 --- a/feature/monster-registration/android/src/main/kotlin/br/alexandregpereira/hunter/monster/registration/ui/form/MonsterHeaderForm.kt +++ b/feature/monster-registration/android/src/main/kotlin/br/alexandregpereira/hunter/monster/registration/ui/form/MonsterHeaderForm.kt @@ -4,6 +4,7 @@ import androidx.annotation.StringRes import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource +import br.alexandregpereira.hunter.domain.model.Color import br.alexandregpereira.hunter.domain.model.Monster import br.alexandregpereira.hunter.domain.model.MonsterType import br.alexandregpereira.hunter.monster.registration.R @@ -39,6 +40,18 @@ internal fun MonsterHeaderForm( } ) + AppTextField( + text = monster.imageData.backgroundColor.light, + label = stringResource(R.string.monster_registration_image_background_color), + onValueChange = { + onMonsterChanged( + monster.copy(imageData = monster.imageData.copy( + backgroundColor = Color(light = it, dark = it) + )) + ) + } + ) + PickerField( value = stringResource(monster.type.toMonsterTypeState().stringRes), label = stringResource(R.string.monster_registration_type), diff --git a/feature/monster-registration/android/src/main/res/values-pt-rBR/strings.xml b/feature/monster-registration/android/src/main/res/values-pt-rBR/strings.xml index b69a03b8..cb649e35 100644 --- a/feature/monster-registration/android/src/main/res/values-pt-rBR/strings.xml +++ b/feature/monster-registration/android/src/main/res/values-pt-rBR/strings.xml @@ -81,4 +81,5 @@ Habilidades Especiais Reações Ações Lendárias + Cor de Fundo da Imagem \ No newline at end of file diff --git a/feature/monster-registration/android/src/main/res/values/strings.xml b/feature/monster-registration/android/src/main/res/values/strings.xml index 98495e7b..5b796b3e 100644 --- a/feature/monster-registration/android/src/main/res/values/strings.xml +++ b/feature/monster-registration/android/src/main/res/values/strings.xml @@ -81,4 +81,5 @@ Special Abilities Reactions Legendary Actions + Image Background Color \ No newline at end of file diff --git a/feature/monster-registration/state-holder/src/commonMain/kotlin/br/alexandregpereira/hunter/monster/registration/domain/NormalizeMonsterUseCase.kt b/feature/monster-registration/state-holder/src/commonMain/kotlin/br/alexandregpereira/hunter/monster/registration/domain/NormalizeMonsterUseCase.kt index 57842645..14ec9b57 100644 --- a/feature/monster-registration/state-holder/src/commonMain/kotlin/br/alexandregpereira/hunter/monster/registration/domain/NormalizeMonsterUseCase.kt +++ b/feature/monster-registration/state-holder/src/commonMain/kotlin/br/alexandregpereira/hunter/monster/registration/domain/NormalizeMonsterUseCase.kt @@ -16,6 +16,7 @@ package br.alexandregpereira.hunter.monster.registration.domain +import br.alexandregpereira.hunter.domain.model.Color import br.alexandregpereira.hunter.domain.model.Monster import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOf @@ -56,6 +57,18 @@ private fun Monster.changeAbilityScoresModifier(): Monster { else -> 10 } ) - } + }, + imageData = monster.imageData.copy( + backgroundColor = monster.imageData.backgroundColor.normalizeColor(), + ), ) -} \ No newline at end of file +} + +private fun Color.normalizeColor(): Color { + val newColor = this.light.takeIf { it.isNotBlank() } + ?.replace("#", "")?.let { "#$it" }?.uppercase().orEmpty() + return this.copy( + light = newColor, + dark = newColor, + ) +} diff --git a/ui/core/src/main/kotlin/br/alexandregpereira/hunter/ui/util/Color.kt b/ui/core/src/main/kotlin/br/alexandregpereira/hunter/ui/util/Color.kt index 8eb2cc76..b3de5de4 100644 --- a/ui/core/src/main/kotlin/br/alexandregpereira/hunter/ui/util/Color.kt +++ b/ui/core/src/main/kotlin/br/alexandregpereira/hunter/ui/util/Color.kt @@ -21,5 +21,5 @@ import androidx.core.graphics.toColorInt fun String.toColor(): Color { if (this.isEmpty()) return Color.Transparent - return Color(this.toColorInt()) + return runCatching { this.toColorInt() }.getOrNull()?.let(::Color) ?: Color.Transparent }