shared(nearby): add AutoScan (scanAndMarkFound + ScanConfig) with parallel fetch & DI
app(main): replace three autoscan effects with one using shared AutoScan; refresh Found on new items app(settings): after “Clear all finds”, run immediate local rescan via shared AutoScan and repopulate UI
This commit is contained in:
@@ -113,10 +113,12 @@ import app.klottr.data.NoteRepository
|
|||||||
import app.klottr.location.AndroidLocationProvider
|
import app.klottr.location.AndroidLocationProvider
|
||||||
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.ScanConfig
|
||||||
import app.klottr.nearby.buildNearbyEntries // Delad builder för “Närliggande”
|
import app.klottr.nearby.buildNearbyEntries // Delad builder för “Närliggande”
|
||||||
import app.klottr.nearby.enrichKlotterLabels // Delad etikett-berikning
|
import app.klottr.nearby.enrichKlotterLabels // Delad etikett-berikning
|
||||||
import app.klottr.nearby.fetchNearbyData // KMP-hjälpare som hämtar alla lager
|
import app.klottr.nearby.fetchNearbyData // KMP-hjälpare som hämtar alla lager
|
||||||
import app.klottr.nearby.resolveFoundOpenAction // Delad öppna-resolver
|
import app.klottr.nearby.resolveFoundOpenAction // Delad öppna-resolver
|
||||||
|
import app.klottr.nearby.scanAndMarkFound
|
||||||
import app.klottr.nearby.subtitleFor // Delad formatterare för undertexten
|
import app.klottr.nearby.subtitleFor // Delad formatterare för undertexten
|
||||||
import app.klottr.net.Api
|
import app.klottr.net.Api
|
||||||
import app.klottr.net.DigitaltMuseum
|
import app.klottr.net.DigitaltMuseum
|
||||||
@@ -516,82 +518,42 @@ private fun KlottrApp(
|
|||||||
onDispose { prefs.unregisterOnSharedPreferenceChangeListener(listener) }
|
onDispose { prefs.unregisterOnSharedPreferenceChangeListener(listener) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-scan Wikipedia
|
LaunchedEffect(showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
|
||||||
LaunchedEffect(showWiki, wikiRadius) {
|
if (!showWiki && !showDm && !showFornsok) return@LaunchedEffect
|
||||||
if (!showWiki) return@LaunchedEffect
|
|
||||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
|
|
||||||
if (gp != null) {
|
|
||||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
|
|
||||||
val items = withContext(Dispatchers.IO) {
|
|
||||||
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 ->
|
|
||||||
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(),
|
|
||||||
label = w.title, lat = w.lat, lon = w.lon, timeMillis = now
|
|
||||||
)
|
|
||||||
if (added) anyNew = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (anyNew) refreshFound()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
val gp = AndroidLocationProvider.currentOrLastKnown(context)
|
||||||
|
|
||||||
// Auto-scan DigitaltMuseum
|
|
||||||
LaunchedEffect(showDm, dmRadius) {
|
|
||||||
if (!showDm) return@LaunchedEffect
|
|
||||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
|
|
||||||
if (gp != null) {
|
if (gp != null) {
|
||||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
|
val anyNew = withContext(Dispatchers.IO) {
|
||||||
val items = withContext(Dispatchers.IO) {
|
scanAndMarkFound(
|
||||||
DigitaltMuseum.getNearby(lat = gp.lat, lon = gp.lon, radiusMeters = dmRadius)
|
lat = gp.lat,
|
||||||
|
lon = gp.lon,
|
||||||
|
config = ScanConfig(
|
||||||
|
showWiki = showWiki,
|
||||||
|
wikiRadius = wikiRadius,
|
||||||
|
showDm = showDm,
|
||||||
|
dmRadius = dmRadius,
|
||||||
|
showFornsok = showFornsok,
|
||||||
|
fornsokRadius = fornsokRadius
|
||||||
|
),
|
||||||
|
fetchWiki = { la, lo, r -> Wikipedia.getNearby(la, lo, radiusMeters = r) },
|
||||||
|
fetchDm = { la, lo, r -> DigitaltMuseum.getNearby(la, lo, radiusMeters = r) },
|
||||||
|
fetchFornsok = { la, lo, r -> Fornsok.getNearby(la, lo, radiusMeters = r) },
|
||||||
|
markFound = { layer, id, label, la, lo, now ->
|
||||||
|
FoundStore.markFound(context, layer, id, label, la, lo, now)
|
||||||
|
},
|
||||||
|
updateAwayFlags = { la, lo ->
|
||||||
|
FoundStore.updateAwayFlags(context, la, lo)
|
||||||
}
|
}
|
||||||
val now = System.currentTimeMillis()
|
|
||||||
var anyNew = false
|
|
||||||
items.forEach { dmi ->
|
|
||||||
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,
|
|
||||||
label = dmi.title, lat = dmi.lat, lon = dmi.lon, timeMillis = now
|
|
||||||
)
|
)
|
||||||
if (added) anyNew = true
|
|
||||||
}
|
}
|
||||||
|
if (anyNew) {
|
||||||
|
forceEmptyFound = false // Sätt tillbaka visning efter “Glöm alla fynd”
|
||||||
|
refreshFound() // Tvinga omritning av “Senaste”
|
||||||
|
context.sendBroadcast(Intent(LocationService.ACTION_FOUND_UPDATED)) // Pinga övriga lyssnare (som tidigare)
|
||||||
}
|
}
|
||||||
if (anyNew) refreshFound()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-scan Fornsök
|
|
||||||
LaunchedEffect(showFornsok, fornsokRadius) {
|
|
||||||
if (!showFornsok) return@LaunchedEffect
|
|
||||||
val gp = AndroidLocationProvider.currentOrLastKnown(context) // Nytt
|
|
||||||
if (gp != null) {
|
|
||||||
FoundStore.updateAwayFlags(context, gp.lat, gp.lon)
|
|
||||||
val items = withContext(Dispatchers.IO) {
|
|
||||||
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(gp.lat, gp.lon, f.lat, f.lon).roundToInt()
|
|
||||||
if (d <= fornsokRadius) {
|
|
||||||
val added = FoundStore.markFound(
|
|
||||||
context, FoundStore.LAYER_FORNSOK, f.id,
|
|
||||||
label = f.title, lat = f.lat, lon = f.lon, timeMillis = now
|
|
||||||
)
|
|
||||||
if (added) anyNew = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (anyNew) refreshFound()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var showAbout by remember { mutableStateOf(false) }
|
var showAbout by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
@@ -923,9 +885,9 @@ private fun KlottrApp(
|
|||||||
onFornsokRadiusChanged = { SettingsStore.setFornsokRadiusMeters(it); fornsokRadius = it },
|
onFornsokRadiusChanged = { SettingsStore.setFornsokRadiusMeters(it); fornsokRadius = it },
|
||||||
|
|
||||||
onClearFinds = {
|
onClearFinds = {
|
||||||
FoundStore.clearAll(context)
|
FoundStore.clearAll(context) // Töm allt
|
||||||
forceEmptyFound = true
|
forceEmptyFound = true // Visa tomt direkt
|
||||||
refreshFound()
|
refreshFound() // Tvinga omritning
|
||||||
|
|
||||||
// 1) Nollställ sessions-gates inne i tjänsten
|
// 1) Nollställ sessions-gates inne i tjänsten
|
||||||
context.startService(
|
context.startService(
|
||||||
@@ -933,15 +895,50 @@ private fun KlottrApp(
|
|||||||
.setAction(LocationService.ACTION_CLEAR_FOUND_SESSION)
|
.setAction(LocationService.ACTION_CLEAR_FOUND_SESSION)
|
||||||
)
|
)
|
||||||
|
|
||||||
// 2) Pinga UI:t att rita om (som du redan gjorde)
|
// 2) Pinga UI:t att rita om (nollat läge)
|
||||||
context.sendBroadcast(Intent(LocationService.ACTION_FOUND_UPDATED))
|
context.sendBroadcast(Intent(LocationService.ACTION_FOUND_UPDATED))
|
||||||
|
|
||||||
// 3) Kör en direkt rescan inifrån tjänsten (→ första fyndet kan ge notis)
|
// 3) Be tjänsten göra en rescan
|
||||||
context.startService(
|
context.startService(
|
||||||
Intent(context, LocationService::class.java)
|
Intent(context, LocationService::class.java)
|
||||||
.setAction(LocationService.ACTION_RESCAN_NOW)
|
.setAction(LocationService.ACTION_RESCAN_NOW)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 4) Gör även en direkt lokal scan nu (parallellt) → snabb UI-återfyllnad
|
||||||
|
// Använder vår delade scanAndMarkFound så listan fylls utan att vänta på tjänsten.
|
||||||
|
val cfg = ScanConfig(
|
||||||
|
showWiki = showWiki,
|
||||||
|
wikiRadius = wikiRadius,
|
||||||
|
showDm = showDm,
|
||||||
|
dmRadius = dmRadius,
|
||||||
|
showFornsok = showFornsok,
|
||||||
|
fornsokRadius = fornsokRadius
|
||||||
|
)
|
||||||
|
scope.launch {
|
||||||
|
val gp = AndroidLocationProvider.currentOrLastKnown(context)
|
||||||
|
if (gp != null) {
|
||||||
|
val anyNew = withContext(Dispatchers.IO) {
|
||||||
|
scanAndMarkFound(
|
||||||
|
lat = gp.lat, lon = gp.lon,
|
||||||
|
config = cfg,
|
||||||
|
fetchWiki = { la, lo, r -> Wikipedia.getNearby(la, lo, radiusMeters = r) },
|
||||||
|
fetchDm = { la, lo, r -> DigitaltMuseum.getNearby(la, lo, radiusMeters = r) },
|
||||||
|
fetchFornsok = { la, lo, r -> Fornsok.getNearby(la, lo, radiusMeters = r) },
|
||||||
|
markFound = { layer, id, label, la, lo, now ->
|
||||||
|
FoundStore.markFound(context, layer, id, label, la, lo, now)
|
||||||
|
},
|
||||||
|
updateAwayFlags = { la, lo ->
|
||||||
|
FoundStore.updateAwayFlags(context, la, lo)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (anyNew) {
|
||||||
|
forceEmptyFound = false // Tillåt visning igen
|
||||||
|
refreshFound() // Rita om “Senaste”
|
||||||
|
context.sendBroadcast(Intent(LocationService.ACTION_FOUND_UPDATED)) // Informera andra lyssnare
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Toast.makeText(context, "Alla fynd rensade.", Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, "Alla fynd rensade.", Toast.LENGTH_SHORT).show()
|
||||||
},
|
},
|
||||||
onClose = { showSettings = false }
|
onClose = { showSettings = false }
|
||||||
|
|||||||
84
shared/src/commonMain/kotlin/app/klottr/nearby/AutoScan.kt
Normal file
84
shared/src/commonMain/kotlin/app/klottr/nearby/AutoScan.kt
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package app.klottr.nearby
|
||||||
|
|
||||||
|
import app.klottr.net.DigitaltMuseumItem
|
||||||
|
import app.klottr.net.FornsokItem
|
||||||
|
import app.klottr.net.WikipediaPage
|
||||||
|
import app.klottr.util.FoundStore
|
||||||
|
import app.klottr.util.distanceMeters
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
/** Konfiguration för autoscan (toggles + radier). */
|
||||||
|
data class ScanConfig(
|
||||||
|
val showWiki: Boolean,
|
||||||
|
val wikiRadius: Int,
|
||||||
|
val showDm: Boolean,
|
||||||
|
val dmRadius: Int,
|
||||||
|
val showFornsok: Boolean,
|
||||||
|
val fornsokRadius: Int
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kör autoscan för valda lager och markerar nya fynd.
|
||||||
|
* Returnerar true om något nytt markerades.
|
||||||
|
* - Plattformsspecifika anrop injiceras via lambdas (DI).
|
||||||
|
*/
|
||||||
|
suspend fun scanAndMarkFound(
|
||||||
|
lat: Double,
|
||||||
|
lon: Double,
|
||||||
|
config: ScanConfig,
|
||||||
|
fetchWiki: suspend (Double, Double, Int) -> List<WikipediaPage>,
|
||||||
|
fetchDm: suspend (Double, Double, Int) -> List<DigitaltMuseumItem>,
|
||||||
|
fetchFornsok: suspend (Double, Double, Int) -> List<FornsokItem>,
|
||||||
|
markFound: (layer: String, id: String, label: String, lat: Double, lon: Double, nowMillis: Long) -> Boolean,
|
||||||
|
updateAwayFlags: (Double, Double) -> Unit
|
||||||
|
): Boolean = coroutineScope {
|
||||||
|
updateAwayFlags(lat, lon)
|
||||||
|
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
|
||||||
|
val wikiDef = async {
|
||||||
|
if (!config.showWiki) emptyList()
|
||||||
|
else runCatching { fetchWiki(lat, lon, config.wikiRadius) }.getOrElse { emptyList() }
|
||||||
|
}
|
||||||
|
val dmDef = async {
|
||||||
|
if (!config.showDm) emptyList()
|
||||||
|
else runCatching { fetchDm(lat, lon, config.dmRadius) }.getOrElse { emptyList() }
|
||||||
|
}
|
||||||
|
val fornDef = async {
|
||||||
|
if (!config.showFornsok) emptyList()
|
||||||
|
else runCatching { fetchFornsok(lat, lon, config.fornsokRadius) }.getOrElse { emptyList() }
|
||||||
|
}
|
||||||
|
|
||||||
|
var anyNew = false
|
||||||
|
|
||||||
|
// Wikipedia
|
||||||
|
for (w in wikiDef.await()) {
|
||||||
|
val d = distanceMeters(lat, lon, w.lat, w.lon).roundToInt()
|
||||||
|
if (d <= config.wikiRadius) {
|
||||||
|
val added = markFound(FoundStore.LAYER_WIKI, w.pageId.toString(), w.title, w.lat, w.lon, now)
|
||||||
|
if (added) anyNew = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DigitaltMuseum
|
||||||
|
for (dmi in dmDef.await()) {
|
||||||
|
val d = distanceMeters(lat, lon, dmi.lat, dmi.lon).roundToInt()
|
||||||
|
if (d <= config.dmRadius) {
|
||||||
|
val added = markFound(FoundStore.LAYER_DM, dmi.id, dmi.title, dmi.lat, dmi.lon, now)
|
||||||
|
if (added) anyNew = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fornsök
|
||||||
|
for (f in fornDef.await()) {
|
||||||
|
val d = distanceMeters(lat, lon, f.lat, f.lon).roundToInt()
|
||||||
|
if (d <= config.fornsokRadius) {
|
||||||
|
val added = markFound(FoundStore.LAYER_FORNSOK, f.id, f.title, f.lat, f.lon, now)
|
||||||
|
if (added) anyNew = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
anyNew
|
||||||
|
}
|
||||||
@@ -59,5 +59,4 @@ fun NearbyContent(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user