Created
June 4, 2021 10:12
-
-
Save adesamp/2fd0038799d236e1f01e716f9847f7bc 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
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