fun SessionDetailScreen()

in health-platform-v1/HealthPlatformSample/app/src/main/java/com/example/healthplatformsample/presentation/ui/SessionDetailScreen/SessionDetailScreen.kt [43:112]


fun SessionDetailScreen(
    healthPlatformManager: HealthPlatformManager,
    uid: String,
    onError: (Context, Throwable?) -> Unit,
    viewModel: SessionDetailViewModel = viewModel(
        factory = SessionDetailViewModelFactory(
            healthPlatformManager = healthPlatformManager
        )
    )
) {
    val details by viewModel.sessionDetails
    val state = viewModel.uiState
    val context = LocalContext.current
    // Remember the last error ID, such that it is possible to avoid re-launching the error
    // notification for the same error when the screen is recomposed, or configuration changes etc.
    val errorId = rememberSaveable { mutableStateOf(UUID.randomUUID()) }

    // The [MainModel.UiState] provides details of whether the last action was a success or resulted
    // in an error. Where an error occurred, for example in reading and writing to Health Platform,
    // the user is notified, and where the error is one that can be recovered from, an attempt to
    // do so is made.
    LaunchedEffect(state) {
        if (state is SessionDetailViewModel.UiState.Error && errorId.value != state.uuid) {
            onError(context, state.exception)
            errorId.value = state.uuid
        }
    }

    LaunchedEffect(uid) {
        viewModel.readSessionData(uid)
    }

    val modifier = Modifier.padding(4.dp)
    details?.let {
        LazyColumn(
            modifier = modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            sessionDetailsItem(labelId = R.string.total_steps) {
                Text(it.totalSteps.total?.longValue.toString())
            }
            sessionDetailsItem(labelId = R.string.total_distance) {
                Text(it.totalDistance.total?.doubleValue.toString())
            }
            sessionDetailsItem(labelId = R.string.total_energy) {
                Text(it.totalEnergy.total?.doubleValue.toString())
            }
            sessionDetailsItem(labelId = R.string.speed_stats) {
                with(it.speedStats) {
                    SessionDetailsMinMaxAvg(
                        min?.formatValue(),
                        max?.formatValue(),
                        avg?.formatValue()
                    )
                }
            }
            sessionDetailsSamples(labelId = R.string.speed_samples, samples = it.speedSamples.data)
            sessionDetailsItem(labelId = R.string.hr_stats) {
                with(it.hrStats) {
                    SessionDetailsMinMaxAvg(
                        min?.formatValue(),
                        max?.formatValue(),
                        avg?.formatValue()
                    )
                }
            }
            sessionDetailsSeries(labelId = R.string.hr_series, series = it.hrSeries.data[0].values)
        }
    }
}