Last active
March 29, 2021 04:44
-
-
Save adesamp/cd4aa848ffacadda6d1ea2f0abf06760 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 | |
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