Skip to content

Commit

Permalink
Fix: Fields not changing and localize speed
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregpereira committed Jan 15, 2024
1 parent 1438839 commit 5ba5beb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal fun MonsterAbilityDescriptionForm(
label = stringResource(R.string.monster_registration_description),
multiline = true,
onValueChange = { newValue ->
onChanged(newAbilityDescriptions.changeAt(index) { copy(name = newValue) })
onChanged(newAbilityDescriptions.changeAt(index) { copy(description = newValue) })
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import androidx.compose.ui.res.stringResource
import br.alexandregpereira.hunter.domain.model.Monster
import br.alexandregpereira.hunter.domain.model.SpeedType
import br.alexandregpereira.hunter.monster.registration.R
import br.alexandregpereira.hunter.monster.registration.ui.changeAt
import br.alexandregpereira.hunter.ui.compose.AppTextField
import br.alexandregpereira.hunter.ui.compose.Form
import br.alexandregpereira.hunter.ui.compose.FormField
import br.alexandregpereira.hunter.ui.compose.PickerField

@Composable
internal fun MonsterSpeedValuesForm(
Expand All @@ -17,42 +19,63 @@ internal fun MonsterSpeedValuesForm(
) {
val speedValues = monster.speed.values
val types = speedValues.map { it.type }
val options = SpeedType.entries.filterNot { types.contains(it) }.map { it.name }
val options = SpeedType.entries.filterNot { types.contains(it) }
val optionsStrings = options.map { it.toTypeState().getString() }
val newSpeedValues = speedValues.toMutableList()

Form(
modifier = modifier,
title = stringResource(R.string.monster_registration_speed),
formFields = monster.speed.values.map { speedValue ->
listOf(
FormField.Picker(
key = "${speedValue.type.name}-type",
label = stringResource(R.string.monster_registration_speed_type),
value = speedValue.type.name,
options = options,
),
FormField.Text(
key = "${speedValue.type.name}-name",
label = speedValue.type.name,
value = speedValue.valueFormatted,
)
) {
monster.speed.values.mapIndexed { index, speedValue ->
val name = speedValue.type.toTypeState().getString()
PickerField(
value = name,
label = stringResource(R.string.monster_registration_speed_type),
options = optionsStrings,
onValueChange = { optionIndex ->
onMonsterChanged(
monster.copy(
speed = monster.speed.copy(
values = newSpeedValues.changeAt(index) {
copy(
type = options[optionIndex],
)
}
)
)
)
}
)
}.reduceOrNull { acc, texts -> acc + texts } ?: emptyList(),
onFormChanged = { field ->
val newSpeedValues = speedValues.toMutableList()
val index = newSpeedValues.indexOfFirst { field.key.startsWith(it.type.name) }
if (index != -1) {
when (field.key) {
"${newSpeedValues[index].type.name}-type" -> {
newSpeedValues[index] = newSpeedValues[index].copy(
type = SpeedType.valueOf(field.stringValue),

AppTextField(
text = speedValue.valueFormatted,
label = name,
onValueChange = { newValue ->
onMonsterChanged(
monster.copy(
speed = monster.speed.copy(
values = newSpeedValues.changeAt(index) {
copy(valueFormatted = newValue)
}
)
)
}
"${newSpeedValues[index].type.name}-name" -> {
newSpeedValues[index] = newSpeedValues[index].copy(valueFormatted = field.stringValue)
}
)
}
onMonsterChanged(monster.copy(speed = monster.speed.copy(values = newSpeedValues)))
}
},
)
}
)
}
}
}

private enum class SpeedTypeState(val stringRes: Int) {
BURROW(R.string.monster_registration_speed_type_burrow),
CLIMB(R.string.monster_registration_speed_type_climb),
FLY(R.string.monster_registration_speed_type_fly),
WALK(R.string.monster_registration_speed_type_walk),
SWIM(R.string.monster_registration_speed_type_swim),
}

private fun SpeedType.toTypeState() = SpeedTypeState.valueOf(name)

@Composable
private fun SpeedTypeState.getString() = stringResource(stringRes)
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@
<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>

<string name="monster_registration_speed_type_walk">Deslocamento</string>
<string name="monster_registration_speed_type_fly">Voo</string>
<string name="monster_registration_speed_type_swim">Natação</string>
<string name="monster_registration_speed_type_climb">Escalagem</string>
<string name="monster_registration_speed_type_burrow">Escavação</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@
<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>

<string name="monster_registration_speed_type_walk">Speed</string>
<string name="monster_registration_speed_type_fly">Fly</string>
<string name="monster_registration_speed_type_swim">Swim</string>
<string name="monster_registration_speed_type_climb">Climb</string>
<string name="monster_registration_speed_type_burrow">Burrow</string>
</resources>

0 comments on commit 5ba5beb

Please sign in to comment.