Skip to content

Commit

Permalink
Move RoutePoint to MarkerData
Browse files Browse the repository at this point in the history
This was a duplication of code, but MarkerData is more neatly written, so
use it.
  • Loading branch information
davecraig committed Feb 5, 2025
1 parent d6c6d28 commit 203b35f
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 82 deletions.
62 changes: 30 additions & 32 deletions app/src/androidTest/java/org/scottishtecharmy/soundscape/GpxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.scottishtecharmy.soundscape.database.local.dao.RoutesDao
import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.MarkerData
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.RoutePoint
import org.scottishtecharmy.soundscape.database.repository.RoutesRepository
import org.scottishtecharmy.soundscape.utils.parseGpxFile

Expand All @@ -26,7 +25,7 @@ import org.scottishtecharmy.soundscape.utils.parseGpxFile
class GpxTest {
private fun testParsing(
filename: String,
expectedValues: List<RoutePoint>,
expectedValues: List<MarkerData>,
expectedName: String = "",
expectedDescription: String = "",
name_override: String? = null,
Expand All @@ -42,7 +41,7 @@ class GpxTest {
for (point in routeData.waypoints) {
Log.d(
"gpxTest",
"Point: " + point.name + " " + point.location?.latitude + " " + point.location?.longitude,
"Point: " + point.addressName + " " + point.location?.latitude + " " + point.location?.longitude,
)

// Lookup the point in the expected values map
Expand All @@ -61,9 +60,8 @@ class GpxTest {
schema =
setOf(
RouteData::class,
RoutePoint::class,
Location::class,
MarkerData::class,
Location::class
),
).inMemory()
.build()
Expand All @@ -88,7 +86,7 @@ class GpxTest {

private fun testDatabase(
name: String,
expectedValues: List<RoutePoint>,
expectedValues: List<MarkerData>,
) {
// Open the database
val config =
Expand All @@ -97,7 +95,7 @@ class GpxTest {
schema =
setOf(
RouteData::class,
RoutePoint::class,
MarkerData::class,
Location::class,
),
).inMemory()
Expand Down Expand Up @@ -186,26 +184,26 @@ class GpxTest {
}
}

private fun expectedHandcraftedValues(): List<RoutePoint> {
val waypoint1 = RoutePoint("George Square, Glasgow", Location(55.8610697, -4.2499327))
val waypoint2 = RoutePoint("Edinburgh Castle", Location(55.9488161, -3.2021476))
val waypoint3 = RoutePoint("Greenwich Prime Meridian, London", Location(51.4779644, 0.0))
val waypoint4 = RoutePoint("Rock of Gibraltar", Location(36.1636308, -5.392716))
val waypoint5 = RoutePoint("Inverness", Location(57.4680357, -4.263057))
private fun expectedHandcraftedValues(): List<MarkerData> {
val waypoint1 = MarkerData("George Square, Glasgow", Location(55.8610697, -4.2499327))
val waypoint2 = MarkerData("Edinburgh Castle", Location(55.9488161, -3.2021476))
val waypoint3 = MarkerData("Greenwich Prime Meridian, London", Location(51.4779644, 0.0))
val waypoint4 = MarkerData("Rock of Gibraltar", Location(36.1636308, -5.392716))
val waypoint5 = MarkerData("Inverness", Location(57.4680357, -4.263057))

return listOf(waypoint1, waypoint2, waypoint3, waypoint4, waypoint5)
}

private fun expectedRideWithGpsValues(): List<RoutePoint> {
val waypoint1 = RoutePoint("Slight Left", Location(55.94722, -4.30844))
val waypoint2 = RoutePoint("Right", Location(55.94628, -4.30901))
val waypoint3 = RoutePoint("Right", Location(55.9442, -4.31081))
val waypoint4 = RoutePoint("Left", Location(55.94245, -4.31335))
val waypoint5 = RoutePoint("Right", Location(55.94181, -4.31338))
val waypoint6 = RoutePoint("Right", Location(55.94182, -4.31358))
val waypoint7 = RoutePoint("Right", Location(55.94198, -4.31612))
val waypoint8 = RoutePoint("Left", Location(55.94229, -4.31637))
val waypoint9 = RoutePoint("Right", Location(55.94218, -4.31735))
private fun expectedRideWithGpsValues(): List<MarkerData> {
val waypoint1 = MarkerData("Slight Left", Location(55.94722, -4.30844))
val waypoint2 = MarkerData("Right", Location(55.94628, -4.30901))
val waypoint3 = MarkerData("Right", Location(55.9442, -4.31081))
val waypoint4 = MarkerData("Left", Location(55.94245, -4.31335))
val waypoint5 = MarkerData("Right", Location(55.94181, -4.31338))
val waypoint6 = MarkerData("Right", Location(55.94182, -4.31358))
val waypoint7 = MarkerData("Right", Location(55.94198, -4.31612))
val waypoint8 = MarkerData("Left", Location(55.94229, -4.31637))
val waypoint9 = MarkerData("Right", Location(55.94218, -4.31735))

return listOf(
waypoint1,
Expand All @@ -220,15 +218,15 @@ class GpxTest {
)
}

private fun expectedSoundscapeValues(): List<RoutePoint> {
val waypoint1 = RoutePoint("Waypoint", Location(55.947256, -4.305852))
val waypoint2 = RoutePoint("Waypoint", Location(55.946412, -4.305621))
val waypoint3 = RoutePoint("Waypoint", Location(55.946409, -4.304833))
val waypoint4 = RoutePoint("Waypoint", Location(55.945706, -4.304774))
val waypoint5 = RoutePoint("Waypoint", Location(55.945727, -4.307563))
val waypoint6 = RoutePoint("Waypoint", Location(55.946424, -4.307574))
val waypoint7 = RoutePoint("Waypoint", Location(55.946436, -4.305745))
val waypoint8 = RoutePoint("Waypoint", Location(55.947286, -4.305696))
private fun expectedSoundscapeValues(): List<MarkerData> {
val waypoint1 = MarkerData("Waypoint", Location(55.947256, -4.305852))
val waypoint2 = MarkerData("Waypoint", Location(55.946412, -4.305621))
val waypoint3 = MarkerData("Waypoint", Location(55.946409, -4.304833))
val waypoint4 = MarkerData("Waypoint", Location(55.945706, -4.304774))
val waypoint5 = MarkerData("Waypoint", Location(55.945727, -4.307563))
val waypoint6 = MarkerData("Waypoint", Location(55.946424, -4.307574))
val waypoint7 = MarkerData("Waypoint", Location(55.946436, -4.305745))
val waypoint8 = MarkerData("Waypoint", Location(55.947286, -4.305696))

return listOf(
waypoint1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.scottishtecharmy.soundscape.database.local.RealmConfiguration
import org.scottishtecharmy.soundscape.database.local.dao.RoutesDao
import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.RoutePoint
import org.scottishtecharmy.soundscape.database.local.model.MarkerData
import org.scottishtecharmy.soundscape.database.repository.RoutesRepository
import org.scottishtecharmy.soundscape.screens.home.Navigator
import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription
Expand Down Expand Up @@ -255,7 +255,7 @@ class SoundscapeIntents
val estimatedAddress = marker.getString("estimatedAddress")

routeData.waypoints.add(
RoutePoint(name, Location(latitude, longitude))
MarkerData(name, Location(latitude, longitude), estimatedAddress)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.realm.kotlin.RealmConfiguration
import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.MarkerData
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.RoutePoint

object RealmConfiguration {
private var markersRealm: Realm? = null
Expand All @@ -32,9 +31,8 @@ object RealmConfiguration {
val config = RealmConfiguration.Builder(
schema = setOf(
RouteData::class,
RoutePoint::class,
Location::class,
MarkerData::class,
MarkerData::class
)
).name("MarkersAndRoutes").build()

Expand All @@ -44,7 +42,14 @@ object RealmConfiguration {
Log.d("RealmConfiguration", "Delete Markers database")
deleteRealm(config)
}
markersRealm = Realm.open(config)
try {
markersRealm = Realm.open(config)
} catch (e: Exception) {
// Delete and re-open database
Log.e("RealmConfiguration", "Failed to open database: ${e.message}")
deleteRealm(config)
markersRealm = Realm.open(config)
}
}

return markersRealm!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.realm.kotlin.types.geo.GeoCircle
import io.realm.kotlin.types.geo.GeoPoint
import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.RoutePoint
import org.scottishtecharmy.soundscape.database.local.model.MarkerData

class RoutesDao(private val realm: Realm) {

Expand All @@ -31,7 +31,7 @@ class RoutesDao(private val realm: Realm) {
return success
}

suspend fun insertWaypoint(waypoint: RoutePoint) : Boolean
suspend fun insertWaypoint(waypoint: MarkerData) : Boolean
{
// If we don't catch the exception here, then it prevents write
// from completing its transaction logic.
Expand All @@ -55,20 +55,20 @@ class RoutesDao(private val realm: Realm) {
return realm.query<RouteData>().find()
}

fun getWaypoints(): List<RoutePoint> {
return realm.query<RoutePoint>().find()
fun getWaypoints(): List<MarkerData> {
return realm.query<MarkerData>().find()
}

@OptIn(ExperimentalGeoSpatialApi::class)
fun getWaypointsNear(location: Location?, kilometre: Double): RealmResults<RoutePoint> {
fun getWaypointsNear(location: Location?, kilometre: Double): RealmResults<MarkerData> {
if(location != null) {
val circle1 = GeoCircle.create(
center = GeoPoint.create(location.latitude, location.longitude),
radius = Distance.fromKilometers(kilometre)
)
return realm.query<RoutePoint>("location GEOWITHIN $circle1").find()
return realm.query<MarkerData>("location GEOWITHIN $circle1").find()
}
return realm.query<RoutePoint>().find()
return realm.query<MarkerData>().find()
}

suspend fun deleteRoute(name: String) = realm.write {
Expand All @@ -78,7 +78,7 @@ class RoutesDao(private val realm: Realm) {
delete(findRoute)

// Clean up all waypoints which have no route
val waypoints = query<RoutePoint>("route.@count == 0").find()
val waypoints = query<MarkerData>("route.@count == 0").find()
Log.d("routeDao", "Deleting waypoints with no route" + waypoints.size)
delete(waypoints)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package org.scottishtecharmy.soundscape.database.local.model

import io.realm.kotlin.ext.backlinks
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.RealmObject
import kotlin.Double.Companion.NaN

class MarkerData(
var addressName: String,
var fullAddress: String,
var location: Location?,
var fullAddress: String = ""
) : RealmObject {
constructor() : this("", "", Location(NaN, NaN))
}
constructor() : this("", Location(NaN, NaN))

val route: RealmResults<RouteData> by backlinks(RouteData::waypoints)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as MarkerData
return (addressName == other.addressName) &&
(location?.latitude == other.location?.latitude) &&
(location?.longitude == other.location?.longitude)
}}
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,13 @@ class Location : EmbeddedRealmObject {
}
}

class RoutePoint(
var name: String,
location: Location,
) : RealmObject {
constructor() : this("", Location(NaN, NaN))

var location: Location? = location
val route: RealmResults<RouteData> by backlinks(RouteData::waypoints)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as RoutePoint
return (name == other.name) &&
(location?.latitude == other.location?.latitude) &&
(location?.longitude == other.location?.longitude)
}
}

class RouteData(
@PrimaryKey var name: String,
var description: String,
) : RealmObject {
constructor() : this("", "")

var waypoints: RealmList<RoutePoint> = realmListOf()
var waypoints: RealmList<MarkerData> = realmListOf()

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import kotlinx.coroutines.withContext
import org.scottishtecharmy.soundscape.database.local.dao.RoutesDao
import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.RoutePoint
import org.scottishtecharmy.soundscape.database.local.model.MarkerData

class RoutesRepository(private val routesDao: RoutesDao) {

suspend fun insertRoute(route: RouteData) = withContext(Dispatchers.IO) {
routesDao.insertRoute(route)
}

suspend fun insertWaypoint(waypoint: RoutePoint) = withContext(Dispatchers.IO) {
suspend fun insertWaypoint(waypoint: MarkerData) = withContext(Dispatchers.IO) {
routesDao.insertWaypoint(waypoint)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ class MarkersViewModel
LocationDescription(
addressName = it.addressName,
fullAddress = it.fullAddress,
latitude = it.location?.latitude ?: 0.0,
longitude = it.location?.longitude ?: 0.0,
distance =
if (userLocation != null && it.location != null) {
userLocation.calculateDistanceTo(it.location!!.latitude, it.location!!.longitude)
} else {
null
},
userLocation?.calculateDistanceTo(
it.location?.latitude ?: userLocation.latitude,
it.location?.longitude ?: userLocation.longitude
),
marker = true,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RoutePlayer(val service: SoundscapeService) {
val location = route.waypoints[index].location!!

service.audioEngine.clearTextToSpeechQueue()
service.audioEngine.createTextToSpeech("Move to marker ${index+1}, ${route.waypoints[index].name}", location.latitude, location.longitude)
service.audioEngine.createTextToSpeech("Move to marker ${index+1}, ${route.waypoints[index].addressName}", location.latitude, location.longitude)

service.createBeacon(location.latitude, location.longitude)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ class RoutePlayer(val service: SoundscapeService) {
var state = ""
state += "Route : ${route.name}\n"
for((index, waypoint) in route.waypoints.withIndex()) {
state += " ${waypoint.name} at ${waypoint.location?.latitude},${waypoint.location?.longitude}"
state += " ${waypoint.addressName} at ${waypoint.location?.latitude},${waypoint.location?.longitude}"
state += if(index == currentMarker) {
" <current>\n"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.ticofab.androidgpxparser.parser.GPXParser
import io.ticofab.androidgpxparser.parser.domain.Gpx
import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.RoutePoint
import org.scottishtecharmy.soundscape.database.local.model.MarkerData
import org.xmlpull.v1.XmlPullParserException
import java.io.IOException
import java.io.InputStream
Expand All @@ -29,14 +29,14 @@ fun parseGpxFile(input : InputStream) : RouteData {
parsedGpx.routes.forEach { route ->
Log.d("gpx", "RoutePoint " + route.routeName)
route.routePoints.forEach { waypoint ->
val point = RoutePoint(waypoint.name, Location(waypoint.latitude, waypoint.longitude))
val point = MarkerData(waypoint.name, Location(waypoint.latitude, waypoint.longitude))
routeData.waypoints.add(point)
}
}
if(routeData.waypoints.isEmpty()) {
parsedGpx.wayPoints.forEach { waypoint ->
Log.d("gpx", "WayPoint " + waypoint.name)
val point = RoutePoint(waypoint.name, Location(waypoint.latitude, waypoint.longitude))
val point = MarkerData(waypoint.name, Location(waypoint.latitude, waypoint.longitude))
routeData.waypoints.add(point)
}
}
Expand Down

0 comments on commit 203b35f

Please sign in to comment.