Location accuracy should be nullable

This commit is contained in:
ganfra
2026-02-26 15:54:41 +01:00
parent b49fd7b467
commit 29f9640053
2 changed files with 17 additions and 7 deletions

View File

@@ -19,7 +19,7 @@ private const val GEO_URI_REGEX = """geo:(?<latitude>-?\d+(?:\.\d+)?),(?<longitu
data class Location(
val lat: Double,
val lon: Double,
val accuracy: Float,
val accuracy: Float? = null,
) : Parcelable {
companion object {
fun fromGeoUri(geoUri: String): Location? {
@@ -27,12 +27,15 @@ data class Location(
return Location(
lat = result.groups["latitude"]?.value?.toDoubleOrNull() ?: return null,
lon = result.groups["longitude"]?.value?.toDoubleOrNull() ?: return null,
accuracy = result.groups["uncertainty"]?.value?.toFloatOrNull() ?: 0f,
accuracy = result.groups["uncertainty"]?.value?.toFloatOrNull(),
)
}
}
fun toGeoUri(): String {
return "geo:$lat,$lon;u=$accuracy"
fun toGeoUri(): String = buildString {
append("geo:$lat,$lon")
if (accuracy != null) {
append(";u=$accuracy")
}
}
}

View File

@@ -33,13 +33,13 @@ internal class LocationKtTest {
assertThat(Location.fromGeoUri("geo:1.234,5.678")).isEqualTo(Location(
lat = 1.234,
lon = 5.678,
accuracy = 0f,
accuracy = null,
))
assertThat(Location.fromGeoUri("geo:1,5")).isEqualTo(Location(
lat = 1.0,
lon = 5.0,
accuracy = 0f,
accuracy = null,
))
assertThat(Location.fromGeoUri("geo:1.234,5.678;u=3000")).isEqualTo(Location(
@@ -68,8 +68,15 @@ internal class LocationKtTest {
}
@Test
fun `encode geoUri - returns geoUri from a Location`() {
fun `encode geoUri - returns geoUri from a Location without accuracy`() {
assertThat(Location(1.0, 2.0, null).toGeoUri())
.isEqualTo("geo:1.0,2.0")
}
@Test
fun `encode geoUri - returns geoUri from a Location with accuracy`() {
assertThat(Location(1.0, 2.0, 3.0f).toGeoUri())
.isEqualTo("geo:1.0,2.0;u=3.0")
}
}