Skip to content

Instantly share code, notes, and snippets.

@onivas
Last active March 10, 2023 11:38
Show Gist options
  • Save onivas/254332fcc6ef8a45444a9eaf39383191 to your computer and use it in GitHub Desktop.
Save onivas/254332fcc6ef8a45444a9eaf39383191 to your computer and use it in GitHub Desktop.
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.input.pointer.pointerInput
fun Modifier.interceptSwipeDown(onSwipeDown: () -> Unit): Modifier = composed {
var startYValue by remember { mutableStateOf(0F) }
var lastYValue by remember { mutableStateOf(0F) }
pointerInput(Unit) {
detectDragGestures(
onDragStart = { startYValue = it.y },
onDragEnd = { if (startYValue < lastYValue) onSwipeDown() }
) { change, _ -> lastYValue = change.position.y }
}
}
fun Modifier.interceptSwipeUp(onSwipeUp: () -> Unit): Modifier = composed {
var startYValue by remember { mutableStateOf(0F) }
var lastYValue by remember { mutableStateOf(0F) }
pointerInput(Unit) {
detectDragGestures(
onDragStart = { startYValue = it.y },
onDragEnd = { if (startYValue > lastYValue) onSwipeUp() }
) { change, _ -> lastYValue = change.position.y }
}
}
fun Modifier.interceptSwipe(onSwipeUp: () -> Unit, onSwipeDown: () -> Unit): Modifier = composed {
var startYValue by remember { mutableStateOf(0F) }
var lastYValue by remember { mutableStateOf(0F) }
pointerInput(Unit) {
detectDragGestures(
onDragStart = { startYValue = it.y },
onDragEnd = {
if (startYValue > lastYValue) onSwipeUp()
else onSwipeDown()
}
) { change, _ -> lastYValue = change.position.y }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment