-
Notifications
You must be signed in to change notification settings - Fork 248
Add Swipe to reveal examples #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
95e6d36
Add Swipe to reveal examples
jakeroseman c85963d
Apply Spotless
jakeroseman fe4f3f2
Adding swipe to dismiss examples to the top compose examples.
jakeroseman 07ce7fa
merging changes from main
jakeroseman c28b438
Merge branch 'main' into swipe-reveal
jakeroseman 73361f7
Merge branch 'main' into swipe-reveal
jakeroseman e0a8942
Tidy up merge conflict errors
jakeroseman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
339 changes: 339 additions & 0 deletions
339
compose/snippets/src/main/java/com/example/compose/snippets/components/SwipeToDismissBox.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,339 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.compose.snippets.components | ||
|
||
import androidx.compose.animation.animateColorAsState | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.CheckBox | ||
import androidx.compose.material.icons.filled.CheckBoxOutlineBlank | ||
import androidx.compose.material.icons.filled.Delete | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.ListItem | ||
import androidx.compose.material3.OutlinedCard | ||
import androidx.compose.material3.SwipeToDismissBox | ||
import androidx.compose.material3.SwipeToDismissBoxValue | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.rememberSwipeToDismissBoxState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateListOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.RectangleShape | ||
import androidx.compose.ui.graphics.lerp | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Preview | ||
@Composable | ||
fun SwipeToDismissBoxExamples() { | ||
Column( | ||
modifier = Modifier | ||
.padding(16.dp) | ||
.fillMaxSize(), | ||
verticalArrangement = Arrangement.spacedBy(24.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
Text("Swipe to dismiss with change of background", fontWeight = FontWeight.Bold) | ||
SwipeItemExample() | ||
Text("Swipe to dismiss with a cross-fade animation", fontWeight = FontWeight.Bold) | ||
SwipeCardItemExample() | ||
} | ||
} | ||
|
||
// [START android_compose_components_todoitem] | ||
data class TodoItem( | ||
var isItemDone: Boolean, | ||
var itemDescription: String | ||
) | ||
// [END android_compose_components_todoitem] | ||
|
||
// [START android_compose_components_swipeitem] | ||
@Composable | ||
fun SwipeItem( | ||
value: TodoItem, | ||
startToEndAction: (TodoItem) -> Unit, | ||
endToStartAction: (TodoItem) -> Unit, | ||
modifier: Modifier = Modifier, | ||
content: @Composable (TodoItem) -> Unit | ||
) { | ||
val swipeToDismissBoxState = rememberSwipeToDismissBoxState( | ||
confirmValueChange = { | ||
when (it) { | ||
SwipeToDismissBoxValue.StartToEnd -> { | ||
startToEndAction(value) | ||
// Do not dismiss this item. | ||
false | ||
} | ||
SwipeToDismissBoxValue.EndToStart -> { | ||
endToStartAction(value) | ||
true | ||
} | ||
SwipeToDismissBoxValue.Settled -> { | ||
false | ||
} | ||
} | ||
} | ||
) | ||
|
||
SwipeToDismissBox( | ||
state = swipeToDismissBoxState, | ||
modifier = modifier | ||
.fillMaxSize(), | ||
backgroundContent = { | ||
Row( | ||
modifier = Modifier | ||
.background( | ||
when (swipeToDismissBoxState.dismissDirection) { | ||
SwipeToDismissBoxValue.StartToEnd -> { | ||
Color.Blue | ||
} | ||
SwipeToDismissBoxValue.EndToStart -> { | ||
Color.Red | ||
} | ||
SwipeToDismissBoxValue.Settled -> { | ||
Color.LightGray | ||
} | ||
} | ||
) | ||
.fillMaxSize(), | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.SpaceBetween | ||
) { | ||
when (swipeToDismissBoxState.dismissDirection) { | ||
SwipeToDismissBoxValue.StartToEnd -> { | ||
if (value.isItemDone) { | ||
Icon( | ||
imageVector = Icons.Default.CheckBox, | ||
contentDescription = "Item done", | ||
tint = Color.White, | ||
modifier = Modifier | ||
.padding(12.dp) | ||
) | ||
} else { | ||
Icon( | ||
imageVector = Icons.Default.CheckBoxOutlineBlank, | ||
contentDescription = "Item not done", | ||
tint = Color.White, | ||
modifier = Modifier | ||
.padding(12.dp) | ||
) | ||
} | ||
} | ||
|
||
SwipeToDismissBoxValue.EndToStart -> { | ||
Spacer(modifier = Modifier) | ||
Icon( | ||
imageVector = Icons.Default.Delete, | ||
contentDescription = "Remove item", | ||
tint = Color.White, | ||
modifier = Modifier | ||
.padding(12.dp) | ||
) | ||
} | ||
|
||
SwipeToDismissBoxValue.Settled -> {} | ||
} | ||
} | ||
} | ||
) { | ||
content(value) | ||
} | ||
} | ||
// [END android_compose_components_swipeitem] | ||
|
||
@Preview(showBackground = true) | ||
// [START android_compose_components_swipeitemexample] | ||
@Composable | ||
private fun SwipeItemExample() { | ||
val todoItems = remember { | ||
mutableStateListOf( | ||
TodoItem(isItemDone = false, itemDescription = "Pay bills"), | ||
TodoItem(isItemDone = false, itemDescription = "Buy groceries"), | ||
TodoItem(isItemDone = false, itemDescription = "Go to gym"), | ||
TodoItem(isItemDone = false, itemDescription = "Get dinner") | ||
) | ||
} | ||
|
||
LazyColumn { | ||
items( | ||
items = todoItems, | ||
key = { it.itemDescription } | ||
) { todoItem -> | ||
SwipeItem( | ||
value = todoItem, | ||
startToEndAction = { | ||
todoItem.isItemDone = !todoItem.isItemDone | ||
}, | ||
endToStartAction = { | ||
todoItems -= todoItem | ||
} | ||
) { | ||
ListItem( | ||
headlineContent = { Text(text = todoItem.itemDescription) }, | ||
supportingContent = { Text(text = "swipe me to update or remove.") } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
// [END android_compose_components_swipeitemexample] | ||
|
||
// [START android_compose_components_swipecarditem] | ||
@Composable | ||
fun SwipeCardItem( | ||
value: TodoItem, | ||
startToEndAction: (TodoItem) -> Unit, | ||
endToStartAction: (TodoItem) -> Unit, | ||
modifier: Modifier = Modifier, | ||
content: @Composable (TodoItem) -> Unit | ||
) { | ||
val swipeToDismissState = rememberSwipeToDismissBoxState( | ||
positionalThreshold = { totalDistance -> totalDistance * 0.25f }, | ||
confirmValueChange = { | ||
when (it) { | ||
SwipeToDismissBoxValue.StartToEnd -> { | ||
startToEndAction(value) | ||
// Do not dismiss this item. | ||
false | ||
} | ||
SwipeToDismissBoxValue.EndToStart -> { | ||
endToStartAction(value) | ||
true | ||
} | ||
SwipeToDismissBoxValue.Settled -> { | ||
false | ||
} | ||
} | ||
} | ||
) | ||
|
||
SwipeToDismissBox( | ||
modifier = Modifier, | ||
state = swipeToDismissState, | ||
backgroundContent = { | ||
// Cross-fade the background color as the drag gesture progresses. | ||
val color by animateColorAsState( | ||
when (swipeToDismissState.targetValue) { | ||
SwipeToDismissBoxValue.Settled -> Color.LightGray | ||
SwipeToDismissBoxValue.StartToEnd -> | ||
lerp(Color.LightGray, Color.Blue, swipeToDismissState.progress) | ||
|
||
SwipeToDismissBoxValue.EndToStart -> | ||
lerp(Color.LightGray, Color.Red, swipeToDismissState.progress) | ||
}, | ||
label = "swipeable card item background color" | ||
) | ||
Row( | ||
modifier = Modifier | ||
.background(color) | ||
.fillMaxSize(), | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.SpaceBetween | ||
) { | ||
when (swipeToDismissState.dismissDirection) { | ||
SwipeToDismissBoxValue.StartToEnd -> { | ||
if (value.isItemDone) { | ||
Icon( | ||
imageVector = Icons.Default.CheckBox, | ||
contentDescription = "Item done", | ||
tint = Color.White, | ||
modifier = Modifier | ||
.padding(12.dp) | ||
) | ||
} else { | ||
Icon( | ||
imageVector = Icons.Default.CheckBoxOutlineBlank, | ||
contentDescription = "Item not done", | ||
tint = Color.White, | ||
modifier = Modifier | ||
.padding(12.dp) | ||
) | ||
} | ||
} | ||
|
||
SwipeToDismissBoxValue.EndToStart -> { | ||
Spacer(modifier = Modifier) | ||
Icon( | ||
imageVector = Icons.Default.Delete, | ||
contentDescription = "Remove item", | ||
tint = Color.White, | ||
modifier = Modifier | ||
.padding(12.dp) | ||
) | ||
} | ||
|
||
SwipeToDismissBoxValue.Settled -> {} | ||
} | ||
} | ||
} | ||
) { | ||
content(value) | ||
} | ||
} | ||
// [END android_compose_components_swipecarditem] | ||
|
||
// [START android_compose_components_swipecarditemexample] | ||
@Preview | ||
@Composable | ||
private fun SwipeCardItemExample() { | ||
val todoItems = remember { | ||
mutableStateListOf( | ||
TodoItem(isItemDone = false, itemDescription = "Pay bills"), | ||
TodoItem(isItemDone = false, itemDescription = "Buy groceries"), | ||
TodoItem(isItemDone = false, itemDescription = "Go to gym"), | ||
TodoItem(isItemDone = false, itemDescription = "Get dinner") | ||
) | ||
} | ||
|
||
LazyColumn { | ||
items( | ||
items = todoItems, | ||
key = { it.itemDescription } | ||
) { todoItem -> | ||
SwipeCardItem( | ||
value = todoItem, | ||
startToEndAction = { | ||
todoItem.isItemDone = !todoItem.isItemDone | ||
}, | ||
endToStartAction = { | ||
todoItems -= todoItem | ||
} | ||
) { | ||
OutlinedCard(shape = RectangleShape) { | ||
ListItem( | ||
headlineContent = { Text(todoItem.itemDescription) }, | ||
supportingContent = { Text("swipe me to update or remove.") } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// [END android_compose_components_swipecarditemexample] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.