diff --git a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionDetails.kt b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionDetails.kt index b0fbfe1..ed7e3a9 100644 --- a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionDetails.kt +++ b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionDetails.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import io.github.tscholze.kennzeichner.android.R import io.github.tscholze.kennzeichner.data.Region +import java.text.NumberFormat /** * Renders region's text-based details. @@ -50,16 +51,16 @@ fun RegionDetails(region: Region) { } // Inhabitants - if (region.inhabitants.isNotEmpty()) { + if (region.inhabitants != 0) { RegionDetailText( - stringResource(id = R.string.region_meta_inhabitants_format, region.inhabitants) + stringResource(id = R.string.region_meta_inhabitants_format, NumberFormat.getNumberInstance().format(region.inhabitants)) ) } // Area - if (region.area.isNotEmpty()) { + if (region.area != 0) { RegionDetailText( - stringResource(id = R.string.region_meta_area_format, region.area) + stringResource(id = R.string.region_meta_area_format, NumberFormat.getNumberInstance().format(region.area)) ) } } diff --git a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionMap.kt b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionMap.kt index b1b30e9..4b403b5 100644 --- a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionMap.kt +++ b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/components/RegionMap.kt @@ -6,6 +6,7 @@ import com.google.android.gms.maps.model.CameraPosition import com.google.android.gms.maps.model.LatLng import com.google.maps.android.compose.GoogleMap import com.google.maps.android.compose.rememberCameraPositionState +import io.github.tscholze.kennzeichner.data.Coordinate import io.github.tscholze.kennzeichner.data.Region /** @@ -15,14 +16,9 @@ import io.github.tscholze.kennzeichner.data.Region */ @Composable fun RegionMap(region: Region, modifier: Modifier) { - val coordinates = LatLng( - region.lat.replace(",", ".").toDouble(), - region.long.replace(",", ".").toDouble() - ) - val cameraPositionState = rememberCameraPositionState { position = CameraPosition.fromLatLngZoom( - coordinates, + LatLng(region.coordinate.latitude, region.coordinate.longitude), 10f ) } diff --git a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/MapScreen.kt b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/MapScreen.kt index 4e72f78..0c914d3 100644 --- a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/MapScreen.kt +++ b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/MapScreen.kt @@ -70,7 +70,7 @@ fun MapScreen(navController: NavController) { Box(modifier = Modifier .zIndex(2f) .padding(12.dp) - .alpha(0.8f)) { + .alpha(0.9f)) { Box( modifier = Modifier .zIndex(2f) @@ -103,17 +103,11 @@ fun MapScreen(navController: NavController) { cameraPositionState = cameraPositionState ) { regions.map { region -> - - val coordinates = LatLng( - region.lat.replace(",", ".").toDouble(), - region.long.replace(",", ".").toDouble() - ) - Marker( - state = rememberMarkerState(position = coordinates), + state = rememberMarkerState(position = LatLng(region.coordinate.latitude, region.coordinate.longitude)), title = region.id, snippet = region.name, - icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE), + icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET), onInfoWindowClick = { selectedRegion = region }, onClick = { _ -> selectedRegion = region diff --git a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/RegionsScreen.kt b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/RegionsScreen.kt index 2bd52c6..533a9a6 100644 --- a/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/RegionsScreen.kt +++ b/app/androidApp/src/main/java/io/github/tscholze/kennzeichner/android/composables/screens/RegionsScreen.kt @@ -21,6 +21,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.navigation.NavController +import com.google.android.gms.maps.model.LatLng import io.github.tscholze.kennzeichner.android.R import io.github.tscholze.kennzeichner.android.composables.components.LoadingIndicator import io.github.tscholze.kennzeichner.android.composables.components.RegionDetails @@ -63,7 +64,7 @@ fun RegionsScreen(navController: NavController) { modifier = Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(24.dp) ) { - items(regions) { region -> + items(regions){ region -> Card( elevation = 8.dp, modifier = Modifier.fillMaxSize(), diff --git a/app/shared/build.gradle.kts b/app/shared/build.gradle.kts index 1701b52..23d5b99 100644 --- a/app/shared/build.gradle.kts +++ b/app/shared/build.gradle.kts @@ -24,8 +24,6 @@ kotlin { } sourceSets { - val ktorVersion = "2.2.4" - val commonMain by getting { dependencies { // Kotlinx diff --git a/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/LicensePlateRepository.kt b/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/LicensePlateRepository.kt index 1519c18..42ccfc4 100644 --- a/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/LicensePlateRepository.kt +++ b/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/LicensePlateRepository.kt @@ -1,11 +1,14 @@ package io.github.tscholze.kennzeichner.data +import io.github.tscholze.kennzeichner.data.dto.RegionDTO import io.github.tscholze.kennzeichner.utils.makeHttpClient -import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.get -import io.ktor.client.statement.bodyAsText +/** + * This repository contains all properties and features + * to work with remotely fetched license plate regions. + */ class LicensePlateRepository { // MARK: - Private properties - @@ -24,9 +27,10 @@ class LicensePlateRepository { return cachedRegions } - cachedRegions = client + cachedRegions = client .get("https://tscholze.github.io/blog/files/lp-regions-data.json") - .body() + .body>() + .mapNotNull { Region.fromDto(it) } return cachedRegions } diff --git a/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/Region.kt b/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/Region.kt index 139cf9c..4a68dc8 100644 --- a/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/Region.kt +++ b/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/Region.kt @@ -1,14 +1,60 @@ package io.github.tscholze.kennzeichner.data -import kotlinx.serialization.Serializable +import io.github.tscholze.kennzeichner.data.dto.RegionDTO -@Serializable -data class Region ( +/** + * Business model of a region. + */ +class Region( val id: String, val name: String, - val lat: String, - val long: String, - val inhabitants: String, + val coordinate: Coordinate, + val inhabitants: Int, val leader: String, - val area: String -) \ No newline at end of file + val area: Int +) { + companion object { + /** + * Creates a new Region object from DTO. + * + * @param dto: DTO to transfer from. + * @return Created instance or null if transferring failed. + */ + fun fromDto(dto: RegionDTO): Region? { + return try { + Region( + dto.id, + dto.name, + Coordinate.fromDto(dto), + Int.fromDtoString(dto.inhabitants), + dto.leader, + Int.fromDtoString(dto.area) + ) + } catch (error: Exception) { + null + } + } + } + } + +data class Coordinate( + val latitude: Double, + val longitude: Double +) { + companion object { + @Throws(NumberFormatException::class) + fun fromDto(dto: RegionDTO): Coordinate { + return Coordinate( + dto.lat.replace(",", ".").toDouble(), + dto.long.replace(",", ".").toDouble() + ) + } + } +} + +@Throws(NumberFormatException::class) +private fun Int.Companion.fromDtoString(value: String): Int { + return value.replace(",", "") + .replace(".", "") + .toInt() +} \ No newline at end of file diff --git a/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/dto/RegionDTO.kt b/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/dto/RegionDTO.kt new file mode 100644 index 0000000..0eeb8d2 --- /dev/null +++ b/app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/dto/RegionDTO.kt @@ -0,0 +1,18 @@ +package io.github.tscholze.kennzeichner.data.dto + +import kotlinx.serialization.Serializable + +/** + * Data transfer model of a remote + * region. + */ +@Serializable +data class RegionDTO ( + val id: String, + val name: String, + val lat: String, + val long: String, + val inhabitants: String, + val leader: String, + val area: String +) \ No newline at end of file