Last active
March 29, 2021 04:45
-
-
Save adesamp/743c8021da9fa95b0a5c64f356e594d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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