Skip to content

Commit

Permalink
Only store the data we actually use of the locations
Browse files Browse the repository at this point in the history
  • Loading branch information
cristan committed Sep 3, 2024
1 parent cfd3e40 commit 98e774c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 32 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/nl/ovfietsbeschikbaarheid/TestData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ object TestData {
"https://places.ns-mlab.nl/api/v2/places/stationfacility/Zelfservice%20OV-fiets%20uitgiftepunt-nvd001",
"nvd001",
"HVS",
10,
true,
latitude = 52.36599,
longitude = 6.469563,
)
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/nl/ovfietsbeschikbaarheid/dto/LocationsDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ data class LocationsDTO(
@Serializable
data class Location(
val description: String,
val city: String? = null,
val street: String? = null,
val houseNumber: String? = null,
val postalCode: String? = null,
val stationCode: String,
val open: OpenDTO,
val lat: Double,
val lng: Double,
val extra: LocationExtra,
Expand All @@ -29,11 +24,5 @@ data class Link(

@Serializable
data class LocationExtra(
// Can be Bemenst, Kluizen, Sleutelautomaat
val serviceType: String? = null,
// basically an Int. Weirdly nullable (see Raalte)
val rentalBikes: Int? = null,
// "type": "OV_FIETS",
val fetchTime: Long,
val locationCode: String,
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package nl.ovfietsbeschikbaarheid.mapper

import dev.jordond.compass.Coordinates
import nl.ovfietsbeschikbaarheid.dto.LocationsDTO
import nl.ovfietsbeschikbaarheid.dto.OpenDTO
import nl.ovfietsbeschikbaarheid.ext.distanceTo
import nl.ovfietsbeschikbaarheid.model.LocationOverviewModel
import nl.ovfietsbeschikbaarheid.model.LocationOverviewWithDistanceModel
import dev.jordond.compass.Coordinates
import java.text.DecimalFormat
import kotlin.math.roundToInt

Expand All @@ -32,10 +31,8 @@ object LocationsMapper {
uri = toMap.link.uri,
locationCode = toMap.extra.locationCode,
stationCode = toMap.stationCode,
rentalBikesAvailable = toMap.extra.rentalBikes,
latitude = toMap.lat,
longitude = toMap.lng,
open = (toMap.open == OpenDTO.Yes || toMap.open == OpenDTO.Unknown)
)
}.sortedBy { it.title }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ data class LocationOverviewModel(
val uri: String,
val locationCode: String,
val stationCode: String,
val rentalBikesAvailable: Int?,
val open: Boolean,
val latitude: Double,
val longitude: Double,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nl.ovfietsbeschikbaarheid.repository

import android.content.Context
import kotlinx.serialization.json.Json
import nl.ovfietsbeschikbaarheid.R
import nl.ovfietsbeschikbaarheid.dto.LocationsDTO
import nl.ovfietsbeschikbaarheid.mapper.LocationsMapper
import nl.ovfietsbeschikbaarheid.model.LocationOverviewModel
import kotlinx.serialization.json.Json

class OverviewRepository(private val context: Context) {
private var allLocations = listOf<LocationOverviewModel>()
Expand All @@ -15,7 +15,7 @@ class OverviewRepository(private val context: Context) {
}

private fun loadLocations(context: Context): LocationsDTO {
val locationsStream = context.resources.openRawResource(R.raw.locaties)
val locationsStream = context.resources.openRawResource(R.raw.locations)
val inputAsString = locationsStream.bufferedReader().use { it.readText() }
return json.decodeFromString<LocationsDTO>(inputAsString)
// return httpClient.get("http://fiets.openov.nl/locaties.json").body<LocationsDTO>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,9 @@ fun SearchResultsPreview() {
val locations = listOf(
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Mahlerplein",
rentalBikesAvailable = 49
),
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Zuidplein",
rentalBikesAvailable = 148
),
)
TestHomeView("Amsterdam Zuid", HomeContent.SearchTermContent(locations, "Amsterdam Zuid", null))
Expand All @@ -406,11 +404,9 @@ fun SearchResultsLoadingNearbyPreview() {
val locations = listOf(
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Mahlerplein",
rentalBikesAvailable = 49
),
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Zuidplein",
rentalBikesAvailable = 148
),
)
TestHomeView("Amsterdam Zuid", HomeContent.SearchTermContent(locations, "Amsterdam Zuid", null))
Expand All @@ -422,26 +418,22 @@ fun SearchResultsNearbyPreview() {
val locations = listOf(
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Mahlerplein",
rentalBikesAvailable = 49
),
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Zuidplein",
rentalBikesAvailable = 148
),
)
val gpsLocations = listOf(
LocationOverviewWithDistanceModel(
"800 m",
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Mahlerplein",
rentalBikesAvailable = 49
)
),
LocationOverviewWithDistanceModel(
"1,1 km",
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Zuidplein",
rentalBikesAvailable = 148
)
),
)
Expand Down Expand Up @@ -492,14 +484,12 @@ fun GpsResultsPreview() {
"800 m",
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Mahlerplein",
rentalBikesAvailable = 49
)
),
LocationOverviewWithDistanceModel(
"1,1 km",
TestData.testLocationOverviewModel.copy(
title = "Amsterdam Zuid Zuidplein",
rentalBikesAvailable = 148
)
),
)
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/raw/locaties.json

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/res/raw/locations.json

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions app/src/test/java/nl/ovfietsbeschikbaarheid/LocationsCrawler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package nl.ovfietsbeschikbaarheid

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File

/**
* Not a test, rather a tool. Added in the test folder to prevent it being added in the app.
*/
object LocationsCrawler {
private val json = Json {
ignoreUnknownKeys = true
}

private val httpClient = HttpClient {
install(ContentNegotiation) {
json(json)
}
}

@JvmStatic
fun main(args: Array<String>): Unit = runBlocking {
val loaded = httpClient.get("http://fiets.openov.nl/locations.json").body<LocationsDTO>()
val asJson = json.encodeToString(loaded)
val file = File("app/src/main/res/raw/locations.json")
file.writeText(asJson)
}
}

@Serializable
data class LocationsDTO(
val locaties: Map<String, Location>
)

@Serializable
data class Location(
val description: String,
val stationCode: String,
val lat: Double,
val lng: Double,
val extra: LocationExtra,
val link: Link
)

@Serializable
data class Link(
val uri: String
)

@Serializable
data class LocationExtra(
val locationCode: String,
)

0 comments on commit 98e774c

Please sign in to comment.