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:
2025-09-14 18:20:38 +02:00
parent 4e2d062329
commit 5eb96a0ff1
3 changed files with 70 additions and 56 deletions

View File

@@ -108,7 +108,7 @@ import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager import androidx.work.WorkManager
import app.klottr.data.AppDatabase import app.klottr.data.AppDatabase
import app.klottr.data.NoteRepository 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.FoundOpenAction // Delat åtgärds-ADT
import app.klottr.nearby.NearbyEntryShared // Delad radmodell import app.klottr.nearby.NearbyEntryShared // Delad radmodell
import app.klottr.nearby.buildNearbyEntries // Delad builder för “Närliggande” 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.theme.KlottrTheme
import app.klottr.ui.FoundList // Delad, material-fri listkomponent för "Hittat" 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.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.NearbyEntryRow
import app.klottr.ui.NearbyList // Delad material-fri listkomponent import app.klottr.ui.NearbyList // Delad material-fri listkomponent
import app.klottr.ui.MapPanelShell import app.klottr.ui.MapPanelShell
@@ -449,27 +450,21 @@ private fun KlottrApp(
} }
} }
// Kickstarta avstånd direkt vid appstart // Kickstarta avstånd direkt vid appstart
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
val client = GmsLocationServices.getFusedLocationProviderClient(context) val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt: centraliserad plats
val loc = try { if (gp != null) {
client.lastLocation.await()
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
} catch (_: Exception) { null }
if (loc != null) {
// Spara som "senaste kända" och uppdatera away-flaggor // Spara som "senaste kända" och uppdatera away-flaggor
LastLocationStore.lastKnownLocation = loc LastLocationStore.lastKnownLocation = android.location.Location("klottr").apply {
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude) 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 // Tvinga omritning så att avstånd visas utan scroll
locationTick++ locationTick++
} }
} }
// Direkt UI-uppdatering från LocationService // Direkt UI-uppdatering från LocationService
DisposableEffect(Unit) { DisposableEffect(Unit) {
val filter = IntentFilter(LocationService.ACTION_FOUND_UPDATED) val filter = IntentFilter(LocationService.ACTION_FOUND_UPDATED)
@@ -521,21 +516,17 @@ private fun KlottrApp(
// Auto-scan Wikipedia // Auto-scan Wikipedia
LaunchedEffect(showWiki, wikiRadius) { LaunchedEffect(showWiki, wikiRadius) {
if (!showWiki) return@LaunchedEffect if (!showWiki) return@LaunchedEffect
val client = GmsLocationServices.getFusedLocationProviderClient(context) val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
val loc = try { if (gp != null) {
client.lastLocation.await() FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
} catch (_: Exception) { null }
if (loc != null) {
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
val items = withContext(Dispatchers.IO) { 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() val now = System.currentTimeMillis()
var anyNew = false var anyNew = false
items.forEach { w -> 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(gp.lat, gp.lon, w.lat, w.lon).roundToInt()
val d = app.klottr.util.distanceMeters(loc.latitude, loc.longitude, w.lat, w.lon).roundToInt()
if (d <= wikiRadius) { if (d <= wikiRadius) {
val added = FoundStore.markFound( val added = FoundStore.markFound(
context, LAYER_WIKI, w.pageId.toString(), context, LAYER_WIKI, w.pageId.toString(),
@@ -546,25 +537,22 @@ private fun KlottrApp(
} }
if (anyNew) refreshFound() if (anyNew) refreshFound()
} }
} }
// Auto-scan DigitaltMuseum // Auto-scan DigitaltMuseum
LaunchedEffect(showDm, dmRadius) { LaunchedEffect(showDm, dmRadius) {
if (!showDm) return@LaunchedEffect if (!showDm) return@LaunchedEffect
val client = GmsLocationServices.getFusedLocationProviderClient(context) val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
val loc = try { if (gp != null) {
client.lastLocation.await() FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
} catch (_: Exception) { null }
if (loc != null) {
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
val items = withContext(Dispatchers.IO) { 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() val now = System.currentTimeMillis()
var anyNew = false var anyNew = false
items.forEach { dmi -> 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) { if (d <= dmRadius) {
val added = FoundStore.markFound( val added = FoundStore.markFound(
context, LAYER_DM, dmi.id, context, LAYER_DM, dmi.id,
@@ -580,23 +568,19 @@ private fun KlottrApp(
// Auto-scan Fornsök // Auto-scan Fornsök
LaunchedEffect(showFornsok, fornsokRadius) { LaunchedEffect(showFornsok, fornsokRadius) {
if (!showFornsok) return@LaunchedEffect if (!showFornsok) return@LaunchedEffect
val client = GmsLocationServices.getFusedLocationProviderClient(context) val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
val loc = try { if (gp != null) {
client.lastLocation.await() FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
} catch (_: Exception) { null }
if (loc != null) {
FoundStore.updateAwayFlags(context, loc.latitude, loc.longitude)
val items = withContext(Dispatchers.IO) { 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() val now = System.currentTimeMillis()
var anyNew = false var anyNew = false
items.forEach { f -> 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) { if (d <= fornsokRadius) {
val added = FoundStore.markFound( 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 label = f.title, lat = f.lat, lon = f.lon, timeMillis = now
) )
if (added) anyNew = true if (added) anyNew = true
@@ -658,30 +642,24 @@ private fun KlottrApp(
showNearby = true showNearby = true
val ctx = context val ctx = context
scope.launch { scope.launch {
val client = GmsLocationServices.getFusedLocationProviderClient(ctx) val gp = AndroidLocationProvider.currentOrLastKnown(ctx) // Nytt: centraliserad plats
val loc = try { if (gp == null) {
client.lastLocation.await()
?: client.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
} catch (_: Exception) { null }
if (loc == null) {
nearbyLoading = false nearbyLoading = false
nearbyError = "Ingen plats ännu – prova igen." nearbyError = "Ingen plats ännu – prova igen."
return@launch return@launch
} }
lastKnownLat = loc.latitude lastKnownLat = gp.lat // Nytt
lastKnownLon = loc.longitude 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}"}
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 // 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) // Hämta alla lager via delad KMP-funktion (DI med våra befintliga klientanrop)
val data = withContext(Dispatchers.IO) { val data = withContext(Dispatchers.IO) {
fetchNearbyData( // fetchNearbyData( //
lat = loc.latitude, // lat = gp.lat, //
lon = loc.longitude, // lon = gp.lon, //
showKlotter = showKlotter, // showKlotter = showKlotter, //
showWiki = showWiki, // showWiki = showWiki, //
showDm = showDm, // showDm = showDm, //

View File

@@ -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
}
}

View File

@@ -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?
}