This file contains hidden or 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
| class MainViewModel(private val dataStore: DataStore<Preferences>) : ViewModel() { | |
| private val _name = MutableLiveData<String>() | |
| val name: LiveData<String> | |
| get() = _name | |
| fun fetch() { | |
| viewModelScope.launch { | |
| dataStore.data | |
| .catch { _name.postValue(it.message) } |
This file contains hidden or 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
| implementation "androidx.datastore:datastore-preferences:1.0.0-alpha08" |
This file contains hidden or 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
| class MainActivity : AppCompatActivity() { | |
| private lateinit var bmiBrain: BmiBrain | |
| // initialize class | |
| bmiBrain = BmiBrain(this) | |
| // load value | |
| binding.apply { | |
| lifecycleScope.launch { | |
| val name = async { bmiBrain.getName() } |
This file contains hidden or 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 | |
| ) |
This file contains hidden or 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
| fun <T> DataStore<Preferences>.getValueFlow( | |
| key: Preferences.Key<T>, | |
| defaultValue: T, | |
| ): Flow<T> { | |
| return this.data | |
| .catch { exception -> | |
| if (exception is IOException) { | |
| emit(emptyPreferences()) | |
| } else { | |
| throw exception |
This file contains hidden or 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) = |
NewerOlder