Skip to content

Commit

Permalink
Format and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Jan 18, 2024
1 parent 832c394 commit 75ec4dd
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 82 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ dependencies {
```

# Usage
If you have ever used a `ViewDragHelper` before, you may have noticed that if you were to translate your views using View.setX() or View.setY(), your helper would no longer correctly allow you to drag the views. This library seeks to correct this. Usage is identical to `ViewDragHelper` usage

If you have ever used a `ViewDragHelper` before, you may have noticed that if you were to translate your views using `View.setX()` or `View.setY()`, your helper would no longer correctly allow you to drag the views. This library seeks to correct this. Usage is identical to `ViewDragHelper` usage:
```kotlin
val callback = TranslationViewDragHelper.Callback {
override fun tryCaptureView(child: View, pointerId: Int): Boolean {
Expand Down
22 changes: 2 additions & 20 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,9 @@ android {
applicationId = "com.commit451.translationviewdraghelper.sample"
minSdk = 21
targetSdk = 34
versionCode = 101
versionName = "1.0.1"
versionCode = 100
versionName = "1.0.0"
}

buildFeatures {
viewBinding = true
}

buildTypes {
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles("proguard-rules.pro", getDefaultProguardFile("proguard-android.txt"))
}
getByName("debug") {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles("proguard-rules.pro", getDefaultProguardFile("proguard-android.txt"))
}
}

}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.commit451.translationviewdraghelper.sample

import android.annotation.TargetApi
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
Expand All @@ -11,7 +10,7 @@ import com.commit451.translationviewdraghelper.TranslationViewDragHelper
/**
* FrameLayout that allows dragging its inner views around
*/
class AllowsForDragFrameLayout : FrameLayout {
class DragFrameLayout : FrameLayout {

private var viewDragHelper: TranslationViewDragHelper

Expand Down Expand Up @@ -43,7 +42,6 @@ class AllowsForDragFrameLayout : FrameLayout {
defStyleAttr
)

@TargetApi(21)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.commit451.translationviewdraghelper.sample

import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<com.commit451.translationviewdraghelper.sample.AllowsForDragFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<com.commit451.translationviewdraghelper.sample.DragFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand All @@ -9,6 +8,8 @@
android:id="@+id/box"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0000" />
android:background="#ff0000"
android:translationX="100dp"
android:translationY="200dp" />

</com.commit451.translationviewdraghelper.sample.AllowsForDragFrameLayout>
</com.commit451.translationviewdraghelper.sample.DragFrameLayout>
6 changes: 0 additions & 6 deletions app/src/main/res/values-w820dp/dimens.xml

This file was deleted.

5 changes: 0 additions & 5 deletions app/src/main/res/values/dimens.xml

This file was deleted.

1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ agp = "8.2.1"
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version = "1.12.0" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version = "1.6.1" }


[plugins]
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "org-jetbrains-kotlin-android" }
com-android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
3 changes: 1 addition & 2 deletions translationviewdraghelper/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.commit451.translationviewdraghelper"/>
<manifest package="com.commit451.translationviewdraghelper" />
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.view.animation.Interpolator

import java.util.Arrays

import androidx.core.view.MotionEventCompat
import androidx.core.view.VelocityTrackerCompat
import androidx.core.view.ViewCompat
import androidx.core.widget.ScrollerCompat
import java.util.Arrays
import kotlin.math.abs
import kotlin.math.roundToInt
import kotlin.math.sin
Expand All @@ -50,7 +48,11 @@ open class TranslationViewDragHelper
* @param parentView Parent view to monitor
* @param callback the callback
*/
private constructor(context: Context, private val parentView: ViewGroup, private val callback: Callback) {
private constructor(
context: Context,
private val parentView: ViewGroup,
private val callback: Callback
) {

companion object {
private const val TAG = "TransViewDragHelper"
Expand Down Expand Up @@ -151,7 +153,11 @@ private constructor(context: Context, private val parentView: ViewGroup, private
* @param cb Callback to provide information and receive events
* @return a new ViewDragHelper instance
*/
fun create(forParent: ViewGroup, sensitivity: Float, cb: Callback): TranslationViewDragHelper {
fun create(
forParent: ViewGroup,
sensitivity: Float,
cb: Callback
): TranslationViewDragHelper {
val helper = create(forParent, cb)
helper.touchSlop = (helper.touchSlop * (1 / sensitivity)).toInt()
return helper
Expand Down Expand Up @@ -544,9 +550,11 @@ private constructor(context: Context, private val parentView: ViewGroup, private
fun settleCapturedViewAt(finalLeft: Int, finalTop: Int): Boolean {
check(releaseInProgress) { "Cannot settleCapturedViewAt outside of a call to " + "Callback#onViewReleased" }

return forceSettleCapturedViewAt(finalLeft, finalTop,
VelocityTrackerCompat.getXVelocity(velocityTracker!!, activePointerId).toInt(),
VelocityTrackerCompat.getYVelocity(velocityTracker!!, activePointerId).toInt())
return forceSettleCapturedViewAt(
finalLeft, finalTop,
VelocityTrackerCompat.getXVelocity(velocityTracker!!, activePointerId).toInt(),
VelocityTrackerCompat.getYVelocity(velocityTracker!!, activePointerId).toInt()
)
}

/**
Expand All @@ -558,7 +566,12 @@ private constructor(context: Context, private val parentView: ViewGroup, private
* @param yvel Vertical velocity
* @return true if animation should continue through [.continueSettling] calls
*/
private fun forceSettleCapturedViewAt(finalLeft: Int, finalTop: Int, xvel: Int, yvel: Int): Boolean {
private fun forceSettleCapturedViewAt(
finalLeft: Int,
finalTop: Int,
xvel: Int,
yvel: Int
): Boolean {
val startLeft = capturedView!!.x.toInt()
val startTop = capturedView!!.y.toInt()
val dx = finalLeft - startLeft
Expand Down Expand Up @@ -679,10 +692,12 @@ private constructor(context: Context, private val parentView: ViewGroup, private
fun flingCapturedView(minLeft: Int, minTop: Int, maxLeft: Int, maxTop: Int) {
check(releaseInProgress) { "Cannot flingCapturedView outside of a call to " + "Callback#onViewReleased" }

scroller.fling(capturedView!!.x.toInt(), capturedView!!.y.toInt(),
VelocityTrackerCompat.getXVelocity(velocityTracker!!, activePointerId).toInt(),
VelocityTrackerCompat.getYVelocity(velocityTracker!!, activePointerId).toInt(),
minLeft, maxLeft, minTop, maxTop)
scroller.fling(
capturedView!!.x.toInt(), capturedView!!.y.toInt(),
VelocityTrackerCompat.getXVelocity(velocityTracker!!, activePointerId).toInt(),
VelocityTrackerCompat.getYVelocity(velocityTracker!!, activePointerId).toInt(),
minLeft, maxLeft, minTop, maxTop
)

setDragState(STATE_SETTLING)
}
Expand Down Expand Up @@ -910,15 +925,21 @@ private constructor(context: Context, private val parentView: ViewGroup, private
// This will not work for transformed views in Honeycomb+
val child = v.getChildAt(i)
if (x + scrollX >= child.x && x + scrollX < child.x + child.width &&
y + scrollY >= child.y && y + scrollY < child.y + child.height &&
canScroll(child, true, dx, dy, (x + scrollX - child.x).toInt(),
(y + scrollY - child.y).toInt())) {
y + scrollY >= child.y && y + scrollY < child.y + child.height &&
canScroll(
child, true, dx, dy, (x + scrollX - child.x).toInt(),
(y + scrollY - child.y).toInt()
)
) {
return true
}
}
}

return checkV && (ViewCompat.canScrollHorizontally(v, -dx) || ViewCompat.canScrollVertically(v, -dy))
return checkV && (ViewCompat.canScrollHorizontally(
v,
-dx
) || ViewCompat.canScrollVertically(v, -dy))
}

/**
Expand Down Expand Up @@ -1011,14 +1032,19 @@ private constructor(context: Context, private val parentView: ViewGroup, private
// all in every dimension with a nonzero range, bail.
val oldLeft = toCapture!!.left
val targetLeft = oldLeft + dx.toInt()
val newLeft = callback.clampViewPositionHorizontal(toCapture,
targetLeft, dx.toInt())
val newLeft = callback.clampViewPositionHorizontal(
toCapture,
targetLeft, dx.toInt()
)
val oldTop = toCapture.top
val targetTop = oldTop + dy.toInt()
val newTop = callback.clampViewPositionVertical(toCapture, targetTop,
dy.toInt())
val newTop = callback.clampViewPositionVertical(
toCapture, targetTop,
dy.toInt()
)
val horizontalDragRange = callback.getViewHorizontalDragRange(
toCapture)
toCapture
)
val verticalDragRange = callback.getViewVerticalDragRange(toCapture)
if ((horizontalDragRange == 0 || horizontalDragRange > 0 && newLeft == oldLeft) && (verticalDragRange == 0 || verticalDragRange > 0 && newTop == oldTop)) {
break
Expand Down Expand Up @@ -1129,7 +1155,12 @@ private constructor(context: Context, private val parentView: ViewGroup, private
val idx = (x - lastMotionX!![activePointerId]).toInt()
val idy = (y - lastMotionY!![activePointerId]).toInt()

dragTo((capturedView!!.x + idx).toInt(), (capturedView!!.y + idy).toInt(), idx, idy)
dragTo(
(capturedView!!.x + idx).toInt(),
(capturedView!!.y + idy).toInt(),
idx,
idy
)

saveLastMotion(ev)
} else {
Expand All @@ -1153,7 +1184,11 @@ private constructor(context: Context, private val parentView: ViewGroup, private
}

val toCapture = findTopChildUnder(x.toInt(), y.toInt())
if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(
toCapture,
pointerId
)
) {
break
}
}
Expand All @@ -1176,7 +1211,11 @@ private constructor(context: Context, private val parentView: ViewGroup, private

val x = ev.getX(i)
val y = ev.getY(i)
if (findTopChildUnder(x.toInt(), y.toInt()) === capturedView && tryCaptureViewForDrag(capturedView, id)) {
if (findTopChildUnder(
x.toInt(),
y.toInt()
) === capturedView && tryCaptureViewForDrag(capturedView, id)
) {
newActivePointer = activePointerId
break
}
Expand Down Expand Up @@ -1232,9 +1271,10 @@ private constructor(context: Context, private val parentView: ViewGroup, private
val absODelta = abs(odelta)

if (initialEdgesTouched!![pointerId] and edge != edge || trackingEdges and edge == 0 ||
edgeDragsLocked!![pointerId] and edge == edge ||
edgeDragsInProgress!![pointerId] and edge == edge ||
absDelta <= touchSlop && absODelta <= touchSlop) {
edgeDragsLocked!![pointerId] and edge == edge ||
edgeDragsInProgress!![pointerId] and edge == edge ||
absDelta <= touchSlop && absODelta <= touchSlop
) {
return false
}
if (absDelta < absODelta * 0.5f && callback.onEdgeLock(edge)) {
Expand Down Expand Up @@ -1369,11 +1409,13 @@ private constructor(context: Context, private val parentView: ViewGroup, private
private fun releaseViewForPointerUp() {
velocityTracker!!.computeCurrentVelocity(1000, maxVelocity)
val xvel = clampMag(
VelocityTrackerCompat.getXVelocity(velocityTracker!!, activePointerId),
minVelocity, maxVelocity)
VelocityTrackerCompat.getXVelocity(velocityTracker!!, activePointerId),
minVelocity, maxVelocity
)
val yvel = clampMag(
VelocityTrackerCompat.getYVelocity(velocityTracker!!, activePointerId),
minVelocity, maxVelocity)
VelocityTrackerCompat.getYVelocity(velocityTracker!!, activePointerId),
minVelocity, maxVelocity
)
dispatchViewReleased(xvel, yvel)
}

Expand All @@ -1394,8 +1436,10 @@ private constructor(context: Context, private val parentView: ViewGroup, private
if (dx != 0 || dy != 0) {
val clampedDx = clampedX - oldLeft
val clampedDy = clampedY - oldTop
callback.onViewPositionChanged(capturedView, clampedX, clampedY,
clampedDx, clampedDy)
callback.onViewPositionChanged(
capturedView, clampedX, clampedY,
clampedDx, clampedDy
)
}
}

Expand Down Expand Up @@ -1443,7 +1487,8 @@ private constructor(context: Context, private val parentView: ViewGroup, private
for (i in childCount - 1 downTo 0) {
val child = parentView.getChildAt(callback.getOrderedChildIndex(i))
if (x >= child.x && x < child.x + child.width &&
y >= child.y && y < child.y + child.height) {
y >= child.y && y < child.y + child.height
) {
return child
}
}
Expand All @@ -1463,9 +1508,11 @@ private constructor(context: Context, private val parentView: ViewGroup, private

private fun isValidPointerForActionMove(pointerId: Int): Boolean {
if (!isPointerDown(pointerId)) {
Log.e(TAG, "Ignoring pointerId=" + pointerId + " because ACTION_DOWN was not received "
+ "for this pointer before ACTION_MOVE. It likely happened because "
+ " ViewDragHelper did not receive all the events in the event stream.")
Log.e(
TAG, "Ignoring pointerId=" + pointerId + " because ACTION_DOWN was not received "
+ "for this pointer before ACTION_MOVE. It likely happened because "
+ " ViewDragHelper did not receive all the events in the event stream."
)
return false
}
return true
Expand Down

0 comments on commit 75ec4dd

Please sign in to comment.