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
@Composable | |
fun Calculator(state: CalculatorViewModel) { | |
Column { | |
TextField(value = state.v1, onValueChange = { state.v1 = it }) | |
TextField(value = state.v2, onValueChange = { state.v2 = it }) | |
Text(text = state.result.collectAsState().value) | |
} | |
} |
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 CalculatorViewModel : ViewModel() { | |
var v1 by mutableStateOf("0") | |
var v2 by mutableStateOf("0") | |
val result = snapshotFlow { v1 to v2 }.mapLatest { | |
sum(it.first, it.second) | |
}.stateIn(viewModelScope, SharingStarted.Lazily, "0") | |
} |
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
@Composable | |
fun Calculator(state: CalculatorViewModel) { | |
Column { | |
TextField(value = state.v1.collectAsState().value, onValueChange = { state.v1.value = it }) | |
TextField(value = state.v2.collectAsState().value, onValueChange = { state.v2.value = it }) | |
Text(text = state.result.collectAsState().value) | |
} | |
} |
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
@Composable | |
fun CalculatorScreen() { | |
val viewModel = viewModel<CalculatorViewModel>() | |
Calculator(state = viewModel) | |
} |
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 CalculatorViewModel : ViewModel() { | |
var v1 = MutableStateFlow("0") | |
var v2 = MutableStateFlow("0") | |
val result = v1.combine(v2) { a, b -> a to b}.mapLatest { | |
sum(it.first, it.second) | |
}.stateIn(viewModelScope, SharingStarted.Lazily, "0") | |
} |
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
@Composable | |
fun Calculator(state: CalculatorState) { | |
Column { | |
TextField(value = state.v1, onValueChange = { state.v1 = it }) | |
TextField(value = state.v2, onValueChange = { state.v2 = it }) | |
Text(text = state.result.collectAsState(initial = "").value) | |
} | |
} |
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 CalculatorState { | |
var v1 by mutableStateOf("0") | |
var v2 by mutableStateOf("0") | |
val result = snapshotFlow { v1 to v2 }.mapLatest { | |
sum(it.first, it.second) //sum is a suspend method here | |
} | |
} |
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
@Composable | |
fun CalculatorScreen { | |
val state = remember { | |
CalculatorState() | |
} | |
Calculator(state = state) | |
} |
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 CalculatorState { | |
var v1 by mutableStateOf("0") | |
var v2 by mutableStateOf("0") | |
val result get() = sum(v1, v2) | |
} | |
fun sum(v1: String, v2: String) = try { | |
(v1.toInt() + v2.toInt()).toString() | |
} catch (e: NumberFormatException) { | |
"Parsing error :(" |
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
@Composable | |
fun Calculator(state: CalculatorState) { | |
Column { | |
TextField(value = state.v1, onValueChange = { state.v1 = it }) | |
TextField(value = state.v2, onValueChange = { state.v2 = it }) | |
Text(text = state.result) | |
} | |
} |