Move stores using SharedPrefs to :shared and use KvPrefs instead
This commit is contained in:
@@ -16,6 +16,10 @@ expect object KvPrefs {
|
||||
fun getStringSet(key: String): Set<String>
|
||||
fun putStringSet(key: String, value: Set<String>)
|
||||
|
||||
fun getLong(key: String, def: Long?): Long?
|
||||
fun putLong(key: String, value: Long)
|
||||
|
||||
fun contains(key: String): Boolean
|
||||
fun remove(key: String)
|
||||
fun clearAll()
|
||||
|
||||
|
||||
24
shared/src/commonMain/kotlin/app/klottr/util/Config.kt
Normal file
24
shared/src/commonMain/kotlin/app/klottr/util/Config.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package app.klottr.util
|
||||
|
||||
object Config {
|
||||
// Finns redan hos dig:
|
||||
const val DEFAULT_KLOTTER_RADIUS_M: Int = 20
|
||||
|
||||
// ---- LAGER-REGISTRET (flexibelt) ----
|
||||
data class LayerSpec(val id: String, val displayName: String, val defaultRadiusM: Int?)
|
||||
|
||||
object Layers {
|
||||
// Du kan lägga till fler senare utan att pilla i kartan
|
||||
val REGISTRY: Map<String, LayerSpec> = mapOf(
|
||||
"klotter" to LayerSpec("klotter", "Klotter", DEFAULT_KLOTTER_RADIUS_M),
|
||||
"wikipedia" to LayerSpec("wikipedia", "Wikipedia", null),
|
||||
"fornsok" to LayerSpec("fornsok", "Fornsök", null),
|
||||
"dm" to LayerSpec("dm", "DigitaltMuseum", null)
|
||||
)
|
||||
|
||||
fun name(id: String): String = REGISTRY[id]?.displayName
|
||||
?: id.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
|
||||
|
||||
fun radius(id: String): Int? = REGISTRY[id]?.defaultRadiusM
|
||||
}
|
||||
}
|
||||
12
shared/src/commonMain/kotlin/app/klottr/util/Debug.kt
Normal file
12
shared/src/commonMain/kotlin/app/klottr/util/Debug.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package app.klottr.util
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
||||
object DebugState {
|
||||
// Uppdateras av LocationService
|
||||
val intervalMs = MutableStateFlow(15_000L)
|
||||
val minDistanceM = MutableStateFlow(5f)
|
||||
val speedMps = MutableStateFlow(0f)
|
||||
val lastLat = MutableStateFlow<Double?>(null)
|
||||
val lastLon = MutableStateFlow<Double?>(null)
|
||||
}
|
||||
20
shared/src/commonMain/kotlin/app/klottr/util/Logs.kt
Normal file
20
shared/src/commonMain/kotlin/app/klottr/util/Logs.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package app.klottr.util
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
||||
data class HitEntry(
|
||||
val text: String,
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
val timeMillis: Long
|
||||
)
|
||||
|
||||
object HitLog {
|
||||
// Senaste överst. Begränsa till t.ex. 200 rader.
|
||||
val entries = MutableStateFlow<List<HitEntry>>(emptyList())
|
||||
|
||||
fun add(text: String, lat: Double, lon: Double, timeMillis: Long) {
|
||||
val now = listOf(HitEntry(text, lat, lon, timeMillis))
|
||||
entries.value = (now + entries.value).take(200)
|
||||
}
|
||||
}
|
||||
70
shared/src/commonMain/kotlin/app/klottr/util/OutboxStore.kt
Normal file
70
shared/src/commonMain/kotlin/app/klottr/util/OutboxStore.kt
Normal file
@@ -0,0 +1,70 @@
|
||||
package app.klottr.util
|
||||
|
||||
import app.klottr.prefs.KvPrefs
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Enkel "utkorg" i KvPrefs. Lagrar klotter som inte kunde postas
|
||||
* för att skickas senare när nätet kommer tillbaka.
|
||||
*/
|
||||
object OutboxStore {
|
||||
private const val KEY = "pending_json"
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
@Serializable
|
||||
data class PendingNote(
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
val createdAt: Long,
|
||||
// TEXT-variant (som tidigare)
|
||||
val text: String? = null,
|
||||
// DOC-variant (ny)
|
||||
val title: String? = null,
|
||||
val body: String? = null,
|
||||
val radiusM: Int = 20,
|
||||
// BILD (ny) – lagras som LOKAL fil i appens lagring
|
||||
val imagePath: String? = null,
|
||||
val imageMime: String? = null
|
||||
)
|
||||
|
||||
fun add(lat: Double, lon: Double, text: String, createdAt: Long) {
|
||||
val list = all().toMutableList()
|
||||
list.add(PendingNote(lat = lat, lon = lon, createdAt = createdAt, text = text))
|
||||
save(list)
|
||||
}
|
||||
|
||||
fun addDoc(
|
||||
lat: Double,
|
||||
lon: Double,
|
||||
title: String,
|
||||
body: String,
|
||||
createdAt: Long,
|
||||
radiusM: Int,
|
||||
imagePath: String?, // absolut sökväg till vår lokala kopia (om vald)
|
||||
imageMime: String? // "image/jpeg" osv (om vald)
|
||||
) {
|
||||
val list = all().toMutableList()
|
||||
list.add(
|
||||
PendingNote(
|
||||
lat = lat, lon = lon, createdAt = createdAt,
|
||||
title = title, body = body, radiusM = radiusM,
|
||||
imagePath = imagePath, imageMime = imageMime
|
||||
)
|
||||
)
|
||||
save(list)
|
||||
}
|
||||
|
||||
fun all(): List<PendingNote> =
|
||||
json.decodeFromString(KvPrefs.getString(KEY, "[]")!!)
|
||||
|
||||
fun replaceAll(items: List<PendingNote>) {
|
||||
save(items)
|
||||
}
|
||||
|
||||
private fun save(items: List<PendingNote>) {
|
||||
KvPrefs.putString(KEY, json.encodeToString(items))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package app.klottr.util
|
||||
|
||||
import app.klottr.prefs.KvPrefs
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.decodeFromString
|
||||
|
||||
/**
|
||||
* Beständig lista över de senaste träffarna ("Funnet klotter") över appstarter.
|
||||
* Lagrar de senaste 200 med (text, lat, lon, timeMillis), nyast först.
|
||||
*/
|
||||
object RecentHitsStore {
|
||||
private const val KEY = "hits_json" // keep the same key name
|
||||
private const val MAX = 200
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
fun append(entry: HitEntry) {
|
||||
val cur = decode().toMutableList()
|
||||
cur.add(0, entry.toDto())
|
||||
if (cur.size > MAX) cur.subList(MAX, cur.size).clear()
|
||||
KvPrefs.putString(KEY, json.encodeToString(cur))
|
||||
}
|
||||
|
||||
fun list(): List<HitEntry> =
|
||||
decode().map { it.toModel() }
|
||||
|
||||
fun clear() = KvPrefs.remove(KEY)
|
||||
|
||||
private fun decode(): List<HitEntryDto> =
|
||||
json.decodeFromString(KvPrefs.getString(KEY, "[]")!!)
|
||||
|
||||
@Serializable
|
||||
private data class HitEntryDto(
|
||||
val text: String,
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
val timeMillis: Long
|
||||
)
|
||||
|
||||
// Map storage DTO <-> your domain model
|
||||
private fun HitEntry.toDto() = HitEntryDto(text = text, lat = lat, lon = lon, timeMillis = timeMillis)
|
||||
private fun HitEntryDto.toModel() = HitEntry(text = text, lat = lat, lon = lon, timeMillis = timeMillis)
|
||||
}
|
||||
29
shared/src/commonMain/kotlin/app/klottr/util/SeenHits.kt
Normal file
29
shared/src/commonMain/kotlin/app/klottr/util/SeenHits.kt
Normal file
@@ -0,0 +1,29 @@
|
||||
package app.klottr.util
|
||||
|
||||
import app.klottr.prefs.KvPrefs
|
||||
|
||||
/**
|
||||
* Lagrar vilka Note-ID:n som redan har visats som träff.
|
||||
* Implementerat med KvPrefs StringSet (fungerar på Android+iOS).
|
||||
*/
|
||||
object SeenHits {
|
||||
private const val KEY_SET = "seen_ids"
|
||||
|
||||
fun isSeen(id: Long): Boolean {
|
||||
if (!KvPrefs.contains(KEY_SET)) return false
|
||||
return KvPrefs.getStringSet(KEY_SET).contains(id.toString())
|
||||
}
|
||||
|
||||
fun markSeen(id: Long) {
|
||||
val s = id.toString()
|
||||
if (!KvPrefs.contains(KEY_SET)) {
|
||||
KvPrefs.putStringSet(KEY_SET, setOf(s))
|
||||
return
|
||||
}
|
||||
val set = KvPrefs.getStringSet(KEY_SET).toMutableSet()
|
||||
if (set.add(s)) KvPrefs.putStringSet(KEY_SET, set)
|
||||
}
|
||||
|
||||
/** För felsökning / återställning - rensa listan */
|
||||
fun clear() = KvPrefs.remove(KEY_SET)
|
||||
}
|
||||
26
shared/src/commonMain/kotlin/app/klottr/util/UserId.kt
Normal file
26
shared/src/commonMain/kotlin/app/klottr/util/UserId.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package app.klottr.util
|
||||
|
||||
import app.klottr.prefs.KvPrefs
|
||||
import kotlin.random.Random
|
||||
|
||||
object UserId {
|
||||
private const val KEY = "user_id"
|
||||
|
||||
/** Ett anonymt men stabilt ID per app-installation (RFC4122 v4-format, multiplatform). */
|
||||
fun get(): String {
|
||||
if (KvPrefs.contains(KEY)) {
|
||||
KvPrefs.getString(KEY, null)?.let { return it }
|
||||
}
|
||||
val fresh = randomUuidV4()
|
||||
KvPrefs.putString(KEY, fresh)
|
||||
return fresh
|
||||
}
|
||||
|
||||
private fun randomUuidV4(): String {
|
||||
val b = Random.nextBytes(16)
|
||||
b[6] = ((b[6].toInt() and 0x0f) or 0x40).toByte() // version 4
|
||||
b[8] = ((b[8].toInt() and 0x3f) or 0x80).toByte() // variant 10
|
||||
val h = b.joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') }
|
||||
return "${h.substring(0,8)}-${h.substring(8,12)}-${h.substring(12,16)}-${h.substring(16,20)}-${h.substring(20)}"
|
||||
}
|
||||
}
|
||||
28
shared/src/commonMain/kotlin/app/klottr/util/WikiHitIndex.kt
Normal file
28
shared/src/commonMain/kotlin/app/klottr/util/WikiHitIndex.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
package app.klottr.util
|
||||
|
||||
import app.klottr.prefs.KvPrefs
|
||||
|
||||
object WikiHitIndex {
|
||||
// Track keys so we can clear later without platform-specific APIs.
|
||||
private const val KEY_INDEX = "wiki_hit_index_keys"
|
||||
private fun keyFor(timeMillis: Long) = "wiki_hit_$timeMillis"
|
||||
|
||||
fun put(timeMillis: Long, pageId: Long) {
|
||||
val k = keyFor(timeMillis)
|
||||
KvPrefs.putLong(k, pageId) // store Long as String (KvPrefs has no Long API)
|
||||
val idx = KvPrefs.getStringSet(KEY_INDEX).toMutableSet()
|
||||
if (idx.add(k)) KvPrefs.putStringSet(KEY_INDEX, idx)
|
||||
}
|
||||
|
||||
fun isWiki(timeMillis: Long): Boolean =
|
||||
KvPrefs.contains(keyFor(timeMillis))
|
||||
|
||||
fun getPageId(timeMillis: Long): Long? =
|
||||
KvPrefs.getLong(keyFor(timeMillis),null)
|
||||
|
||||
fun clear() {
|
||||
val idx = KvPrefs.getStringSet(KEY_INDEX)
|
||||
idx.forEach { KvPrefs.remove(it) }
|
||||
KvPrefs.remove(KEY_INDEX)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user