From 2a2d37e221e21dbbe4709c7545dd2c901a45f4dc Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 9 Feb 2018 16:41:17 +0100 Subject: [PATCH] fix some glitches with swiping to sides with Instant media change enabled --- .../gallery/fragments/PhotoFragment.kt | 4 ++ .../gallery/helpers/Constants.kt | 2 + .../gallery/helpers/MediaSideScroll.kt | 3 +- .../gallery/views/InstantItemSwitch.kt | 67 +++++++++++++++++++ app/src/main/res/layout/pager_photo_item.xml | 8 +-- 5 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/views/InstantItemSwitch.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt index 0831224cb..63d795c2e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt @@ -61,6 +61,10 @@ class PhotoFragment : ViewPagerFragment() { gif_view.setOnClickListener { photoClicked() } instant_prev_item.setOnClickListener { listener?.goToPrevItem() } instant_next_item.setOnClickListener { listener?.goToNextItem() } + + instant_prev_item.parentView = container + instant_next_item.parentView = container + photo_brightness_controller.setOnTouchListener { v, event -> mediaSideScroll.handleBrightnessTouched(event) true diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index 156bbf6b2..232708083 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -64,6 +64,8 @@ const val SLIDESHOW_SCROLL_DURATION = 500L const val NOMEDIA = ".nomedia" const val MAX_COLUMN_COUNT = 20 const val SHOW_TEMP_HIDDEN_DURATION = 600000L +const val CLICK_MAX_DURATION = 150 +const val DRAG_THRESHOLD = 10 const val DIRECTORY = "directory" const val MEDIUM = "medium" diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt index 8b89d7caf..7af072a4b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt @@ -11,7 +11,6 @@ import com.simplemobiletools.gallery.activities.ViewPagerActivity import com.simplemobiletools.gallery.extensions.audioManager class MediaSideScroll(val activity: Activity, val slideInfoView: TextView, val callback: () -> Unit) { - private val CLICK_MAX_DURATION = 150 private val SLIDE_INFO_FADE_DELAY = 1000L private var mTouchDownX = 0f private var mTouchDownY = 0f @@ -38,7 +37,7 @@ class MediaSideScroll(val activity: Activity, val slideInfoView: TextView, val c val diffX = mTouchDownX - event.x val diffY = mTouchDownY - event.y - if (Math.abs(diffY) > 20 && Math.abs(diffY) > Math.abs(diffX)) { + if (Math.abs(diffY) > DRAG_THRESHOLD && Math.abs(diffY) > Math.abs(diffX)) { var percent = ((diffY / ViewPagerActivity.screenHeight) * 100).toInt() * 3 percent = Math.min(100, Math.max(-100, percent)) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/views/InstantItemSwitch.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/views/InstantItemSwitch.kt new file mode 100644 index 000000000..f0d642a85 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/views/InstantItemSwitch.kt @@ -0,0 +1,67 @@ +package com.simplemobiletools.gallery.views + +import android.content.Context +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.ViewGroup +import android.widget.RelativeLayout +import com.simplemobiletools.gallery.helpers.CLICK_MAX_DURATION +import com.simplemobiletools.gallery.helpers.DRAG_THRESHOLD + +// handle only one finger clicks, pass other events to the parent view and ignore it when received again +class InstantItemSwitch(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) { + private var mTouchDownTime = 0L + private var mTouchDownX = 0f + private var mTouchDownY = 0f + private var passTouches = false + + var parentView: ViewGroup? = null + + override fun dispatchTouchEvent(ev: MotionEvent): Boolean { + if (passTouches) { + if (ev.actionMasked == MotionEvent.ACTION_DOWN) { + passTouches = false + } + return false + } + return super.dispatchTouchEvent(ev) + } + + override fun onTouchEvent(event: MotionEvent): Boolean { + if (passTouches) { + return false + } + + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + mTouchDownX = event.x + mTouchDownY = event.y + mTouchDownTime = System.currentTimeMillis() + } + MotionEvent.ACTION_UP -> { + if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { + performClick() + } + } + MotionEvent.ACTION_MOVE -> { + if (passTouches) { + return false + } + + val diffX = mTouchDownX - event.x + val diffY = mTouchDownY - event.y + if (Math.abs(diffX) > DRAG_THRESHOLD || Math.abs(diffY) > DRAG_THRESHOLD) { + if (!passTouches) { + event.action = MotionEvent.ACTION_DOWN + event.setLocation(event.rawX, event.y) + parentView?.dispatchTouchEvent(event) + } + passTouches = true + parentView?.dispatchTouchEvent(event) + return false + } + } + } + return true + } +} diff --git a/app/src/main/res/layout/pager_photo_item.xml b/app/src/main/res/layout/pager_photo_item.xml index 9a82b9b16..095783b21 100644 --- a/app/src/main/res/layout/pager_photo_item.xml +++ b/app/src/main/res/layout/pager_photo_item.xml @@ -35,8 +35,8 @@ android:id="@+id/photo_brightness_controller" android:layout_width="@dimen/media_side_slider_width" android:layout_height="match_parent" - android:layout_alignParentStart="true" - android:layout_alignParentLeft="true"/> + android:layout_alignParentLeft="true" + android:layout_alignParentStart="true"/> - -