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.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<ReaderActivity> {
private Source source;
private Chapter chapter;
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_IMAGES = 2;
@ -30,21 +31,20 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
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);
}
});
}
@ -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 -> {
return mNetworkService
.getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToPageUrls(unparsedHtml)))
.flatMap(this::convertToPages)
.flatMap(unparsedHtml -> {
List<String> 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<List<Page>> convertToPages(List<String> pageUrls) {
private List<Page> convertToPages(List<String> pageUrls) {
List<Page> 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<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.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<ReaderPresenter> {
@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<ReaderPresenter> {
ButterKnife.bind(this);
createAdapter();
viewPager.setOffscreenPageLimit(3);
setupViewPager();
}
private void createAdapter() {
@ -41,11 +44,48 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
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) {
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);
}
}

View File

@ -29,6 +29,10 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
return ReaderPageFragment.newInstance(pages.get(position));
}
public List<Page> getPages() {
return pages;
}
public void setPages(List<Page> 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) {

View File

@ -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,9 +44,11 @@ public class ReaderPageFragment extends Fragment {
}
public void setPage(Page page) {
if (!page.getImageUrl().equals(mUrl)) {
mUrl = page.getImageUrl();
loadImage();
}
}
private void loadImage() {
Glide.with(getActivity())

View File

@ -9,5 +9,14 @@
android:layout_height="wrap_content">
</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>