Skip to content

KryptoStore is a thin wrapper around Jetpack DataStore Preferences that provides useful features.

License

Notifications You must be signed in to change notification settings

rumboalla/kryptostore

Repository files navigation

KryptoStore

KryptoStore is a thin wrapper around Jetpack DataStore Preferences that provides useful features.

Features

  • Small
  • Easily work with primitive preferences
  • Serialization for complex objects
  • Encryption

Basic Usage

Add the Jitpack repository

maven { url = uri("https://www.jitpack.io" ) }

Import the library

implementation("com.github.rumboalla.kryptostore:core:0.1.3")

Use preferences

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.preference.booleanPref
import com.github.rumboalla.kryptostore.preference.doublePref
import com.github.rumboalla.kryptostore.preference.floatPref
import com.github.rumboalla.kryptostore.preference.intPref
import com.github.rumboalla.kryptostore.preference.stringPref
import com.github.rumboalla.kryptostore.preference.stringSetPref

private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")

class Prefs(context: Context) {
    val boolean = booleanPref(context.store, "boolean", true)
    val int = intPref(context.store, "int", 42)
    val float = floatPref(context.store, "float", 42f)
    val double = doublePref(context.store, "double", 42.0)
    val string = stringPref(context.store, "string", "Don't Panic")
    val stringSet = stringSetPref(context.store, "stringSet", setOf("Mostly Harmless", "Don't Panic"))
}

suspend fun doSomething(context: Context) {
    val prefs = Prefs(context)
    val boolean = prefs.boolean.get()
    prefs.boolean.set(!boolean)
    val int = prefs.int.get()
    prefs.int.set(int + 1)
    val float = prefs.float.get()
    prefs.float.set(float + 1f)
    val double = prefs.double.get()
    prefs.double.set(double + 1.0)
    val string = prefs.string.get()
    prefs.string.set("$string!")
    val stringSet = prefs.stringSet.get()
    prefs.stringSet.set(emptySet())
}

Serialization (Gson)

Import the gson library for serialization

implementation("com.github.rumboalla.kryptostore:gson:0.1.3")

Use serialized preferences

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.gsonPref
import com.google.gson.Gson

private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")
private val gson = Gson()

data class Data(val key: String = "", val value: Double = 0.0)

class Prefs(context: Context) {
    val data = gsonPref(context.store, "data", Data(), gson)
}

suspend fun doSomething(context: Context) {
    val prefs = Prefs(context)
    val data = prefs.data.get()
    prefs.data.set(data.copy(key = "key", value = 42.0))
}

Serialization (kotlinx.serialization)

Import the kotlinx.serialization library for serialization

implementation("com.github.rumboalla.kryptostore:kxs:0.1.3")

Use serialized preferences

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.kxsPref
import kotlinx.serialization.Serializable

private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")

@Serializable
data class Data(val key: String = "", val value: Double = 0.0)

class Prefs(context: Context) {
    val data = kxsPref(context.store, "data", Data(), gson)
}

suspend fun doSomething(context: Context) {
    val prefs = Prefs(context)
    val data = prefs.data.get()
    prefs.data.set(data.copy(key = "key", value = 42.0))
}

Encryption

Import the library for encryption

implementation("com.github.rumboalla.kryptostore:keystore:0.1.3")

Use encrypted preferences

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.createGsonTransform
import com.google.gson.Gson

private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")
private val gson = Gson()

data class Data(val key: String = "", val value: Double = 0.0)

class Prefs(context: Context) {
    val data = encryptedKeystorePref(context.store, "data", Data(), createGsonTransform(gson))
}

suspend fun doSomething(context: Context) {
    val prefs = Prefs(context)
    val data = prefs.data.get()
    prefs.data.set(data.copy(key = "key", value = 42.0))
}

Compose

Extensions for compose. Import the library

implementation("com.github.rumboalla.kryptostore:compose:0.1.3")

Use it

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.github.rumboalla.kryptostore.preference.booleanPref

private val Context.store: DataStore<Preferences> by preferencesDataStore(name = "prefs")

class Prefs(context: Context) {
    val boolean = booleanPref(context.store, "boolean", true)
}

@Composable
fun Component(prefs: Prefs) {
    val state = prefs.boolean.collectAsStateWithLifecycle()
    if (state.value) {
        Text("Pref is true.")
    } else {
        Text("Pref is false.")
    }
}

Roadmap

  • More serialization options: Moshi.
  • More encryption options.

License

Copyright © 2024 rumboalla.
Licensed under the MIT license.