feat(layers): server-driven layer config (get_layers.php) + client integration; apply grMin/grMax and render new slugs dynamically
This commit is contained in:
@@ -70,6 +70,7 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
@@ -119,9 +120,11 @@ import app.klottr.net.DigitaltMuseum
|
||||
import app.klottr.net.DigitaltMuseumItem
|
||||
import app.klottr.net.FornsokShared
|
||||
import app.klottr.net.Geocache
|
||||
import app.klottr.net.LayerDTO
|
||||
import app.klottr.net.UserStats
|
||||
import app.klottr.net.Wikipedia
|
||||
import app.klottr.net.createNoteDoc
|
||||
import app.klottr.net.fetchLayers
|
||||
import app.klottr.net.fetchMe
|
||||
import app.klottr.net.fetchNoteDetail
|
||||
import app.klottr.net.fetchUserNotes
|
||||
@@ -179,7 +182,6 @@ fun KlottrAppShell(
|
||||
app.klottr.util.AuthShared.loadFromStorage()
|
||||
}
|
||||
|
||||
|
||||
// Inloggning
|
||||
val loggedIn by AuthShared.loggedIn.collectAsState(initial = app.klottr.util.AuthShared.loggedIn.value)
|
||||
|
||||
@@ -198,7 +200,6 @@ fun KlottrAppShell(
|
||||
.mapToList(Dispatchers.IO)
|
||||
.collectAsState(initial = emptyList())
|
||||
|
||||
|
||||
// Settings-state
|
||||
// Läser från KMP SettingsStore (no-arg-API). Inga Context-beroenden här längre.
|
||||
var onlyNew by remember { mutableStateOf(SettingsStore.getOnlyNewHits()) }
|
||||
@@ -212,6 +213,16 @@ fun KlottrAppShell(
|
||||
var dmRadius by remember { mutableStateOf(SettingsStore.getDigitaltMuseumRadiusMeters()) }
|
||||
var showFornsok by remember { mutableStateOf(SettingsStore.getShowFornsok()) }
|
||||
var fornsokRadius by remember { mutableStateOf(SettingsStore.getFornsokRadiusMeters()) }
|
||||
|
||||
// 🔸 Lagrar dynamiska lagergränser som hämtas från servern
|
||||
var layerRanges by remember { mutableStateOf<Map<String, Pair<Int, Int>>>(emptyMap()) }
|
||||
var layers by remember { mutableStateOf<List<LayerDTO>>(emptyList()) }
|
||||
|
||||
// Dynamisk visning/konfiguration för lager som inte har hårdkopplad SettingsStore-koppling ännu
|
||||
val dynamicVisible = remember { mutableStateMapOf<String, Boolean>() } // slug -> on/off
|
||||
val dynamicRadius = remember { mutableStateMapOf<String, Int>() } // slug -> aktuell radius
|
||||
|
||||
|
||||
// Aktiv flik (lyfts upp så top-baren kan läsa den)
|
||||
var selectedTab by rememberSaveable { mutableStateOf("home") }
|
||||
|
||||
@@ -262,6 +273,8 @@ fun KlottrAppShell(
|
||||
var forceEmptyFound by remember { mutableStateOf(false) }
|
||||
fun refreshFound() { foundRefresh++ }
|
||||
|
||||
|
||||
|
||||
LaunchedEffect(loggedIn) {
|
||||
if (!loggedIn) {
|
||||
showUsernameDialog = false
|
||||
@@ -298,6 +311,44 @@ fun KlottrAppShell(
|
||||
}
|
||||
}
|
||||
|
||||
// 🔸 Hämtar dynamiska lagerdata vid start
|
||||
LaunchedEffect(Unit) {
|
||||
val res = fetchLayers()
|
||||
if (res.ok && res.items.isNotEmpty()) {
|
||||
val tmp = mutableMapOf<String, Pair<Int, Int>>()
|
||||
for (layer in res.items) {
|
||||
val minVal = layer.grMin ?: continue
|
||||
val maxVal = layer.grMax ?: continue
|
||||
tmp[layer.slug] = minVal to maxVal
|
||||
}
|
||||
layerRanges = tmp
|
||||
layers = res.items.sortedBy { it.sortOrder }
|
||||
|
||||
// Initiera dynamiska states för slugs som inte är “kända”
|
||||
val known = setOf("klotter", "wikipedia", "digitaltmuseum", "fornsok")
|
||||
for (l in layers) {
|
||||
if (l.slug !in known) {
|
||||
// om inte redan satta: default av = false, default radius = grMin
|
||||
dynamicVisible.putIfAbsent(l.slug, false)
|
||||
layerRanges[l.slug]?.first?.let { defMin ->
|
||||
dynamicRadius.putIfAbsent(l.slug, defMin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println("LAGERDBG: ✅ Lager hämtade: ${layers.size} st")
|
||||
layers.forEach { l ->
|
||||
val p = layerRanges[l.slug]
|
||||
println("LAGERDBG: - ${l.slug} (${l.displayName}): ${p?.first}..${p?.second} m")
|
||||
}
|
||||
} else {
|
||||
println("LAGERDBG: ⚠️ Misslyckades att hämta lager från servern.")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 🔴 NYTT: Omedelbar UI-uppdatering när FoundStore ändras
|
||||
// Found updates via shared event bus (no broadcasts)
|
||||
LaunchedEffect(Unit) {
|
||||
@@ -571,7 +622,6 @@ fun KlottrAppShell(
|
||||
.pointerInput(selectedTab) { } // 👈 blockerar klick när fliken inte är aktiv
|
||||
|
||||
) {
|
||||
// Samma innehåll som SettingsScreen men reducerat till endast lagren
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@@ -579,78 +629,103 @@ fun KlottrAppShell(
|
||||
.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
// 🔹 Klotter
|
||||
DiscreteSliderRow(
|
||||
label = "Visningsradie för Klotter",
|
||||
value = if (showKlotter) 20 else -1,
|
||||
grMin = 20,
|
||||
grMax = 20,
|
||||
onValueChange = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowKlotter(true)
|
||||
showKlotter = true
|
||||
} else {
|
||||
SettingsStore.setShowKlotter(false)
|
||||
showKlotter = false
|
||||
}
|
||||
}
|
||||
)
|
||||
for (layer in layers) {
|
||||
val bounds = layerRanges[layer.slug] ?: continue
|
||||
val (minVal, maxVal) = bounds
|
||||
|
||||
// 🔹 Diskret slider för Wikipedia-radie
|
||||
DiscreteSliderRow(
|
||||
label = "Visningsradie för Wikipedia",
|
||||
value = if (showWiki) wikiRadius else -1,
|
||||
grMin = 20,
|
||||
grMax = 5000,
|
||||
onValueChange = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowWikipedia(true)
|
||||
SettingsStore.setWikiRadiusMeters(newVal)
|
||||
wikiRadius = newVal
|
||||
showWiki = true
|
||||
} else {
|
||||
SettingsStore.setShowWikipedia(false)
|
||||
showWiki = false
|
||||
}
|
||||
}
|
||||
)
|
||||
// Hämta aktuellt värde/datakälla för value och en onChange-handler
|
||||
// 1) Kända slugs → använd befintlig SettingsStore-koppling (behåller appbeteendet).
|
||||
// 2) Övriga slugs → använd dynamiska mappar (syns direkt när DB får ett nytt lager).
|
||||
val (value: Int, onChange: (Int, Boolean) -> Unit) =
|
||||
when (layer.slug) {
|
||||
|
||||
// 🔹 Diskret slider för DigitaltMuseum-radie
|
||||
DiscreteSliderRow(
|
||||
label = "Visningsrade för DigitaltMuseum",
|
||||
value = if (showDm) dmRadius else -1,
|
||||
grMin = 50,
|
||||
grMax = 2000,
|
||||
onValueChange = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowDigitaltMuseum(true)
|
||||
SettingsStore.setDigitaltMuseumRadiusMeters(newVal)
|
||||
dmRadius = newVal
|
||||
showDm = true
|
||||
} else {
|
||||
SettingsStore.setShowDigitaltMuseum(false)
|
||||
showDm = false
|
||||
"klotter" -> {
|
||||
val v = if (showKlotter) minVal else -1 // klotter har fast radie (min==max enligt DB)
|
||||
val h: (Int, Boolean) -> Unit = { _, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowKlotter(true)
|
||||
showKlotter = true
|
||||
} else {
|
||||
SettingsStore.setShowKlotter(false)
|
||||
showKlotter = false
|
||||
}
|
||||
}
|
||||
v to h
|
||||
}
|
||||
|
||||
"wikipedia" -> {
|
||||
val v = if (showWiki) wikiRadius else -1
|
||||
val h: (Int, Boolean) -> Unit = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowWikipedia(true)
|
||||
SettingsStore.setWikiRadiusMeters(newVal)
|
||||
wikiRadius = newVal
|
||||
showWiki = true
|
||||
} else {
|
||||
SettingsStore.setShowWikipedia(false)
|
||||
showWiki = false
|
||||
}
|
||||
}
|
||||
v to h
|
||||
}
|
||||
|
||||
"digitaltmuseum" -> {
|
||||
val v = if (showDm) dmRadius else -1
|
||||
val h: (Int, Boolean) -> Unit = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowDigitaltMuseum(true)
|
||||
SettingsStore.setDigitaltMuseumRadiusMeters(newVal)
|
||||
dmRadius = newVal
|
||||
showDm = true
|
||||
} else {
|
||||
SettingsStore.setShowDigitaltMuseum(false)
|
||||
showDm = false
|
||||
}
|
||||
}
|
||||
v to h
|
||||
}
|
||||
|
||||
"fornsok" -> {
|
||||
val v = if (showFornsok) fornsokRadius else -1
|
||||
val h: (Int, Boolean) -> Unit = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowFornsok(true)
|
||||
SettingsStore.setFornsokRadiusMeters(newVal)
|
||||
fornsokRadius = newVal
|
||||
showFornsok = true
|
||||
} else {
|
||||
SettingsStore.setShowFornsok(false)
|
||||
showFornsok = false
|
||||
}
|
||||
}
|
||||
v to h
|
||||
}
|
||||
|
||||
else -> {
|
||||
// DYNAMISK standard: visa slider för okända slugs, spara i minnes-mapparna.
|
||||
val curVisible = dynamicVisible[layer.slug] == true
|
||||
val curRadius = dynamicRadius[layer.slug] ?: minVal
|
||||
val v = if (curVisible) curRadius else -1
|
||||
val h: (Int, Boolean) -> Unit = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
dynamicVisible[layer.slug] = true
|
||||
dynamicRadius[layer.slug] = newVal
|
||||
} else {
|
||||
dynamicVisible[layer.slug] = false
|
||||
}
|
||||
}
|
||||
v to h
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
// 🔹 Diskret slider för Fornsök-radie
|
||||
DiscreteSliderRow(
|
||||
label = "Visningsradie för Fornsök",
|
||||
value = if (showFornsok) fornsokRadius else -1,
|
||||
grMin = 50,
|
||||
grMax = 5000,
|
||||
onValueChange = { newVal, enabled ->
|
||||
if (enabled) {
|
||||
SettingsStore.setShowFornsok(true)
|
||||
SettingsStore.setFornsokRadiusMeters(newVal)
|
||||
fornsokRadius = newVal
|
||||
showFornsok = true
|
||||
} else {
|
||||
SettingsStore.setShowFornsok(false)
|
||||
showFornsok = false
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
DiscreteSliderRow(
|
||||
label = "Visningsradie för ${layer.displayName}",
|
||||
value = value,
|
||||
grMin = minVal,
|
||||
grMax = maxVal,
|
||||
onValueChange = onChange
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1977,3 +2052,4 @@ private fun DiscreteSliderRow(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -320,3 +320,31 @@ suspend fun fetchUserNotes(
|
||||
limit = limit
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* get_layers.php → shared.
|
||||
*
|
||||
* Hämtar lista över tillgängliga lager samt deras konfig (t.ex. gränser för radie).
|
||||
* Går alltid via POST (inte GET) eftersom servern blockerar GET-anrop.
|
||||
*
|
||||
* @param lastEtag senaste ETag som klienten har cache:at (eller null)
|
||||
* @return LayersResult – alltid icke-null, men kan innehålla ok=false om fel
|
||||
*/
|
||||
suspend fun fetchLayers(
|
||||
lastEtag: String? = null
|
||||
): LayersResult {
|
||||
return try {
|
||||
// Anropa delade KMP-funktionen som pratar med get_layers.php
|
||||
KlottrApiShared.getLayers(
|
||||
baseUrl = ApiConfig.SERVER_API_BASE_URL,
|
||||
apiKey = ApiConfig.SERVER_API_KEY,
|
||||
bearerToken = AuthShared.idToken,
|
||||
lastEtag = lastEtag
|
||||
)
|
||||
} catch (t: Throwable) {
|
||||
// Vid fel loggar vi, men returnerar tomt resultat så appen inte kraschar
|
||||
KLog.d { "get_layers exception: ${t.message}" }
|
||||
LayersResult(ok = false, etag = null, items = emptyList())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.request.forms.formData
|
||||
import io.ktor.client.request.forms.submitForm
|
||||
import io.ktor.client.request.forms.submitFormWithBinaryData
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.Headers
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.Parameters
|
||||
import io.ktor.http.contentType
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.json.*
|
||||
@@ -701,5 +709,41 @@ object KlottrApiShared {
|
||||
if (resp.status.value != 200) return null
|
||||
return try { resp.body<ByteArray>() } catch (_: Throwable) { null }
|
||||
}
|
||||
/**
|
||||
* get_layers.php → Hämtar tillgängliga lager och deras gränser.
|
||||
* Körs via POST (inte GET).
|
||||
* Skickar API-nyckel i headern "X-API-Key" enligt serversidans krav.
|
||||
*/
|
||||
suspend fun getLayers(
|
||||
baseUrl: String,
|
||||
apiKey: String,
|
||||
bearerToken: String?,
|
||||
lastEtag: String?
|
||||
): LayersResult {
|
||||
val client = HttpClient()
|
||||
val url = baseUrl + "get_layers.php"
|
||||
|
||||
// Skickar bara ETag i kroppen
|
||||
val payload = buildJsonObject {
|
||||
put("etag", lastEtag ?: "")
|
||||
}
|
||||
|
||||
return try {
|
||||
val response: HttpResponse = client.post(url) {
|
||||
contentType(ContentType.Application.Json)
|
||||
header("X-API-Key", apiKey) // ⬅️ VIKTIGT: API-nyckeln i header
|
||||
bearerToken?.let { header(HttpHeaders.Authorization, "Bearer $it") }
|
||||
setBody(payload.toString())
|
||||
}
|
||||
|
||||
val text = response.bodyAsText()
|
||||
println("LAGERDBG: 🟡 get_layers raw response: ${response.status.value} ${response.status.description}")
|
||||
println("LAGERDBG: 🟢 get_layers body: $text")
|
||||
Json.decodeFromString<LayersResult>(text)
|
||||
} catch (t: Throwable) {
|
||||
LayersResult(ok = false, etag = null, items = emptyList())
|
||||
} finally {
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Data-klass för ett lager (motsvarar en rad i tabellen layers).
|
||||
* Fältnamn matchar exakt serverns JSON-struktur i get_layers.php.
|
||||
*/
|
||||
@Serializable
|
||||
data class LayerDTO(
|
||||
val id: Int,
|
||||
val slug: String,
|
||||
val displayName: String,
|
||||
val notesTableOverride: String? = null,
|
||||
val external: Boolean = false,
|
||||
val sortOrder: Int = 0,
|
||||
val markerIcon: String? = null,
|
||||
val markerColour: String? = null,
|
||||
val encrypted: Boolean = false,
|
||||
val grMin: Int? = null,
|
||||
val grMax: Int? = null
|
||||
)
|
||||
|
||||
/**
|
||||
* Hela svaret från get_layers.php.
|
||||
* Om ok=false har ett fel uppstått eller JSON kunde inte tolkas.
|
||||
*/
|
||||
@Serializable
|
||||
data class LayersResult(
|
||||
val ok: Boolean,
|
||||
val etag: String? = null,
|
||||
val notModified: Boolean? = null,
|
||||
val items: List<LayerDTO> = emptyList()
|
||||
)
|
||||
Reference in New Issue
Block a user