Skip to content

Instantly share code, notes, and snippets.

@widiarifki
Last active August 31, 2021 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save widiarifki/c0ad3a42c8d903ce14557ceeac0f3ac1 to your computer and use it in GitHub Desktop.
Save widiarifki/c0ad3a42c8d903ce14557ceeac0f3ac1 to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ShoppingItemCard(
shoppingItem: ShoppingItem,
onToggleTickItem: (ShoppingItem) -> Unit,
onDeleteItem: (ShoppingItem) -> Unit
) {
val swiperState = rememberDismissState(
confirmStateChange = { dismissValue ->
val isItemDismissed = dismissValue == DismissValue.DismissedToEnd
// event delete hanya ketika item digeser sampai ujung kanan (DismissedToEnd)
if (isItemDismissed) onDeleteItem(shoppingItem)
// definisikan Boolean value, dimana kita menganggap swipe akan merubah state
isItemDismissed
}
)
var textStyle = MaterialTheme.typography.body1
if (shoppingItem.isTicked) {
textStyle = textStyle.copy(textDecoration = TextDecoration.LineThrough)
}
SwipeToDismiss(
state = swiperState,
background = {
// UI yg tampil saat item digeser
Box(
modifier = Modifier.fillMaxHeight(),
contentAlignment = Alignment.CenterStart
) {
Text(
text = "Swipe left to cancel delete",
style = MaterialTheme.typography.body2
)
}
}
) {
Card {
Row(
modifier = Modifier
.fillMaxWidth(1f)
.padding(10.dp)
) {
// Teks item belanja
Text(
text = shoppingItem.name,
style = textStyle,
modifier = Modifier.weight(1f)
)
// Done checkbox
Checkbox(
checked = shoppingItem.isTicked,
onCheckedChange = {
onToggleTickItem(shoppingItem)
}
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment