feat: add settings screen #47
22
shared/src/commonMain/kotlin/su/reya/coop/Settings.kt
Normal file
22
shared/src/commonMain/kotlin/su/reya/coop/Settings.kt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package su.reya.coop
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Settings(
|
||||||
|
val theme: Theme = Theme.System,
|
||||||
|
val dynamicColor: Boolean = true,
|
||||||
|
val media: Media = Media.AlwaysEnabled,
|
||||||
|
val screening: Boolean = true,
|
||||||
|
val blossomServer: String? = "https://blossom.band",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
enum class Theme {
|
||||||
|
Light, Dark, System
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
enum class Media {
|
||||||
|
Disabled, DisabledForMobileData, AlwaysEnabled
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package su.reya.coop.repository
|
||||||
|
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import su.reya.coop.AppStorage
|
||||||
|
import su.reya.coop.Settings
|
||||||
|
|
||||||
|
class SettingsRepository(
|
||||||
|
private val storage: AppStorage
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
private const val KEY_SETTINGS = "app_settings"
|
||||||
|
}
|
||||||
|
|
||||||
|
private val json = Json {
|
||||||
|
ignoreUnknownKeys = true
|
||||||
|
encodeDefaults = true
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun save(settings: Settings) {
|
||||||
|
val jsonString = json.encodeToString(settings)
|
||||||
|
storage.set(KEY_SETTINGS, jsonString)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun load(): Settings {
|
||||||
|
val jsonString = storage.get(KEY_SETTINGS)
|
||||||
|
return if (jsonString != null) {
|
||||||
|
try {
|
||||||
|
json.decodeFromString<Settings>(jsonString)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
Settings()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Settings()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user