easy-datastore
1.0.0-alpha1indexedSimplifies storing and retrieving key-value pairs with a user-friendly API, supporting asynchronous operations and customizable data store file locations.
3
Stars
—
Used by
dependents
—
Health
/ 100
Simplifies storing and retrieving key-value pairs with a user-friendly API, supporting asynchronous operations and customizable data store file locations.
A wrapper around Multiplatform Datastore to expose easy to use API to store and retrieve key value pairs.
Usage
class TestDataStore(
context: Any? = null,
path: String? = null
): AbstractPreferenceDataStore(PREFERENCE_NAME, context = context, path = path) {
fun getTestString(): Flow<String?> {
return getAsync(TEST_STRING_KEY)
}
suspend fun setTestString(value: String) {
set(TEST_STRING_KEY, value)
}
companion object {
private const val PREFERENCE_NAME = "testPreference"
private val TEST_STRING_KEY = stringPreferencesKey("TEST_STRING")
}
}
Example usage of TestDataStore in a composable
@Composable
fun Screen(testDataStore: TestDataStore) {
preferenceValue remember { mutableStateOf<String?>() }
coroutineScope = remember {
CoroutineScope(Dispatchers.IO)
}
LaunchedEffect() {
testDataStore.getTestString().collect {
preferenceValue = it
}
}
Column {
text remember { mutableStateOf() }
TextField(
value = text,
onValueChange = {
text = it
}
)
Spacer(Modifier.height(dp))
Button(onClick = {
coroutineScope.launch { testDataStore.setTestString(text) }
}) {
Text()
}
Spacer(Modifier.height(dp))
Text()
}
}
You can specify the data store file location using, path variable
val testDataStore = remember { TestDataStore(path = "${System.getProperty("user.home")}/Library/Application Support/sdsadsad") }
Note: Either context or path in AbstractPreferenceDataStore is mandatory in Android. An exception is thrown is both are not set.
Surfaced from shared tags and platforms — no rankings paid for.