Skip to content

Commit

Permalink
Add image background edit field (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregpereira authored Jan 13, 2024
1 parent f077357 commit 78af2aa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@
<string name="monster_registration_special_abilities">Habilidades Especiais</string>
<string name="monster_registration_reactions">Reações</string>
<string name="monster_registration_legendary_actions">Ações Lendárias</string>
<string name="monster_registration_image_background_color">Cor de Fundo da Imagem</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@
<string name="monster_registration_special_abilities">Special Abilities</string>
<string name="monster_registration_reactions">Reactions</string>
<string name="monster_registration_legendary_actions">Legendary Actions</string>
<string name="monster_registration_image_background_color">Image Background Color</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +57,18 @@ private fun Monster.changeAbilityScoresModifier(): Monster {
else -> 10
}
)
}
},
imageData = monster.imageData.copy(
backgroundColor = monster.imageData.backgroundColor.normalizeColor(),
),
)
}
}

private fun Color.normalizeColor(): Color {
val newColor = this.light.takeIf { it.isNotBlank() }
?.replace("#", "")?.let { "#$it" }?.uppercase().orEmpty()
return this.copy(
light = newColor,
dark = newColor,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 78af2aa

Please sign in to comment.