Volume keys scroll pages. Closes #95

This commit is contained in:
inorichi 2016-02-10 15:06:18 +01:00
parent e50c683159
commit 0c0ebe06e5
5 changed files with 54 additions and 10 deletions

View file

@ -10,6 +10,7 @@ import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.Surface; import android.view.Surface;
@ -153,6 +154,24 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
} }
} }
@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() { public void onChapterError() {
finish(); finish();
ToastUtil.showShort(this, R.string.page_list_error); ToastUtil.showShort(this, R.string.page_list_error);

View file

@ -93,6 +93,8 @@ public abstract class BaseReader extends BaseFragment {
public abstract void setSelectedPage(int pageNumber); public abstract void setSelectedPage(int pageNumber);
public abstract void onSetChapter(Chapter chapter, Page currentPage); public abstract void onSetChapter(Chapter chapter, Page currentPage);
public abstract void onAppendChapter(Chapter chapter); public abstract void onAppendChapter(Chapter chapter);
public abstract void moveToNext();
public abstract void moveToPrevious();
public void setDecoderClass(int value) { public void setDecoderClass(int value) {
switch (value) { switch (value) {

View file

@ -148,14 +148,14 @@ public abstract class PagerReader extends BaseReader {
} }
protected void onLeftSideTap() { protected void onLeftSideTap() {
if (pager.getCurrentItem() != 0) { moveToPrevious();
pager.setCurrentItem(pager.getCurrentItem() - 1, transitions);
} else {
getReaderActivity().requestPreviousChapter();
}
} }
protected void onRightSideTap() { protected void onRightSideTap() {
moveToNext();
}
public void moveToNext() {
if (pager.getCurrentItem() != pager.getAdapter().getCount() - 1) { if (pager.getCurrentItem() != pager.getAdapter().getCount() - 1) {
pager.setCurrentItem(pager.getCurrentItem() + 1, transitions); pager.setCurrentItem(pager.getCurrentItem() + 1, transitions);
} else { } 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) { private void setImageScaleType(int scaleType) {
this.scaleType = scaleType; this.scaleType = scaleType;
} }

View file

@ -19,12 +19,12 @@ public class RightToLeftReader extends PagerReader {
@Override @Override
protected void onLeftSideTap() { protected void onLeftSideTap() {
super.onRightSideTap(); moveToNext();
} }
@Override @Override
protected void onRightSideTap() { protected void onRightSideTap() {
super.onLeftSideTap(); moveToPrevious();
} }
} }

View file

@ -69,9 +69,9 @@ public class WebtoonReader extends BaseReader {
final float positionX = e.getX(); final float positionX = e.getX();
if (positionX < recycler.getWidth() * LEFT_REGION) { if (positionX < recycler.getWidth() * LEFT_REGION) {
recycler.smoothScrollBy(0, -scrollDistance); moveToPrevious();
} else if (positionX > recycler.getWidth() * RIGHT_REGION) { } else if (positionX > recycler.getWidth() * RIGHT_REGION) {
recycler.smoothScrollBy(0, scrollDistance); moveToNext();
} else { } else {
getReaderActivity().onCenterSingleTap(); getReaderActivity().onCenterSingleTap();
} }
@ -113,6 +113,16 @@ public class WebtoonReader extends BaseReader {
recycler.scrollToPosition(pageNumber); recycler.scrollToPosition(pageNumber);
} }
@Override
public void moveToNext() {
recycler.smoothScrollBy(0, scrollDistance);
}
@Override
public void moveToPrevious() {
recycler.smoothScrollBy(0, -scrollDistance);
}
@Override @Override
public void onSetChapter(Chapter chapter, Page currentPage) { public void onSetChapter(Chapter chapter, Page currentPage) {
pages = new ArrayList<>(chapter.getPages()); pages = new ArrayList<>(chapter.getPages());
@ -134,6 +144,9 @@ public class WebtoonReader extends BaseReader {
if (recycler != null) { if (recycler != null) {
adapter.setPages(pages); adapter.setPages(pages);
adapter.notifyItemRangeInserted(insertStart, chapter.getPages().size()); 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) { private void observeStatus(int position) {
if (position == pages.size()) if (position == pages.size()) {
unsubscribeStatus();
return; return;
}
final Page page = pages.get(position); final Page page = pages.get(position);