From dea1a8ad840e1edf6240ea4fd071801e4c0d8767 Mon Sep 17 00:00:00 2001 From: Dave Craig Date: Fri, 31 Jan 2025 13:41:29 +0000 Subject: [PATCH] Move APIs with doubles for lng/lat to use LngLatAlt instead. --- .../soundscape/AudioEngineTest.kt | 7 +- .../soundscape/SoundscapeIntents.kt | 13 ++-- .../soundscape/SoundscapeServiceConnection.kt | 4 +- .../soundscape/audio/AudioEngine.kt | 3 +- .../soundscape/audio/NativeAudioEngine.kt | 5 +- .../soundscape/components/LocationItem.kt | 4 +- .../database/local/model/RouteData.kt | 3 + .../soundscape/geoengine/GeoEngine.kt | 26 ++----- .../geojsonparser/geojson/LngLatAlt.kt | 13 +++- .../locationprovider/GpxDrivenProvider.kt | 2 +- .../StaticLocationProvider.kt | 24 +++--- .../soundscape/screens/home/HomeScreen.kt | 6 +- .../screens/home/data/LocationDescription.kt | 5 +- .../soundscape/screens/home/home/Home.kt | 12 ++- .../screens/home/home/HomeContent.kt | 26 +++---- .../screens/home/home/MapContainerLibre.kt | 21 +++--- .../locationDetails/LocationDetailsScreen.kt | 74 +++++++------------ .../screens/markersscreen/MarkersScreen.kt | 4 +- .../markersscreen/MarkersScreenList.kt | 1 - .../screens/markersscreen/MarkersViewModel.kt | 20 +++-- .../audiobeacons/AudioBeaconsViewModel.kt | 3 +- .../soundscape/services/RoutePlayer.kt | 3 +- .../soundscape/services/SoundscapeService.kt | 18 +++-- .../soundscape/utils/LocationExt.kt | 53 ++----------- .../viewmodels/LocationDetailsViewModel.kt | 17 ++--- .../soundscape/viewmodels/home/HomeState.kt | 5 +- .../viewmodels/home/HomeViewModel.kt | 26 +++---- 27 files changed, 170 insertions(+), 228 deletions(-) diff --git a/app/src/androidTest/java/org/scottishtecharmy/soundscape/AudioEngineTest.kt b/app/src/androidTest/java/org/scottishtecharmy/soundscape/AudioEngineTest.kt index 5201d2b5..9d7ebde5 100644 --- a/app/src/androidTest/java/org/scottishtecharmy/soundscape/AudioEngineTest.kt +++ b/app/src/androidTest/java/org/scottishtecharmy/soundscape/AudioEngineTest.kt @@ -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 { @@ -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) @@ -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) } diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeIntents.kt b/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeIntents.kt index b2b8de52..c4b9e7d0 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeIntents.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeIntents.kt @@ -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 @@ -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)) } @@ -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)) } @@ -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 { @@ -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)) } diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeServiceConnection.kt b/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeServiceConnection.kt index 35c251d9..55963a7d 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeServiceConnection.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeServiceConnection.kt @@ -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) { diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/audio/AudioEngine.kt b/app/src/main/java/org/scottishtecharmy/soundscape/audio/AudioEngine.kt index bb9a48c8..13c3aba1 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/audio/AudioEngine.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/audio/AudioEngine.kt @@ -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 diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/audio/NativeAudioEngine.kt b/app/src/main/java/org/scottishtecharmy/soundscape/audio/NativeAudioEngine.kt index ef17a443..43cb95ab 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/audio/NativeAudioEngine.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/audio/NativeAudioEngine.kt @@ -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 @@ -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 diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/components/LocationItem.kt b/app/src/main/java/org/scottishtecharmy/soundscape/components/LocationItem.kt index 5c99cfab..fa32c218 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/components/LocationItem.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/components/LocationItem.kt @@ -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 @@ -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, diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/database/local/model/RouteData.kt b/app/src/main/java/org/scottishtecharmy/soundscape/database/local/model/RouteData.kt index 23ab3ee6..c8fc6aeb 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/database/local/model/RouteData.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/database/local/model/RouteData.kt @@ -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 { @@ -40,6 +41,8 @@ class Location : EmbeddedRealmObject { set(value) { coordinates[0] = value } + + fun location(): LngLatAlt { return LngLatAlt(longitude, latitude) } } class RouteData( diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/geoengine/GeoEngine.kt b/app/src/main/java/org/scottishtecharmy/soundscape/geoengine/GeoEngine.kt index fc6aa9aa..10291c8b 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/geoengine/GeoEngine.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/geoengine/GeoEngine.kt @@ -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 } @@ -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 @@ -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, ) } } @@ -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, ) } } diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/geojsonparser/geojson/LngLatAlt.kt b/app/src/main/java/org/scottishtecharmy/soundscape/geojsonparser/geojson/LngLatAlt.kt index 20c023f7..149e826a 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/geojsonparser/geojson/LngLatAlt.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/geojsonparser/geojson/LngLatAlt.kt @@ -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 @@ -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) } @@ -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( @@ -79,4 +86,8 @@ open class LngLatAlt( } return shortestDistance } -} \ No newline at end of file +} + +fun fromLatLng(loc:LatLng): LngLatAlt { + return LngLatAlt(loc.longitude, loc.latitude) +} diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/GpxDrivenProvider.kt b/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/GpxDrivenProvider.kt index 14386382..59f5d3e5 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/GpxDrivenProvider.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/GpxDrivenProvider.kt @@ -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 diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/StaticLocationProvider.kt b/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/StaticLocationProvider.kt index 0c9d5dcf..93f75949 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/StaticLocationProvider.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/locationprovider/StaticLocationProvider.kt @@ -5,7 +5,7 @@ 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() { @@ -13,20 +13,20 @@ class StaticLocationProvider(private var latitude: Double, private var longitude 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 { diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/HomeScreen.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/HomeScreen.kt index 07714ef0..60e86396 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/HomeScreen.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/HomeScreen.kt @@ -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) }, @@ -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), diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/data/LocationDescription.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/data/LocationDescription.kt index 100a3258..58697837 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/data/LocationDescription.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/data/LocationDescription.kt @@ -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 ) diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/Home.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/Home.kt index 0655d2ca..aa734fe3 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/Home.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/Home.kt @@ -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 @@ -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 = {}, @@ -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, @@ -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), diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/HomeContent.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/HomeContent.kt index 80313608..44fae29b 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/HomeContent.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/HomeContent.kt @@ -12,15 +12,15 @@ import org.maplibre.android.geometry.LatLng import org.scottishtecharmy.soundscape.R import org.scottishtecharmy.soundscape.components.NavigationButton import org.scottishtecharmy.soundscape.geoengine.StreetPreviewState +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import org.scottishtecharmy.soundscape.screens.home.HomeRoutes import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription import org.scottishtecharmy.soundscape.screens.home.locationDetails.generateLocationDetailsRoute @Composable fun HomeContent( - latitude: Double?, - longitude: Double?, - beaconLocation: LatLng?, + location: LngLatAlt?, + beaconLocation: LngLatAlt?, heading: Float, onNavigate: (String) -> Unit, onMapLongClick: (LatLng) -> Boolean, @@ -38,8 +38,7 @@ fun HomeContent( verticalArrangement = Arrangement.spacedBy(16.dp), ) { if(streetPreviewState.enabled) { - if(latitude != null && longitude != null) - StreetPreview(streetPreviewState, heading, streetPreviewGo, streetPreviewExit) + StreetPreview(streetPreviewState, heading, streetPreviewGo, streetPreviewExit) } else { searchBar() @@ -55,8 +54,7 @@ fun HomeContent( val ld = LocationDescription( addressName = "Barrowland Ballroom", - latitude = 55.8552688, - longitude = -4.2366753, + location = LngLatAlt(-4.2366753, 55.8552688) ) onNavigate(generateLocationDetailsRoute(ld)) }, @@ -72,7 +70,7 @@ fun HomeContent( // Current location NavigationButton( onClick = { - if (latitude != null && longitude != null) { + if (location != null) { val ld = getCurrentLocationDescription() onNavigate(generateLocationDetailsRoute(ld)) } @@ -81,13 +79,13 @@ fun HomeContent( ) } } - if (latitude != null && longitude != null) { + if (location != null) { MapContainerLibre( beaconLocation = beaconLocation, - mapCenter = LatLng(latitude, longitude), + mapCenter = location, allowScrolling = false, mapViewRotation = 0.0F, - userLocation = LatLng(latitude, longitude), + userLocation = location, userSymbolRotation = heading, onMapLongClick = onMapLongClick, onMarkerClick = onMarkerClick, @@ -101,8 +99,7 @@ fun HomeContent( @Composable fun StreetPreviewHomeContent() { HomeContent( - latitude = null, - longitude = null, + location = null, beaconLocation = null, heading = 0.0f, onNavigate = {}, @@ -121,8 +118,7 @@ fun StreetPreviewHomeContent() { @Composable fun PreviewHomeContent() { HomeContent( - latitude = null, - longitude = null, + location = null, beaconLocation = null, heading = 0.0f, onNavigate = {}, diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/MapContainerLibre.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/MapContainerLibre.kt index 4e2bd9f0..2ef0975e 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/MapContainerLibre.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/home/MapContainerLibre.kt @@ -30,6 +30,7 @@ import org.maplibre.android.style.sources.GeoJsonSource import org.maplibre.geojson.FeatureCollection import org.scottishtecharmy.soundscape.BuildConfig import org.scottishtecharmy.soundscape.R +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import java.io.File const val USER_POSITION_MARKER_NAME = "USER_POSITION_MARKER_NAME" @@ -47,12 +48,12 @@ const val USER_POSITION_MARKER_NAME = "USER_POSITION_MARKER_NAME" */ @Composable fun MapContainerLibre( - mapCenter: LatLng, + mapCenter: LngLatAlt, allowScrolling: Boolean, mapViewRotation: Float, - userLocation: LatLng, + userLocation: LngLatAlt, userSymbolRotation: Float, - beaconLocation: LatLng?, + beaconLocation: LngLatAlt?, modifier: Modifier = Modifier, onMapLongClick: (LatLng) -> Boolean, onMarkerClick: (Marker) -> Boolean, @@ -64,13 +65,13 @@ fun MapContainerLibre( // been disallowed val cp = CameraPosition.Builder().bearing(mapViewRotation.toDouble()) if(!allowScrolling) - cp.target(mapCenter) + cp.target(mapCenter.toLatLng()) cp.build() } val symbolOptions = remember(userLocation, userSymbolRotation) { SymbolOptions() - .withLatLng(userLocation) + .withLatLng(userLocation.toLatLng()) .withIconImage(USER_POSITION_MARKER_NAME) .withIconAnchor("center") .withIconRotate(userSymbolRotation) @@ -157,7 +158,7 @@ fun MapContainerLibre( } mapLibre.cameraPosition = CameraPosition.Builder() - .target(mapCenter) + .target(mapCenter.toLatLng()) .zoom(15.0) // we set the zoom only at init .bearing(mapViewRotation.toDouble()) .build() @@ -169,7 +170,7 @@ fun MapContainerLibre( if (beaconLocation != null && beaconLocationMarker.value == null) { // first time beacon is created val markerOptions = MarkerOptions() - .position(beaconLocation) + .position(beaconLocation.toLatLng()) beaconLocationMarker.value = mapLibre.addMarker(markerOptions) } } @@ -185,7 +186,7 @@ fun MapContainerLibre( mapLibre.cameraPosition = cameraPosition symbol.value?.let { sym -> // We have a symbol, so update it - sym.latLng = userLocation + sym.latLng = userLocation.toLatLng() sym.iconRotate = userSymbolRotation symbolManager.value?.update(sym) } @@ -194,13 +195,13 @@ fun MapContainerLibre( // beacon to display beaconLocationMarker.value?.let { currentBeaconMarker -> // update beacon position - currentBeaconMarker.position = beaconLocation + currentBeaconMarker.position = beaconLocation.toLatLng() mapLibre.updateMarker(currentBeaconMarker) } ?: { // new beacon to display val markerOptions = MarkerOptions() - .position(beaconLocation) + .position(beaconLocation.toLatLng()) beaconLocationMarker.value = mapLibre.addMarker(markerOptions) } } else { diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/locationDetails/LocationDetailsScreen.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/locationDetails/LocationDetailsScreen.kt index 777d6952..b75d5ae7 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/locationDetails/LocationDetailsScreen.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/home/locationDetails/LocationDetailsScreen.kt @@ -34,9 +34,9 @@ import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavHostController import com.google.gson.GsonBuilder -import org.maplibre.android.geometry.LatLng import org.scottishtecharmy.soundscape.R import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt +import org.scottishtecharmy.soundscape.geojsonparser.geojson.fromLatLng import org.scottishtecharmy.soundscape.screens.home.HomeRoutes import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription import org.scottishtecharmy.soundscape.screens.home.home.MapContainerLibre @@ -57,8 +57,7 @@ fun generateLocationDetailsRoute(locationDescription: LocationDescription): Stri @Composable fun LocationDetailsScreen( locationDescription: LocationDescription, - latitude: Double?, - longitude: Double?, + location : LngLatAlt?, heading: Float, onNavigateUp: () -> Unit, navController: NavHostController, @@ -71,25 +70,23 @@ fun LocationDetailsScreen( onNavigateUp = onNavigateUp, navController = navController, locationDescription = locationDescription, - createBeacon = { lat, lng -> - viewModel.createBeacon(lat, lng) + createBeacon = { loc -> + viewModel.createBeacon(loc) }, saveMarker = { description -> viewModel.createMarker(description) }, - enableStreetPreview = { lat, lng -> - viewModel.enableStreetPreview(lat, lng) + enableStreetPreview = { loc -> + viewModel.enableStreetPreview(loc) }, - getLocationDescription = { location -> - viewModel.getLocationDescription(location) ?: + getLocationDescription = { locationForDescription -> + viewModel.getLocationDescription(locationForDescription) ?: LocationDescription( addressName = context.getString(R.string.general_error_location_services_find_location_error), - latitude = location.latitude, - longitude = location.longitude + location = locationForDescription ) }, - latitude = latitude, - longitude = longitude, + location = location, heading = heading, modifier = modifier, ) @@ -100,14 +97,14 @@ fun LocationDetails( locationDescription : LocationDescription, onNavigateUp: () -> Unit, navController: NavHostController, - latitude: Double?, - longitude: Double?, + location: LngLatAlt?, heading: Float, - createBeacon: (latitude: Double, longitude: Double) -> Unit, + createBeacon: (location: LngLatAlt) -> Unit, saveMarker: (description: LocationDescription) -> Unit, - enableStreetPreview: (latitude: Double, longitude: Double) -> Unit, + enableStreetPreview: (location: LngLatAlt) -> Unit, getLocationDescription: (location: LngLatAlt) -> LocationDescription, modifier: Modifier = Modifier) { + Column( modifier = modifier.fillMaxHeight(), ) { @@ -134,15 +131,11 @@ fun LocationDetails( } MapContainerLibre( - beaconLocation = - LatLng( - locationDescription.latitude, - locationDescription.longitude, - ), + beaconLocation = locationDescription.location, allowScrolling = true, onMapLongClick = { latLong -> - val location = LngLatAlt(latLong.longitude, latLong.latitude) - val ld = getLocationDescription(location) + val clickLocation = fromLatLng(latLong) + val ld = getLocationDescription(clickLocation) // This effectively replaces the current screen with the new one navController.navigate(generateLocationDetailsRoute(ld)) { @@ -157,16 +150,8 @@ fun LocationDetails( }, onMarkerClick = { false }, // Center on the beacon - mapCenter = - LatLng( - locationDescription.latitude, - locationDescription.longitude, - ), - userLocation = - LatLng( - latitude ?: 0.0, - longitude ?: 0.0, - ), + mapCenter = locationDescription.location, + userLocation = location?:LngLatAlt(), mapViewRotation = 0.0F, userSymbolRotation = heading, modifier = @@ -180,10 +165,10 @@ fun LocationDetails( @Composable private fun LocationDescriptionButtonsSection( - createBeacon: (latitude: Double, longitude: Double) -> Unit, + createBeacon: (location: LngLatAlt) -> Unit, saveMarker: (description: LocationDescription) -> Unit, locationDescription: LocationDescription, - enableStreetPreview: (latitude: Double, longitude: Double) -> Unit, + enableStreetPreview: (location: LngLatAlt) -> Unit, onNavigateUp: () -> Unit, ) { Column( @@ -193,7 +178,7 @@ private fun LocationDescriptionButtonsSection( icon = Icons.Filled.LocationOn, text = stringResource(R.string.create_an_audio_beacon), ) { - createBeacon(locationDescription.latitude, locationDescription.longitude) + createBeacon(locationDescription.location) } IconWithTextButton( @@ -207,10 +192,7 @@ private fun LocationDescriptionButtonsSection( icon = Icons.Filled.Navigation, text = stringResource(R.string.user_activity_street_preview_title), ) { - enableStreetPreview( - locationDescription.latitude, - locationDescription.longitude, - ) + enableStreetPreview(locationDescription.location) onNavigateUp() } } @@ -317,23 +299,21 @@ fun LocationDetailsPreview() { LocationDescription( addressName = "Pizza hut", distance = "3,5 km", - latitude = 0.0, - longitude = 0.0, + location = LngLatAlt(), fullAddress = "139 boulevard gambetta \n59000 Lille\nFrance", ), - createBeacon = { _, _ -> + createBeacon = { _ -> }, saveMarker = { _ -> }, - enableStreetPreview = { _, _ -> + enableStreetPreview = { _ -> }, getLocationDescription = { _ -> LocationDescription() }, onNavigateUp = {}, navController = NavHostController(LocalContext.current), - latitude = null, - longitude = null, + location = null, heading = 0.0F, ) } diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreen.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreen.kt index e33a3bb3..d389cfa7 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreen.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreen.kt @@ -32,6 +32,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavController import androidx.navigation.compose.rememberNavController import org.scottishtecharmy.soundscape.R +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription import org.scottishtecharmy.soundscape.screens.markers_routes.components.MarkersAndRoutesListSort import org.scottishtecharmy.soundscape.ui.theme.SoundscapeTheme @@ -167,8 +168,7 @@ fun MarkersScreenPopulatedPreview() { LocationDescription( "Waypoint 1", "Street Blabla, Blabla City", - latitude = Double.NaN, - longitude = Double.NaN, + location = LngLatAlt(), distance = "2 km", ), ), diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreenList.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreenList.kt index 9299b6d8..30388fe6 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreenList.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersScreenList.kt @@ -10,7 +10,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import androidx.navigation.NavController import org.scottishtecharmy.soundscape.components.LocationItem -import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription import org.scottishtecharmy.soundscape.screens.home.locationDetails.generateLocationDetailsRoute import org.scottishtecharmy.soundscape.screens.markers_routes.navigation.ScreensForMarkersAndRoutes diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersViewModel.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersViewModel.kt index 56b3e7c4..76c8cc87 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersViewModel.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/markers_routes/screens/markersscreen/MarkersViewModel.kt @@ -12,10 +12,11 @@ import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.launch import org.scottishtecharmy.soundscape.SoundscapeServiceConnection import org.scottishtecharmy.soundscape.database.repository.MarkersRepository +import org.scottishtecharmy.soundscape.geoengine.formatDistance +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription import org.scottishtecharmy.soundscape.screens.markers_routes.screens.getSortOrderPreference import org.scottishtecharmy.soundscape.screens.markers_routes.screens.saveSortOrderPreference -import org.scottishtecharmy.soundscape.utils.calculateDistanceTo import javax.inject.Inject @HiltViewModel @@ -61,17 +62,20 @@ class MarkersViewModel val userLocation = soundscapeServiceConnection.getLocationFlow()?.firstOrNull() val markerVMs = markersRepository.getMarkers().map { + val markerLngLat = LngLatAlt(it.location?.longitude ?: 0.0, it.location?.latitude ?: 0.0) LocationDescription( addressName = it.addressName, fullAddress = it.fullAddress, - latitude = it.location?.latitude ?: 0.0, - longitude = it.location?.longitude ?: 0.0, + location = markerLngLat, distance = - userLocation?.calculateDistanceTo( - it.location?.latitude ?: userLocation.latitude, - it.location?.longitude ?: userLocation.longitude - ), - marker = true, + if(userLocation == null) + "" + else { + val userLngLat = + LngLatAlt(userLocation.longitude, userLocation.latitude) + formatDistance(userLngLat.distance(markerLngLat), context) + }, + marker = true ) } val isAscending = getSortOrderPreference(context) diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/screens/onboarding/audiobeacons/AudioBeaconsViewModel.kt b/app/src/main/java/org/scottishtecharmy/soundscape/screens/onboarding/audiobeacons/AudioBeaconsViewModel.kt index 2859b053..d6c1fff6 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/screens/onboarding/audiobeacons/AudioBeaconsViewModel.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/screens/onboarding/audiobeacons/AudioBeaconsViewModel.kt @@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import org.scottishtecharmy.soundscape.MainActivity import org.scottishtecharmy.soundscape.audio.NativeAudioEngine +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import javax.inject.Inject @HiltViewModel @@ -36,7 +37,7 @@ class AudioBeaconsViewModel @Inject constructor(@ApplicationContext val context: audioEngine.setBeaconType(type) if(beacon != 0L) audioEngine.destroyBeacon(beacon) - beacon = audioEngine.createBeacon(1.0, 0.0) + beacon = audioEngine.createBeacon(LngLatAlt(1.0, 0.0)) _state.value = state.value.copy(selectedBeacon = type) // Store the preference for future use val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/services/RoutePlayer.kt b/app/src/main/java/org/scottishtecharmy/soundscape/services/RoutePlayer.kt index 6dded7db..780fe3e4 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/services/RoutePlayer.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/services/RoutePlayer.kt @@ -11,6 +11,7 @@ import org.scottishtecharmy.soundscape.database.local.dao.RoutesDao import org.scottishtecharmy.soundscape.database.local.model.RouteData import org.scottishtecharmy.soundscape.database.repository.RoutesRepository import org.scottishtecharmy.soundscape.geoengine.utils.distance +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt class RoutePlayer(val service: SoundscapeService) { private var currentRouteData: RouteData? = null @@ -59,7 +60,7 @@ class RoutePlayer(val service: SoundscapeService) { service.audioEngine.clearTextToSpeechQueue() service.audioEngine.createTextToSpeech("Move to marker ${index+1}, ${route.waypoints[index].addressName}", location.latitude, location.longitude) - service.createBeacon(location.latitude, location.longitude) + service.createBeacon(LngLatAlt(location.latitude, location.longitude)) } } diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/services/SoundscapeService.kt b/app/src/main/java/org/scottishtecharmy/soundscape/services/SoundscapeService.kt index a7211d11..224a194c 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/services/SoundscapeService.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/services/SoundscapeService.kt @@ -112,14 +112,16 @@ class SoundscapeService : MediaSessionService() { return binder!! } - fun setStreetPreviewMode(on: Boolean, latitude: Double, longitude: Double) { + fun setStreetPreviewMode(on: Boolean, location: LngLatAlt?) { directionProvider.destroy() locationProvider.destroy() geoEngine.stop() if (on) { // Use static location, but phone's direction - locationProvider = StaticLocationProvider(latitude, longitude) - directionProvider = AndroidDirectionProvider(this) + if(location != null) { + locationProvider = StaticLocationProvider(location) + directionProvider = AndroidDirectionProvider(this) + } } else { // Switch back to phone's location and direction locationProvider = AndroidLocationProvider(this) @@ -317,13 +319,15 @@ class SoundscapeService : MediaSessionService() { Realm.deleteRealm(config) }*/ - fun createBeacon(latitude: Double, longitude: Double) { + fun createBeacon(location: LngLatAlt?) { + if(location == null) return + if (audioBeacon != 0L) { audioEngine.destroyBeacon(audioBeacon) } - audioBeacon = audioEngine.createBeacon(latitude, longitude) + audioBeacon = audioEngine.createBeacon(location) // Report any change in beacon back to application - _beaconFlow.value = LngLatAlt(longitude, latitude) + _beaconFlow.value = location } fun destroyBeacon() { @@ -410,7 +414,7 @@ class SoundscapeService : MediaSessionService() { /** * streetPreviewGo is called when the 'GO' button is pressed when in StreetPreview mode. - * It indicates that the user has selected the direction of travel in which they wich to move. + * It indicates that the user has selected the direction of travel in which they which to move. */ fun streetPreviewGo() { _streetPreviewFlow.value = StreetPreviewState(true, geoEngine.streetPreviewGo()) diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/utils/LocationExt.kt b/app/src/main/java/org/scottishtecharmy/soundscape/utils/LocationExt.kt index bbe6d216..1184978d 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/utils/LocationExt.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/utils/LocationExt.kt @@ -1,14 +1,15 @@ package org.scottishtecharmy.soundscape.utils -import android.location.Location +import android.content.Context +import org.scottishtecharmy.soundscape.geoengine.formatDistance import org.scottishtecharmy.soundscape.geojsonparser.geojson.Feature +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import org.scottishtecharmy.soundscape.geojsonparser.geojson.Point import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription -import java.util.Locale fun ArrayList.toLocationDescriptions( - currentLocationLatitude: Double, - currentLocationLongitude: Double, + currentLocation: LngLatAlt, + localizedContext: Context ): List = mapNotNull { feature -> feature.properties?.let { properties -> @@ -30,15 +31,10 @@ fun ArrayList.toLocationDescriptions( fullAddress = fullAddress, distance = formatDistance( - calculateDistance( - lat1 = currentLocationLatitude, - lon1 = currentLocationLongitude, - lat2 = (feature.geometry as Point).coordinates.latitude, - lon2 = (feature.geometry as Point).coordinates.longitude, - ), + currentLocation.distance((feature.geometry as Point).coordinates), + localizedContext ), - latitude = (feature.geometry as Point).coordinates.latitude, - longitude = (feature.geometry as Point).coordinates.longitude, + location = (feature.geometry as Point).coordinates, ) } } @@ -59,36 +55,3 @@ fun buildAddressFormat( else -> addressFormat.joinToString("\n") } } - -private fun calculateDistance( - lat1: Double, - lon1: Double, - lat2: Double, - lon2: Double, -): Float { - val results = FloatArray(1) - Location.distanceBetween(lat1, lon1, lat2, lon2, results) - return results[0] -} - -private fun formatDistance(distanceInMeters: Float): String { - val distanceInKm = distanceInMeters / 1000 - return String.format(Locale.getDefault(), "%.1f km", distanceInKm) -} - -fun Location.calculateDistanceTo( - lat: Double, - lon: Double, -): String { - val results = FloatArray(1) - Location.distanceBetween( - this.latitude, - this.longitude, - lat, - lon, - results, - ) - val distanceInMeters = results[0] - val distanceInKm = distanceInMeters / 1000 - return String.format(Locale.getDefault(), "%.1f km", distanceInKm) -} diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/LocationDetailsViewModel.kt b/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/LocationDetailsViewModel.kt index d76d572c..6c07039a 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/LocationDetailsViewModel.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/LocationDetailsViewModel.kt @@ -19,18 +19,13 @@ class LocationDetailsViewModel @Inject constructor( private val routesRepository: RoutesRepository ): ViewModel() { - fun createBeacon( - latitude: Double, - longitude: Double, - ) { - soundscapeServiceConnection.soundscapeService?.createBeacon(latitude, longitude) + fun createBeacon(location: LngLatAlt) { + soundscapeServiceConnection.soundscapeService?.createBeacon(location) } - fun enableStreetPreview( - latitude: Double, - longitude: Double, - ) { - soundscapeServiceConnection.setStreetPreviewMode(true, latitude, longitude) + fun enableStreetPreview(location: LngLatAlt) { + soundscapeServiceConnection.setStreetPreviewMode(true, location) + } fun createMarker(locationDescription: LocationDescription) { @@ -42,7 +37,7 @@ class LocationDetailsViewModel @Inject constructor( MarkerData( addressName = name, fullAddress = locationDescription.fullAddress ?: "", // TODO Fanny is it possible to get no full address ? - location = Location(latitude = locationDescription.latitude, longitude = locationDescription.longitude), + location = Location(latitude = locationDescription.location.latitude, longitude = locationDescription.location.longitude), ) try { routesRepository.insertWaypoint(marker) diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeState.kt b/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeState.kt index 6c296e22..e14b42ea 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeState.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeState.kt @@ -2,12 +2,13 @@ package org.scottishtecharmy.soundscape.viewmodels.home import org.maplibre.android.geometry.LatLng import org.scottishtecharmy.soundscape.geoengine.StreetPreviewState +import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription data class HomeState( var heading: Float = 0.0f, - var location: LatLng? = null, - var beaconLocation: LatLng? = null, + var location: LngLatAlt? = null, + var beaconLocation: LngLatAlt? = null, var streetPreviewState: StreetPreviewState = StreetPreviewState(false), var tileGridGeoJson: String = "", var isSearching: Boolean = false, diff --git a/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeViewModel.kt b/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeViewModel.kt index ca6ef5d0..a0917982 100644 --- a/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeViewModel.kt +++ b/app/src/main/java/org/scottishtecharmy/soundscape/viewmodels/home/HomeViewModel.kt @@ -12,6 +12,7 @@ import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow @@ -23,7 +24,6 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import org.maplibre.android.annotations.Marker -import org.maplibre.android.geometry.LatLng import org.scottishtecharmy.soundscape.SoundscapeServiceConnection import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription @@ -38,7 +38,8 @@ class HomeViewModel @Inject constructor( private val soundscapeServiceConnection: SoundscapeServiceConnection, -) : ViewModel() { + @ApplicationContext context: Context + ) : ViewModel() { private val _state: MutableStateFlow = MutableStateFlow(HomeState()) val state: StateFlow = _state.asStateFlow() private val _searchText: MutableStateFlow = MutableStateFlow("") @@ -49,7 +50,7 @@ class HomeViewModel init { handleMonitoring() - fetchSearchResult() + fetchSearchResult(context) } private fun handleMonitoring() { @@ -59,7 +60,7 @@ class HomeViewModel if (serviceBoundState) { // The service has started, so start monitoring the location and heading startMonitoringLocation() - // And start monitoring the street preview mode + // And start monitoring the street preview state startMonitoringStreetPreviewState() } else { // The service has gone away so remove the current location marker @@ -90,7 +91,7 @@ class HomeViewModel soundscapeServiceConnection.getLocationFlow()?.collectLatest { value -> if (value != null) { Log.d(TAG, "Location $value") - _state.update { it.copy(location = LatLng(value.latitude, value.longitude)) } + _state.update { it.copy(location = LngLatAlt(value.longitude, value.latitude)) } } } } @@ -110,9 +111,9 @@ class HomeViewModel _state.update { it.copy( beaconLocation = - LatLng( - value.latitude, + LngLatAlt( value.longitude, + value.latitude, ), ) } @@ -157,7 +158,8 @@ class HomeViewModel fun onMarkerClick(marker: Marker): Boolean { Log.d(TAG, "marker click") - if (marker.position == _state.value.beaconLocation) { + if ((marker.position.latitude == _state.value.beaconLocation?.latitude) && + (marker.position.longitude == _state.value.beaconLocation?.longitude)){ soundscapeServiceConnection.soundscapeService?.destroyBeacon() _state.update { it.copy(beaconLocation = null) } @@ -235,7 +237,7 @@ class HomeViewModel _searchText.value = text } - private fun fetchSearchResult() { + private fun fetchSearchResult(context: Context) { viewModelScope.launch { _searchText .debounce(500) @@ -251,10 +253,8 @@ class HomeViewModel it.copy( searchItems = result?.toLocationDescriptions( - currentLocationLatitude = state.value.location?.latitude ?: 0.0, - currentLocationLongitude = - state.value.location?.longitude - ?: 0.0, + currentLocation = state.value.location ?: LngLatAlt(), + localizedContext = context ), ) }