Skip to content

Instantly share code, notes, and snippets.

@agustarc
Created December 27, 2021 10:22
Show Gist options
  • Save agustarc/d1dee712aa3d2b7f4405897598c476d6 to your computer and use it in GitHub Desktop.
Save agustarc/d1dee712aa3d2b7f4405897598c476d6 to your computer and use it in GitHub Desktop.
class ExampleDataStore(context: Context) {
private val dataStore: DataStore<Preferences> = context.dataStore
suspend fun setExampleData(exampleData: String?) {
dataStore.storeValue(EXAMPLE_DATA, exampleData)
}
suspend fun getExampleData(): String? {
return dataStore.readValue(EXAMPLE_DATA)
}
companion object {
private const val PREF_NAME = "example_pref"
private val EXAMPLE_DATA = stringPreferencesKey("exampleData")
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
name = PREF_NAME,
produceMigrations = { context -> listOf(SharedPreferencesMigration(context, PREF_NAME)) }
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment