brandonmcansh
07/31/2020, 12:18 PMmatvei
07/31/2020, 12:28 PMdraggable
in general is highly appreciated :)brandonmcansh
07/31/2020, 12:29 PMbrandonmcansh
07/31/2020, 12:29 PMbrandonmcansh
07/31/2020, 12:30 PMbrandonmcansh
07/31/2020, 12:31 PMHalil Ozercan
07/31/2020, 12:33 PMbrandonmcansh
07/31/2020, 12:33 PMTimo Drick
08/01/2020, 12:55 PM@Composable
fun Modifier.swipeToRemove(
orientation: Orientation = Orientation.Horizontal,
onRemoved: () -> Unit,
enabled: Boolean = true
) = composed {
val positionOffset = animatedFloat(0f)
val animatedCollapseSize = animatedValue(initVal = 0, converter = IntToVectorConverter)
var removed by state { false }
var swipeSizeF by state { 0f }
var collapseSize by state { 0 }
Modifier.draggable(
startDragImmediately = positionOffset.isRunning,
orientation = orientation,
onDragStopped = { velocity ->
val config = FlingConfig(anchors = listOf(-swipeSizeF, 0f, swipeSizeF))
positionOffset.fling(velocity, config) { _, endValue, _ ->
//log("Fling end $endValue")
if (endValue != 0f) {
animatedCollapseSize.snapTo(collapseSize)
animatedCollapseSize.animateTo(0, onEnd = { _, _ ->
//log("Collapsed")
removed = true
onRemoved()
}, anim = tween(500))
}
}
},
onDrag = { delta ->
positionOffset.snapTo(positionOffset.value + delta)
},
enabled = enabled
) + object : LayoutModifier {
override fun MeasureScope.measure(
measurable: Measurable, constraints: Constraints,
layoutDirection: LayoutDirection
): MeasureScope.MeasureResult {
val childPlaceable = measurable.measure(constraints)
val swipeSize = when (orientation) {
Orientation.Horizontal -> {
collapseSize = childPlaceable.height
constraints.maxWidth
}
Orientation.Vertical -> {
collapseSize = childPlaceable.width
constraints.maxHeight
}
}
swipeSizeF = swipeSize.toFloat()
positionOffset.setBounds(-swipeSizeF, swipeSizeF)
val lwidth = when (orientation) {
Orientation.Horizontal -> childPlaceable.width
Orientation.Vertical -> if (animatedCollapseSize.isRunning || removed) animatedCollapseSize.value else childPlaceable.width
}
val lheight = when (orientation) {
Orientation.Vertical -> childPlaceable.height
Orientation.Horizontal -> if (animatedCollapseSize.isRunning || removed) animatedCollapseSize.value else childPlaceable.height
}
return layout(lwidth, lheight) {
if (orientation == Orientation.Vertical)
childPlaceable.place(0, positionOffset.value.roundToInt())
else
childPlaceable.place(positionOffset.value.roundToInt(), 0)
}
}
}
}
Timo Drick
08/01/2020, 12:57 PMbrandonmcansh
08/01/2020, 1:01 PMTimo Drick
08/01/2020, 1:04 PMbrandonmcansh
08/01/2020, 2:39 PMbrandonmcansh
08/02/2020, 5:59 PMbrandonmcansh
08/02/2020, 6:00 PMcalintat
08/04/2020, 10:18 AMModifier.swipeable
for handling swiping between a set of anchors with built-in fling support. This is the original CL: https://android-review.googlesource.com/c/platform/frameworks/support/+/1362598, but it’s still a work in progress and more stuff will be added soon like resistance. Also, I have added a Material component SwipeToDismiss (https://android-review.googlesource.com/c/platform/frameworks/support/+/1352507). These will both be available in the next release of Compose, so if you’re interested in giving them a go, I would love to hear your feedback. I had a look at your swipeToDelete/swipeToRemove modifiers, and I think SwipeToDismiss should cover your use cases, but of course let me know if it’s missing anything.Timo Drick
08/04/2020, 10:55 AMTimo Drick
08/04/2020, 10:59 AMcalintat
08/04/2020, 11:16 AMDismissState
, and listen to changes to its value using onCommit(dismissState.value)
calintat
08/04/2020, 11:17 AMbrandonmcansh
08/04/2020, 11:21 PM