Skip to content

Commit

Permalink
Separated DTO and BM
Browse files Browse the repository at this point in the history
re #9
  • Loading branch information
Tobias Scholze committed Apr 3, 2023
1 parent 5010df6 commit a309e02
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 0 additions & 2 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ kotlin {
}

sourceSets {
val ktorVersion = "2.2.4"

val commonMain by getting {
dependencies {
// Kotlinx
Expand Down
Original file line number Diff line number Diff line change
@@ -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 -
Expand All @@ -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<List<RegionDTO>>()
.mapNotNull { Region.fromDto(it) }

return cachedRegions
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
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()
}
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit a309e02

Please sign in to comment.