Francis Mariano
07/25/2023, 3:45 PMFrancis Mariano
07/25/2023, 3:45 PMval state by component.states.collectAsState()
SliderProgress(
value = state.velocity.toInt(),
onFunctionClicked = component::onVelocityChanged,
)
@Composable
internal fun SliderProgress(
value: Int,
onFunctionClicked: (Int) -> Unit,
) {
var sliderPosition by remember { mutableStateOf(value.toFloat()) }
Slider(
value = sliderPosition,
valueRange = 1f..50f,
onValueChange = { sliderPosition = it },
onValueChangeFinished = { onFunctionClicked(sliderPosition.toInt()) },
)
}
When state is updated in the first time, the Slider is recomposed. When onValueChange is called the Slider is recomposed as well. But when state is updated after the slider is not recomposed anymore.Francis Mariano
07/25/2023, 3:46 PMval state by component.states.collectAsState()
var sliderValue by remember { mutableStateOf(state.velocity.toFloat()) }
SliderProgress(
value = sliderValue,
onValueChange = { value -> sliderValue = value },
onValueChangeFinished = component::onVelocityChanged,
)
@Composable
internal fun SliderProgress(
value: Float,
onValueChange: (Float) -> Unit,
onValueChangeFinished: (Int) -> Unit,
) {
Slider(
value = value,
valueRange = 1f..50f,
onValueChange = { onValueChange(it) },
onValueChangeFinished = { onValueChangeFinished(value.toInt()) },
)
}
Francis Mariano
07/25/2023, 3:55 PMStavFX
07/25/2023, 4:26 PMsliderValue
is only evaluated once because it's wrapped in remember
.
how about:
var sliderValue by remember { mutableStateOf(0) }
val state by component.states.collectAsState()
sliderValue = state.velocity.toFloat()
....
But I think the most obvious answer is don't have 2 sources of truthFrancis Mariano
07/25/2023, 5:13 PMstate.velocity.toFloat()
againStavFX
07/25/2023, 5:45 PMvar sliderValue by remember { mutableStateOf(0) }
val state by component.states.collectAsState()
LaunchedEffect(state.velocity) { sliderValue = state.velocity.toFloat() }
Also, I'll stress again - consider making state
your single source of truth.
Maybe have state.localVelocity vs state.savedVelocity and forward both onValueChange
and onValueChangeFinished
to your componentFrancis Mariano
07/25/2023, 6:40 PMStavFX
07/25/2023, 6:40 PMstate.velocity
, which is what you wantFrancis Mariano
07/25/2023, 6:43 PMFrancis Mariano
07/25/2023, 6:43 PMFrancis Mariano
07/25/2023, 6:45 PMval state by component.states.collectAsState()
SliderProgress(
value = state.velocity.toFloat(),
onValueChange = component::onVelocityChanged,
onValueChangeFinished = component::onVelocityChangeFinished,
)
@Composable
internal fun SliderProgress(
value: Float,
onValueChange: (Float) -> Unit,
onValueChangeFinished: () -> Unit,
) {
Slider(
value = value,
valueRange = 1f..50f,
onValueChange = { onValueChange(it) },
onValueChangeFinished = { onValueChangeFinished() },
)
}