From eb8d3818d87305cc87854ab1c9aaaff882a43e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Wed, 4 Oct 2023 09:54:48 +0200 Subject: [PATCH 1/9] update project to newer kotlin and gradle libraries and compileSdk 33 --- build.gradle | 6 +- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 59 +++++++++++-------- .../thanel/swipeactionview/SwipeActionView.kt | 2 +- sample/build.gradle | 19 +++--- sample/src/main/AndroidManifest.xml | 3 +- .../swipeactionview/sample/MainActivity.java | 1 + 7 files changed, 54 insertions(+), 40 deletions(-) diff --git a/build.gradle b/build.gradle index d5daa35..9e68e50 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,12 @@ buildscript { - ext.kotlin_version = '1.3.0' + ext.kotlin_version = '1.9.10' repositories { mavenCentral() - jcenter() google() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.2' + classpath 'com.android.tools.build:gradle:8.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' } @@ -16,7 +15,6 @@ buildscript { allprojects { repositories { mavenCentral() - jcenter() google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7397aef..2040eac 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu Mar 07 19:06:55 CET 2019 +#Wed Oct 04 09:22:37 CEST 2023 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip diff --git a/library/build.gradle b/library/build.gradle index 962696a..b4b55d1 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,57 +1,66 @@ -apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' -apply plugin: 'maven-publish' - +plugins { + id 'com.android.library' + id 'kotlin-android' + id 'maven-publish' +} android { - compileSdkVersion 28 - buildToolsVersion "28.0.3" - defaultConfig { + namespace = "me.thanel.swipeactionview" minSdkVersion 14 - targetSdkVersion 28 + targetSdkVersion 33 + compileSdk 33 } sourceSets { main.java.srcDirs += 'src/main/kotlin' } -} -publishing { - publications { - release(MavenPublication) { - artifact(bundleReleaseAar) - artifact sourcesJar - artifact javadocJar + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } - groupId = 'com.github.Tunous' - artifactId = 'SwipeActionView' - version = '1.4.0-SNAPSHOT' - } + kotlinOptions { + jvmTarget = "17" } } +/* +project.afterEvaluate { + publishing { + publications { + aar(MavenPublication) { + groupId = 'com.github.Tunous' + artifactId = 'SwipeActionView' + version = '1.4.0-SNAPSHOT' + + artifact bundleReleaseAar + artifact sourcesJar + artifact javadocJar + } + } + } +}*/ dependencies { - implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.appcompat:appcompat:1.6.1' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } + task sourcesJar(type: Jar) { from android.sourceSets.main.java.srcDirs - classifier = 'sources' + archiveClassifier = 'sources' } task javadoc(type: Javadoc) { failOnError false source = android.sourceSets.main.java.sourceFiles classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) - classpath += configurations.compile } task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' + archiveClassifier = 'javadoc' from javadoc.destinationDir } - - diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index 77e79da..cf73723 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -911,7 +911,7 @@ class SwipeActionView : FrameLayout { setFloatValues(targetTranslationX) removeAllListeners() addListener(object : AnimatorListenerAdapter() { - override fun onAnimationEnd(animation: Animator?) { + override fun onAnimationEnd(animation: Animator) { onEnd() } }) diff --git a/sample/build.gradle b/sample/build.gradle index 0a1cce9..6b718f2 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,15 +1,20 @@ -apply plugin: 'com.android.application' +plugins { + id 'com.android.application' + id 'kotlin-android' + //id 'maven-publish' +} + android { - compileSdkVersion 28 - buildToolsVersion "28.0.3" defaultConfig { applicationId "me.thanel.swipeactionview.sample" + namespace = "me.thanel.swipeactionview.sample" minSdkVersion 14 - targetSdkVersion 28 - versionCode 2 - versionName "0.2" + targetSdkVersion 33 + compileSdk 33 + versionCode 3 + versionName "0.3" } buildTypes { release { @@ -20,7 +25,7 @@ android { } dependencies { - implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.cardview:cardview:1.0.0' implementation project(':library') diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 320704f..a327951 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -9,7 +9,8 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java index 193f800..f58486a 100644 --- a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java +++ b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java @@ -22,6 +22,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; + import me.thanel.swipeactionview.SwipeActionView; import me.thanel.swipeactionview.SwipeDirection; import me.thanel.swipeactionview.SwipeGestureListener; From 55ca0d046a915c487558cb090def3604618dbe08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Wed, 4 Oct 2023 11:50:08 +0200 Subject: [PATCH 2/9] implement multi-swipe-container --- .../swipeactionview/MultiSwipeActionView.kt | 15 ++++ .../thanel/swipeactionview/SwipeActionView.kt | 86 ++++++++++++++++++- .../swipeactionview/SwipeGestureListener.kt | 24 ++++++ .../swipeactionview/sample/MainActivity.java | 44 +++++++++- sample/src/main/res/layout/activity_main.xml | 43 +++++++++- sample/src/main/res/values/strings.xml | 1 + sample/src/main/res/values/styles.xml | 6 +- 7 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 library/src/main/kotlin/me/thanel/swipeactionview/MultiSwipeActionView.kt diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/MultiSwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/MultiSwipeActionView.kt new file mode 100644 index 0000000..36de7bc --- /dev/null +++ b/library/src/main/kotlin/me/thanel/swipeactionview/MultiSwipeActionView.kt @@ -0,0 +1,15 @@ +package me.thanel.swipeactionview + +import android.content.Context +import android.util.AttributeSet +import android.widget.LinearLayout + +class MultiSwipeActionView : LinearLayout { + constructor(context: Context?) : super(context) + constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) +} \ No newline at end of file diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index cf73723..d59f636 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -28,6 +28,7 @@ import android.os.Build import android.os.Handler import android.os.Message import android.util.AttributeSet +import android.util.Log import android.view.* import android.view.animation.DecelerateInterpolator import android.widget.FrameLayout @@ -41,6 +42,8 @@ import me.thanel.swipeactionview.utils.marginStart import me.thanel.swipeactionview.utils.radius import me.thanel.swipeactionview.utils.setBoundsFrom import me.thanel.swipeactionview.utils.totalWidth +import kotlin.math.absoluteValue + /** * View that allows users to perform various actions by swiping it to the left or right sides. @@ -186,6 +189,14 @@ class SwipeActionView : FrameLayout { */ private var maxRightSwipeDistance = 0f + + /** + * The radius in which dragging should be increased because the first element is gone. + * Only used for MultiSwipeActionView + * Should be coordinated with resistance-multiplier + */ + private val distanceFromCenterpoint = 14 + /** * The minimum distance required to execute swipe callbacks when swiping to the left side. */ @@ -760,6 +771,39 @@ class SwipeActionView : FrameLayout { else -> false } + /** + * Tell whether the user has swiped the view far enough to perform a swipe callback + * for the first element in a MultiSwipeActionView + * + * @param swipeDistance The performed swipe distance. + * + * @return Whether the user has swiped far enough + */ + private fun hasSwipedFarEnoughMultiContainerFirstElement(swipeDistance: Float): Boolean { + + val normalizedTranslation = swipeDistance.absoluteValue + //swiping right + if(swipeDistance>0){ + if (rightSwipeView is MultiSwipeActionView) { + val maxSwipe = (rightSwipeView as MultiSwipeActionView).width*0.75 + val halfway = (rightSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: 0 + if(halfway-distanceFromCenterpoint < normalizedTranslation && maxSwipe > normalizedTranslation) { + return true + } + } + } else { + if (leftSwipeView is MultiSwipeActionView) { + val maxSwipe = (leftSwipeView as MultiSwipeActionView).width*0.75 + val vg = (leftSwipeView as MultiSwipeActionView) + val halfway = vg.getChildAt(vg.childCount-1)?.width ?: 0 + if(halfway-distanceFromCenterpoint < normalizedTranslation && maxSwipe > normalizedTranslation) { + return true + } + } + } + return false + } + /** * Perform the drag by horizontally moving the view by movement delta. * @@ -768,10 +812,34 @@ class SwipeActionView : FrameLayout { private fun performDrag(e: MotionEvent) { var delta = e.rawX - lastX + var resistance = dragResistance + val normalizedTranslation = container.translationX.absoluteValue + val resistanceMulitplier = 8 + + //swiping right + if(container.translationX>0){ + if (rightSwipeView is MultiSwipeActionView) { + val halfway = (rightSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: 0 + if(halfway+distanceFromCenterpoint > normalizedTranslation && + normalizedTranslation > halfway-distanceFromCenterpoint) { + resistance *=resistanceMulitplier + } + } + } else { + if (leftSwipeView is MultiSwipeActionView) { + val vg = (leftSwipeView as MultiSwipeActionView) + val halfway = vg.getChildAt(vg.childCount-1)?.width ?: 0 + if(halfway+distanceFromCenterpoint > normalizedTranslation && + normalizedTranslation > halfway-distanceFromCenterpoint) { + resistance *=resistanceMulitplier + } + } + } + // If we are swiping view away from view's default position make the swiping feel much // harder to drag. if (delta > 0 == container.translationX > 0 || container.translationX == 0f) { - delta /= dragResistance + delta /= resistance } container.translationX += delta @@ -810,6 +878,10 @@ class SwipeActionView : FrameLayout { return } + if(hasSwipedFarEnoughMultiContainerFirstElement(container.translationX)){ + activate(container.translationX > 0, true) + } + if (hasSwipedFarEnough(container.translationX) || swipedFastEnough) { activate(container.translationX > 0) } else { @@ -838,7 +910,7 @@ class SwipeActionView : FrameLayout { * * @param swipedRight Tells whether the view was swiped to the right instead of left side. */ - private fun activate(swipedRight: Boolean) { + private fun activate(swipedRight: Boolean, isHalfwaySwipe: Boolean = false) { // If activation animation didn't finish, move the view to original position without // executing activate callback. if (!canPerformSwipeAction) { @@ -852,8 +924,15 @@ class SwipeActionView : FrameLayout { } else { leftSwipeRipple.restart() } + if(isHalfwaySwipe) { + if (swipedRight) { + swipeGestureListener?.onSwipedHalfwayRight(this) + } else { + swipeGestureListener?.onSwipedHalfwayLeft(this) + } + } - val targetTranslationX = if (swipedRight) maxRightSwipeDistance else -maxLeftSwipeDistance + var targetTranslationX = if (swipedRight) maxRightSwipeDistance else -maxLeftSwipeDistance animateContainer(targetTranslationX, swipeAnimationDuration) { val shouldFinish = if (swipedRight) { swipeGestureListener?.onSwipedRight(this) @@ -868,6 +947,7 @@ class SwipeActionView : FrameLayout { } else { swipeGestureListener?.onSwipeLeftComplete(this) } + swipeGestureListener?.onSwipeLeftComplete(this) } } } diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeGestureListener.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeGestureListener.kt index 88a7404..9d174b4 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeGestureListener.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeGestureListener.kt @@ -47,6 +47,30 @@ interface SwipeGestureListener { */ fun onSwipedRight(swipeActionView: SwipeActionView): Boolean {return true} + /** + * Callback method to be invoked when user swipes the [SwipeActionView] to the + * left, but only the first element. Requires MultiSwipeActionView as a Container. + * + * @param swipeActionView The [SwipeActionView] from which this method was invoked. + * + * @return Whether the container should return to default position. When `false`, then you + * should manually call `reset` method to return to default position. + * @see SwipeActionView + */ + fun onSwipedHalfwayLeft(swipeActionView: SwipeActionView): Boolean {return true} + + /** + * Callback method to be invoked when user swipes the [SwipeActionView] to the + * right, but only the first element. Requires MultiSwipeActionView as a Container. + * + * @param swipeActionView The [SwipeActionView] from which this method was invoked. + * + * @return Whether the container should return to default position. When `false`, then you + * should manually call `reset` method to return to default position. + * @see SwipeActionView + */ + fun onSwipedHalfwayRight(swipeActionView: SwipeActionView): Boolean {return true} + /** * Callback method to be invoked when the left swipe is complete. * A swipe is considered complete once the view returns back to its original position. diff --git a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java index f58486a..fe6411e 100644 --- a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java +++ b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java @@ -37,6 +37,18 @@ protected void onCreate(final Bundle savedInstanceState) { setContentView(R.layout.activity_main); SwipeGestureListener swipeGestureListener = new SwipeGestureListener() { + @Override + public boolean onSwipedHalfwayRight(@NonNull SwipeActionView swipeActionView) { + showToast(true, true); + return false; + } + + @Override + public boolean onSwipedHalfwayLeft(@NonNull SwipeActionView swipeActionView) { + showToast(false, true); + return false; + } + @Override public void onSwipeRightComplete(SwipeActionView swipeActionView) { // do nothing @@ -49,13 +61,13 @@ public void onSwipeLeftComplete(SwipeActionView swipeActionView) { @Override public boolean onSwipedLeft(@NonNull SwipeActionView swipeActionView) { - showToast(false); + showToast(false, false); return true; } @Override public boolean onSwipedRight(@NonNull SwipeActionView swipeActionView) { - showToast(true); + showToast(true, false); return true; } }; @@ -77,6 +89,16 @@ public boolean onSwipedRight(@NonNull SwipeActionView swipeActionView) { swipeCardView.setSwipeGestureListener(swipeGestureListener); SwipeGestureListener delayedSwipeGestureListener = new SwipeGestureListener() { + @Override + public boolean onSwipedHalfwayRight(@NonNull SwipeActionView swipeActionView) { + return true; + } + + @Override + public boolean onSwipedHalfwayLeft(@NonNull SwipeActionView swipeActionView) { + return true; + } + @Override public void onSwipeRightComplete(SwipeActionView swipeActionView) { //this won't be called since onSwipedRight returns false @@ -109,6 +131,16 @@ public boolean onSwipedRight(@NonNull SwipeActionView swipeActionView) { SwipeGestureListener completeGestureListener = new SwipeGestureListener() { + @Override + public boolean onSwipedHalfwayLeft(@NonNull SwipeActionView swipeActionView) { + return true; + } + + @Override + public boolean onSwipedHalfwayRight(@NonNull SwipeActionView swipeActionView) { + return true; + } + @Override public void onSwipeRightComplete(SwipeActionView swipeActionView) { Toast.makeText(MainActivity.this, R.string.swipe_right_complete, Toast.LENGTH_SHORT).show(); @@ -136,10 +168,14 @@ public boolean onSwipedRight(@NonNull SwipeActionView swipeActionView) { swipeComplete.setSwipeGestureListener(completeGestureListener); } - private void showToast(Boolean swipedRight) { + private void showToast(Boolean swipedRight, Boolean wasHalfway) { int resId = swipedRight ? R.string.swiped_right : R.string.swiped_left; + String text = getString(resId); + if(wasHalfway) { + text+= getString(R.string.swiped_halfway); + } - Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); + Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); } public void swipeLeft(View view) { diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index a60625b..57598c1 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -31,7 +31,25 @@ android:id="@+id/swipe_right" style="@style/SwipeItem"> - + + + + + + + + - + + + + + + + + + Swipe left complete Swipe right complete + and only first icon diff --git a/sample/src/main/res/values/styles.xml b/sample/src/main/res/values/styles.xml index b62f30b..a73e3bd 100644 --- a/sample/src/main/res/values/styles.xml +++ b/sample/src/main/res/values/styles.xml @@ -35,10 +35,10 @@ From 101b63826d9713eca922770dcf22a3f40698eb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Wed, 4 Oct 2023 12:39:29 +0200 Subject: [PATCH 3/9] improve swipe behaviour --- .../main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index d59f636..e396fbd 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -195,7 +195,7 @@ class SwipeActionView : FrameLayout { * Only used for MultiSwipeActionView * Should be coordinated with resistance-multiplier */ - private val distanceFromCenterpoint = 14 + private val distanceFromCenterpoint = 8 /** * The minimum distance required to execute swipe callbacks when swiping to the left side. @@ -814,7 +814,7 @@ class SwipeActionView : FrameLayout { var resistance = dragResistance val normalizedTranslation = container.translationX.absoluteValue - val resistanceMulitplier = 8 + val resistanceMulitplier = 4 //swiping right if(container.translationX>0){ From e1df0d4ab973642ca57cf004a0b99c89a339a806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Thu, 12 Oct 2023 10:37:25 +0200 Subject: [PATCH 4/9] Update library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Łukasz Rutkowski --- .../kotlin/me/thanel/swipeactionview/SwipeActionView.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index e396fbd..77b8309 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -878,12 +878,10 @@ class SwipeActionView : FrameLayout { return } - if(hasSwipedFarEnoughMultiContainerFirstElement(container.translationX)){ - activate(container.translationX > 0, true) - } - if (hasSwipedFarEnough(container.translationX) || swipedFastEnough) { activate(container.translationX > 0) + } else if (hasSwipedFarEnoughMultiContainerFirstElement(container.translationX)) { + activate(container.translationX > 0, true) } else { animateToOriginalPosition() } From ffcbb4c9e2373ec55656b97a2a756d6db898ba90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Thu, 12 Oct 2023 10:39:54 +0200 Subject: [PATCH 5/9] Update library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Łukasz Rutkowski --- .../src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index 77b8309..3a3bbd2 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -945,7 +945,6 @@ class SwipeActionView : FrameLayout { } else { swipeGestureListener?.onSwipeLeftComplete(this) } - swipeGestureListener?.onSwipeLeftComplete(this) } } } From 80f8d6f104d429887f5d5d9619319dec806b7c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Thu, 12 Oct 2023 10:52:39 +0200 Subject: [PATCH 6/9] remove unnessessary checks --- .../kotlin/me/thanel/swipeactionview/SwipeActionView.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index 3a3bbd2..5f95263 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -785,18 +785,16 @@ class SwipeActionView : FrameLayout { //swiping right if(swipeDistance>0){ if (rightSwipeView is MultiSwipeActionView) { - val maxSwipe = (rightSwipeView as MultiSwipeActionView).width*0.75 val halfway = (rightSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: 0 - if(halfway-distanceFromCenterpoint < normalizedTranslation && maxSwipe > normalizedTranslation) { + if(halfway-distanceFromCenterpoint < normalizedTranslation) { return true } } } else { if (leftSwipeView is MultiSwipeActionView) { - val maxSwipe = (leftSwipeView as MultiSwipeActionView).width*0.75 val vg = (leftSwipeView as MultiSwipeActionView) val halfway = vg.getChildAt(vg.childCount-1)?.width ?: 0 - if(halfway-distanceFromCenterpoint < normalizedTranslation && maxSwipe > normalizedTranslation) { + if(halfway-distanceFromCenterpoint < normalizedTranslation) { return true } } From 9519dd82e7fe00049244c66d85d2224645623fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sun, 22 Oct 2023 13:46:00 +0200 Subject: [PATCH 7/9] move swipe decision into animate container --- .../thanel/swipeactionview/SwipeActionView.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index 5f95263..eb5ef27 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -920,20 +920,21 @@ class SwipeActionView : FrameLayout { } else { leftSwipeRipple.restart() } - if(isHalfwaySwipe) { - if (swipedRight) { - swipeGestureListener?.onSwipedHalfwayRight(this) - } else { - swipeGestureListener?.onSwipedHalfwayLeft(this) - } - } var targetTranslationX = if (swipedRight) maxRightSwipeDistance else -maxLeftSwipeDistance animateContainer(targetTranslationX, swipeAnimationDuration) { val shouldFinish = if (swipedRight) { - swipeGestureListener?.onSwipedRight(this) + if(isHalfwaySwipe) { + swipeGestureListener?.onSwipedHalfwayRight(this) + } else { + swipeGestureListener?.onSwipedRight(this) + } } else { - swipeGestureListener?.onSwipedLeft(this) + if(isHalfwaySwipe) { + swipeGestureListener?.onSwipedHalfwayLeft(this) + } else { + swipeGestureListener?.onSwipedLeft(this) + } } if (shouldFinish != false) { From 7d1731720b880b2c8eb6f75ab46d4a1dd8207cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sun, 22 Oct 2023 13:57:19 +0200 Subject: [PATCH 8/9] Implement Suggestions --- .../thanel/swipeactionview/SwipeActionView.kt | 23 +++++++++++++++++-- .../swipeactionview/sample/MainActivity.java | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index eb5ef27..8eaaf38 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -921,8 +921,7 @@ class SwipeActionView : FrameLayout { leftSwipeRipple.restart() } - var targetTranslationX = if (swipedRight) maxRightSwipeDistance else -maxLeftSwipeDistance - animateContainer(targetTranslationX, swipeAnimationDuration) { + animateContainer(getTargetTranslationX(swipedRight, isHalfwaySwipe), swipeAnimationDuration) { val shouldFinish = if (swipedRight) { if(isHalfwaySwipe) { swipeGestureListener?.onSwipedHalfwayRight(this) @@ -1038,6 +1037,26 @@ class SwipeActionView : FrameLayout { else -> maxRightSwipeDistance } + /** + * Get the translation distance for the animate container + * + * @param swipedRight whether the user swiped right or left (true for right, false for left) + * @param isHalfwaySwipe whether the user swiped ony the first item + * + * @return Translation Value + */ + private fun getTargetTranslationX(swipedRight: Boolean, isHalfwaySwipe: Boolean): Float { + return if(isHalfwaySwipe) { + return if (swipedRight) { + ((rightSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: maxRightSwipeDistance).toFloat() + } else { + ((leftSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: -maxLeftSwipeDistance).toFloat() + } + } else { + if (swipedRight) maxRightSwipeDistance else -maxLeftSwipeDistance + } + } + /** * Get min activation distance for the specified delta. Once users swipes view above this * distance swipe callback will be called upon view release. diff --git a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java index fe6411e..2ce64c8 100644 --- a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java +++ b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java @@ -40,7 +40,7 @@ protected void onCreate(final Bundle savedInstanceState) { @Override public boolean onSwipedHalfwayRight(@NonNull SwipeActionView swipeActionView) { showToast(true, true); - return false; + return true; } @Override From f823b92bb907b808f940c8bcd50da00fb469aac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rutkowski?= Date: Sun, 29 Oct 2023 16:04:56 +0100 Subject: [PATCH 9/9] Fix incorrect translation for halfway swipe left --- .../main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt | 2 +- .../java/me/thanel/swipeactionview/sample/MainActivity.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt index 8eaaf38..f8fec10 100644 --- a/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt +++ b/library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt @@ -1050,7 +1050,7 @@ class SwipeActionView : FrameLayout { return if (swipedRight) { ((rightSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: maxRightSwipeDistance).toFloat() } else { - ((leftSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: -maxLeftSwipeDistance).toFloat() + -((leftSwipeView as MultiSwipeActionView).getChildAt(0)?.width ?: maxLeftSwipeDistance).toFloat() } } else { if (swipedRight) maxRightSwipeDistance else -maxLeftSwipeDistance diff --git a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java index 2ce64c8..de71b60 100644 --- a/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java +++ b/sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java @@ -46,7 +46,7 @@ public boolean onSwipedHalfwayRight(@NonNull SwipeActionView swipeActionView) { @Override public boolean onSwipedHalfwayLeft(@NonNull SwipeActionView swipeActionView) { showToast(false, true); - return false; + return true; } @Override