Compare commits

..
2 Commits
Author SHA1 Message Date
Dimitris 60b842d883 Nominatim 2026-03-30 10:00:16 +02:00
Dimitris 9def7a5c64 Nominatim 2026-03-30 09:58:13 +02:00
2 changed files with 11 additions and 47 deletions
@@ -28,9 +28,7 @@ abstract class NavigationRepository {
abstract fun getTraffic(context: Context, location: Location, carOrientation: Float): String
fun getRouteDistance(
currentLocation: Location,
location: Location,
carOrientation: Float,
context: Context
location: Location
): Double {
if (currentLocation.latitude == 0.0)
return 0.0
@@ -38,12 +36,16 @@ abstract class NavigationRepository {
}
fun searchPlaces(search: String, location: Location): String {
val box = calculateSquareRadius(location.latitude, location.longitude, 800.0)
val box = calculateSquareRadius(location.latitude, location.longitude, 50.0)
val viewbox = "&bounded=1&viewbox=${box}"
return fetchUrl(
var result = fetchUrl(
"${nominatimUrl}search?q=$search&format=jsonv2&addressdetails=true$viewbox",
true
false
)
if (result == "[]") {
result = fetchUrl("${nominatimUrl}search?q=$search&format=jsonv2&addressdetails=true", false)
}
return result
}
fun reverseAddress(location: Location): String {
@@ -67,13 +69,13 @@ abstract class NavigationRepository {
}
})
}
Log.d("fetchUrl", url)
Log.d("NavigationRepository", url)
val httpURLConnection = URL(url).openConnection() as HttpURLConnection
httpURLConnection.setRequestProperty(
"Accept",
"application/json"
) // The format of response we want to get from the server
httpURLConnection.setRequestProperty("User-Agent", "email=nominatim@kouros-online.de");
httpURLConnection.setRequestProperty("User-Agent", "email=nominatim@kouros-online.de")
httpURLConnection.requestMethod = "GET"
val responseCode = httpURLConnection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
@@ -5,8 +5,6 @@ import android.content.Context
import android.location.Location
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.toMutableStateList
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
@@ -103,32 +101,6 @@ class NavigationViewModel(private val repository: NavigationRepository) : ViewMo
}
/**
* Loads the most recent place from Preferences and calculates its distance.
* Posts the result to recentPlace LiveData if distance > 1km.
*/
fun loadRecentPlace(location: Location, carOrientation: Float, context: Context) {
viewModelScope.launch(Dispatchers.IO) {
try {
val settingsRepository = getSettingsRepository(context)
val recentPlaces = settingsRepository.recentPlacesFlow.first()
val gson = GsonBuilder().serializeNulls().create()
val places = gson.fromJson(recentPlaces, Places::class.java)
for (place in places.places.sortedBy { it.lastDate }) {
val plLocation = location(place.longitude, place.latitude)
val distance = plLocation.distanceTo(location)
place.distance = distance
if (place.distance > 200F) {
recentPlace.postValue(place)
return@launch
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
/**
* Loads all recent places from Preferences and calculates distances.
* Posts the sorted list to places LiveData.
@@ -151,9 +123,7 @@ class NavigationViewModel(private val repository: NavigationRepository) : ViewMo
val distance =
repository.getRouteDistance(
location,
plLocation,
carOrientation,
context
plLocation
)
place.distance = distance.toFloat()
place.id = id
@@ -494,14 +464,6 @@ class NavigationViewModel(private val repository: NavigationRepository) : ViewMo
deletePlace(context, place)
}
/**
* Deletes a place from recent destinations in Preferences.
*/
fun deleteRecent(context: Context, place: Place) {
place.category = Constants.RECENT
deletePlace(context, place)
}
/**
* Deletes a place from Preferences matching name and category.
*/