Skip to content

Instantly share code, notes, and snippets.

@fabioCollini
Created September 19, 2021 17:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fabioCollini/7f64865cc8828fb5894d30f172a8e997 to your computer and use it in GitHub Desktop.
class CalculatorViewModel(
state: SavedStateHandle,
) : ViewModel() {
var v1 by state.mutableStateOf("0")
var v2 by state.mutableStateOf("0")
val result by state.mutableStateOf("0") { valueLoadedFromState, setter ->
snapshotFlow { v1 to v2 }
.drop(if (valueLoadedFromState != null) 1 else 0)
.mapLatest {
sum(it.first, it.second)
}
.onEach {
setter(it)
}
.launchIn(viewModelScope)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment