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:
2025-09-14 22:47:52 +02:00
parent 4c87ef9490
commit ac451816e0
3 changed files with 155 additions and 75 deletions

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

View File

@@ -59,5 +59,4 @@ fun NearbyContent(
}
}
}
}