Decouple viewer from the reader activity

This commit is contained in:
inorichi 2015-10-24 16:04:42 +02:00
parent ef7613f7ad
commit d3c83f0bf2
10 changed files with 233 additions and 82 deletions

View file

@ -46,13 +46,13 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
.doOnNext(pages -> pageList = pages) .doOnNext(pages -> pageList = pages)
.doOnCompleted(() -> start(GET_PAGE_IMAGES)), .doOnCompleted(() -> start(GET_PAGE_IMAGES)),
(view, pages) -> { (view, pages) -> {
view.onPageList(pages); view.onPageListReady(pages);
}); });
restartableReplay(GET_PAGE_IMAGES, restartableReplay(GET_PAGE_IMAGES,
this::getPageImagesObservable, this::getPageImagesObservable,
(view, page) -> { (view, page) -> {
view.onPageDownloaded(page); view.onImageReady(page);
if (page.getPageNumber() == savedSelectedPage) { if (page.getPageNumber() == savedSelectedPage) {
view.setCurrentPage(savedSelectedPage); view.setCurrentPage(savedSelectedPage);
} }

View file

@ -2,11 +2,11 @@ package eu.kanade.mangafeed.ui.activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.view.ViewPager; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.List; import java.util.List;
@ -17,18 +17,18 @@ import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.presenter.ReaderPresenter; import eu.kanade.mangafeed.presenter.ReaderPresenter;
import eu.kanade.mangafeed.ui.activity.base.BaseRxActivity; import eu.kanade.mangafeed.ui.activity.base.BaseRxActivity;
import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; import eu.kanade.mangafeed.ui.viewer.LeftToRightViewer;
import eu.kanade.mangafeed.widget.ReaderViewPager; import eu.kanade.mangafeed.ui.viewer.base.BaseViewer;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(ReaderPresenter.class) @RequiresPresenter(ReaderPresenter.class)
public class ReaderActivity extends BaseRxActivity<ReaderPresenter> { public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
@Bind(R.id.view_pager) ReaderViewPager viewPager;
@Bind(R.id.page_number) TextView pageNumber; @Bind(R.id.page_number) TextView pageNumber;
@Bind(R.id.viewer) FrameLayout container;
private ReaderPageAdapter adapter;
private int currentPage; private int currentPage;
private BaseViewer viewer;
public static Intent newInstance(Context context) { public static Intent newInstance(Context context) {
return new Intent(context, ReaderActivity.class); return new Intent(context, ReaderActivity.class);
@ -40,8 +40,8 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
setContentView(R.layout.activity_reader); setContentView(R.layout.activity_reader);
ButterKnife.bind(this); ButterKnife.bind(this);
createAdapter(); viewer = new LeftToRightViewer(this, container);
setupViewPager();
enableHardwareAcceleration(); enableHardwareAcceleration();
} }
@ -51,76 +51,27 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
super.onDestroy(); super.onDestroy();
} }
private void createAdapter() { public void onPageListReady(List<Page> pages) {
adapter = new ReaderPageAdapter(getSupportFragmentManager()); viewer.onPageListReady(pages);
viewPager.setAdapter(adapter);
} }
private void setupViewPager() { public void onImageReady(Page page) {
viewPager.setOffscreenPageLimit(3); viewer.onImageReady(page);
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 ReaderViewPager getViewPager() { public void onPageChanged(int currentPage, int totalPages) {
return viewPager; String page = currentPage + "/" + totalPages;
}
public void onPageList(List<Page> 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();
pageNumber.setText(page); pageNumber.setText(page);
} }
public void hideStatusBar() { public void setCurrentPage(int page) {
if (Build.VERSION.SDK_INT < 16) { currentPage = page;
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 hideStatusBar() {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
} }
public void enableHardwareAcceleration() { public void enableHardwareAcceleration() {
@ -129,4 +80,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
} }
public boolean onImageTouch(MotionEvent motionEvent) {
return viewer.onImageTouch(motionEvent);
}
} }

View file

@ -71,7 +71,7 @@ public class ReaderPageFragment extends Fragment {
imageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); imageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE); imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE);
imageView.setOnTouchListener((v, motionEvent) -> imageView.setOnTouchListener((v, motionEvent) ->
((ReaderActivity) getActivity()).getViewPager().onImageTouch(motionEvent)); ((ReaderActivity) getActivity()).onImageTouch(motionEvent));
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);

View file

@ -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() {
}
}

View file

@ -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<Page> pages) {
ArrayList<Page> 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() {
}
}

View file

@ -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<Page> pages);
public abstract boolean onImageTouch(MotionEvent motionEvent);
}

View file

@ -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<Page> 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();
}

View file

@ -51,7 +51,7 @@ public class ReaderViewPager extends ViewPager {
float displacement = ev.getX() - startDragX; float displacement = ev.getX() - startDragX;
if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) { if (ev.getX() > startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
mOnChapterBoundariesOutListener.onFirstPageOut(); mOnChapterBoundariesOutListener.onFirstPageOutEvent();
return true; return true;
} }
@ -62,7 +62,7 @@ public class ReaderViewPager extends ViewPager {
float displacement = startDragX - ev.getX(); float displacement = startDragX - ev.getX();
if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) { if (ev.getX() < startDragX && displacement > getWidth() * SWIPE_TOLERANCE) {
mOnChapterBoundariesOutListener.onLastPageOut(); mOnChapterBoundariesOutListener.onLastPageOutEvent();
return true; return true;
} }
@ -84,9 +84,9 @@ public class ReaderViewPager extends ViewPager {
} }
public interface OnChapterBoundariesOutListener { public interface OnChapterBoundariesOutListener {
public void onFirstPageOut(); public void onFirstPageOutEvent();
public void onLastPageOut(); public void onLastPageOutEvent();
} }
public interface OnChapterSingleTapListener { public interface OnChapterSingleTapListener {
@ -114,7 +114,7 @@ public class ReaderViewPager extends ViewPager {
setCurrentItem(position - 1, true); setCurrentItem(position - 1, true);
} else { } else {
if (mOnChapterBoundariesOutListener != null) { if (mOnChapterBoundariesOutListener != null) {
mOnChapterBoundariesOutListener.onFirstPageOut(); mOnChapterBoundariesOutListener.onFirstPageOutEvent();
} }
} }
} else if (positionX > getWidth() * RIGHT_REGION) { } else if (positionX > getWidth() * RIGHT_REGION) {
@ -122,7 +122,7 @@ public class ReaderViewPager extends ViewPager {
setCurrentItem(position + 1, true); setCurrentItem(position + 1, true);
} else { } else {
if (mOnChapterBoundariesOutListener != null) { if (mOnChapterBoundariesOutListener != null) {
mOnChapterBoundariesOutListener.onLastPageOut(); mOnChapterBoundariesOutListener.onLastPageOutEvent();
} }
} }
} }

View file

@ -3,11 +3,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<eu.kanade.mangafeed.widget.ReaderViewPager <FrameLayout
android:id="@+id/view_pager" android:id="@+id/viewer"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
</eu.kanade.mangafeed.widget.ReaderViewPager> </FrameLayout>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<eu.kanade.mangafeed.widget.ReaderViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</eu.kanade.mangafeed.widget.ReaderViewPager>