From d3c83f0bf2ef5c6e4d439ec540fa1b0595de135d Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 24 Oct 2015 16:04:42 +0200 Subject: [PATCH] Decouple viewer from the reader activity --- .../mangafeed/presenter/ReaderPresenter.java | 4 +- .../mangafeed/ui/activity/ReaderActivity.java | 94 +++++-------------- .../ui/fragment/ReaderPageFragment.java | 2 +- .../ui/viewer/LeftToRightViewer.java | 24 +++++ .../ui/viewer/RightToLeftViewer.java | 47 ++++++++++ .../mangafeed/ui/viewer/base/BaseViewer.java | 38 ++++++++ .../ui/viewer/base/ViewPagerViewer.java | 81 ++++++++++++++++ .../mangafeed/widget/ReaderViewPager.java | 12 +-- app/src/main/res/layout/activity_reader.xml | 6 +- app/src/main/res/layout/viewer_viewpager.xml | 7 ++ 10 files changed, 233 insertions(+), 82 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/viewer/LeftToRightViewer.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/viewer/RightToLeftViewer.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/ViewPagerViewer.java create mode 100644 app/src/main/res/layout/viewer_viewpager.xml diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java index c65533f65c..66cfda77fe 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -46,13 +46,13 @@ public class ReaderPresenter extends BasePresenter { .doOnNext(pages -> pageList = pages) .doOnCompleted(() -> start(GET_PAGE_IMAGES)), (view, pages) -> { - view.onPageList(pages); + view.onPageListReady(pages); }); restartableReplay(GET_PAGE_IMAGES, this::getPageImagesObservable, (view, page) -> { - view.onPageDownloaded(page); + view.onImageReady(page); if (page.getPageNumber() == savedSelectedPage) { view.setCurrentPage(savedSelectedPage); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java index 6df3e43b76..396a7d06c4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java @@ -2,11 +2,11 @@ package eu.kanade.mangafeed.ui.activity; import android.content.Context; import android.content.Intent; -import android.os.Build; import android.os.Bundle; -import android.support.v4.view.ViewPager; +import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; +import android.widget.FrameLayout; import android.widget.TextView; import java.util.List; @@ -17,18 +17,18 @@ import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.presenter.ReaderPresenter; import eu.kanade.mangafeed.ui.activity.base.BaseRxActivity; -import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; -import eu.kanade.mangafeed.widget.ReaderViewPager; +import eu.kanade.mangafeed.ui.viewer.LeftToRightViewer; +import eu.kanade.mangafeed.ui.viewer.base.BaseViewer; import nucleus.factory.RequiresPresenter; @RequiresPresenter(ReaderPresenter.class) public class ReaderActivity extends BaseRxActivity { - @Bind(R.id.view_pager) ReaderViewPager viewPager; @Bind(R.id.page_number) TextView pageNumber; + @Bind(R.id.viewer) FrameLayout container; - private ReaderPageAdapter adapter; private int currentPage; + private BaseViewer viewer; public static Intent newInstance(Context context) { return new Intent(context, ReaderActivity.class); @@ -40,8 +40,8 @@ public class ReaderActivity extends BaseRxActivity { setContentView(R.layout.activity_reader); ButterKnife.bind(this); - createAdapter(); - setupViewPager(); + viewer = new LeftToRightViewer(this, container); + enableHardwareAcceleration(); } @@ -51,76 +51,27 @@ public class ReaderActivity extends BaseRxActivity { super.onDestroy(); } - private void createAdapter() { - adapter = new ReaderPageAdapter(getSupportFragmentManager()); - viewPager.setAdapter(adapter); + public void onPageListReady(List pages) { + viewer.onPageListReady(pages); } - private void setupViewPager() { - viewPager.setOffscreenPageLimit(3); - viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { - @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - - } - - @Override - public void onPageSelected(int position) { - currentPage = position; - updatePageNumber(); - } - - @Override - public void onPageScrollStateChanged(int state) { - - } - }); - viewPager.setOnChapterBoundariesOutListener(new ReaderViewPager.OnChapterBoundariesOutListener() { - @Override - public void onFirstPageOut() { - // TODO load previous chapter - } - - @Override - public void onLastPageOut() { - // TODO load next chapter - } - }); + public void onImageReady(Page page) { + viewer.onImageReady(page); } - public ReaderViewPager getViewPager() { - return viewPager; - } - - - public void onPageList(List pages) { - adapter.setPages(pages); - updatePageNumber(); - } - - public void onPageDownloaded(Page page) { - adapter.replacePage(page.getPageNumber(), page); - } - - public void setCurrentPage(int position) { - viewPager.setCurrentItem(position); - } - - private void updatePageNumber() { - String page = (currentPage+1) + "/" + adapter.getCount(); + public void onPageChanged(int currentPage, int totalPages) { + String page = currentPage + "/" + totalPages; pageNumber.setText(page); } - public void hideStatusBar() { - if (Build.VERSION.SDK_INT < 16) { - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - } else { - View decorView = getWindow().getDecorView(); - int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; - decorView.setSystemUiVisibility(uiOptions); - } + public void setCurrentPage(int page) { + currentPage = page; + } + public void hideStatusBar() { + View decorView = getWindow().getDecorView(); + int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; + decorView.setSystemUiVisibility(uiOptions); } public void enableHardwareAcceleration() { @@ -129,4 +80,7 @@ public class ReaderActivity extends BaseRxActivity { WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); } + public boolean onImageTouch(MotionEvent motionEvent) { + return viewer.onImageTouch(motionEvent); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java index 1b3619b07a..1ff2e12484 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java @@ -71,7 +71,7 @@ public class ReaderPageFragment extends Fragment { imageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE); imageView.setOnTouchListener((v, motionEvent) -> - ((ReaderActivity) getActivity()).getViewPager().onImageTouch(motionEvent)); + ((ReaderActivity) getActivity()).onImageTouch(motionEvent)); progressBar.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/LeftToRightViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/LeftToRightViewer.java new file mode 100644 index 0000000000..41ef89933b --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/LeftToRightViewer.java @@ -0,0 +1,24 @@ +package eu.kanade.mangafeed.ui.viewer; + +import android.widget.FrameLayout; + +import eu.kanade.mangafeed.ui.activity.ReaderActivity; +import eu.kanade.mangafeed.ui.viewer.base.ViewPagerViewer; + +public class LeftToRightViewer extends ViewPagerViewer { + + public LeftToRightViewer(ReaderActivity activity, FrameLayout container) { + super(activity, container); + } + + @Override + public void onFirstPageOut() { + + } + + @Override + public void onLastPageOut() { + + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/RightToLeftViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/RightToLeftViewer.java new file mode 100644 index 0000000000..aab9bee741 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/RightToLeftViewer.java @@ -0,0 +1,47 @@ +package eu.kanade.mangafeed.ui.viewer; + +import android.widget.FrameLayout; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import eu.kanade.mangafeed.data.models.Page; +import eu.kanade.mangafeed.ui.activity.ReaderActivity; +import eu.kanade.mangafeed.ui.viewer.base.ViewPagerViewer; + +public class RightToLeftViewer extends ViewPagerViewer { + + public RightToLeftViewer(ReaderActivity activity, FrameLayout container) { + super(activity, container); + } + + @Override + public void onPageListReady(List pages) { + ArrayList inversedPages = new ArrayList<>(pages); + Collections.reverse(inversedPages); + adapter.setPages(inversedPages); + viewPager.setCurrentItem(adapter.getCount()-1); + } + + @Override + public int getCurrentPageFromPos(int position) { + return getTotalPages() - position; + } + + @Override + public int getPosFromPage(Page page) { + return getTotalPages() - (page.getPageNumber() + 1); + } + + @Override + public void onFirstPageOut() { + + } + + @Override + public void onLastPageOut() { + + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java new file mode 100644 index 0000000000..9c9a1d27e4 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java @@ -0,0 +1,38 @@ +package eu.kanade.mangafeed.ui.viewer.base; + +import android.view.MotionEvent; +import android.widget.FrameLayout; + +import java.util.List; + +import eu.kanade.mangafeed.data.models.Page; +import eu.kanade.mangafeed.ui.activity.ReaderActivity; + +public abstract class BaseViewer { + + protected ReaderActivity activity; + protected FrameLayout container; + protected int currentPosition; + + public BaseViewer(ReaderActivity activity, FrameLayout container) { + this.activity = activity; + this.container = container; + } + + public void updatePageNumber() { + activity.onPageChanged(getCurrentPageFromPos(currentPosition), getTotalPages()); + } + + public int getCurrentPageFromPos(int position) { + return position + 1; + } + + public int getPosFromPage(Page page) { + return page.getPageNumber(); + } + + public abstract int getTotalPages(); + public abstract void onImageReady(Page page); + public abstract void onPageListReady(List pages); + public abstract boolean onImageTouch(MotionEvent motionEvent); +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/ViewPagerViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/ViewPagerViewer.java new file mode 100644 index 0000000000..b74f46f98e --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/ViewPagerViewer.java @@ -0,0 +1,81 @@ +package eu.kanade.mangafeed.ui.viewer.base; + +import android.view.MotionEvent; +import android.widget.FrameLayout; + +import java.util.List; + +import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Page; +import eu.kanade.mangafeed.ui.activity.ReaderActivity; +import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; +import eu.kanade.mangafeed.widget.ReaderViewPager; + +public abstract class ViewPagerViewer extends BaseViewer { + + protected ReaderViewPager viewPager; + protected ReaderPageAdapter adapter; + + public ViewPagerViewer(ReaderActivity activity, FrameLayout container) { + super(activity, container); + activity.getLayoutInflater().inflate(R.layout.viewer_viewpager, container); + + adapter = new ReaderPageAdapter(activity.getSupportFragmentManager()); + viewPager = (ReaderViewPager) activity.findViewById(R.id.view_pager); + viewPager.setAdapter(adapter); + viewPager.setOffscreenPageLimit(3); + viewPager.addOnPageChangeListener(new ReaderViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + currentPosition = position; + updatePageNumber(); + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); + viewPager.setOnChapterBoundariesOutListener(new ReaderViewPager.OnChapterBoundariesOutListener() { + @Override + public void onFirstPageOutEvent() { + onFirstPageOut(); + } + + @Override + public void onLastPageOutEvent() { + onLastPageOut(); + } + }); + } + + @Override + public int getTotalPages() { + return adapter.getCount(); + } + + @Override + public void onPageListReady(List pages) { + adapter.setPages(pages); + updatePageNumber(); + } + + @Override + public void onImageReady(Page page) { + adapter.replacePage(getPosFromPage(page), page); + } + + @Override + public boolean onImageTouch(MotionEvent motionEvent) { + return viewPager.onImageTouch(motionEvent); + } + + public abstract void onFirstPageOut(); + public abstract void onLastPageOut(); + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/widget/ReaderViewPager.java b/app/src/main/java/eu/kanade/mangafeed/widget/ReaderViewPager.java index a8b9f88ade..ca5ab6c83f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/widget/ReaderViewPager.java +++ b/app/src/main/java/eu/kanade/mangafeed/widget/ReaderViewPager.java @@ -51,7 +51,7 @@ public class ReaderViewPager extends ViewPager { float displacement = ev.getX() - startDragX; if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) { - mOnChapterBoundariesOutListener.onFirstPageOut(); + mOnChapterBoundariesOutListener.onFirstPageOutEvent(); return true; } @@ -62,7 +62,7 @@ public class ReaderViewPager extends ViewPager { float displacement = startDragX - ev.getX(); if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) { - mOnChapterBoundariesOutListener.onLastPageOut(); + mOnChapterBoundariesOutListener.onLastPageOutEvent(); return true; } @@ -84,9 +84,9 @@ public class ReaderViewPager extends ViewPager { } public interface OnChapterBoundariesOutListener { - public void onFirstPageOut(); + public void onFirstPageOutEvent(); - public void onLastPageOut(); + public void onLastPageOutEvent(); } public interface OnChapterSingleTapListener { @@ -114,7 +114,7 @@ public class ReaderViewPager extends ViewPager { setCurrentItem(position - 1, true); } else { if (mOnChapterBoundariesOutListener != null) { - mOnChapterBoundariesOutListener.onFirstPageOut(); + mOnChapterBoundariesOutListener.onFirstPageOutEvent(); } } } else if (positionX > getWidth() * RIGHT_REGION) { @@ -122,7 +122,7 @@ public class ReaderViewPager extends ViewPager { setCurrentItem(position + 1, true); } else { if (mOnChapterBoundariesOutListener != null) { - mOnChapterBoundariesOutListener.onLastPageOut(); + mOnChapterBoundariesOutListener.onLastPageOutEvent(); } } } diff --git a/app/src/main/res/layout/activity_reader.xml b/app/src/main/res/layout/activity_reader.xml index c4ffa0a83e..e9b6ab91cb 100644 --- a/app/src/main/res/layout/activity_reader.xml +++ b/app/src/main/res/layout/activity_reader.xml @@ -3,11 +3,11 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - + + + \ No newline at end of file