Skip to content

Instantly share code, notes, and snippets.

@delacrixmorgan
Created August 6, 2024 19:14
Show Gist options
  • Save delacrixmorgan/f6c7c4e86ae4e2dbdfd590294d0fea13 to your computer and use it in GitHub Desktop.
Save delacrixmorgan/f6c7c4e86ae4e2dbdfd590294d0fea13 to your computer and use it in GitHub Desktop.
KMP - SwipeToDismissBox
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun EditableNameTimeView(
    viewModel: TodayViewModel,
    location: Location,
    onEdit: ((Location) -> Unit),
    onDelete: ((Location) -> Unit)
) {
    val dismissState = rememberSwipeToDismissBoxState(
        confirmValueChange = { state ->
            when (state) {
                SwipeToDismissBoxValue.StartToEnd -> onEdit(location)
                SwipeToDismissBoxValue.EndToStart -> onDelete(location)
                SwipeToDismissBoxValue.Settled -> Unit
            }
            false
        }
    )
    val color = when (dismissState.dismissDirection) {
        SwipeToDismissBoxValue.StartToEnd -> MaterialTheme.colorScheme.tertiary
        SwipeToDismissBoxValue.EndToStart -> MaterialTheme.colorScheme.error
        SwipeToDismissBoxValue.Settled -> Color.Transparent
    }
    SwipeToDismissBox(
        state = dismissState,
        backgroundContent = {
            Row(
                modifier = Modifier
                    .fillMaxSize()
                    .background(color)
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Icon(
                    Icons.Rounded.Edit,
                    contentDescription = "Edit"
                )
                Spacer(modifier = Modifier)
                Icon(
                    Icons.Rounded.Delete,
                    contentDescription = "Delete"
                )
            }
        },
    ) {
        NameTimeView(
            locationTypePreference = viewModel.locationTypePreference.value,
            dateFormatPreference = viewModel.dateFormatPreference.value,
            offsetInMinutes = viewModel.offsetInMinutes.value,
            location = location
        )
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment