ExitNumber, Flow
This commit is contained in:
@@ -3,6 +3,7 @@ package com.kouros.navigation.car
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.location.Location
|
||||
import android.location.LocationListener
|
||||
import android.location.LocationManager
|
||||
import androidx.car.app.CarContext
|
||||
import androidx.core.location.LocationListenerCompat
|
||||
@@ -10,7 +11,9 @@ import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -59,6 +62,26 @@ class DeviceLocationManager(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a flow of location updates from device GPS.
|
||||
*
|
||||
* deviceLocationManager.locationFlow().asLiveData().observe(this, ::updateLocation2)
|
||||
*
|
||||
* @return Flow emitting location updates
|
||||
*/
|
||||
@SuppressLint("MissingPermission")
|
||||
fun locationFlow(): Flow<Location> = callbackFlow {
|
||||
val listener = LocationListener { location ->
|
||||
if (shouldUseDeviceLocation && isListening) {
|
||||
trySend(location) // Emit the location update to the flow
|
||||
}
|
||||
}
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 5f, listener)
|
||||
awaitClose {
|
||||
locationManager.removeUpdates(listener) // Unregister listener on cancellation
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts requesting location updates from device GPS.
|
||||
* Provides initial location via callback and then starts continuous updates.
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.os.CountDownTimer
|
||||
import android.os.Handler
|
||||
import android.util.Log
|
||||
import androidx.car.app.CarContext
|
||||
import androidx.car.app.CarToast
|
||||
import androidx.car.app.Screen
|
||||
import androidx.car.app.model.Action
|
||||
import androidx.car.app.model.Action.FLAG_IS_PERSISTENT
|
||||
@@ -21,7 +20,6 @@ import androidx.car.app.navigation.model.Destination
|
||||
import androidx.car.app.navigation.model.MapWithContentTemplate
|
||||
import androidx.car.app.navigation.model.MessageInfo
|
||||
import androidx.car.app.navigation.model.NavigationTemplate
|
||||
import androidx.car.app.navigation.model.PanModeListener
|
||||
import androidx.car.app.navigation.model.RoutingInfo
|
||||
import androidx.car.app.navigation.model.Step
|
||||
import androidx.car.app.navigation.model.TravelEstimate
|
||||
@@ -100,6 +98,7 @@ open class NavigationScreen(
|
||||
|
||||
repository.tripSuggestionFlow.asLiveData().observe(this, Observer {
|
||||
navigationViewModel.recentPlaces.observe(this, observerRecentPlaces)
|
||||
//navigationViewModel.recentPlacesFlow(carContext, surfaceRenderer.lastLocation ).asLiveData().observe(this, ::test)
|
||||
tripSuggestion = it
|
||||
})
|
||||
repository.alternativeRoutesFlow.asLiveData().observe(this, Observer {
|
||||
|
||||
@@ -221,6 +221,10 @@ private fun exitNumber(
|
||||
) {
|
||||
0
|
||||
} else {
|
||||
instruction.exitNumber.toInt()
|
||||
try {
|
||||
instruction.exitNumber.toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.kouros.navigation.model
|
||||
//import com.kouros.navigation.data.Preferences.boxStore
|
||||
import android.content.Context
|
||||
import android.location.Location
|
||||
import android.location.LocationListener
|
||||
import android.location.LocationManager
|
||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
||||
import androidx.compose.runtime.toMutableStateList
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
@@ -22,6 +24,9 @@ import com.kouros.navigation.utils.Levenshtein
|
||||
import com.kouros.navigation.utils.getSettingsRepository
|
||||
import com.kouros.navigation.utils.location
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@@ -102,6 +107,16 @@ class NavigationViewModel(private val repository: NavigationRepository) : ViewMo
|
||||
}
|
||||
|
||||
val gson: com.google.gson.Gson = GsonBuilder().create()
|
||||
/**
|
||||
* Retrieves recent places from Preferences as a Flow.
|
||||
*/
|
||||
fun recentPlacesFlow(context: Context, location: Location,): Flow<Place> = callbackFlow {
|
||||
for (place in recentPlaces.value!!) {
|
||||
trySend(place)
|
||||
}
|
||||
awaitClose {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all recent places from Preferences and calculates distances.
|
||||
|
||||
Reference in New Issue
Block a user