Better error handling for images

This commit is contained in:
inorichi 2015-10-27 17:12:16 +01:00
parent 35b8be6c75
commit 68e5efb02b
11 changed files with 69 additions and 39 deletions

View file

@ -6,6 +6,11 @@ public class Page {
private String url; private String url;
private String imageUrl; private String imageUrl;
private String imagePath; private String imagePath;
private int status;
public static final int DOWNLOAD = 0;
public static final int READY = 1;
public static final int ERROR = 2;
public Page(int pageNumber, String url, String imageUrl, String imagePath) { public Page(int pageNumber, String url, String imageUrl, String imagePath) {
this.pageNumber = pageNumber; this.pageNumber = pageNumber;
@ -42,6 +47,14 @@ public class Page {
this.imagePath = imagePath; this.imagePath = imagePath;
} }
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Override @Override
public String toString() { public String toString() {
return "Page{" + return "Page{" +

View file

@ -52,7 +52,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
restartableReplay(GET_PAGE_IMAGES, restartableReplay(GET_PAGE_IMAGES,
this::getPageImagesObservable, this::getPageImagesObservable,
(view, page) -> { (view, page) -> {
view.onImageReady(page); view.onNextPage(page);
if (page.getPageNumber() == savedSelectedPage) { if (page.getPageNumber() == savedSelectedPage) {
view.setCurrentPage(savedSelectedPage); view.setCurrentPage(savedSelectedPage);
} }
@ -100,9 +100,10 @@ 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)
)
.flatMap(this::downloadImage) .flatMap(this::downloadImage)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
@ -115,9 +116,9 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
try { try {
File cacheFile = future.get(); File cacheFile = future.get();
page.setImagePath(cacheFile.getCanonicalPath()); page.setImagePath(cacheFile.getCanonicalPath());
page.setStatus(Page.READY);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); page.setStatus(Page.ERROR);
} }
return Observable.just(page); return Observable.just(page);

View file

@ -80,9 +80,10 @@ public abstract class Source extends BaseSource {
public Observable<Page> getRemainingImageUrlsFromPageList(final List<Page> pages) { public Observable<Page> getRemainingImageUrlsFromPageList(final List<Page> pages) {
return Observable.from(pages) return Observable.from(pages)
.filter(page -> page.getImageUrl() == null) .filter(page -> page.getImageUrl() == null)
.doOnNext(page -> page.setStatus(Page.DOWNLOAD))
.window(overrideNumberOfConcurrentPageDownloads()) .window(overrideNumberOfConcurrentPageDownloads())
.concatMap(batchedPages -> .concatMap(batchedPages ->
batchedPages.concatMap(this::getImageUrlFromPage) batchedPages.concatMap(this::getImageUrlFromPage)
); );
} }
@ -90,6 +91,10 @@ public abstract class Source extends BaseSource {
return mNetworkService return mNetworkService
.getStringResponse(overrideRemainingPagesUrl(page.getUrl()), mRequestHeaders, null) .getStringResponse(overrideRemainingPagesUrl(page.getUrl()), mRequestHeaders, null)
.flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml))) .flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml)))
.onErrorResumeNext(e -> {
page.setStatus(Page.ERROR);
return Observable.just(null);
})
.flatMap(imageUrl -> { .flatMap(imageUrl -> {
page.setImageUrl(imageUrl); page.setImageUrl(imageUrl);
return Observable.just(page); return Observable.just(page);

View file

@ -71,8 +71,8 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
viewer.onPageListReady(pages); viewer.onPageListReady(pages);
} }
public void onImageReady(Page page) { public void onNextPage(Page page) {
viewer.onImageReady(page); viewer.onNextPage(page);
} }
public void onPageChanged(int currentPage, int totalPages) { public void onPageChanged(int currentPage, int totalPages) {
@ -101,8 +101,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
} }
private BaseViewer getViewer() { private BaseViewer getViewer() {
int prefsViewer = prefs.getDefaultViewer(); switch (prefs.getDefaultViewer()) {
switch (prefsViewer) {
case LEFT_TO_RIGHT: case LEFT_TO_RIGHT:
return new LeftToRightViewer(this, container); return new LeftToRightViewer(this, container);
case RIGHT_TO_LEFT: case RIGHT_TO_LEFT:

View file

@ -43,7 +43,7 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position); ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position);
if (fragment != null) { if (fragment != null) {
fragment.setPage(page); fragment.replacePage(page);
} }
} }

View file

@ -7,6 +7,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView;
import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
@ -18,19 +19,17 @@ import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.ui.activity.ReaderActivity;
public class ReaderPageFragment extends Fragment { public class ReaderPageFragment extends Fragment {
public static final String URL_ARGUMENT_KEY = "UrlArgumentKey";
@Bind(R.id.page_image_view) SubsamplingScaleImageView imageView; @Bind(R.id.page_image_view) SubsamplingScaleImageView imageView;
@Bind(R.id.progress) ProgressBar progressBar; @Bind(R.id.progress) ProgressBar progressBar;
@Bind(R.id.image_error) TextView errorText;
private String imagePath; private Page page;
public static ReaderPageFragment newInstance(Page page) { public static ReaderPageFragment newInstance(Page page) {
ReaderPageFragment newInstance = new ReaderPageFragment(); ReaderPageFragment fragment = new ReaderPageFragment();
Bundle arguments = new Bundle(); fragment.setPage(page);
arguments.putString(URL_ARGUMENT_KEY, page.getImagePath()); return fragment;
newInstance.setArguments(arguments);
return newInstance;
} }
@Override @Override
@ -38,26 +37,32 @@ public class ReaderPageFragment extends Fragment {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setRetainInstance(true); setRetainInstance(true);
}
Bundle arguments = getArguments(); public void replacePage(Page page) {
if (arguments != null) { this.page = page;
if (arguments.containsKey(URL_ARGUMENT_KEY)) { loadImage();
imagePath = arguments.getString(URL_ARGUMENT_KEY);
}
}
} }
public void setPage(Page page) { public void setPage(Page page) {
if (!page.getImageUrl().equals(imagePath)) { this.page = page;
imagePath = page.getImagePath();
loadImage();
}
} }
private void loadImage() { private void loadImage() {
if (imagePath != null) { if (page == null)
progressBar.setVisibility(View.GONE); return;
imageView.setImage(ImageSource.uri(imagePath).tilingDisabled());
switch (page.getStatus()) {
case (Page.READY):
imageView.setImage(ImageSource.uri(page.getImagePath()).tilingDisabled());
progressBar.setVisibility(View.GONE);
break;
case (Page.DOWNLOAD):
progressBar.setVisibility(View.VISIBLE);
break;
case (Page.ERROR):
progressBar.setVisibility(View.GONE);
errorText.setVisibility(View.VISIBLE);
} }
} }
@ -73,8 +78,6 @@ public class ReaderPageFragment extends Fragment {
imageView.setOnTouchListener((v, motionEvent) -> imageView.setOnTouchListener((v, motionEvent) ->
((ReaderActivity) getActivity()).onImageTouch(motionEvent)); ((ReaderActivity) getActivity()).onImageTouch(motionEvent));
progressBar.setVisibility(View.VISIBLE);
loadImage(); loadImage();
return view; return view;

View file

@ -59,7 +59,7 @@ public class VerticalViewer extends BaseViewer {
} }
@Override @Override
public void onImageReady(Page page) { public void onNextPage(Page page) {
adapter.replacePage(getPosFromPage(page), page); adapter.replacePage(getPosFromPage(page), page);
} }

View file

@ -34,7 +34,7 @@ public class WebtoonViewer extends BaseViewer {
} }
@Override @Override
public void onImageReady(Page page) { public void onNextPage(Page page) {
adapter.setPage(getPosFromPage(page), page); adapter.setPage(getPosFromPage(page), page);
} }

View file

@ -33,6 +33,6 @@ public abstract class BaseViewer {
public abstract int getTotalPages(); public abstract int getTotalPages();
public abstract void onPageListReady(List<Page> pages); public abstract void onPageListReady(List<Page> pages);
public abstract void onImageReady(Page page); public abstract void onNextPage(Page page);
public abstract boolean onImageTouch(MotionEvent motionEvent); public abstract boolean onImageTouch(MotionEvent motionEvent);
} }

View file

@ -72,7 +72,7 @@ public abstract class ViewPagerViewer extends BaseViewer {
} }
@Override @Override
public void onImageReady(Page page) { public void onNextPage(Page page) {
adapter.replacePage(getPosFromPage(page), page); adapter.replacePage(getPosFromPage(page), page);
} }

View file

@ -13,6 +13,15 @@
android:layout_gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone" /> android:visibility="gone" />
<include layout="@layout/chapter_image"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_error"
android:text=":("
android:layout_gravity="center"
android:textSize="32sp"
android:visibility="gone"/>
<include layout="@layout/chapter_image"/>
</FrameLayout> </FrameLayout>