KMP/location + UI: centralize location lookups and use shared panel shell in NearbyDialog
shared(commonMain): add location/LocationProvider (GeoPoint + interface). app(location): add AndroidLocationProvider using FusedLocationProviderClient. app(MainActivity): replace ad-hoc location calls with provider in startup, auto-scans (Wiki/DM/Fornsok), and “Nearby” button; persist lastKnownLat/Lon. app(ui): use MapPanelShell inside NearbyDialog and set sensible max height (~80% of screen). app(found): fix fetchNearbyData call to use gp.lat/gp.lon.
This commit is contained in:
@@ -108,7 +108,7 @@ import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import app.klottr.data.AppDatabase
|
||||
import app.klottr.data.NoteRepository
|
||||
import app.klottr.ui.NearbyContent // Delat material-fritt innehåll för Närliggande
|
||||
import app.klottr.location.AndroidLocationProvider // Använd vår provider
|
||||
import app.klottr.nearby.FoundOpenAction // Delat åtgärds-ADT
|
||||
import app.klottr.nearby.NearbyEntryShared // Delad radmodell
|
||||
import app.klottr.nearby.buildNearbyEntries // Delad builder för “Närliggande”
|
||||
@@ -134,6 +134,7 @@ import app.klottr.shared.SharedPing
|
||||
import app.klottr.theme.KlottrTheme
|
||||
import app.klottr.ui.FoundList // Delad, material-fri listkomponent för "Hittat"
|
||||
import app.klottr.ui.FoundContent // Delat innehåll för “Hittat”
|
||||
import app.klottr.ui.NearbyContent // Delat material-fritt innehåll för Närliggande
|
||||
import app.klottr.ui.NearbyEntryRow
|
||||
import app.klottr.ui.NearbyList // Delad material-fri listkomponent
|
||||
import app.klottr.ui.MapPanelShell
|
||||
@@ -449,27 +450,21 @@ private fun KlottrApp(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Kickstarta avstånd direkt vid appstart
|
||||
LaunchedEffect(Unit) {
|
||||
val client = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||
val loc = try {
|
||||
client.lastLocation.await()
|
||||
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
} catch (_: Exception) { null }
|
||||
|
||||
if (loc != null) {
|
||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt: centraliserad plats
|
||||
if (gp != null) {
|
||||
// Spara som "senaste kända" och uppdatera away-flaggor
|
||||
LastLocationStore.lastKnownLocation = loc
|
||||
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
|
||||
LastLocationStore.lastKnownLocation = android.location.Location("klottr").apply {
|
||||
latitude = gp.lat; longitude = gp.lon
|
||||
} // Nytt: spegla in i din befintliga store
|
||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon) // Oför: men nu med delad modell
|
||||
|
||||
// Tvinga omritning så att avstånd visas utan scroll
|
||||
locationTick++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Direkt UI-uppdatering från LocationService
|
||||
DisposableEffect(Unit) {
|
||||
val filter = IntentFilter(LocationService.ACTION_FOUND_UPDATED)
|
||||
@@ -521,21 +516,17 @@ private fun KlottrApp(
|
||||
// Auto-scan Wikipedia
|
||||
LaunchedEffect(showWiki, wikiRadius) {
|
||||
if (!showWiki) return@LaunchedEffect
|
||||
val client = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||
val loc = try {
|
||||
client.lastLocation.await()
|
||||
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
} catch (_: Exception) { null }
|
||||
if (loc != null) {
|
||||
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
|
||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
|
||||
if (gp != null) {
|
||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
|
||||
val items = withContext(Dispatchers.IO) {
|
||||
Wikipedia.getNearby(lat = loc.latitude, lon = loc.longitude, radiusMeters = wikiRadius)
|
||||
Wikipedia.getNearby(lat = gp.lat, lon = gp.lon, radiusMeters = wikiRadius)
|
||||
}
|
||||
// Resten oförändrat men använd gp.lat/gp.lon i stället för loc.latitude/longitude
|
||||
val now = System.currentTimeMillis()
|
||||
var anyNew = false
|
||||
items.forEach { w ->
|
||||
// Använder FQN för att säkert länka den delade distanceMeters från :shared.
|
||||
val d = app.klottr.util.distanceMeters(loc.latitude, loc.longitude, w.lat, w.lon).roundToInt()
|
||||
val d = app.klottr.util.distanceMeters(gp.lat, gp.lon, w.lat, w.lon).roundToInt()
|
||||
if (d <= wikiRadius) {
|
||||
val added = FoundStore.markFound(
|
||||
context, LAYER_WIKI, w.pageId.toString(),
|
||||
@@ -546,25 +537,22 @@ private fun KlottrApp(
|
||||
}
|
||||
if (anyNew) refreshFound()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Auto-scan DigitaltMuseum
|
||||
LaunchedEffect(showDm, dmRadius) {
|
||||
if (!showDm) return@LaunchedEffect
|
||||
val client = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||
val loc = try {
|
||||
client.lastLocation.await()
|
||||
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
} catch (_: Exception) { null }
|
||||
if (loc != null) {
|
||||
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
|
||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
|
||||
if (gp != null) {
|
||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
|
||||
val items = withContext(Dispatchers.IO) {
|
||||
DigitaltMuseum.getNearby(lat = loc.latitude, lon = loc.longitude, radiusMeters = dmRadius)
|
||||
DigitaltMuseum.getNearby(lat = gp.lat, lon = gp.lon, radiusMeters = dmRadius)
|
||||
}
|
||||
val now = System.currentTimeMillis()
|
||||
var anyNew = false
|
||||
items.forEach { dmi ->
|
||||
val d = app.klottr.util.distanceMeters(loc.latitude, loc.longitude, dmi.lat, dmi.lon).roundToInt()
|
||||
val d = app.klottr.util.distanceMeters(gp.lat, gp.lon, dmi.lat, dmi.lon).roundToInt()
|
||||
if (d <= dmRadius) {
|
||||
val added = FoundStore.markFound(
|
||||
context, LAYER_DM, dmi.id,
|
||||
@@ -580,23 +568,19 @@ private fun KlottrApp(
|
||||
// Auto-scan Fornsök
|
||||
LaunchedEffect(showFornsok, fornsokRadius) {
|
||||
if (!showFornsok) return@LaunchedEffect
|
||||
val client = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||
val loc = try {
|
||||
client.lastLocation.await()
|
||||
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
} catch (_: Exception) { null }
|
||||
if (loc != null) {
|
||||
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
|
||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
|
||||
if (gp != null) {
|
||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
|
||||
val items = withContext(Dispatchers.IO) {
|
||||
Fornsok.getNearby(lat = loc.latitude, lon = loc.longitude, radiusMeters = fornsokRadius)
|
||||
Fornsok.getNearby(lat = gp.lat, lon = gp.lon, radiusMeters = fornsokRadius)
|
||||
}
|
||||
val now = System.currentTimeMillis()
|
||||
var anyNew = false
|
||||
items.forEach { f ->
|
||||
val d = app.klottr.util.distanceMeters(loc.latitude, loc.longitude, f.lat, f.lon).roundToInt()
|
||||
val d = app.klottr.util.distanceMeters(gp.lat, gp.lon, f.lat, f.lon).roundToInt()
|
||||
if (d <= fornsokRadius) {
|
||||
val added = FoundStore.markFound(
|
||||
context, FoundStore.LAYER_FORNSOK, f.id, // [ÄNDRAD] Använd delade konstanten från FoundStore (KMP)
|
||||
context, FoundStore.LAYER_FORNSOK, f.id,
|
||||
label = f.title, lat = f.lat, lon = f.lon, timeMillis = now
|
||||
)
|
||||
if (added) anyNew = true
|
||||
@@ -658,30 +642,24 @@ private fun KlottrApp(
|
||||
showNearby = true
|
||||
val ctx = context
|
||||
scope.launch {
|
||||
val client = GmsLocationServices.getFusedLocationProviderClient(ctx)
|
||||
val loc = try {
|
||||
client.lastLocation.await()
|
||||
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
} catch (_: Exception) { null }
|
||||
|
||||
if (loc == null) {
|
||||
val gp = AndroidLocationProvider.currentOrLastKnown(ctx) // Nytt: centraliserad plats
|
||||
if (gp == null) {
|
||||
nearbyLoading = false
|
||||
nearbyError = "Ingen plats ännu – prova igen."
|
||||
return@launch
|
||||
}
|
||||
lastKnownLat = loc.latitude
|
||||
lastKnownLon = loc.longitude
|
||||
|
||||
app.klottr.util.AppLog.d { "Närliggande-debug: toggles klotter=$showKlotter wiki=$showWiki dm=$showDm forn=$showFornsok"}
|
||||
app.klottr.util.AppLog.d { "Närliggande-debug: loc=${lastKnownLat},${lastKnownLon}"}
|
||||
lastKnownLat = gp.lat // Nytt
|
||||
lastKnownLon = gp.lon // Nytt
|
||||
|
||||
app.klottr.util.AppLog.d { "Närliggande-debug: toggles klotter=$showKlotter wiki=$showWiki dm=$showDm forn=$showFornsok"}
|
||||
app.klottr.util.AppLog.d { "Närliggande-debug: loc=${lastKnownLat},${lastKnownLon}"}
|
||||
|
||||
// Re-arm: markera alla tidigare fynd som "borta" om vi nu står >200 m från dem
|
||||
// Hämta alla lager via delad KMP-funktion (DI med våra befintliga klientanrop)
|
||||
val data = withContext(Dispatchers.IO) {
|
||||
fetchNearbyData( //
|
||||
lat = loc.latitude, //
|
||||
lon = loc.longitude, //
|
||||
lat = gp.lat, //
|
||||
lon = gp.lon, //
|
||||
showKlotter = showKlotter, //
|
||||
showWiki = showWiki, //
|
||||
showDm = showDm, //
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package app.klottr.location
|
||||
|
||||
import android.content.Context
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.google.android.gms.location.Priority
|
||||
import kotlinx.coroutines.tasks.await
|
||||
|
||||
/** Android-implementation som använder FusedLocationProvider. */
|
||||
object AndroidLocationProvider : LocationProvider {
|
||||
/** Hämtar lastLocation och faller tillbaka till getCurrentLocation. */
|
||||
suspend fun currentOrLastKnown(context: Context): GeoPoint? {
|
||||
val client = LocationServices.getFusedLocationProviderClient(context) // Använder befintligt GMS-bibliotek
|
||||
return try {
|
||||
val last = client.lastLocation.await()
|
||||
val loc = last ?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
loc?.let { GeoPoint(it.latitude, it.longitude) }
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun currentOrLastKnown(): GeoPoint? {
|
||||
// Ej använd i appen just nu (kräver Context), men finns för framtida DI.
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package app.klottr.location
|
||||
|
||||
/** Enkel delad platsmodell för KMP. */
|
||||
data class GeoPoint(val lat: Double, val lon: Double)
|
||||
|
||||
/** Gränssnitt för plattforms-specifik platsinhämtning. */
|
||||
interface LocationProvider {
|
||||
/** Returnerar aktuell plats eller senaste kända, eller null vid fel/tidsutlöpning. */
|
||||
suspend fun currentOrLastKnown(): GeoPoint?
|
||||
}
|
||||
Reference in New Issue
Block a user