Skip to content

Commit fd036de

Browse files
authored
Merge pull request #250 from fmasa/journal-crash
Trim all text field values and make TextInput API safer
2 parents 0729d57 + 00eaf4b commit fd036de

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/character/trappings/NonCompendiumTrappingForm.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,10 @@ private class TrappingTypeFormData(
456456
override fun toValue(): TrappingType? = when (type.value) {
457457
TrappingTypeOption.AMMUNITION -> TrappingType.Ammunition(
458458
weaponGroups = ammunitionWeaponGroups.value,
459-
range = AmmunitionRangeExpression(ammunitionRange.normalizedValue),
459+
range = AmmunitionRangeExpression(ammunitionRange.value),
460460
qualities = weaponQualities.toMap(),
461461
flaws = weaponFlaws.toMap(),
462-
damage = DamageExpression(damage.normalizedValue),
462+
damage = DamageExpression(damage.value),
463463
)
464464
TrappingTypeOption.ARMOUR -> TrappingType.Armour(
465465
locations = armourLocations.value,
@@ -483,16 +483,16 @@ private class TrappingTypeFormData(
483483
TrappingTypeOption.MELEE_WEAPON -> TrappingType.MeleeWeapon(
484484
group = meleeWeaponGroup.value,
485485
reach = weaponReach.value,
486-
damage = DamageExpression(damage.normalizedValue),
486+
damage = DamageExpression(damage.value),
487487
qualities = weaponQualities.toMap(),
488488
flaws = weaponFlaws.toMap(),
489489
equipped = weaponEquipped.value,
490490
)
491491
TrappingTypeOption.PROSTHETIC -> TrappingType.Prosthetic(worn = worn.value)
492492
TrappingTypeOption.RANGED_WEAPON -> TrappingType.RangedWeapon(
493493
group = rangedWeaponGroup.value,
494-
range = WeaponRangeExpression(weaponRange.normalizedValue),
495-
damage = DamageExpression(damage.normalizedValue),
494+
range = WeaponRangeExpression(weaponRange.value),
495+
damage = DamageExpression(damage.value),
496496
qualities = weaponQualities.toMap(),
497497
flaws = weaponFlaws.toMap(),
498498
equipped = weaponEquipped.value,

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/compendium/journal/JournalEntryDialog.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ private data class JournalEntryFormData(
105105
.split(JournalEntry.PARENT_SEPARATOR)
106106
.filter { it.isNotBlank() }
107107
.map { it.trim() },
108-
text = text.normalizedValue,
109-
gmText = gmText.normalizedValue,
108+
text = text.value,
109+
gmText = gmText.value,
110110
isPinned = isPinned,
111111
isVisibleToPlayers = isVisibleToPlayers,
112112
)

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/compendium/trapping/TrappingDialog.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ private class TrappingTypeFormData(
505505
override fun toValue(): TrappingType? = when (type.value) {
506506
TrappingTypeOption.AMMUNITION -> TrappingType.Ammunition(
507507
weaponGroups = ammunitionWeaponGroups.value,
508-
range = AmmunitionRangeExpression(ammunitionRange.normalizedValue),
508+
range = AmmunitionRangeExpression(ammunitionRange.value),
509509
qualities = weaponQualities.toMap(),
510510
flaws = weaponFlaws.toMap(),
511511
damage = DamageExpression(damage.value.trim()),
@@ -528,15 +528,15 @@ private class TrappingTypeFormData(
528528
TrappingTypeOption.MELEE_WEAPON -> TrappingType.MeleeWeapon(
529529
group = meleeWeaponGroup.value,
530530
reach = weaponReach.value,
531-
damage = DamageExpression(damage.normalizedValue),
531+
damage = DamageExpression(damage.value),
532532
qualities = weaponQualities.toMap(),
533533
flaws = weaponFlaws.toMap(),
534534
)
535535
TrappingTypeOption.PROSTHETIC -> TrappingType.Prosthetic
536536
TrappingTypeOption.RANGED_WEAPON -> TrappingType.RangedWeapon(
537537
group = rangedWeaponGroup.value,
538-
range = WeaponRangeExpression(weaponRange.normalizedValue),
539-
damage = DamageExpression(damage.normalizedValue),
538+
range = WeaponRangeExpression(weaponRange.value),
539+
damage = DamageExpression(damage.value),
540540
qualities = weaponQualities.toMap(),
541541
flaws = weaponFlaws.toMap(),
542542
)

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/core/ui/forms/TextInput.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ interface Filter {
6060
// TODO: Use this version everywhere where input must be validated
6161
// as it allows to define validation only on InputValue and then use value.isValid()
6262
@Composable
63+
@OptIn(InternalTextInputApi::class)
6364
fun TextInput(
6465
value: InputValue,
6566
validate: Boolean,
@@ -77,9 +78,9 @@ fun TextInput(
7778
visualTransformation: VisualTransformation = VisualTransformation.None,
7879
filters: List<Filter> = emptyList(),
7980
) {
80-
TextInput(
81-
value = value.value,
82-
onValueChange = { value.value = it },
81+
TextInputImpl(
82+
value = value.rawTextFieldValue,
83+
onValueChange = { value.rawTextFieldValue = it },
8384
label = label,
8485
helperText = helperText,
8586
validate = validate,
@@ -99,7 +100,7 @@ fun TextInput(
99100
}
100101

101102
@Composable
102-
private fun TextInput(
103+
private fun TextInputImpl(
103104
value: String,
104105
onValueChange: (String) -> Unit,
105106
validate: Boolean,
@@ -201,14 +202,24 @@ private fun TextInput(
201202
}
202203
}
203204

205+
@Target(AnnotationTarget.PROPERTY)
206+
@RequiresOptIn(level = RequiresOptIn.Level.ERROR)
207+
annotation class InternalTextInputApi
208+
204209
@Stable
205210
class InputValue(
206211
state: MutableState<String>,
207212
internal val rules: Rules,
208213
private val normalize: (String) -> String = { it.trim() },
209214
) {
210-
var value: String by state
211-
val normalizedValue get() = normalize(value)
215+
@InternalTextInputApi
216+
var rawTextFieldValue by state
217+
@OptIn(InternalTextInputApi::class)
218+
var value: String
219+
get() = normalize(rawTextFieldValue)
220+
set(value) {
221+
rawTextFieldValue = value
222+
}
212223

213224
fun isValid() = rules.errorMessage(value) == null
214225

desktop/src/jvmMain/kotlin/cz/frantisekmasa/wfrp_master/desktop/auth/ResetPasswordDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fun ResetPasswordDialog(
7979
processing = true
8080
coroutineScope.launchLogged(Dispatchers.IO) {
8181
try {
82-
when (auth.resetPassword(email.normalizedValue)) {
82+
when (auth.resetPassword(email.value)) {
8383
PasswordResetResult.Success -> {
8484
snackbarHolder.showSnackbar(
8585
messageEmailSent,

0 commit comments

Comments
 (0)