Move stores using SharedPrefs to :shared and use KvPrefs instead

This commit is contained in:
Olof Hedman
2025-09-20 20:26:06 +02:00
parent a73f475957
commit 61fa43ff2c
18 changed files with 223 additions and 265 deletions

View File

@@ -1013,7 +1013,6 @@ private fun KlottrAppShell(
},
addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime ->
OutboxStore.addDoc(
context = context,
lat = la,
lon = lo,
title = t,
@@ -1084,7 +1083,6 @@ private fun KlottrAppShell(
},
addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime ->
OutboxStore.addDoc(
context = context,
lat = la,
lon = lo,
title = t,

View File

@@ -38,7 +38,7 @@ class OutboxWorker(
override suspend fun doWork(): Result {
val ctx = applicationContext
val pending = OutboxStore.all(ctx)
val pending = OutboxStore.all()
if (pending.isEmpty()) return Result.success()
val keep = mutableListOf<OutboxStore.PendingNote>()
@@ -137,7 +137,7 @@ class OutboxWorker(
if (!ok) keep.add(p)
}
OutboxStore.replaceAll(ctx, keep)
OutboxStore.replaceAll( keep)
return if (keep.isEmpty()) Result.success() else Result.retry()
}
}

View File

@@ -1,24 +0,0 @@
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
}
}

View File

@@ -1,12 +0,0 @@
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)
}

View File

@@ -1,20 +0,0 @@
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)
}
}

View File

@@ -1,126 +0,0 @@
package app.klottr.util
import android.content.Context
import org.json.JSONArray
import org.json.JSONObject
/**
* Enkel "utkorg" i SharedPreferences. Lagrar klotter som inte kunde postas
* för att skickas senare när nätet kommer tillbaka.
*/
object OutboxStore {
private const val PREFS = "klottr_outbox"
private const val KEY = "pending_json"
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
)
private fun prefs(ctx: Context) =
ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
fun add(context: Context, lat: Double, lon: Double, text: String, createdAt: Long) {
val list = all(context).toMutableList()
list.add(
PendingNote(
lat = lat,
lon = lon,
createdAt = createdAt,
text = text
)
)
save(context, list)
}
fun addDoc(
context: Context,
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(context).toMutableList()
list.add(
PendingNote(
lat = lat, lon = lon, createdAt = createdAt,
title = title, body = body, radiusM = radiusM,
imagePath = imagePath, imageMime = imageMime
)
)
save(context, list)
}
fun all(context: Context): List<PendingNote> {
val s = prefs(context).getString(KEY, "[]") ?: "[]"
val arr = JSONArray(s)
val out = ArrayList<PendingNote>(arr.length())
for (i in 0 until arr.length()) {
val o = arr.getJSONObject(i)
val lat = o.getDouble("lat")
val lon = o.getDouble("lon")
val createdAt = o.getLong("createdAt")
// text fanns alltid förr – men gör den valfri nu
val text = if (o.has("text")) o.optString("text", null).let { if (it.isNullOrEmpty()) null else it } else null
// nya fält (kan saknas i äldre JSON)
val title = if (o.has("title")) o.optString("title", null).let { if (it.isNullOrEmpty()) null else it } else null
val body = if (o.has("body")) o.optString("body", null).let { if (it.isNullOrEmpty()) null else it } else null
val radiusM = if (o.has("radiusM")) o.optInt("radiusM", 20) else 20
val imagePath = if (o.has("imagePath")) o.optString("imagePath", null).let { if (it.isNullOrEmpty()) null else it } else null
val imageMime = if (o.has("imageMime")) o.optString("imageMime", null).let { if (it.isNullOrEmpty()) null else it } else null
out.add(
PendingNote(
lat = lat, lon = lon, createdAt = createdAt,
text = text,
title = title, body = body, radiusM = radiusM,
imagePath = imagePath, imageMime = imageMime
)
)
}
return out
}
fun replaceAll(context: Context, items: List<PendingNote>) {
save(context, items)
}
private fun save(context: Context, items: List<PendingNote>) {
val arr = JSONArray()
for (p in items) {
val o = JSONObject()
o.put("lat", p.lat)
o.put("lon", p.lon)
o.put("createdAt", p.createdAt)
// skriv bara nycklar som har värden
p.text?.let { o.put("text", it) }
p.title?.let { o.put("title", it) }
p.body?.let { o.put("body", it) }
if (p.radiusM != 20) o.put("radiusM", p.radiusM)
p.imagePath?.let { o.put("imagePath", it) }
p.imageMime?.let { o.put("imageMime", it) }
arr.put(o)
}
prefs(context).edit().putString(KEY, arr.toString()).apply()
}
}

View File

@@ -1,57 +0,0 @@
package app.klottr.util
import android.content.Context
import org.json.JSONArray
import org.json.JSONObject
/**
* 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 PREFS = "klottr_recent_hits"
private const val KEY = "hits_json"
private const val MAX = 200
private fun prefs(ctx: Context) = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
fun append(ctx: Context, entry: HitEntry) {
val arr = JSONArray(prefs(ctx).getString(KEY, "[]") ?: "[]")
// stoppa in först (nyast först)
val obj = JSONObject()
obj.put("text", entry.text)
obj.put("lat", entry.lat)
obj.put("lon", entry.lon)
obj.put("timeMillis", entry.timeMillis)
// ny lista: [obj] + befintligt, upp till MAX
val newArr = JSONArray()
newArr.put(obj)
for (i in 0 until arr.length()) {
if (i + 1 >= MAX) break
newArr.put(arr.getJSONObject(i))
}
prefs(ctx).edit().putString(KEY, newArr.toString()).apply()
}
fun list(ctx: Context): List<HitEntry> {
val s = prefs(ctx).getString(KEY, "[]") ?: "[]"
val arr = JSONArray(s)
val out = ArrayList<HitEntry>(arr.length())
for (i in 0 until arr.length()) {
val o = arr.getJSONObject(i)
out.add(
HitEntry(
text = o.optString("text"),
lat = o.optDouble("lat"),
lon = o.optDouble("lon"),
timeMillis = o.optLong("timeMillis")
)
)
}
return out
}
fun clear(ctx: Context) {
prefs(ctx).edit().remove(KEY).apply()
}
}

View File

@@ -1,33 +0,0 @@
package app.klottr.util
import android.content.Context
/**
* Lagrar vilka Note-ID:n som redan har visats som träff.
* Implementerat med SharedPreferences StringSet för enkelhet.
*/
object SeenHits {
private const val PREFS_NAME = "klottr_seen_hits"
private const val KEY_SET = "seen_ids"
private fun prefs(ctx: Context) =
ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
fun isSeen(ctx: Context, id: Long): Boolean {
val set = prefs(ctx).getStringSet(KEY_SET, emptySet()) ?: emptySet()
return set.contains(id.toString())
}
fun markSeen(ctx: Context, id: Long) {
val p = prefs(ctx)
val set = (p.getStringSet(KEY_SET, emptySet()) ?: emptySet()).toMutableSet()
if (set.add(id.toString())) {
p.edit().putStringSet(KEY_SET, set).apply()
}
}
/** För felsökning / återställning - rensa listan */
fun clear(ctx: Context) {
prefs(ctx).edit().remove(KEY_SET).apply()
}
}

View File

@@ -1,19 +0,0 @@
package app.klottr.util
import android.content.Context
import java.util.UUID
object UserId {
private const val PREFS = "klottr_prefs"
private const val KEY = "user_id"
/** Ett anonymt men stabilt ID per app-installation. */
fun get(context: Context): String {
val sp = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
val existing = sp.getString(KEY, null)
if (existing != null) return existing
val fresh = UUID.randomUUID().toString()
sp.edit().putString(KEY, fresh).apply()
return fresh
}
}

View File

@@ -1,26 +0,0 @@
package app.klottr.util
import android.content.Context
object WikiHitIndex {
private const val PREFS = "klottr_wiki_hit_index"
private fun prefs(ctx: Context) = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
fun put(ctx: Context, timeMillis: Long, pageId: Long) {
prefs(ctx).edit().putLong(timeMillis.toString(), pageId).apply()
}
fun isWiki(ctx: Context, timeMillis: Long): Boolean {
return prefs(ctx).contains(timeMillis.toString())
}
fun getPageId(ctx: Context, timeMillis: Long): Long? {
val v = prefs(ctx).getLong(timeMillis.toString(), Long.MIN_VALUE)
return if (v == Long.MIN_VALUE) null else v
}
fun clear(ctx: Context) {
prefs(ctx).edit().clear().apply()
}
}