Hi hivemind,
I’m trying to use a slider as input for a simplified function using a Viewmodel (to get the hang of proper MVVM architecture) but I’m getting ‘type mismatch’ and ‘no value passed’ errors - any ideas?
The function takes the float output of the slider (sliderP1) inverts it, and displays the output on the screen (cog).
MainScreen(
sliderP1 = viewModel.sliderP1,
onValueChange = {viewModel.sliderP1 = it},
onValueChangeFinished = {},
cog = viewModel.cog,
calcs = {viewModel.calcs()}
)
}
@Composable
// builds UI
fun MainScreen(
modifier: Modifier = Modifier,
sliderP1: Float,
onValueChange: (Float) -> Unit,
onValueChangeFinished: () -> Unit,
calcs:(Float) -> Unit,
cog: Float
)
{
Column(modifier = Modifier
.fillMaxSize()
.padding(20.dp),
horizontalAlignment = Alignment.Start
) {
Slider(
value = sliderP1,
onValueChange = onValueChange,
onValueChangeFinished = calcs(),
valueRange = 0f..10f
)
Text(text = "Raw slider value: $sliderP1")
Spacer(modifier = Modifier.height(20.dp))
Text(text = "Function output: $cog", modifier = Modifier.background(Color.Yellow))
}
}
and my ViewModel:
class COGViewModel: ViewModel() {
var sliderP1 by mutableStateOf(0f)
var cog by mutableStateOf(0f) // result
fun calcs(): Float {
cog = sliderP1*-1
return cog
}
}