mirror of https://git.sr.ht/~oppen/ariane
72 lines
2.3 KiB
Kotlin
72 lines
2.3 KiB
Kotlin
package oppen.tva.io.history
|
|
|
|
import android.content.Context
|
|
import java.net.URI
|
|
import kotlin.text.StringBuilder
|
|
|
|
/**
|
|
*
|
|
* This is slow, unsafe, awful, and synchronous but I don't want get bogged down implementing Room just yet
|
|
*
|
|
*/
|
|
class BasicCache(context: Context): CacheInterface {
|
|
|
|
private val DELIM = "||"
|
|
private val prefsKey = "oppen.tva.io.history.BasicCache.PREFS_KEY"
|
|
private val prefsCacheKey = "oppen.tva.io.history.BasicCache.PREFS_CACHE_KEY"
|
|
private val prefsActiveIndexKey = "oppen.tva.io.history.BasicCache.PREFS_ACTIVE_INDEX_KEY"
|
|
private val prefs = context.getSharedPreferences(prefsKey, Context.MODE_PRIVATE)
|
|
|
|
override fun update(tabs: List<Tab>, activeIndex: Int) {
|
|
val sb = StringBuilder()
|
|
tabs.forEach { tab ->
|
|
sb.append("${tab.index}$DELIM")
|
|
tab.history.forEach { uri ->
|
|
sb.append("$uri$DELIM")
|
|
}
|
|
sb.append("\n")
|
|
}
|
|
|
|
val edit = prefs.edit()
|
|
val raw = sb.toString()
|
|
|
|
println("------------------------------------")
|
|
println(raw)
|
|
println("------------------------------------")
|
|
|
|
edit.putString(prefsCacheKey, sb.toString())
|
|
edit.putInt(prefsActiveIndexKey, activeIndex)
|
|
edit.apply()
|
|
}
|
|
|
|
override fun getTabs(onTabs: (tabs: MutableList<Tab>, activeIndex: Int) -> Unit) {
|
|
val raw = prefs.getString(prefsCacheKey, null)
|
|
|
|
println("====================================")
|
|
println(raw)
|
|
println("====================================")
|
|
|
|
if (raw == null) {
|
|
onTabs(arrayListOf(), 0)
|
|
} else {
|
|
val activeIndex = prefs.getInt(prefsActiveIndexKey, 0)
|
|
val tabs = mutableListOf<Tab>()
|
|
raw.lines().forEach { line ->
|
|
if (line.trim().isNotEmpty()) {
|
|
val segs = line.split(DELIM)
|
|
val tabIndex = segs.first()
|
|
val tab = Tab(tabIndex.toInt())
|
|
for (index in 1 until segs.size) {
|
|
val address = segs[index]
|
|
if (address.isNotBlank()) {
|
|
tab.history.add(URI.create(address))
|
|
}
|
|
}
|
|
tabs.add(tab)
|
|
}
|
|
}
|
|
|
|
onTabs(tabs, activeIndex)
|
|
}
|
|
}
|
|
} |