From 2dbb79a586ce79006e07ea70dc5663b50c6b38a9 Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Wed, 17 Sep 2025 12:25:01 +0200 Subject: [PATCH] Allow minus sign in custom range inputs (#68) --- app/build.gradle.kts | 2 +- .../karoopowerbar/screens/MainScreen.kt | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f944c95..7ab8b64 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -72,7 +72,7 @@ tasks.register("generateManifest") { "latestVersionCode" to android.defaultConfig.versionCode, "developer" to "github.com/timklge", "description" to "Open-source extension that adds colored progress bars representing power, heart rate etc. to the edge of the screen, similar to the LED bars on older Wahoo computers", - "releaseNotes" to "* Swap red / blue colorization for pedal balance source\n* Fix gear bar refresh\n* Add german localization\n* Add gear data sources\n* Show zero value on bars to indicate sensor availability\n* Fix pedal balance values", + "releaseNotes" to "* Allow minus sign in custom range input fields", "screenshotUrls" to listOf( "$baseUrl/powerbar_min.gif", "$baseUrl/powerbar0.png", diff --git a/app/src/main/kotlin/de/timklge/karoopowerbar/screens/MainScreen.kt b/app/src/main/kotlin/de/timklge/karoopowerbar/screens/MainScreen.kt index 7c17c56..b6f8e00 100644 --- a/app/src/main/kotlin/de/timklge/karoopowerbar/screens/MainScreen.kt +++ b/app/src/main/kotlin/de/timklge/karoopowerbar/screens/MainScreen.kt @@ -303,6 +303,9 @@ fun MainScreen(onFinish: () -> Unit) { onPauseOrDispose { } } + fun isCharAllowed(index: Int, c: Char): Boolean { + return c.isDigit() || (c == '-' && index == 0) + } Box(modifier = Modifier.fillMaxSize()){ Column(modifier = Modifier @@ -508,7 +511,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(right = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { minSpeed = it.filter { c -> c.isDigit() } }, + onValueChange = { minSpeed = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.min_speed)) }, suffix = { Text(stringResource(if (isImperial) R.string.unit_mph else R.string.unit_kph)) }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), @@ -519,7 +522,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(left = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { maxSpeed = it.filter { c -> c.isDigit() } }, + onValueChange = { maxSpeed = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.max_speed)) }, suffix = { Text(stringResource(if (isImperial) R.string.unit_mph else R.string.unit_kph)) }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), @@ -547,7 +550,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(right = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { customMinPower = it.filter { c -> c.isDigit() } }, + onValueChange = { customMinPower = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.min_power), fontSize = 12.sp) }, suffix = { Text(stringResource(R.string.unit_watts)) }, placeholder = { Text("$profileMinPower") }, @@ -559,7 +562,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(left = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { customMaxPower = it.filter { c -> c.isDigit() } }, + onValueChange = { customMaxPower = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.max_power), fontSize = 12.sp) }, suffix = { Text(stringResource(R.string.unit_watts)) }, placeholder = { Text("$profileMaxPower") }, @@ -589,7 +592,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(right = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { customMinHr = it.filter { c -> c.isDigit() } }, + onValueChange = { customMinHr = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.min_hr)) }, suffix = { Text(stringResource(R.string.unit_bpm)) }, placeholder = { if(profileRestHr > 0) Text("$profileRestHr") else Unit }, @@ -601,7 +604,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(left = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { customMaxHr = it.filter { c -> c.isDigit() } }, + onValueChange = { customMaxHr = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.max_hr)) }, suffix = { Text(stringResource(R.string.unit_bpm)) }, placeholder = { if(profileMaxHr > 0) Text("$profileMaxHr") else Unit }, @@ -623,7 +626,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(right = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { minCadence = it.filter { c -> c.isDigit() } }, + onValueChange = { minCadence = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.min_cadence)) }, suffix = { Text(stringResource(R.string.unit_rpm)) }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), @@ -634,7 +637,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(left = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { maxCadence = it.filter { c -> c.isDigit() } }, + onValueChange = { maxCadence = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.max_cadence)) }, suffix = { Text(stringResource(R.string.unit_rpm)) }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), @@ -652,7 +655,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(right = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { minGrade = it.filterIndexed { index, c -> c.isDigit() || (c == '-' && index == 0) } }, + onValueChange = { minGrade = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.min_grade)) }, suffix = { Text(stringResource(R.string.unit_percent)) }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), @@ -663,7 +666,7 @@ fun MainScreen(onFinish: () -> Unit) { .weight(1f) .absolutePadding(left = 2.dp) .onFocusEvent(::updateFocus), - onValueChange = { maxGrade = it.filterIndexed { index, c -> c.isDigit() || (c == '-' && index == 0) } }, + onValueChange = { maxGrade = it.filterIndexed(::isCharAllowed) }, label = { Text(stringResource(R.string.max_grade)) }, suffix = { Text(stringResource(R.string.unit_percent)) }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),