Skip to content

Instantly share code, notes, and snippets.

@adesamp
Last active March 29, 2021 04:44
Show Gist options
  • Save adesamp/cd4aa848ffacadda6d1ea2f0abf06760 to your computer and use it in GitHub Desktop.
Save adesamp/cd4aa848ffacadda6d1ea2f0abf06760 to your computer and use it in GitHub Desktop.
// inisialisasi
companion object {
private val NAME_KEY = stringPreferencesKey("NAME_KEY")
}
private val Context._dataStore: DataStore<Preferences> by preferencesDataStore(
name = "sampingan_data_store",
produceMigrations = ::sharedPreferencesMigration
)
private fun sharedPreferencesMigration(context: Context) =
listOf(SharedPreferencesMigration(context, "pref_name"))
private val dataStore: DataStore<Preferences> = activity._dataStore
var name: String
// get/load value
get() = runBlocking{
withContext(Dispatchers.Default){
dataStore.getValueFlow(NAME_KEY, "").first()
}
}
// set/save value
set(value) = runBlocking {
withContext(Dispatchers.Default){
dataStore.edit {
it[NAME_KEY] = value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment