Last active
September 17, 2020 15:20
-
-
Save joetimmins/b580a6359ee29881ce5acb3e8017cceb 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
fun <T : Any> MutableLiveData<T>.update(transform: (T) -> T) { | |
val current = value ?: return | |
postValue(transform(current)) | |
} | |
class TinyViewModel() { | |
private val initialState = UiState(greeting = "nah mate") | |
private val _uiState = MutableLiveData(initialState) | |
val uiState = _uiState as LiveData<String> | |
fun sayYes() = _uiState.update { | |
it.copy(greeting = "YES MATE!") | |
} | |
} | |
data class UiState( | |
val greeting: String | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment