Skip to content

Instantly share code, notes, and snippets.

@joetimmins
Last active September 17, 2020 15:20
Show Gist options
  • Save joetimmins/b580a6359ee29881ce5acb3e8017cceb to your computer and use it in GitHub Desktop.
Save joetimmins/b580a6359ee29881ce5acb3e8017cceb to your computer and use it in GitHub Desktop.
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