Skip to content

Commit

Permalink
Move APIs with doubles for lng/lat to use LngLatAlt instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
davecraig committed Feb 5, 2025
1 parent 0bf65a1 commit dea1a8a
Show file tree
Hide file tree
Showing 27 changed files with 170 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.scottishtecharmy.soundscape.audio.AudioEngine
import org.scottishtecharmy.soundscape.audio.NativeAudioEngine
import org.junit.Assert
import org.junit.Test
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt

class AudioEngineTest {

Expand Down Expand Up @@ -60,14 +61,14 @@ class AudioEngineTest {
fun soundBeacon() {
val audioEngine = initializeAudioEngine()

val beacon = audioEngine.createBeacon(1.0, 0.0)
val beacon = audioEngine.createBeacon(LngLatAlt(1.0, 0.0))
moveListener(audioEngine, 4000)
audioEngine.destroyBeacon(beacon)

audioEngine.createTextToSpeech("Beacon here!")
moveListener(audioEngine, 4000)

val beacon3 = audioEngine.createBeacon(1.0, 0.0)
val beacon3 = audioEngine.createBeacon(LngLatAlt(1.0, 0.0))
moveListener(audioEngine, 4000)
audioEngine.destroyBeacon(beacon3)

Expand All @@ -85,7 +86,7 @@ class AudioEngineTest {
Log.d(TAG, "Test beacon type $beaconType")
audioEngine.setBeaconType(beaconType)

val beacon = audioEngine.createBeacon(1.0, 0.0)
val beacon = audioEngine.createBeacon(LngLatAlt(1.0, 0.0))
moveListener(audioEngine, 6000)
audioEngine.destroyBeacon(beacon)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.scottishtecharmy.soundscape.database.local.model.Location
import org.scottishtecharmy.soundscape.database.local.model.RouteData
import org.scottishtecharmy.soundscape.database.local.model.MarkerData
import org.scottishtecharmy.soundscape.database.repository.RoutesRepository
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import org.scottishtecharmy.soundscape.screens.home.Navigator
import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription
import org.scottishtecharmy.soundscape.screens.home.locationDetails.generateLocationDetailsRoute
Expand Down Expand Up @@ -54,8 +55,7 @@ class SoundscapeIntents
val ld =
LocationDescription(
addressName = address.getAddressLine(0),
latitude = address.latitude,
longitude = address.longitude,
location = LngLatAlt(address.longitude, address.latitude)
)
navigator.navigate(generateLocationDetailsRoute(ld))
}
Expand All @@ -72,8 +72,7 @@ class SoundscapeIntents
val ld =
LocationDescription(
addressName = address.getAddressLine(0),
latitude = address.latitude,
longitude = address.longitude,
location = LngLatAlt(address.longitude, address.latitude)
)
navigator.navigate(generateLocationDetailsRoute(ld))
}
Expand Down Expand Up @@ -193,8 +192,7 @@ class SoundscapeIntents
// Switch to Street Preview mode
mainActivity.soundscapeServiceConnection.setStreetPreviewMode(
true,
latitude.toDouble(),
longitude.toDouble(),
LngLatAlt(longitude.toDouble(), latitude.toDouble())
)
} else {
try {
Expand All @@ -205,8 +203,7 @@ class SoundscapeIntents
val ld =
LocationDescription(
addressName = URLEncoder.encode(uriData, "utf-8"),
latitude = latitude.toDouble(),
longitude = longitude.toDouble(),
location = LngLatAlt(longitude.toDouble(), latitude.toDouble())
)
mainActivity.navigator.navigate(generateLocationDetailsRoute(ld))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class SoundscapeServiceConnection @Inject constructor() {
return soundscapeService?.streetPreviewFlow
}

fun setStreetPreviewMode(on : Boolean, latitude: Double = 0.0, longitude: Double = 0.0) {
fun setStreetPreviewMode(on : Boolean, location: LngLatAlt? = null) {
Log.d(TAG, "setStreetPreviewMode $on")
soundscapeService?.setStreetPreviewMode(on, latitude, longitude)
soundscapeService?.setStreetPreviewMode(on, location)
}

fun startRoute(routeName: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.scottishtecharmy.soundscape.audio

import android.content.SharedPreferences
import android.speech.tts.Voice
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import java.util.Locale

interface AudioEngine {
fun createBeacon(latitude: Double, longitude: Double) : Long
fun createBeacon(location: LngLatAlt) : Long
fun destroyBeacon(beaconHandle : Long)
fun createTextToSpeech(text: String, latitude: Double = Double.NaN, longitude: Double = Double.NaN) : Long
fun createEarcon(asset: String, latitude: Double = Double.NaN, longitude: Double = Double.NaN) : Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import org.scottishtecharmy.soundscape.MainActivity
import org.scottishtecharmy.soundscape.R
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import org.scottishtecharmy.soundscape.utils.getCurrentLocale
import java.util.Locale
import javax.inject.Inject
Expand Down Expand Up @@ -184,12 +185,12 @@ class NativeAudioEngine @Inject constructor(): AudioEngine, TextToSpeech.OnInitL
}
}

override fun createBeacon(latitude: Double, longitude: Double) : Long
override fun createBeacon(location: LngLatAlt) : Long
{
synchronized(engineMutex) {
if(engineHandle != 0L) {
Log.d(TAG, "Call createNativeBeacon")
return createNativeBeacon(engineHandle, latitude, longitude)
return createNativeBeacon(engineHandle, location.latitude, location.longitude)
}

return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription
import org.scottishtecharmy.soundscape.ui.theme.Foreground2
import org.scottishtecharmy.soundscape.ui.theme.IntroductionTheme
Expand Down Expand Up @@ -100,8 +101,7 @@ fun PreviewSearchItemButton() {
addressName = "Bristol",
fullAddress = "18 Street \n59000 Lille\nFrance",
distance = "17 Km",
latitude = 9.55,
longitude = 8.00,
location = LngLatAlt(8.00, 9.55)
)
LocationItem(
item = test,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.PrimaryKey
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import kotlin.Double.Companion.NaN

class Location : EmbeddedRealmObject {
Expand Down Expand Up @@ -40,6 +41,8 @@ class Location : EmbeddedRealmObject {
set(value) {
coordinates[0] = value
}

fun location(): LngLatAlt { return LngLatAlt(longitude, latitude) }
}

class RouteData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,8 @@ class GeoEngine {
}
}
if(geocode != null) {
var distance = locationProvider.get().distance(LngLatAlt(geocode.longitude, geocode.latitude))
if(distance > 1000) {
val km = (distance.toInt() / 100).toFloat() / 10
geocode.distance =
localizedContext.getString(R.string.distance_format_km, km.toString())
} else {
val m = distance.toInt()
geocode.distance =
localizedContext.getString(R.string.distance_format_meters, m.toString())
}
val distance = locationProvider.get().distance(geocode.location)
geocode.distance = formatDistance(distance, localizedContext)
return geocode
}

Expand All @@ -566,15 +558,11 @@ class GeoEngine {

// The geocode result includes the location for the POI. In the case of something
// like a park this could be a long way from the point that was passed in.
val ld = result?.features?.toLocationDescriptions(
currentLocationLatitude = currentLocation.latitude,
currentLocationLongitude = currentLocation.longitude
)
val ld = result?.features?.toLocationDescriptions(currentLocation, localizedContext)
if (!ld.isNullOrEmpty()) {
if(preserveLocation) {
val overwritten = ld.first()
overwritten.latitude = location.latitude
overwritten.longitude = location.longitude
overwritten.location = location
if(overwritten.addressName != null) {
overwritten.addressName = localizedContext.getString(R.string.directions_near_name).format(overwritten.addressName)
overwritten
Expand Down Expand Up @@ -680,8 +668,7 @@ fun localReverseGeocode(location: LngLatAlt,
if(name != null) {
return LocationDescription(
addressName = localizedContext.getString(R.string.directions_at_poi).format(name as String),
longitude = location.longitude,
latitude = location.latitude,
location = location,
)
}
}
Expand All @@ -699,8 +686,7 @@ fun localReverseGeocode(location: LngLatAlt,
}
return LocationDescription(
addressName = localizedContext.getString(R.string.directions_near_name).format(roadName as String),
longitude = location.longitude,
latitude = location.latitude
location = location,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scottishtecharmy.soundscape.geojsonparser.geojson

import com.squareup.moshi.JsonClass
import org.maplibre.android.geometry.LatLng
import org.scottishtecharmy.soundscape.geoengine.utils.distance
import java.io.Serializable

Expand Down Expand Up @@ -35,6 +36,10 @@ open class LngLatAlt(
return "$longitude,$latitude"
}

fun toLatLng(): LatLng {
return LatLng(latitude, longitude)
}

fun distance(other: LngLatAlt): Double {
return distance(latitude, longitude, other.latitude, other.longitude)
}
Expand All @@ -52,6 +57,8 @@ open class LngLatAlt(
* Distance to a LineString from current location.
* @param lineStringCoordinates
* LineString that we are working out the distance from
* @param nearestPoint
* Point in the line nearest that had the shortest distance
* @return The distance of the point to the LineString
*/
fun distanceToLineString(
Expand Down Expand Up @@ -79,4 +86,8 @@ open class LngLatAlt(
}
return shortestDistance
}
}
}

fun fromLatLng(loc:LatLng): LngLatAlt {
return LngLatAlt(loc.longitude, loc.latitude)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.io.InputStream

class GpxDrivenProvider {

var locationProvider = StaticLocationProvider(0.0,0.0)
var locationProvider = StaticLocationProvider(LngLatAlt())
var directionProvider = DirectionProvider()

private var parsedGpx: Gpx? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import android.location.Location
import android.location.LocationManager
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt

class StaticLocationProvider(private var latitude: Double, private var longitude: Double) :
class StaticLocationProvider(private var location: LngLatAlt) :
LocationProvider() {

override fun destroy() {
}

override fun start(context : Context){
// Simply set our flow source as the passed in location with 10m accuracy so that it's not ignored
val location = Location(LocationManager.PASSIVE_PROVIDER)
location.latitude = latitude
location.longitude = longitude
location.accuracy = 10.0F
mutableLocationFlow.value = location
val passiveLocation = Location(LocationManager.PASSIVE_PROVIDER)
passiveLocation.latitude = location.latitude
passiveLocation.longitude = location.longitude
passiveLocation.accuracy = 10.0F
mutableLocationFlow.value = passiveLocation
}

override fun updateLocation(newLocation: LngLatAlt, speed: Float) {
val location = Location(LocationManager.PASSIVE_PROVIDER)
location.latitude = newLocation.latitude
location.longitude = newLocation.longitude
location.speed = speed
location.accuracy = 10.0F
mutableLocationFlow.value = location
val passiveLocation = Location(LocationManager.PASSIVE_PROVIDER)
passiveLocation.latitude = newLocation.latitude
passiveLocation.longitude = newLocation.longitude
passiveLocation.speed = speed
passiveLocation.accuracy = 10.0F
mutableLocationFlow.value = passiveLocation
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ fun HomeScreen(
composable(HomeRoutes.Home.route) {
val context = LocalContext.current
Home(
latitude = state.value.location?.latitude,
longitude = state.value.location?.longitude,
location = state.value.location,
beaconLocation = state.value.beaconLocation,
heading = state.value.heading,
onNavigate = { dest -> navController.navigate(dest) },
Expand Down Expand Up @@ -137,8 +136,7 @@ fun HomeScreen(
}
}
},
latitude = state.value.location?.latitude,
longitude = state.value.location?.longitude,
location = state.value.location,
navController = navController,
heading = state.value.heading,
modifier = Modifier.windowInsetsPadding(WindowInsets.safeDrawing),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.scottishtecharmy.soundscape.screens.home.data

import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt

data class LocationDescription(
var addressName: String? = null,
val fullAddress: String? = null,
val country: String? = null,
var distance: String? = null,
var latitude: Double = Double.NaN,
var longitude: Double = Double.NaN,
var location: LngLatAlt = LngLatAlt(),
val marker: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.scottishtecharmy.soundscape.MainActivity
import org.scottishtecharmy.soundscape.R
import org.scottishtecharmy.soundscape.components.MainSearchBar
import org.scottishtecharmy.soundscape.geoengine.StreetPreviewState
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import org.scottishtecharmy.soundscape.screens.home.DrawerContent
import org.scottishtecharmy.soundscape.screens.home.HomeRoutes
import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription
Expand All @@ -45,8 +46,7 @@ import org.scottishtecharmy.soundscape.ui.theme.OnPrimary
@Composable
fun HomePreview() {
Home(
latitude = null,
longitude = null,
location = null,
beaconLocation = null,
heading = 0.0f,
onNavigate = {},
Expand All @@ -72,9 +72,8 @@ fun HomePreview() {

@Composable
fun Home(
latitude: Double?,
longitude: Double?,
beaconLocation: LatLng?,
location: LngLatAlt?,
beaconLocation: LngLatAlt?,
heading: Float,
onNavigate: (String) -> Unit,
onMapLongClick: (LatLng) -> Boolean,
Expand Down Expand Up @@ -131,8 +130,7 @@ fun Home(
contentWindowInsets = WindowInsets(0, 0, 0, 0),
) { innerPadding ->
HomeContent(
latitude = latitude,
longitude = longitude,
location = location,
beaconLocation = beaconLocation,
heading = heading,
modifier = Modifier.padding(innerPadding),
Expand Down
Loading

0 comments on commit dea1a8a

Please sign in to comment.