as stated in the title im having problems with my ui and the text it should display. im working inside a kotlin file and created this composable:
@Composable
fun Home(navController: NavHostController) {
val viewModel: HomeViewModel = viewModel()
val firmwareInfoState by viewModel.getFirmwareInfo().collectAsState()
Column {
firmwareInfoState?.let { info ->
Text(text = info)
}
}
}
and this the view model im using:
class HomeViewModel : ViewModel() {
private val _firmwareInfo = MutableStateFlow<String?>(null)
fun getFirmwareInfo(): StateFlow<String?> {
viewModelScope.launch {
val firmware = dataloggerViewModel.getFIRMWARE()
_firmwareInfo.value = firmware
}
return _firmwareInfo
}
}
i swapped from my “ConnectionScreen” to the “HomeScreen” with this Composable:
@Composable
fun NavigationButton(navController: NavHostController) {
val context = LocalContext.current
var baudRate = BaudRate()
var test = 1200000
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(65.dp))
Button(
onClick = {
dataloggerViewModel.open(getUsbSerialPort(context))
navController.navigate(Screen.HomeScreen.route)
}
) {
Text(text = "connect to available dk-logger")
}
}
}
Normally you would expect the Text(text =””) to directly display the info but for some reason it happens when im rotating the screen. What that indicates is that when the recompostion gets scheduled, the info is there but not when the ui gets initially built. Due to the fact that it is my first time using State, LiveData etc. my knowledge isnt enough to solve this problem on my own. I tried some things, as using live data for example but that wasnt really helping me.
For further understanding:
This programm should get the data from a datalogger and display them in real time. For that, my co-worker already has a programm for windows and an API of which commands should be sent to the usb-port to get the desired answers. dataloggerViewModel.open(getusbSerialPort) opens the connection to my desired usb-device and .getFIRMWARE() sends a command to the usb-device which returns the firmware as a string (i can give the the code to these parts but as of now i dont think these have anything to do with the UI)