ildar.i [Android]
05/03/2022, 7:05 AMCommonInput
in viewModel, which gets posted to StateFlow and then converted into a @Composable fun CommonInputDisplay
in our Composable screen. CommonInput
has a value
param that gets converted to MutableState
and used in TextField
as value
.
The problem is that once it has initialized, we can’t change TextField
value
by modifying state object from ViewModel anymore. How can we initiate a recomposition with a new value from viewModel by modifying CommonInput
?Filip Wiesner
05/03/2022, 7:15 AMCommonInput::value
could be a Compose State
🤷class CommonInput(
val initialValue: String? = null
...
) {
var value by mutableStateOf(initialalue)
private set
}
ildar.i [Android]
05/03/2022, 7:17 AMyschimke
05/03/2022, 7:17 AMFilip Wiesner
05/03/2022, 7:18 AMIdeally it should just post new state and Compose screen just updatesSure but in that case the
CommonInput
cannot have mutable properties and cannot mutate iteslf. ViewModel has to do that like yschmke suggests:
make it a val and pass in a new CommonInput instance
yschimke
05/03/2022, 7:19 AMildar.i [Android]
05/03/2022, 7:34 AMFilip Wiesner
05/03/2022, 7:35 AMCommonInput
class
to data class
ildar.i [Android]
05/03/2022, 7:38 AMdata class
Filip Wiesner
05/03/2022, 7:41 AMildar.i [Android]
05/03/2022, 7:54 AMLaunchedEffect(item.value) {
if (item.value.orEmpty() != inputText) inputText = item.value.orEmpty()
}
I added this after inputText
initialization and it started to work properly. Is that a correct thing to do here?Filip Wiesner
05/03/2022, 8:08 AMinpuText
should just be val inputText = item.value.orEmpty()
. No need to create internal stateildar.i [Android]
05/03/2022, 8:09 AMFilip Wiesner
05/03/2022, 8:09 AMonValueChange
should just call the VM to emit new state - single source of truth