Skip to content

Instantly share code, notes, and snippets.

@adesamp
Last active March 29, 2021 04:45
Show Gist options
  • Save adesamp/743c8021da9fa95b0a5c64f356e594d1 to your computer and use it in GitHub Desktop.
Save adesamp/743c8021da9fa95b0a5c64f356e594d1 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
// set/save value
suspend fun saveName(name: String) {
dataStore.edit { it[NAME_KEY] = name }
}
// get/load value
suspend fun getName(): String {
return dataStore.getValueFlow(NAME_KEY, "").first()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment