Show page number, improve the load of the first image of a chapter

This commit is contained in:
inorichi 2015-10-21 16:40:50 +02:00
parent 74da971b2e
commit 4c786077a8
6 changed files with 87 additions and 19 deletions

View File

@ -11,6 +11,7 @@ import eu.kanade.mangafeed.sources.Source;
import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.ui.activity.ReaderActivity;
import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.EventBusHook;
import eu.kanade.mangafeed.util.events.SourceChapterEvent; import eu.kanade.mangafeed.util.events.SourceChapterEvent;
import icepick.State;
import rx.Observable; import rx.Observable;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
@ -20,7 +21,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private Source source; private Source source;
private Chapter chapter; private Chapter chapter;
private List<Page> pageList; private List<Page> pageList;
private boolean pageListStarted; @State int savedSelectedPage = -1;
private static final int GET_PAGE_LIST = 1; private static final int GET_PAGE_LIST = 1;
private static final int GET_PAGE_IMAGES = 2; private static final int GET_PAGE_IMAGES = 2;
@ -30,21 +31,20 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
super.onCreate(savedState); super.onCreate(savedState);
restartableLatestCache(GET_PAGE_LIST, restartableLatestCache(GET_PAGE_LIST,
this::getPageListObservable, () -> getPageListObservable()
.doOnNext(pages -> pageList = pages)
.doOnCompleted(() -> start(GET_PAGE_IMAGES)),
(view, pages) -> { (view, pages) -> {
pageList = pages;
view.onPageList(pages); view.onPageList(pages);
if (!pageListStarted) {
pageListStarted = true;
start(GET_PAGE_IMAGES);
}
}); });
restartableReplay(GET_PAGE_IMAGES, restartableReplay(GET_PAGE_IMAGES,
this::getPageImagesObservable, this::getPageImagesObservable,
(view, page) -> { (view, page) -> {
view.onPageDownloaded(page); view.onPageDownloaded(page);
if (page.getPageNumber() == savedSelectedPage) {
view.setCurrentPage(savedSelectedPage);
}
}); });
} }
@ -85,9 +85,9 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private Observable<Page> getPageImagesObservable() { private Observable<Page> getPageImagesObservable() {
return Observable.merge( return Observable.merge(
Observable.from(pageList).filter(page -> page.getImageUrl() != null), Observable.from(pageList).filter(page -> page.getImageUrl() != null),
source.getRemainingImageUrlsFromPageList(pageList) source.getRemainingImageUrlsFromPageList(pageList)
.doOnNext(this::replacePageUrl)) .doOnNext(this::replacePageUrl))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
} }
@ -100,4 +100,8 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
} }
} }
} }
public void setCurrentPage(int savedPage) {
this.savedSelectedPage = savedPage;
}
} }

View File

@ -98,8 +98,10 @@ public abstract class Source {
.onErrorResumeNext(throwable -> { .onErrorResumeNext(throwable -> {
return mNetworkService return mNetworkService
.getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders) .getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToPageUrls(unparsedHtml))) .flatMap(unparsedHtml -> {
.flatMap(this::convertToPages) List<String> pageUrls = parseHtmlToPageUrls(unparsedHtml);
return Observable.just(getFirstImageFromPageUrls(pageUrls, unparsedHtml));
})
.doOnNext(pages -> savePageList(chapterUrl, pages)); .doOnNext(pages -> savePageList(chapterUrl, pages));
}) })
.onBackpressureBuffer(); .onBackpressureBuffer();
@ -134,13 +136,19 @@ public abstract class Source {
mCacheManager.putPageUrlsToDiskCache(chapterUrl, pages); mCacheManager.putPageUrlsToDiskCache(chapterUrl, pages);
} }
private Observable<List<Page>> convertToPages(List<String> pageUrls) { private List<Page> convertToPages(List<String> pageUrls) {
List<Page> pages = new ArrayList<>(); List<Page> pages = new ArrayList<>();
for (int i = 0; i < pageUrls.size(); i++) { for (int i = 0; i < pageUrls.size(); i++) {
pages.add(new Page(i, pageUrls.get(i))); pages.add(new Page(i, pageUrls.get(i)));
} }
return Observable.just(pages); return pages;
} }
private List<Page> getFirstImageFromPageUrls(List<String> pageUrls, String unparsedHtml) {
List<Page> pages = convertToPages(pageUrls);
String firstImage = parseHtmlToImageUrl(unparsedHtml);
pages.get(0).setImageUrl(firstImage);
return pages;
}
} }

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.widget.TextView;
import java.util.List; import java.util.List;
@ -19,8 +20,10 @@ import nucleus.factory.RequiresPresenter;
public class ReaderActivity extends BaseActivity<ReaderPresenter> { public class ReaderActivity extends BaseActivity<ReaderPresenter> {
@Bind(R.id.view_pager) ViewPager viewPager; @Bind(R.id.view_pager) ViewPager viewPager;
@Bind(R.id.page_number) TextView pageNumber;
private ReaderPageAdapter adapter; private ReaderPageAdapter adapter;
private int currentPage;
public static Intent newInstance(Context context) { public static Intent newInstance(Context context) {
return new Intent(context, ReaderActivity.class); return new Intent(context, ReaderActivity.class);
@ -33,7 +36,7 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
ButterKnife.bind(this); ButterKnife.bind(this);
createAdapter(); createAdapter();
viewPager.setOffscreenPageLimit(3); setupViewPager();
} }
private void createAdapter() { private void createAdapter() {
@ -41,11 +44,48 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
viewPager.setAdapter(adapter); 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<Page> pages) { public void onPageList(List<Page> pages) {
adapter.setPages(pages); adapter.setPages(pages);
updatePageNumber();
} }
public void onPageDownloaded(Page page) { public void onPageDownloaded(Page page) {
adapter.replacePage(page.getPageNumber(), 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);
}
} }

View File

@ -29,6 +29,10 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
return ReaderPageFragment.newInstance(pages.get(position)); return ReaderPageFragment.newInstance(pages.get(position));
} }
public List<Page> getPages() {
return pages;
}
public void setPages(List<Page> pages) { public void setPages(List<Page> pages) {
this.pages = pages; this.pages = pages;
notifyDataSetChanged(); notifyDataSetChanged();
@ -36,7 +40,6 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
public void replacePage(int position, Page page) { public void replacePage(int position, Page page) {
pages.set(position, page); pages.set(position, page);
notifyDataSetChanged();
ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position); ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position);
if (fragment != null) { if (fragment != null) {

View File

@ -33,6 +33,8 @@ public class ReaderPageFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setRetainInstance(true);
Bundle arguments = getArguments(); Bundle arguments = getArguments();
if (arguments != null) { if (arguments != null) {
if (arguments.containsKey(URL_ARGUMENT_KEY)) { if (arguments.containsKey(URL_ARGUMENT_KEY)) {
@ -42,8 +44,10 @@ public class ReaderPageFragment extends Fragment {
} }
public void setPage(Page page) { public void setPage(Page page) {
mUrl = page.getImageUrl(); if (!page.getImageUrl().equals(mUrl)) {
loadImage(); mUrl = page.getImageUrl();
loadImage();
}
} }
private void loadImage() { private void loadImage() {

View File

@ -9,5 +9,14 @@
android:layout_height="wrap_content"> android:layout_height="wrap_content">
</android.support.v4.view.ViewPager> </android.support.v4.view.ViewPager>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="bottom|left"
android:background="@color/bg_light_grey"
android:textColor="@color/black_87pc"
android:textSize="12sp"
android:id="@+id/page_number"/>
</FrameLayout> </FrameLayout>