Compose beta and velocity tracker

This commit is contained in:
Oleksandr Balan
2022-05-14 21:42:03 +02:00
parent 1de8caee0f
commit d62d35c898
3 changed files with 24 additions and 7 deletions

1
.idea/gradle.xml generated
View File

@@ -14,7 +14,6 @@
<option value="$PROJECT_DIR$/app" /> <option value="$PROJECT_DIR$/app" />
</set> </set>
</option> </option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings> </GradleProjectSettings>
</option> </option>
</component> </component>

View File

@@ -1,13 +1,16 @@
package eu.wewox.pagecurl.page package eu.wewox.pagecurl.page
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.calculateTargetValue
import androidx.compose.animation.splineBasedDecay
import androidx.compose.foundation.gestures.awaitFirstDown import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.drag import androidx.compose.foundation.gestures.drag
import androidx.compose.foundation.gestures.forEachGesture import androidx.compose.foundation.gestures.forEachGesture
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.IntSize
import eu.wewox.pagecurl.config.CurlDirection import eu.wewox.pagecurl.config.CurlDirection
import eu.wewox.pagecurl.utils.rotate import eu.wewox.pagecurl.utils.rotate
@@ -25,6 +28,7 @@ fun Modifier.curlGesture(
return@pointerInput return@pointerInput
} }
val velocityTracker = VelocityTracker()
val startRect by lazy { direction.start.multiply(size) } val startRect by lazy { direction.start.multiply(size) }
val endRect by lazy { direction.end.multiply(size) } val endRect by lazy { direction.end.multiply(size) }
forEachGesture { forEachGesture {
@@ -41,12 +45,26 @@ fun Modifier.curlGesture(
var dragCurrent = dragStart var dragCurrent = dragStart
drag(down.id) { change -> drag(down.id) { change ->
dragCurrent = change.position dragCurrent = change.position
change.consumeAllChanges() velocityTracker.addPosition(System.currentTimeMillis(), dragCurrent)
change.consume()
val vector = (dragStart - dragCurrent).rotate(PI.toFloat() / 2) val vector = (dragStart - dragCurrent).rotate(PI.toFloat() / 2)
onCurl(dragCurrent - vector, dragCurrent + vector) onCurl(dragCurrent - vector, dragCurrent + vector)
} }
if (endRect.contains(dragCurrent)) { val velocity = velocityTracker.calculateVelocity()
val decay = splineBasedDecay<Offset>(this)
val target = decay.calculateTargetValue(
Offset.VectorConverter,
dragCurrent,
Offset(velocity.x, velocity.y)
).let {
Offset(
it.x.coerceIn(0f, size.width.toFloat() - 1),
it.y.coerceIn(0f, size.height.toFloat() - 1)
)
}
if (endRect.contains(target)) {
onEnd() onEnd()
} else { } else {
onCancel() onCancel()

View File

@@ -1,12 +1,12 @@
buildscript { buildscript {
ext { ext {
compose_version = '1.2.0-alpha07' compose_version = '1.2.0-beta01'
}
} }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '7.1.1' apply false id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
} }
task clean(type: Delete) { task clean(type: Delete) {