From 4c786077a8a20848e69fc47be2797e154e937457 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 21 Oct 2015 16:40:50 +0200 Subject: [PATCH] Show page number, improve the load of the first image of a chapter --- .../mangafeed/presenter/ReaderPresenter.java | 26 +++++++----- .../eu/kanade/mangafeed/sources/Source.java | 16 +++++-- .../mangafeed/ui/activity/ReaderActivity.java | 42 ++++++++++++++++++- .../ui/adapter/ReaderPageAdapter.java | 5 ++- .../ui/fragment/ReaderPageFragment.java | 8 +++- app/src/main/res/layout/activity_reader.xml | 9 ++++ 6 files changed, 87 insertions(+), 19 deletions(-) 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 aba72bcec..669891e20 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -11,6 +11,7 @@ import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.events.SourceChapterEvent; +import icepick.State; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -20,7 +21,7 @@ public class ReaderPresenter extends BasePresenter { private Source source; private Chapter chapter; private List pageList; - private boolean pageListStarted; + @State int savedSelectedPage = -1; private static final int GET_PAGE_LIST = 1; private static final int GET_PAGE_IMAGES = 2; @@ -30,21 +31,20 @@ public class ReaderPresenter extends BasePresenter { super.onCreate(savedState); restartableLatestCache(GET_PAGE_LIST, - this::getPageListObservable, + () -> getPageListObservable() + .doOnNext(pages -> pageList = pages) + .doOnCompleted(() -> start(GET_PAGE_IMAGES)), (view, pages) -> { - pageList = pages; view.onPageList(pages); - if (!pageListStarted) { - pageListStarted = true; - start(GET_PAGE_IMAGES); - } - }); restartableReplay(GET_PAGE_IMAGES, this::getPageImagesObservable, (view, page) -> { view.onPageDownloaded(page); + if (page.getPageNumber() == savedSelectedPage) { + view.setCurrentPage(savedSelectedPage); + } }); } @@ -85,9 +85,9 @@ public class ReaderPresenter extends BasePresenter { private Observable getPageImagesObservable() { return Observable.merge( - Observable.from(pageList).filter(page -> page.getImageUrl() != null), - source.getRemainingImageUrlsFromPageList(pageList) - .doOnNext(this::replacePageUrl)) + Observable.from(pageList).filter(page -> page.getImageUrl() != null), + source.getRemainingImageUrlsFromPageList(pageList) + .doOnNext(this::replacePageUrl)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } @@ -100,4 +100,8 @@ public class ReaderPresenter extends BasePresenter { } } } + + public void setCurrentPage(int savedPage) { + this.savedSelectedPage = savedPage; + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java b/app/src/main/java/eu/kanade/mangafeed/sources/Source.java index 87d2a8e92..046b1923d 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/Source.java @@ -98,8 +98,10 @@ public abstract class Source { .onErrorResumeNext(throwable -> { return mNetworkService .getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) - .flatMap(unparsedHtml -> Observable.just(parseHtmlToPageUrls(unparsedHtml))) - .flatMap(this::convertToPages) + .flatMap(unparsedHtml -> { + List pageUrls = parseHtmlToPageUrls(unparsedHtml); + return Observable.just(getFirstImageFromPageUrls(pageUrls, unparsedHtml)); + }) .doOnNext(pages -> savePageList(chapterUrl, pages)); }) .onBackpressureBuffer(); @@ -134,13 +136,19 @@ public abstract class Source { mCacheManager.putPageUrlsToDiskCache(chapterUrl, pages); } - private Observable> convertToPages(List pageUrls) { + private List convertToPages(List pageUrls) { List pages = new ArrayList<>(); for (int i = 0; i < pageUrls.size(); i++) { pages.add(new Page(i, pageUrls.get(i))); } - return Observable.just(pages); + return pages; } + private List getFirstImageFromPageUrls(List pageUrls, String unparsedHtml) { + List pages = convertToPages(pageUrls); + String firstImage = parseHtmlToImageUrl(unparsedHtml); + pages.get(0).setImageUrl(firstImage); + return pages; + } } 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 964b5d442..0e6eff9b3 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 @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.ViewPager; +import android.widget.TextView; import java.util.List; @@ -19,8 +20,10 @@ import nucleus.factory.RequiresPresenter; public class ReaderActivity extends BaseActivity { @Bind(R.id.view_pager) ViewPager viewPager; + @Bind(R.id.page_number) TextView pageNumber; private ReaderPageAdapter adapter; + private int currentPage; public static Intent newInstance(Context context) { return new Intent(context, ReaderActivity.class); @@ -33,7 +36,7 @@ public class ReaderActivity extends BaseActivity { ButterKnife.bind(this); createAdapter(); - viewPager.setOffscreenPageLimit(3); + setupViewPager(); } private void createAdapter() { @@ -41,11 +44,48 @@ public class ReaderActivity extends BaseActivity { viewPager.setAdapter(adapter); } + 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) { + + } + }); + } + + @Override + public void onDestroy() { + getPresenter().setCurrentPage(currentPage); + super.onDestroy(); + } + 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(); + pageNumber.setText(page); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java index 8df53fec1..522a0f1bc 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java @@ -29,6 +29,10 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter { return ReaderPageFragment.newInstance(pages.get(position)); } + public List getPages() { + return pages; + } + public void setPages(List pages) { this.pages = pages; notifyDataSetChanged(); @@ -36,7 +40,6 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter { public void replacePage(int position, Page page) { pages.set(position, page); - notifyDataSetChanged(); ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position); if (fragment != null) { 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 d487a7d12..f2192e6f7 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 @@ -33,6 +33,8 @@ public class ReaderPageFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setRetainInstance(true); + Bundle arguments = getArguments(); if (arguments != null) { if (arguments.containsKey(URL_ARGUMENT_KEY)) { @@ -42,8 +44,10 @@ public class ReaderPageFragment extends Fragment { } public void setPage(Page page) { - mUrl = page.getImageUrl(); - loadImage(); + if (!page.getImageUrl().equals(mUrl)) { + mUrl = page.getImageUrl(); + loadImage(); + } } private void loadImage() { diff --git a/app/src/main/res/layout/activity_reader.xml b/app/src/main/res/layout/activity_reader.xml index e3bcf9b3d..486e6e4a8 100644 --- a/app/src/main/res/layout/activity_reader.xml +++ b/app/src/main/res/layout/activity_reader.xml @@ -9,5 +9,14 @@ android:layout_height="wrap_content"> + \ No newline at end of file