feat: add ScheduleCache with multiplatform-settings for offline caching
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package ch.parano.myice.cache
|
||||
|
||||
import ch.parano.myice.models.Event
|
||||
import com.russhwolf.settings.Settings
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.ExperimentalTime
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
class ScheduleCache(private val settings: Settings) {
|
||||
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
private val eventListSerializer = ListSerializer(Event.serializer())
|
||||
|
||||
private fun eventsKey(account: String) = "cached_events_$account"
|
||||
private fun agegroupKey(account: String) = "filters_agegroup_$account"
|
||||
private fun subgroupKey(account: String) = "filters_subgroup_$account"
|
||||
private fun typeFilterKey(account: String) = "filters_typefilter_$account"
|
||||
private fun timestampKey(account: String) = "cached_events_timestamp_$account"
|
||||
|
||||
fun saveEvents(account: String, events: List<Event>) {
|
||||
settings.putString(eventsKey(account), json.encodeToString(eventListSerializer, events))
|
||||
settings.putLong(timestampKey(account), Clock.System.now().toEpochMilliseconds())
|
||||
}
|
||||
|
||||
fun loadEvents(account: String): List<Event>? {
|
||||
val jsonString = settings.getStringOrNull(eventsKey(account)) ?: return null
|
||||
return try {
|
||||
json.decodeFromString(eventListSerializer, jsonString)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun getLastUpdated(account: String): Long? {
|
||||
return settings.getLongOrNull(timestampKey(account))
|
||||
}
|
||||
|
||||
fun saveFilters(account: String, agegroup: String?, subgroup: String?) {
|
||||
if (agegroup != null) settings.putString(agegroupKey(account), agegroup)
|
||||
else settings.remove(agegroupKey(account))
|
||||
if (subgroup != null) settings.putString(subgroupKey(account), subgroup)
|
||||
else settings.remove(subgroupKey(account))
|
||||
}
|
||||
|
||||
fun loadFilters(account: String): Pair<String?, String?> {
|
||||
return settings.getStringOrNull(agegroupKey(account)) to
|
||||
settings.getStringOrNull(subgroupKey(account))
|
||||
}
|
||||
|
||||
fun saveTypeFilter(account: String, typeFilter: String) {
|
||||
settings.putString(typeFilterKey(account), typeFilter)
|
||||
}
|
||||
|
||||
fun loadTypeFilter(account: String): String? {
|
||||
return settings.getStringOrNull(typeFilterKey(account))
|
||||
}
|
||||
|
||||
fun clearCache(account: String) {
|
||||
settings.remove(eventsKey(account))
|
||||
settings.remove(timestampKey(account))
|
||||
settings.remove(agegroupKey(account))
|
||||
settings.remove(subgroupKey(account))
|
||||
settings.remove(typeFilterKey(account))
|
||||
}
|
||||
|
||||
fun clearAllCache() {
|
||||
val keys = settings.keys.filter { key ->
|
||||
key.startsWith("cached_events_") ||
|
||||
key.startsWith("filters_") ||
|
||||
key.startsWith("cached_events_timestamp_")
|
||||
}
|
||||
keys.forEach { settings.remove(it) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user