Skip to content

Instantly share code, notes, and snippets.

@adesamp
Created June 4, 2021 10:12
Show Gist options
  • Save adesamp/2fd0038799d236e1f01e716f9847f7bc to your computer and use it in GitHub Desktop.
Save adesamp/2fd0038799d236e1f01e716f9847f7bc to your computer and use it in GitHub Desktop.
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) }
.map {
_name.postValue(it[NAME_KEY])
...
}
.first()
}
fun saveName(name: String) {
viewModelScope.launch {
dataStore.edit { it[NAME_KEY] = name }
_name.postValue(name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment