From 26b283d44d869e52ac8ea08faedaf170fce67fad Mon Sep 17 00:00:00 2001 From: len Date: Sat, 10 Dec 2016 14:01:16 +0100 Subject: [PATCH] Fix webtoon reader touch events. #561 --- .../reader/viewer/webtoon/WebtoonAdapter.kt | 7 +- .../ui/reader/viewer/webtoon/WebtoonHolder.kt | 4 +- .../ui/reader/viewer/webtoon/WebtoonReader.kt | 78 ++++++++++++------- 3 files changed, 56 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt index 697f92d5c..145d76ad0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt @@ -22,7 +22,12 @@ class WebtoonAdapter(val fragment: WebtoonReader) : RecyclerView.Adapter fragment.gestureDetector.onTouchEvent(ev) } + val imageTouchListener = View.OnTouchListener { v, ev -> fragment.imageGestureDetector.onTouchEvent(ev) } + + /** + * Touch listener for the other views. + */ + val viewTouchListener = View.OnTouchListener { v, ev -> fragment.viewGestureDetector.onTouchEvent(ev) } /** * Returns the number of pages. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonHolder.kt index c06d9d440..fd3008da2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonHolder.kt @@ -63,7 +63,7 @@ class WebtoonHolder(private val view: View, private val adapter: WebtoonAdapter) setRegionDecoderClass(webtoonReader.regionDecoderClass) setBitmapDecoderClass(webtoonReader.bitmapDecoderClass) setVerticalScrollingParent(true) - setOnTouchListener(adapter.touchListener) + setOnTouchListener(adapter.imageTouchListener) setOnImageEventListener(object : SubsamplingScaleImageView.DefaultOnImageEventListener() { override fun onReady() { onImageDecoded() @@ -77,7 +77,7 @@ class WebtoonHolder(private val view: View, private val adapter: WebtoonAdapter) view.progress_container.minimumHeight = view.resources.displayMetrics.heightPixels * 2 - view.setOnTouchListener(adapter.touchListener) + view.setOnTouchListener(adapter.viewTouchListener) view.retry_button.setOnTouchListener { v, event -> if (event.action == MotionEvent.ACTION_UP) { readerActivity.presenter.retryPage(page) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt index f3e65e2f3..20e6a0a2b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt @@ -55,9 +55,14 @@ class WebtoonReader : BaseReader() { private set /** - * Gesture detector for touch events. + * Gesture detector for image touch events. */ - val gestureDetector by lazy { createGestureDetector() } + val imageGestureDetector by lazy { GestureDetector(context, ImageGestureListener()) } + + /** + * Gesture detector for other views touch events. + */ + val viewGestureDetector by lazy { GestureDetector(context, ViewGestureListener()) } /** * Subscriptions used while the view exists. @@ -122,39 +127,52 @@ class WebtoonReader : BaseReader() { } /** - * Creates the gesture detector for the reader. - * - * @return a gesture detector. + * Gesture detector for Subsampling Scale Image View. [onDown] needs to be false or else + * double tap to zoom doesn't work. */ - protected fun createGestureDetector(): GestureDetector { - return GestureDetector(context, object : SimpleOnGestureListener() { - override fun onSingleTapConfirmed(e: MotionEvent): Boolean { - if (isAdded) { - val positionX = e.x + inner open class ImageGestureListener : SimpleOnGestureListener() { - if (positionX < recycler.width * LEFT_REGION) { - if (tappingEnabled) moveToPrevious() - } else if (positionX > recycler.width * RIGHT_REGION) { - if (tappingEnabled) moveToNext() - } else { - readerActivity.toggleMenu() - } - } - return true - } + override fun onDown(e: MotionEvent): Boolean { + return false + } - override fun onLongPress(e: MotionEvent) { - if (isAdded) { - val child = recycler.findChildViewUnder(e.rawX, e.rawY) - val position = recycler.getChildAdapterPosition(child) - val page = adapter.pages?.getOrNull(position) - if (page != null) - readerActivity.onLongPress(page) - else - context.toast(getString(R.string.unknown_error)) + override fun onSingleTapConfirmed(e: MotionEvent): Boolean { + if (isAdded) { + val positionX = e.x + + if (positionX < recycler.width * LEFT_REGION) { + if (tappingEnabled) moveToPrevious() + } else if (positionX > recycler.width * RIGHT_REGION) { + if (tappingEnabled) moveToNext() + } else { + readerActivity.toggleMenu() } } - }) + return true + } + + override fun onLongPress(e: MotionEvent) { + if (isAdded) { + val child = recycler.findChildViewUnder(e.rawX, e.rawY) + val position = recycler.getChildAdapterPosition(child) + val page = adapter.pages?.getOrNull(position) + if (page != null) + readerActivity.onLongPress(page) + else + context.toast(getString(R.string.unknown_error)) + } + } + } + + /** + * Gesture detector for other views. [onDown] needs to be true here or it will incorrectly + * detect events. + */ + inner class ViewGestureListener : ImageGestureListener() { + + override fun onDown(e: MotionEvent): Boolean { + return true + } } /**