fun ListSessionsScreen()

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


fun ListSessionsScreen(
    healthPlatformManager: HealthPlatformManager,
    onDetailsClick: (String) -> Unit,
    onError: (Context, Throwable?) -> Unit,
    viewModel: ListSessionsViewModel = viewModel(
        factory = ListSessionsViewModelFactory(
            healthPlatformManager = healthPlatformManager
        )
    )
) {
    val sessionsList by viewModel.sessionsList
    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 ListSessionsViewModel.UiState.Error && errorId.value != state.uuid) {
            onError(context, state.exception)
            errorId.value = state.uuid
        }
    }

    val modifier = Modifier.padding(4.dp)
    LazyColumn(
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        item {
            Button(
                modifier = modifier
                    .fillMaxWidth()
                    .height(48.dp),
                onClick = {
                    viewModel.insertSessionData()
                }
            ) {
                Text(stringResource(id = R.string.add_session))
            }
        }
        items(sessionsList) { session ->
            SessionRow(
                session.start,
                session.end,
                session.uid,
                session.name,
                modifier = modifier,
                onDeleteClick = { uid ->
                    viewModel.deleteSession(uid)
                },
                onDetailsClick = onDetailsClick
            )
        }
    }
}