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 imageUrl;
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) {
this.pageNumber = pageNumber;
@ -42,6 +47,14 @@ public class Page {
this.imagePath = imagePath;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Override
public String toString() {
return "Page{" +

View File

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

View File

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

View File

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

View File

@ -43,7 +43,7 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position);
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.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
@ -18,19 +19,17 @@ import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.ui.activity.ReaderActivity;
public class ReaderPageFragment extends Fragment {
public static final String URL_ARGUMENT_KEY = "UrlArgumentKey";
@Bind(R.id.page_image_view) SubsamplingScaleImageView imageView;
@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) {
ReaderPageFragment newInstance = new ReaderPageFragment();
Bundle arguments = new Bundle();
arguments.putString(URL_ARGUMENT_KEY, page.getImagePath());
newInstance.setArguments(arguments);
return newInstance;
ReaderPageFragment fragment = new ReaderPageFragment();
fragment.setPage(page);
return fragment;
}
@Override
@ -38,26 +37,32 @@ public class ReaderPageFragment extends Fragment {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
Bundle arguments = getArguments();
if (arguments != null) {
if (arguments.containsKey(URL_ARGUMENT_KEY)) {
imagePath = arguments.getString(URL_ARGUMENT_KEY);
}
}
public void replacePage(Page page) {
this.page = page;
loadImage();
}
public void setPage(Page page) {
if (!page.getImageUrl().equals(imagePath)) {
imagePath = page.getImagePath();
loadImage();
}
this.page = page;
}
private void loadImage() {
if (imagePath != null) {
progressBar.setVisibility(View.GONE);
imageView.setImage(ImageSource.uri(imagePath).tilingDisabled());
if (page == null)
return;
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) ->
((ReaderActivity) getActivity()).onImageTouch(motionEvent));
progressBar.setVisibility(View.VISIBLE);
loadImage();
return view;

View File

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

View File

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

View File

@ -33,6 +33,6 @@ public abstract class BaseViewer {
public abstract int getTotalPages();
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);
}

View File

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

View File

@ -13,6 +13,15 @@
android:layout_gravity="center_vertical|center_horizontal"
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>