Move stores using SharedPrefs to :shared and use KvPrefs instead
This commit is contained in:
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user