mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-19 00:59:17 +01:00
Changes to viewpager readers
This commit is contained in:
parent
e2795f5480
commit
273527e7fb
@ -57,6 +57,11 @@ public abstract class BaseReader {
|
||||
}
|
||||
}
|
||||
|
||||
public void onPageChanged(int position) {
|
||||
currentPosition = position;
|
||||
updatePageNumber();
|
||||
}
|
||||
|
||||
public void destroy() {}
|
||||
|
||||
public abstract int getTotalPages();
|
||||
|
@ -0,0 +1,6 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.common;
|
||||
|
||||
public interface OnChapterBoundariesOutListener {
|
||||
void onFirstPageOutEvent();
|
||||
void onLastPageOutEvent();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.common;
|
||||
|
||||
public interface OnChapterSingleTapListener {
|
||||
void onCenterTap();
|
||||
void onLeftSideTap();
|
||||
void onRightSideTap();
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.common;
|
||||
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
public class ViewPagerGestureListener extends GestureDetector.SimpleOnGestureListener {
|
||||
|
||||
private ViewPagerInterface viewPager;
|
||||
|
||||
private static final float LEFT_REGION = 0.33f;
|
||||
private static final float RIGHT_REGION = 0.66f;
|
||||
|
||||
public ViewPagerGestureListener(ViewPagerInterface viewPager) {
|
||||
this.viewPager = viewPager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSingleTapConfirmed(MotionEvent e) {
|
||||
final int position = viewPager.getCurrentItem();
|
||||
final float positionX = e.getX();
|
||||
|
||||
if (positionX < viewPager.getWidth() * LEFT_REGION) {
|
||||
if (position != 0) {
|
||||
onLeftSideTap();
|
||||
} else {
|
||||
onFirstPageOut();
|
||||
}
|
||||
} else if (positionX > viewPager.getWidth() * RIGHT_REGION) {
|
||||
if (position != viewPager.getAdapter().getCount() - 1) {
|
||||
onRightSideTap();
|
||||
} else {
|
||||
onLastPageOut();
|
||||
}
|
||||
} else {
|
||||
onCenterTap();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void onLeftSideTap() {
|
||||
if (viewPager.getChapterSingleTapListener() != null) {
|
||||
viewPager.getChapterSingleTapListener().onLeftSideTap();
|
||||
}
|
||||
}
|
||||
|
||||
private void onRightSideTap() {
|
||||
if (viewPager.getChapterSingleTapListener() != null) {
|
||||
viewPager.getChapterSingleTapListener().onRightSideTap();
|
||||
}
|
||||
}
|
||||
|
||||
private void onCenterTap() {
|
||||
if (viewPager.getChapterSingleTapListener() != null) {
|
||||
viewPager.getChapterSingleTapListener().onCenterTap();
|
||||
}
|
||||
}
|
||||
|
||||
private void onFirstPageOut() {
|
||||
if (viewPager.getChapterBoundariesListener() != null) {
|
||||
viewPager.getChapterBoundariesListener().onFirstPageOutEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private void onLastPageOut() {
|
||||
if (viewPager.getChapterBoundariesListener() != null) {
|
||||
viewPager.getChapterBoundariesListener().onLastPageOutEvent();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.common;
|
||||
|
||||
import android.support.v4.view.PagerAdapter;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
public interface ViewPagerInterface {
|
||||
|
||||
void setOffscreenPageLimit(int limit);
|
||||
|
||||
int getCurrentItem();
|
||||
void setCurrentItem(int item, boolean smoothScroll);
|
||||
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
|
||||
PagerAdapter getAdapter();
|
||||
void setAdapter(PagerAdapter adapter);
|
||||
|
||||
boolean onImageTouch(MotionEvent motionEvent);
|
||||
|
||||
void setOnChapterBoundariesOutListener(OnChapterBoundariesOutListener listener);
|
||||
void setOnChapterSingleTapListener(OnChapterSingleTapListener listener);
|
||||
|
||||
OnChapterBoundariesOutListener getChapterBoundariesListener();
|
||||
OnChapterSingleTapListener getChapterSingleTapListener();
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.common;
|
||||
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import eu.kanade.mangafeed.data.source.model.Page;
|
||||
import eu.kanade.mangafeed.ui.reader.ReaderActivity;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.base.BaseReader;
|
||||
import rx.Subscription;
|
||||
|
||||
public abstract class ViewPagerReader extends BaseReader {
|
||||
|
||||
protected ViewPagerReaderAdapter adapter;
|
||||
protected ViewPagerInterface viewPager;
|
||||
|
||||
protected boolean transitions;
|
||||
protected Subscription transitionsSubscription;
|
||||
|
||||
public ViewPagerReader(ReaderActivity activity) {
|
||||
super(activity);
|
||||
|
||||
transitionsSubscription = activity.getPreferences().enableTransitions().asObservable()
|
||||
.subscribe(value -> transitions = value);
|
||||
}
|
||||
|
||||
protected void initializeViewPager() {
|
||||
viewPager.setOffscreenPageLimit(2);
|
||||
viewPager.setOnChapterBoundariesOutListener(new OnChapterBoundariesOutListener() {
|
||||
@Override
|
||||
public void onFirstPageOutEvent() {
|
||||
onFirstPageOut();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLastPageOutEvent() {
|
||||
onLastPageOut();
|
||||
}
|
||||
});
|
||||
viewPager.setOnChapterSingleTapListener(new OnChapterSingleTapListener() {
|
||||
@Override
|
||||
public void onCenterTap() {
|
||||
activity.onCenterSingleTap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeftSideTap() {
|
||||
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, transitions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRightSideTap() {
|
||||
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, transitions);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalPages() {
|
||||
return adapter.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedPage(int pageNumber) {
|
||||
viewPager.setCurrentItem(getCurrentPageIndex(pageNumber), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onImageTouch(MotionEvent motionEvent) {
|
||||
return viewPager.onImageTouch(motionEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageListReady(List<Page> pages) {
|
||||
currentPosition = 0;
|
||||
adapter = new ViewPagerReaderAdapter(activity.getSupportFragmentManager(), pages);
|
||||
viewPager.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@CallSuper
|
||||
public void destroy() {
|
||||
transitionsSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
public abstract void onFirstPageOut();
|
||||
public abstract void onLastPageOut();
|
||||
|
||||
}
|
@ -1,100 +1,26 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.horizontal;
|
||||
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import eu.kanade.mangafeed.R;
|
||||
import eu.kanade.mangafeed.data.source.model.Page;
|
||||
import eu.kanade.mangafeed.ui.reader.ReaderActivity;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.base.BaseReader;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerReaderAdapter;
|
||||
import rx.Subscription;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerInterface;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerReader;
|
||||
|
||||
public abstract class HorizontalReader extends BaseReader {
|
||||
|
||||
@Bind(R.id.view_pager) HorizontalViewPager viewPager;
|
||||
|
||||
protected ViewPagerReaderAdapter adapter;
|
||||
|
||||
private boolean transitions;
|
||||
private Subscription transitionsSubscription;
|
||||
public abstract class HorizontalReader extends ViewPagerReader {
|
||||
|
||||
public HorizontalReader(ReaderActivity activity) {
|
||||
super(activity);
|
||||
activity.getLayoutInflater().inflate(R.layout.reader_horizontal, container);
|
||||
ButterKnife.bind(this, container);
|
||||
|
||||
transitionsSubscription = activity.getPreferences().enableTransitions().asObservable()
|
||||
.subscribe(value -> transitions = value);
|
||||
|
||||
viewPager.setOffscreenPageLimit(2);
|
||||
viewPager.addOnPageChangeListener(new HorizontalViewPager.SimpleOnPageChangeListener() {
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
currentPosition = position;
|
||||
updatePageNumber();
|
||||
}
|
||||
});
|
||||
viewPager.setOnChapterBoundariesOutListener(new HorizontalViewPager.OnChapterBoundariesOutListener() {
|
||||
@Override
|
||||
public void onFirstPageOutEvent() {
|
||||
onFirstPageOut();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLastPageOutEvent() {
|
||||
onLastPageOut();
|
||||
}
|
||||
});
|
||||
viewPager.setOnChapterSingleTapListener(new HorizontalViewPager.OnChapterSingleTapListener() {
|
||||
@Override
|
||||
public void onCenterTap() {
|
||||
activity.onCenterSingleTap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeftSideTap() {
|
||||
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, transitions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRightSideTap() {
|
||||
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, transitions);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalPages() {
|
||||
return adapter.getCount();
|
||||
viewPager = (ViewPagerInterface) container.findViewById(R.id.view_pager);
|
||||
initializeViewPager();
|
||||
((HorizontalViewPager) viewPager).addOnPageChangeListener(new PageChangeListener());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedPage(int pageNumber) {
|
||||
viewPager.setCurrentItem(getCurrentPageIndex(pageNumber), false);
|
||||
private class PageChangeListener extends HorizontalViewPager.SimpleOnPageChangeListener {
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
onPageChanged(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageListReady(List<Page> pages) {
|
||||
currentPosition = 0;
|
||||
adapter = new ViewPagerReaderAdapter(activity.getSupportFragmentManager(), pages);
|
||||
viewPager.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onImageTouch(MotionEvent motionEvent) {
|
||||
return viewPager.onImageTouch(motionEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
transitionsSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
public abstract void onFirstPageOut();
|
||||
public abstract void onLastPageOut();
|
||||
|
||||
}
|
||||
|
@ -6,137 +6,100 @@ import android.util.AttributeSet;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
public class HorizontalViewPager extends ViewPager {
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.OnChapterBoundariesOutListener;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.OnChapterSingleTapListener;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerGestureListener;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerInterface;
|
||||
|
||||
public class HorizontalViewPager extends ViewPager implements ViewPagerInterface {
|
||||
|
||||
private GestureDetector gestureDetector;
|
||||
|
||||
private OnChapterBoundariesOutListener mOnChapterBoundariesOutListener;
|
||||
private OnChapterSingleTapListener mOnChapterSingleTapListener;
|
||||
private OnChapterBoundariesOutListener onChapterBoundariesOutListener;
|
||||
private OnChapterSingleTapListener onChapterSingleTapListener;
|
||||
|
||||
private static final float LEFT_REGION = 0.33f;
|
||||
private static final float RIGHT_REGION = 0.66f;
|
||||
private static final float SWIPE_TOLERANCE = 0.25f;
|
||||
private float startDragX;
|
||||
|
||||
public HorizontalViewPager(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public HorizontalViewPager(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
gestureDetector = new GestureDetector(getContext(), new ReaderViewGestureListener());
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
gestureDetector = new GestureDetector(context, new ViewPagerGestureListener(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
try {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
||||
if (this.getCurrentItem() == 0 || this.getCurrentItem() == this.getAdapter().getCount() - 1) {
|
||||
startDragX = ev.getX();
|
||||
}
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
||||
if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1) {
|
||||
startDragX = ev.getX();
|
||||
}
|
||||
|
||||
return super.onInterceptTouchEvent(ev);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Do Nothing.
|
||||
}
|
||||
|
||||
return false;
|
||||
return super.onInterceptTouchEvent(ev);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
try {
|
||||
if (mOnChapterBoundariesOutListener != null) {
|
||||
if (this.getCurrentItem() == 0) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = ev.getX() - startDragX;
|
||||
if (onChapterBoundariesOutListener != null) {
|
||||
if (getCurrentItem() == 0) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = ev.getX() - startDragX;
|
||||
|
||||
if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
|
||||
mOnChapterBoundariesOutListener.onFirstPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
startDragX = 0;
|
||||
if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
|
||||
onChapterBoundariesOutListener.onFirstPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
} else if (this.getCurrentItem() == this.getAdapter().getCount() - 1) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = startDragX - ev.getX();
|
||||
|
||||
if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
|
||||
mOnChapterBoundariesOutListener.onLastPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
startDragX = 0;
|
||||
}
|
||||
} else if (getCurrentItem() == getAdapter().getCount() - 1) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = startDragX - ev.getX();
|
||||
|
||||
startDragX = 0;
|
||||
if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
|
||||
onChapterBoundariesOutListener.onLastPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
startDragX = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return super.onTouchEvent(ev);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Do Nothing.
|
||||
}
|
||||
|
||||
return false;
|
||||
return super.onTouchEvent(ev);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onImageTouch(MotionEvent event) {
|
||||
return gestureDetector.onTouchEvent(event);
|
||||
}
|
||||
|
||||
public interface OnChapterBoundariesOutListener {
|
||||
void onFirstPageOutEvent();
|
||||
void onLastPageOutEvent();
|
||||
@Override
|
||||
public void setOnChapterBoundariesOutListener(OnChapterBoundariesOutListener listener) {
|
||||
onChapterBoundariesOutListener = listener;
|
||||
}
|
||||
|
||||
public interface OnChapterSingleTapListener {
|
||||
void onCenterTap();
|
||||
void onLeftSideTap();
|
||||
void onRightSideTap();
|
||||
@Override
|
||||
public void setOnChapterSingleTapListener(OnChapterSingleTapListener listener) {
|
||||
onChapterSingleTapListener = listener;
|
||||
}
|
||||
|
||||
public void setOnChapterBoundariesOutListener(OnChapterBoundariesOutListener onChapterBoundariesOutListener) {
|
||||
mOnChapterBoundariesOutListener = onChapterBoundariesOutListener;
|
||||
@Override
|
||||
public OnChapterBoundariesOutListener getChapterBoundariesListener() {
|
||||
return onChapterBoundariesOutListener;
|
||||
}
|
||||
|
||||
public void setOnChapterSingleTapListener(OnChapterSingleTapListener onChapterSingleTapListener) {
|
||||
mOnChapterSingleTapListener = onChapterSingleTapListener;
|
||||
}
|
||||
|
||||
|
||||
private class ReaderViewGestureListener extends GestureDetector.SimpleOnGestureListener {
|
||||
|
||||
@Override
|
||||
public boolean onSingleTapConfirmed(MotionEvent e) {
|
||||
final int position = getCurrentItem();
|
||||
final float positionX = e.getX();
|
||||
|
||||
if (positionX < getWidth() * LEFT_REGION) {
|
||||
if (position != 0) {
|
||||
if (mOnChapterSingleTapListener != null) {
|
||||
mOnChapterSingleTapListener.onLeftSideTap();
|
||||
}
|
||||
} else {
|
||||
if (mOnChapterBoundariesOutListener != null) {
|
||||
mOnChapterBoundariesOutListener.onFirstPageOutEvent();
|
||||
}
|
||||
}
|
||||
} else if (positionX > getWidth() * RIGHT_REGION) {
|
||||
if (position != getAdapter().getCount() - 1) {
|
||||
if (mOnChapterSingleTapListener != null) {
|
||||
mOnChapterSingleTapListener.onRightSideTap();
|
||||
}
|
||||
} else {
|
||||
if (mOnChapterBoundariesOutListener != null) {
|
||||
mOnChapterBoundariesOutListener.onLastPageOutEvent();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (mOnChapterSingleTapListener != null) {
|
||||
mOnChapterSingleTapListener.onCenterTap();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OnChapterSingleTapListener getChapterSingleTapListener() {
|
||||
return onChapterSingleTapListener;
|
||||
}
|
||||
|
||||
}
|
@ -1,97 +1,36 @@
|
||||
package eu.kanade.mangafeed.ui.reader.viewer.vertical;
|
||||
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import eu.kanade.mangafeed.R;
|
||||
import eu.kanade.mangafeed.data.source.model.Page;
|
||||
import eu.kanade.mangafeed.ui.reader.ReaderActivity;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.base.BaseReader;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerReaderAdapter;
|
||||
import rx.Subscription;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerInterface;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerReader;
|
||||
|
||||
public class VerticalReader extends BaseReader {
|
||||
|
||||
@Bind(R.id.view_pager) VerticalViewPager viewPager;
|
||||
|
||||
private ViewPagerReaderAdapter adapter;
|
||||
|
||||
private boolean transitions;
|
||||
private Subscription transitionsSubscription;
|
||||
public class VerticalReader extends ViewPagerReader {
|
||||
|
||||
public VerticalReader(ReaderActivity activity) {
|
||||
super(activity);
|
||||
activity.getLayoutInflater().inflate(R.layout.reader_vertical, container);
|
||||
ButterKnife.bind(this, container);
|
||||
|
||||
transitionsSubscription = activity.getPreferences().enableTransitions().asObservable()
|
||||
.subscribe(value -> transitions = value);
|
||||
|
||||
viewPager.setOffscreenPageLimit(2);
|
||||
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
currentPosition = position;
|
||||
updatePageNumber();
|
||||
}
|
||||
});
|
||||
viewPager.setOnChapterBoundariesOutListener(new VerticalViewPager.OnChapterBoundariesOutListener() {
|
||||
@Override
|
||||
public void onFirstPageOutEvent() {
|
||||
requestPreviousChapter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLastPageOutEvent() {
|
||||
requestNextChapter();
|
||||
}
|
||||
});
|
||||
viewPager.setOnChapterSingleTapListener(new VerticalViewPager.OnChapterSingleTapListener() {
|
||||
@Override
|
||||
public void onCenterTap() {
|
||||
activity.onCenterSingleTap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeftSideTap() {
|
||||
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, transitions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRightSideTap() {
|
||||
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, transitions);
|
||||
}
|
||||
});
|
||||
viewPager = (ViewPagerInterface) container.findViewById(R.id.view_pager);
|
||||
initializeViewPager();
|
||||
((VerticalViewPager) viewPager).addOnPageChangeListener(new PageChangeListener());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalPages() {
|
||||
return adapter.getCount();
|
||||
public void onFirstPageOut() {
|
||||
requestPreviousChapter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedPage(int pageNumber) {
|
||||
viewPager.setCurrentItem(getCurrentPageIndex(pageNumber), false);
|
||||
public void onLastPageOut() {
|
||||
requestNextChapter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageListReady(List<Page> pages) {
|
||||
currentPosition = 0;
|
||||
adapter = new ViewPagerReaderAdapter(activity.getSupportFragmentManager(), pages);
|
||||
viewPager.setAdapter(adapter);
|
||||
private class PageChangeListener extends VerticalViewPagerImpl.SimpleOnPageChangeListener {
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
onPageChanged(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onImageTouch(MotionEvent motionEvent) {
|
||||
return viewPager.onImageTouch(motionEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
transitionsSubscription.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
@ -5,135 +5,115 @@ import android.util.AttributeSet;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
public class VerticalViewPager extends fr.castorflex.android.verticalviewpager.VerticalViewPager {
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.OnChapterBoundariesOutListener;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.OnChapterSingleTapListener;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerGestureListener;
|
||||
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerInterface;
|
||||
|
||||
public class VerticalViewPager extends VerticalViewPagerImpl implements ViewPagerInterface {
|
||||
|
||||
private GestureDetector gestureDetector;
|
||||
|
||||
private OnChapterBoundariesOutListener mOnChapterBoundariesOutListener;
|
||||
private OnChapterSingleTapListener mOnChapterSingleTapListener;
|
||||
private OnChapterBoundariesOutListener onChapterBoundariesOutListener;
|
||||
private OnChapterSingleTapListener onChapterSingleTapListener;
|
||||
|
||||
private static final float LEFT_REGION = 0.33f;
|
||||
private static final float RIGHT_REGION = 0.66f;
|
||||
private static final float SWIPE_TOLERANCE = 0.25f;
|
||||
private float startDragY;
|
||||
|
||||
public VerticalViewPager(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public VerticalViewPager(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
gestureDetector = new GestureDetector(getContext(), new ReaderViewGestureListener());
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
gestureDetector = new GestureDetector(context, new VerticalViewPagerGestureListener(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
try {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
||||
if (this.getCurrentItem() == 0 || this.getCurrentItem() == this.getAdapter().getCount() - 1) {
|
||||
startDragY = ev.getY();
|
||||
}
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
|
||||
if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1) {
|
||||
startDragY = ev.getY();
|
||||
}
|
||||
|
||||
return super.onInterceptTouchEvent(ev);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Do Nothing.
|
||||
}
|
||||
|
||||
return false;
|
||||
return super.onInterceptTouchEvent(ev);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
try {
|
||||
if (mOnChapterBoundariesOutListener != null) {
|
||||
if (this.getCurrentItem() == 0) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = ev.getY() - startDragY;
|
||||
if (onChapterBoundariesOutListener != null) {
|
||||
if (getCurrentItem() == 0) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = ev.getY() - startDragY;
|
||||
|
||||
if (ev.getY() > startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
|
||||
mOnChapterBoundariesOutListener.onFirstPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
startDragY = 0;
|
||||
if (ev.getY() > startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
|
||||
onChapterBoundariesOutListener.onFirstPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
} else if (this.getCurrentItem() == this.getAdapter().getCount() - 1) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = startDragY - ev.getY();
|
||||
|
||||
if (ev.getY() < startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
|
||||
mOnChapterBoundariesOutListener.onLastPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
startDragY = 0;
|
||||
}
|
||||
} else if (getCurrentItem() == getAdapter().getCount() - 1) {
|
||||
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
|
||||
float displacement = startDragY - ev.getY();
|
||||
|
||||
startDragY = 0;
|
||||
if (ev.getY() < startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
|
||||
onChapterBoundariesOutListener.onLastPageOutEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
startDragY = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return super.onTouchEvent(ev);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Do Nothing.
|
||||
}
|
||||
|
||||
return false;
|
||||
return super.onTouchEvent(ev);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onImageTouch(MotionEvent event) {
|
||||
return gestureDetector.onTouchEvent(event);
|
||||
}
|
||||
|
||||
public interface OnChapterBoundariesOutListener {
|
||||
void onFirstPageOutEvent();
|
||||
void onLastPageOutEvent();
|
||||
@Override
|
||||
public void setOnChapterBoundariesOutListener(OnChapterBoundariesOutListener listener) {
|
||||
onChapterBoundariesOutListener = listener;
|
||||
}
|
||||
|
||||
public interface OnChapterSingleTapListener {
|
||||
void onCenterTap();
|
||||
void onLeftSideTap();
|
||||
void onRightSideTap();
|
||||
@Override
|
||||
public void setOnChapterSingleTapListener(OnChapterSingleTapListener listener) {
|
||||
onChapterSingleTapListener = listener;
|
||||
}
|
||||
|
||||
public void setOnChapterBoundariesOutListener(OnChapterBoundariesOutListener onChapterBoundariesOutListener) {
|
||||
mOnChapterBoundariesOutListener = onChapterBoundariesOutListener;
|
||||
@Override
|
||||
public OnChapterBoundariesOutListener getChapterBoundariesListener() {
|
||||
return onChapterBoundariesOutListener;
|
||||
}
|
||||
|
||||
public void setOnChapterSingleTapListener(OnChapterSingleTapListener onChapterSingleTapListener) {
|
||||
mOnChapterSingleTapListener = onChapterSingleTapListener;
|
||||
@Override
|
||||
public OnChapterSingleTapListener getChapterSingleTapListener() {
|
||||
return onChapterSingleTapListener;
|
||||
}
|
||||
|
||||
private class ReaderViewGestureListener extends GestureDetector.SimpleOnGestureListener {
|
||||
private class VerticalViewPagerGestureListener extends ViewPagerGestureListener {
|
||||
|
||||
@Override
|
||||
public boolean onSingleTapConfirmed(MotionEvent e) {
|
||||
final int position = getCurrentItem();
|
||||
final float positionX = e.getX();
|
||||
|
||||
if (positionX < getWidth() * LEFT_REGION) {
|
||||
if (position != 0) {
|
||||
if (mOnChapterSingleTapListener != null) {
|
||||
mOnChapterSingleTapListener.onLeftSideTap();
|
||||
}
|
||||
} else {
|
||||
if (mOnChapterBoundariesOutListener != null) {
|
||||
mOnChapterBoundariesOutListener.onFirstPageOutEvent();
|
||||
}
|
||||
}
|
||||
} else if (positionX > getWidth() * RIGHT_REGION) {
|
||||
if (position != getAdapter().getCount() - 1) {
|
||||
if (mOnChapterSingleTapListener != null) {
|
||||
mOnChapterSingleTapListener.onRightSideTap();
|
||||
}
|
||||
} else {
|
||||
if (mOnChapterBoundariesOutListener != null) {
|
||||
mOnChapterBoundariesOutListener.onLastPageOutEvent();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (mOnChapterSingleTapListener != null) {
|
||||
mOnChapterSingleTapListener.onCenterTap();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
public VerticalViewPagerGestureListener(ViewPagerInterface viewPager) {
|
||||
super(viewPager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onDown(MotionEvent e) {
|
||||
// Vertical view pager ignores scrolling events sometimes.
|
||||
// Returning true here fixes it, but we lose touch events on the image like
|
||||
// double tap to zoom
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -28,6 +28,6 @@
|
||||
<color name="line_grey">@color/md_light_dividers</color>
|
||||
<color name="light_grey">@color/md_grey_300</color>
|
||||
<color name="page_number_background">#AAE9E9E9</color>
|
||||
<color name="page_number_background_black">#AA252525</color>
|
||||
<color name="page_number_background_black">#99252525</color>
|
||||
<color name="reader_menu_background">@color/colorPrimarySuperDark</color>
|
||||
</resources>
|
Loading…
Reference in New Issue
Block a user