Skip to content

Commit

Permalink
Use unsigned integers for values that should not be below zero
Browse files Browse the repository at this point in the history
  • Loading branch information
tonelli-m committed Dec 5, 2024
1 parent c06facb commit 3ad40b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ sealed class SettingsKey<T>(val serializer: KSerializer<T>, val defaultValue: T)
defaultValue = false,
)

data object SettingMaxMeasurementDuration : SettingsKey<Int>(
Int.serializer(),
defaultValue = 30,
data object SettingMaxMeasurementDuration : SettingsKey<UInt>(
UInt.serializer(),
defaultValue = 30u,
)

data object SettingSpectrogramScaleMode : SettingsKey<SpectrogramScaleMode>(
Expand All @@ -74,14 +74,14 @@ sealed class SettingsKey<T>(val serializer: KSerializer<T>, val defaultValue: T)
defaultValue = 0.0,
)

data object SettingCalibrationCountdown : SettingsKey<Int>(
Int.serializer(),
defaultValue = 4,
data object SettingCalibrationCountdown : SettingsKey<UInt>(
UInt.serializer(),
defaultValue = 4u,
)

data object SettingCalibrationDuration : SettingsKey<Int>(
Int.serializer(),
defaultValue = 6,
data object SettingCalibrationDuration : SettingsKey<UInt>(
UInt.serializer(),
defaultValue = 6u,
)

data object SettingTestSignalAudioOutput : SettingsKey<CalibrationTestAudioOutput>(
Expand All @@ -90,8 +90,8 @@ sealed class SettingsKey<T>(val serializer: KSerializer<T>, val defaultValue: T)
)

// Map
data object SettingMapMaxMeasurementsCount : SettingsKey<Int>(
Int.serializer(),
defaultValue = 500,
data object SettingMapMaxMeasurementsCount : SettingsKey<UInt>(
UInt.serializer(),
defaultValue = 500u,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ fun <T : Any> SettingsItem(
SettingsBooleanInput(viewModel as SettingsItemViewModel<Boolean>)
}

// UInt and ULong are not handled as Number types in Kotlin so we need to handle
// those in a separate when branch
is UInt, ULong -> {
SettingsNumericalInput(viewModel)
}

is Number -> {
SettingsNumericalInput(viewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ fun <T : Any> SettingsNumericalInput(
fun getNumericalValue(): T? {
return when (viewModel.settingKey.defaultValue) {
is Int -> textFieldValueState.text.toIntOrNull() as T?
is UInt -> textFieldValueState.text.toUIntOrNull() as T?
is Long -> textFieldValueState.text.toLongOrNull() as T?
is ULong -> textFieldValueState.text.toULongOrNull() as T?
is Double -> textFieldValueState.text.toDoubleOrNull() as T?
is Float -> textFieldValueState.text.toFloatOrNull() as T?
else -> null
Expand Down

0 comments on commit 3ad40b1

Please sign in to comment.