From 9f51fb4730058b91b6dc081f419dff84ce627b93 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 1 Jul 2019 21:26:31 -0700 Subject: Use recommended velocities by default --- core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt | 4 ---- core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index 1f564ce..d26fb92 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -162,12 +162,8 @@ internal class SwipeBackLayout @JvmOverloads constructor( init { dragHelper = ViewDragHelper.create(this, ViewDragCallback()) - val density = resources.displayMetrics.density - val minVel = MIN_FLING_VELOCITY * density //allow touch from anywhere on the screen edgeSize = Math.max(resources.displayMetrics.widthPixels, resources.displayMetrics.heightPixels) - minVelocity = minVel -// maxVelocity = 2.5f * minVel edgeFlag = edgeFlag sensitivity = 0.3f addListener(chromeFadeListener) diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java b/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java index ac7fb7e..dab9bb8 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java @@ -25,10 +25,10 @@ import static ca.allanwang.kau.swipe.SwipeBackHelperKt.SWIPE_EDGE_TOP; * of useful operations and state tracking for allowing a user to drag and reposition * views within their parent ViewGroup. *

- * This is an extension of {@link androidx.core.widget.ViewDragHelper} + * This is an extension of {@link androidx.customview.widget.ViewDragHelper} * Along with additional methods defined in {@link ViewDragHelperExtras} */ -class ViewDragHelper implements ViewDragHelperExtras { +public class ViewDragHelper implements ViewDragHelperExtras { private static final String TAG = "ViewDragHelper"; /** -- cgit v1.2.3 From 347f820db539de4823572a22ae8c16611167af72 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 1 Jul 2019 23:32:16 -0700 Subject: Revert viewdraghelper exposure --- core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt | 6 +++--- core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java | 2 +- sample/src/main/res/xml/kau_changelog.xml | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index d26fb92..8f1da04 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -236,10 +236,10 @@ internal class SwipeBackLayout @JvmOverloads constructor( override fun onInterceptTouchEvent(event: MotionEvent): Boolean { if (!swipeEnabled || disallowIntercept) return false - try { - return dragHelper.shouldInterceptTouchEvent(event) + return try { + dragHelper.shouldInterceptTouchEvent(event) } catch (e: Exception) { - return false + false } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java b/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java index dab9bb8..f6e907a 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java @@ -28,7 +28,7 @@ import static ca.allanwang.kau.swipe.SwipeBackHelperKt.SWIPE_EDGE_TOP; * This is an extension of {@link androidx.customview.widget.ViewDragHelper} * Along with additional methods defined in {@link ViewDragHelperExtras} */ -public class ViewDragHelper implements ViewDragHelperExtras { +class ViewDragHelper implements ViewDragHelperExtras { private static final String TAG = "ViewDragHelper"; /** diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index 0a7b401..f91355b 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -11,7 +11,6 @@ - -- cgit v1.2.3 From 9cb9c1f057e5326f34cf2838177504df1fad64ed Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 2 Jul 2019 16:38:08 -0700 Subject: Expose swipe back --- .../kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt index 1c6de12..90e6e6d 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt @@ -34,8 +34,12 @@ internal object SwipeBackHelper { private operator fun get(activity: Activity): SwipeBackPage? = pageStack.firstOrNull { it.activityRef.get() === activity } - fun onCreate(activity: Activity, builder: SwipeBackContract.() -> Unit = {}) { - val page = this[activity] ?: pageStack.push(SwipeBackPage(activity).apply { builder() }) + fun onCreate( + activity: Activity, + builder: SwipeBackContract.() -> Unit = {} + ): SwipeBackContract { + val swipeBackPage = SwipeBackPage(activity).apply(builder) + val page = this[activity] ?: pageStack.push(swipeBackPage) val startAnimation: Int = when (page.edgeFlag) { SWIPE_EDGE_LEFT -> R.anim.kau_slide_in_right SWIPE_EDGE_RIGHT -> R.anim.kau_slide_in_left @@ -45,6 +49,7 @@ internal object SwipeBackHelper { activity.overridePendingTransition(startAnimation, 0) page.onPostCreate() KL.v { "KauSwipe onCreate ${activity.localClassName}" } + return swipeBackPage } fun onDestroy(activity: Activity) { @@ -73,13 +78,17 @@ internal object SwipeBackHelper { * * Lastly, don't forget to call [kauSwipeOnDestroy] as well when the activity is destroyed. */ -fun Activity.kauSwipeOnCreate(builder: SwipeBackContract.() -> Unit = {}) = SwipeBackHelper.onCreate(this, builder) +fun Activity.kauSwipeOnCreate(builder: SwipeBackContract.() -> Unit = {}): SwipeBackContract = + SwipeBackHelper.onCreate(this, builder) /** * Deprecated as onPostCreate seems unreliable. * We will instead initialize everything during [kauSwipeOnCreate] */ -@Deprecated(level = DeprecationLevel.WARNING, message = "All functionality has been moved to kauSwipeOnCreate") +@Deprecated( + level = DeprecationLevel.WARNING, + message = "All functionality has been moved to kauSwipeOnCreate" +) fun Activity.kauSwipeOnPostCreate() { } -- cgit v1.2.3 From 00ba1bcf7a756252baa808e62a0916fd18e95946 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 2 Jul 2019 16:48:43 -0700 Subject: Add disallow on touch and document --- core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt | 2 +- core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index 8f1da04..c5b64e3 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -245,7 +245,7 @@ internal class SwipeBackLayout @JvmOverloads constructor( @SuppressLint("ClickableViewAccessibility") override fun onTouchEvent(event: MotionEvent): Boolean { - if (!swipeEnabled) return false + if (!swipeEnabled || disallowIntercept) return false try { dragHelper.processTouchEvent(event) } catch (e: Exception) { diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt index bc5b459..c85625a 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt @@ -75,6 +75,7 @@ internal interface SwipeBackContractInternal : SwipeBackContract { interface SwipeBackContract { /** * Toggle main touch intercept + * Disabling will remove the swipe back handler */ var swipeEnabled: Boolean /** @@ -91,6 +92,10 @@ interface SwipeBackContract { * Set the scrolling threshold for wish a page is deemed closing */ var scrollThreshold: Float + /** + * Toggle main touch intercept + * Disabling will just ignore touch events, but the handler will remain attached + */ var disallowIntercept: Boolean /** * Set the min velocity of the view drag helper -- cgit v1.2.3 From e5753162b7aa2d4050d735ace785b29dc410d248 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 22 Sep 2019 21:58:12 -0700 Subject: Update math usage and remove min velocity override --- .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 33 +++++++++------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index 5f462b4..bf6d09d 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -30,6 +30,9 @@ import ca.allanwang.kau.utils.adjustAlpha import ca.allanwang.kau.utils.navigationBarColor import ca.allanwang.kau.utils.statusBarColor import java.lang.ref.WeakReference +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min /** * The layout that handles all the touch events @@ -165,10 +168,8 @@ internal class SwipeBackLayout @JvmOverloads constructor( init { dragHelper = ViewDragHelper.create(this, ViewDragCallback()) - val density = resources.displayMetrics.density - val minVel = MIN_FLING_VELOCITY * density // allow touch from anywhere on the screen - edgeSize = Math.max(resources.displayMetrics.widthPixels, resources.displayMetrics.heightPixels) + edgeSize = max(resources.displayMetrics.widthPixels, resources.displayMetrics.heightPixels) edgeFlag = edgeFlag sensitivity = 0.3f addListener(chromeFadeListener) @@ -369,10 +370,8 @@ internal class SwipeBackLayout @JvmOverloads constructor( val contentView = contentViewRef.get() ?: return KL.e { "KauSwipe cannot change view position as contentView is null; is onPostCreate called?" } // make sure that we are using the proper axis - scrollPercent = Math.abs( - if (horizontal) left.toFloat() / contentView.width - else (top.toFloat() / contentView.height) - ) + scrollPercent = abs(if (horizontal) left.toFloat() / contentView.width + else (top.toFloat() / contentView.height)) contentOffset = if (horizontal) left else top invalidate() if (scrollPercent < scrollThreshold && !isScrollOverValid) @@ -397,10 +396,10 @@ internal class SwipeBackLayout @JvmOverloads constructor( var result = Pair(0, 0) if (scrollPercent <= scrollThreshold) { // threshold not met; check velocities - if ((edgeFlag == SWIPE_EDGE_LEFT && xvel > MIN_FLING_VELOCITY) || - (edgeFlag == SWIPE_EDGE_RIGHT && xvel < -MIN_FLING_VELOCITY) || - (edgeFlag == SWIPE_EDGE_TOP && yvel > MIN_FLING_VELOCITY) || - (edgeFlag == SWIPE_EDGE_BOTTOM && yvel < -MIN_FLING_VELOCITY) + if ((edgeFlag == SWIPE_EDGE_LEFT && xvel > minVelocity) || + (edgeFlag == SWIPE_EDGE_RIGHT && xvel < -minVelocity) || + (edgeFlag == SWIPE_EDGE_TOP && yvel > minVelocity) || + (edgeFlag == SWIPE_EDGE_BOTTOM && yvel < -minVelocity) ) result = exitCaptureOffsets(edgeFlag, releasedChild) } else { @@ -425,26 +424,22 @@ internal class SwipeBackLayout @JvmOverloads constructor( override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int { return when (edgeFlag) { - SWIPE_EDGE_RIGHT -> Math.min(0, Math.max(left, -child.width)) - SWIPE_EDGE_LEFT -> Math.min(child.width, Math.max(left, 0)) + SWIPE_EDGE_RIGHT -> min(0, max(left, -child.width)) + SWIPE_EDGE_LEFT -> min(child.width, max(left, 0)) else -> 0 } } override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int { return when (edgeFlag) { - SWIPE_EDGE_BOTTOM -> Math.min(0, Math.max(top, -child.height)) - SWIPE_EDGE_TOP -> Math.min(child.height, Math.max(top, 0)) + SWIPE_EDGE_BOTTOM -> min(0, max(top, -child.height)) + SWIPE_EDGE_TOP -> min(child.height, max(top, 0)) else -> 0 } } } companion object { - /** - * Minimum velocity that will be detected as a fling - */ - const val MIN_FLING_VELOCITY = 400 // dips per second const val DEFAULT_SCRIM_COLOR = 0x99000000.toInt() -- cgit v1.2.3