From e160f53bb2ba13c1e79aae68d488583d2377ca89 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 28 Oct 2015 17:20:00 +0100 Subject: [PATCH] Better handling of loading progress and image load. --- .../eu/kanade/mangafeed/data/models/Page.java | 21 ++- .../mangafeed/presenter/ReaderPresenter.java | 4 - .../kanade/mangafeed/sources/base/Source.java | 4 +- .../mangafeed/ui/activity/ReaderActivity.java | 4 - .../ui/adapter/ReaderPageAdapter.java | 9 -- .../ui/fragment/ReaderPageFragment.java | 136 ++++++++++++------ .../mangafeed/ui/viewer/VerticalViewer.java | 5 - .../mangafeed/ui/viewer/WebtoonViewer.java | 5 - .../mangafeed/ui/viewer/base/BaseViewer.java | 1 - .../ui/viewer/base/ViewPagerViewer.java | 5 - app/src/main/res/layout/fragment_page.xml | 1 + 11 files changed, 108 insertions(+), 87 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java b/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java index 15fe0cbcc..8f152d8e1 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java @@ -1,6 +1,7 @@ package eu.kanade.mangafeed.data.models; import eu.kanade.mangafeed.data.helpers.NetworkHelper; +import rx.subjects.PublishSubject; public class Page implements NetworkHelper.ProgressListener { @@ -8,12 +9,16 @@ public class Page implements NetworkHelper.ProgressListener { private String url; private String imageUrl; private String imagePath; - private int status; - private int progress; + private transient int status; + private transient int progress; - public static final int DOWNLOAD = 0; - public static final int READY = 1; - public static final int ERROR = 2; + private transient PublishSubject statusSubject; + + public static final int QUEUE = 0; + public static final int LOAD_PAGE = 1; + public static final int DOWNLOAD_IMAGE = 2; + public static final int READY = 3; + public static final int ERROR = 4; public Page(int pageNumber, String url, String imageUrl, String imagePath) { this.pageNumber = pageNumber; @@ -56,6 +61,8 @@ public class Page implements NetworkHelper.ProgressListener { public void setStatus(int status) { this.status = status; + if (statusSubject != null) + statusSubject.onNext(status); } public int getProgress() { @@ -76,4 +83,8 @@ public class Page implements NetworkHelper.ProgressListener { progress = (int) ((100 * bytesRead) / contentLength); } + public void setStatusSubject(PublishSubject subject) { + this.statusSubject = subject; + } + } 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 16522ddd3..11b757915 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -47,10 +47,6 @@ public class ReaderPresenter extends BasePresenter { restartableReplay(GET_PAGE_IMAGES, this::getPageImagesObservable, (view, page) -> { - view.onNextPage(page); - if (page.getPageNumber() == savedSelectedPage) { - view.setCurrentPage(savedSelectedPage); - } }, (view, error) -> Timber.e("An error occurred while downloading an image")); } diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java b/app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java index 98429d1db..afd068b8c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/base/Source.java @@ -80,7 +80,6 @@ public abstract class Source extends BaseSource { public Observable getRemainingImageUrlsFromPageList(final List pages) { return Observable.from(pages) .filter(page -> page.getImageUrl() == null) - .doOnNext(page -> page.setStatus(Page.DOWNLOAD)) .window(overrideNumberOfConcurrentPageDownloads()) .concatMap(batchedPages -> batchedPages.concatMap(this::getImageUrlFromPage) @@ -88,6 +87,7 @@ public abstract class Source extends BaseSource { } private Observable getImageUrlFromPage(final Page page) { + page.setStatus(Page.LOAD_PAGE); return mNetworkService .getStringResponse(overrideRemainingPagesUrl(page.getUrl()), mRequestHeaders, null) .flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml))) @@ -108,7 +108,7 @@ public abstract class Source extends BaseSource { return obs; if (!mCacheManager.isImageInCache(page.getImageUrl())) { - page.setStatus(Page.DOWNLOAD); + page.setStatus(Page.DOWNLOAD_IMAGE); obs = cacheImage(page); } 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 704b354de..31cc6c18b 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 @@ -71,10 +71,6 @@ public class ReaderActivity extends BaseRxActivity { viewer.onPageListReady(pages); } - public void onNextPage(Page page) { - viewer.onNextPage(page); - } - public void onPageChanged(int currentPage, int totalPages) { String page = currentPage + "/" + totalPages; 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 2deb8f5fe..2df97cf70 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 @@ -38,13 +38,4 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter { notifyDataSetChanged(); } - public void replacePage(int position, Page page) { - pages.set(position, page); - - ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position); - if (fragment != null) { - fragment.replacePage(page); - } - } - } 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 90bb99e33..044c56a26 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 @@ -24,6 +24,7 @@ import rx.Observable; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; +import rx.subjects.PublishSubject; public class ReaderPageFragment extends Fragment { @@ -35,6 +36,7 @@ public class ReaderPageFragment extends Fragment { private Page page; private Subscription progressSubscription; + private Subscription statusSubscription; public static ReaderPageFragment newInstance(Page page) { ReaderPageFragment fragment = new ReaderPageFragment(); @@ -48,35 +50,6 @@ public class ReaderPageFragment extends Fragment { setRetainInstance(true); } - public void replacePage(Page page) { - unsubscribeProgress(); - this.page = page; - loadImage(); - } - - public void setPage(Page page) { - this.page = page; - } - - private void loadImage() { - if (page == null) - return; - - switch (page.getStatus()) { - case (Page.READY): - imageView.setImage(ImageSource.uri(page.getImagePath()).tilingDisabled()); - progressContainer.setVisibility(View.GONE); - break; - case (Page.DOWNLOAD): - progressContainer.setVisibility(View.VISIBLE); - break; - case (Page.ERROR): - progressContainer.setVisibility(View.GONE); - errorText.setVisibility(View.VISIBLE); - } - - } - @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_page, container, false); @@ -88,42 +61,111 @@ public class ReaderPageFragment extends Fragment { imageView.setOnTouchListener((v, motionEvent) -> ((ReaderActivity) getActivity()).onImageTouch(motionEvent)); - observeProgress(); - loadImage(); - return view; } + public void onStart() { + super.onStart(); + observeStatus(); + } + @Override public void onStop() { - super.onStop(); unsubscribeProgress(); + unsubscribeStatus(); + super.onStop(); + } + + public void setPage(Page page) { + this.page = page; + } + + private void showImage() { + if (page == null || page.getImagePath() == null) + return; + + imageView.setImage(ImageSource.uri(page.getImagePath())); + progressContainer.setVisibility(View.GONE); + } + + private void showDownloading() { + progressText.setVisibility(View.VISIBLE); + } + + private void showLoading() { + progressText.setVisibility(View.VISIBLE); + progressText.setText(R.string.downloading); + } + + private void showError() { + progressContainer.setVisibility(View.GONE); + errorText.setVisibility(View.VISIBLE); + } + + private void processStatus(int status) { + switch (status) { + case Page.READY: + showImage(); + unsubscribeProgress(); + unsubscribeStatus(); + break; + case Page.LOAD_PAGE: + showLoading(); + break; + case Page.DOWNLOAD_IMAGE: + showDownloading(); + break; + case Page.ERROR: + showError(); + unsubscribeProgress(); + break; + } + } + + private void observeStatus() { + if (page == null) + return; + + if (page.getStatus() == Page.READY) { + showImage(); + } else { + processStatus(page.getStatus()); + + PublishSubject statusSubject = PublishSubject.create(); + page.setStatusSubject(statusSubject); + + observeProgress(); + + statusSubscription = statusSubject + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::processStatus); + } } private void observeProgress() { - if (page == null || page.getStatus() != Page.DOWNLOAD) - return; - progressSubscription = Observable.interval(75, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(tick -> { - if (page.getProgress() == 0) { - progressText.setText(R.string.downloading); - } - else if (page.getProgress() == 100) { - progressContainer.setVisibility(View.GONE); - unsubscribeProgress(); - } - else { - progressText.setText(getString(R.string.download_progress, page.getProgress())); - } + if (page.getProgress() != 0) + progressText.setText( + getString(R.string.download_progress, page.getProgress())); }); } + private void unsubscribeStatus() { + if (statusSubscription != null) { + page.setStatusSubject(null); + statusSubscription.unsubscribe(); + statusSubscription = null; + } + } + private void unsubscribeProgress() { - if (progressSubscription != null) + if (progressSubscription != null) { progressSubscription.unsubscribe(); + progressSubscription = null; + } } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java index cda338966..d92aa2712 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java @@ -58,11 +58,6 @@ public class VerticalViewer extends BaseViewer { updatePageNumber(); } - @Override - public void onNextPage(Page page) { - adapter.replacePage(getPosFromPage(page), page); - } - @Override public boolean onImageTouch(MotionEvent motionEvent) { return false; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java index 574061134..9e4a4d773 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java @@ -33,11 +33,6 @@ public class WebtoonViewer extends BaseViewer { return adapter.getItemCount(); } - @Override - public void onNextPage(Page page) { - adapter.setPage(getPosFromPage(page), page); - } - @Override public void onPageListReady(List pages) { adapter.setPages(pages); 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 index d3b89a710..0869e57e2 100644 --- 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 @@ -33,6 +33,5 @@ public abstract class BaseViewer { public abstract int getTotalPages(); public abstract void onPageListReady(List pages); - public abstract void onNextPage(Page page); 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 index 21cac584b..da3a6b9ef 100644 --- 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 @@ -71,11 +71,6 @@ public abstract class ViewPagerViewer extends BaseViewer { updatePageNumber(); } - @Override - public void onNextPage(Page page) { - adapter.replacePage(getPosFromPage(page), page); - } - @Override public boolean onImageTouch(MotionEvent motionEvent) { return viewPager.onImageTouch(motionEvent); diff --git a/app/src/main/res/layout/fragment_page.xml b/app/src/main/res/layout/fragment_page.xml index bed5761b9..0768b9ce2 100644 --- a/app/src/main/res/layout/fragment_page.xml +++ b/app/src/main/res/layout/fragment_page.xml @@ -25,6 +25,7 @@ android:layout_marginTop="16dp" android:id="@+id/progress_text" android:layout_gravity="center" + android:visibility="invisible" android:textSize="16sp" />