From 0c0ebe06e57bfa83eaceaa2f3e3c1e8616f356af Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 10 Feb 2016 15:06:18 +0100 Subject: [PATCH] Volume keys scroll pages. Closes #95 --- .../tachiyomi/ui/reader/ReaderActivity.java | 19 +++++++++++++++++ .../ui/reader/viewer/base/BaseReader.java | 2 ++ .../ui/reader/viewer/pager/PagerReader.java | 18 +++++++++++----- .../pager/horizontal/RightToLeftReader.java | 4 ++-- .../reader/viewer/webtoon/WebtoonReader.java | 21 ++++++++++++++++--- 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java index baffb9397..4f960e096 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java @@ -10,6 +10,7 @@ import android.support.annotation.NonNull; import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v7.widget.Toolbar; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.Surface; @@ -153,6 +154,24 @@ public class ReaderActivity extends BaseRxActivity { } } + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + int action = event.getAction(); + int keyCode = event.getKeyCode(); + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_DOWN: + if (action == KeyEvent.ACTION_UP && viewer != null) + viewer.moveToNext(); + return true; + case KeyEvent.KEYCODE_VOLUME_UP: + if (action == KeyEvent.ACTION_UP && viewer != null) + viewer.moveToPrevious(); + return true; + default: + return super.dispatchKeyEvent(event); + } + } + public void onChapterError() { finish(); ToastUtil.showShort(this, R.string.page_list_error); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/base/BaseReader.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/base/BaseReader.java index 3a9a29e32..4167c0143 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/base/BaseReader.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/base/BaseReader.java @@ -93,6 +93,8 @@ public abstract class BaseReader extends BaseFragment { public abstract void setSelectedPage(int pageNumber); public abstract void onSetChapter(Chapter chapter, Page currentPage); public abstract void onAppendChapter(Chapter chapter); + public abstract void moveToNext(); + public abstract void moveToPrevious(); public void setDecoderClass(int value) { switch (value) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java index 86d8b3e04..13fa73850 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerReader.java @@ -148,14 +148,14 @@ public abstract class PagerReader extends BaseReader { } protected void onLeftSideTap() { - if (pager.getCurrentItem() != 0) { - pager.setCurrentItem(pager.getCurrentItem() - 1, transitions); - } else { - getReaderActivity().requestPreviousChapter(); - } + moveToPrevious(); } protected void onRightSideTap() { + moveToNext(); + } + + public void moveToNext() { if (pager.getCurrentItem() != pager.getAdapter().getCount() - 1) { pager.setCurrentItem(pager.getCurrentItem() + 1, transitions); } else { @@ -163,6 +163,14 @@ public abstract class PagerReader extends BaseReader { } } + public void moveToPrevious() { + if (pager.getCurrentItem() != 0) { + pager.setCurrentItem(pager.getCurrentItem() - 1, transitions); + } else { + getReaderActivity().requestPreviousChapter(); + } + } + private void setImageScaleType(int scaleType) { this.scaleType = scaleType; } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/horizontal/RightToLeftReader.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/horizontal/RightToLeftReader.java index 3ae824648..2f74201bd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/horizontal/RightToLeftReader.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/horizontal/RightToLeftReader.java @@ -19,12 +19,12 @@ public class RightToLeftReader extends PagerReader { @Override protected void onLeftSideTap() { - super.onRightSideTap(); + moveToNext(); } @Override protected void onRightSideTap() { - super.onLeftSideTap(); + moveToPrevious(); } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java index 0d987184d..7cda3d155 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.java @@ -69,9 +69,9 @@ public class WebtoonReader extends BaseReader { final float positionX = e.getX(); if (positionX < recycler.getWidth() * LEFT_REGION) { - recycler.smoothScrollBy(0, -scrollDistance); + moveToPrevious(); } else if (positionX > recycler.getWidth() * RIGHT_REGION) { - recycler.smoothScrollBy(0, scrollDistance); + moveToNext(); } else { getReaderActivity().onCenterSingleTap(); } @@ -113,6 +113,16 @@ public class WebtoonReader extends BaseReader { recycler.scrollToPosition(pageNumber); } + @Override + public void moveToNext() { + recycler.smoothScrollBy(0, scrollDistance); + } + + @Override + public void moveToPrevious() { + recycler.smoothScrollBy(0, -scrollDistance); + } + @Override public void onSetChapter(Chapter chapter, Page currentPage) { pages = new ArrayList<>(chapter.getPages()); @@ -134,6 +144,9 @@ public class WebtoonReader extends BaseReader { if (recycler != null) { adapter.setPages(pages); adapter.notifyItemRangeInserted(insertStart, chapter.getPages().size()); + if (subscription != null && subscription.isUnsubscribed()) { + observeStatus(insertStart); + } } } @@ -162,8 +175,10 @@ public class WebtoonReader extends BaseReader { } private void observeStatus(int position) { - if (position == pages.size()) + if (position == pages.size()) { + unsubscribeStatus(); return; + } final Page page = pages.get(position);