From ef7613f7adda6659221b7c31ef31935faac28a0e Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 23 Oct 2015 01:40:27 +0200 Subject: [PATCH] Cache chapter images from presenter with glide. --- .../main/java/eu/kanade/mangafeed/App.java | 4 +- .../eu/kanade/mangafeed/data/models/Page.java | 15 +++++++- .../injection/module/DataModule.java | 9 +++++ .../presenter/MangaChaptersPresenter.java | 4 +- .../presenter/MangaInfoPresenter.java | 2 +- .../mangafeed/presenter/ReaderPresenter.java | 24 +++++++++++- .../eu/kanade/mangafeed/sources/Source.java | 14 +++---- .../ui/fragment/ReaderPageFragment.java | 37 +++++++++++-------- .../kanade/mangafeed/util/PageFileTarget.java | 36 ------------------ app/src/main/res/layout/fragment_page.xml | 19 +++++++++- 10 files changed, 93 insertions(+), 71 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/mangafeed/util/PageFileTarget.java diff --git a/app/src/main/java/eu/kanade/mangafeed/App.java b/app/src/main/java/eu/kanade/mangafeed/App.java index ceb02d8ed9..071f6dad5e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/App.java +++ b/app/src/main/java/eu/kanade/mangafeed/App.java @@ -5,10 +5,10 @@ import android.content.Context; import org.acra.annotation.ReportsCrashes; -import eu.kanade.mangafeed.injection.component.AppComponent; -import eu.kanade.mangafeed.injection.module.AppModule; import eu.kanade.mangafeed.injection.ComponentReflectionInjector; +import eu.kanade.mangafeed.injection.component.AppComponent; import eu.kanade.mangafeed.injection.component.DaggerAppComponent; +import eu.kanade.mangafeed.injection.module.AppModule; import timber.log.Timber; @ReportsCrashes( 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 e02f072c7a..2ac2b4f480 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 @@ -5,15 +5,17 @@ public class Page { private int pageNumber; private String url; private String imageUrl; + private String imagePath; - public Page(int pageNumber, String url, String imageUrl) { + public Page(int pageNumber, String url, String imageUrl, String imagePath) { this.pageNumber = pageNumber; this.url = url; this.imageUrl = imageUrl; + this.imagePath = imagePath; } public Page(int pageNumber, String url) { - this(pageNumber, url, null); + this(pageNumber, url, null, null); } public int getPageNumber() { @@ -32,6 +34,14 @@ public class Page { this.imageUrl = imageUrl; } + public String getImagePath() { + return imagePath; + } + + public void setImagePath(String imagePath) { + this.imagePath = imagePath; + } + @Override public String toString() { return "Page{" + @@ -40,4 +50,5 @@ public class Page { ", imageUrl='" + imageUrl + '\'' + '}'; } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java b/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java index 0c5cd3fa36..8359efd0ca 100644 --- a/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java +++ b/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java @@ -2,6 +2,9 @@ package eu.kanade.mangafeed.injection.module; import android.app.Application; +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; + import javax.inject.Singleton; import dagger.Module; @@ -55,4 +58,10 @@ public class DataModule { SourceManager provideSourceManager(NetworkHelper networkHelper, CacheManager cacheManager) { return new SourceManager(networkHelper, cacheManager); } + + @Provides + @Singleton + RequestManager provideGlideDownloader(Application app) { + return Glide.with(app); + } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java index fd94d59019..17a25aa4d4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -13,11 +13,11 @@ import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.SourceManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.events.ChapterCountEvent; +import eu.kanade.mangafeed.events.SourceChapterEvent; import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.ui.fragment.MangaChaptersFragment; import eu.kanade.mangafeed.util.EventBusHook; -import eu.kanade.mangafeed.events.ChapterCountEvent; -import eu.kanade.mangafeed.events.SourceChapterEvent; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java index 29343770dd..a4bbe7cc05 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java @@ -6,9 +6,9 @@ import javax.inject.Inject; import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.events.ChapterCountEvent; import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment; import eu.kanade.mangafeed.util.EventBusHook; -import eu.kanade.mangafeed.events.ChapterCountEvent; import rx.Observable; public class MangaInfoPresenter extends BasePresenter { 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 355c46eb27..c65533f65c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -2,6 +2,11 @@ package eu.kanade.mangafeed.presenter; import android.os.Bundle; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.request.FutureTarget; +import com.bumptech.glide.request.target.Target; + +import java.io.File; import java.util.List; import javax.inject.Inject; @@ -10,10 +15,10 @@ import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Page; +import eu.kanade.mangafeed.events.SourceChapterEvent; import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.util.EventBusHook; -import eu.kanade.mangafeed.events.SourceChapterEvent; import icepick.State; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; @@ -22,6 +27,7 @@ import rx.schedulers.Schedulers; public class ReaderPresenter extends BasePresenter { @Inject PreferencesHelper prefs; + @Inject RequestManager glideDownloader; private Source source; private Chapter chapter; @@ -97,10 +103,26 @@ public class ReaderPresenter extends BasePresenter { Observable.from(pageList).filter(page -> page.getImageUrl() != null), source.getRemainingImageUrlsFromPageList(pageList) .doOnNext(this::replacePageUrl)) + .flatMap(this::downloadImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } + private Observable downloadImage(Page page) { + FutureTarget future = glideDownloader.load(page.getImageUrl()) + .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); + + try { + File cacheFile = future.get(); + page.setImagePath(cacheFile.getCanonicalPath()); + + } catch (Exception e) { + e.printStackTrace(); + } + + return Observable.just(page); + } + private void replacePageUrl(Page page) { for (int i = 0; i < pageList.size(); i++) { if (pageList.get(i).getPageNumber() == page.getPageNumber()) { diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java b/app/src/main/java/eu/kanade/mangafeed/sources/Source.java index 046b1923d4..e41d1d5fbb 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/Source.java +++ b/app/src/main/java/eu/kanade/mangafeed/sources/Source.java @@ -46,7 +46,7 @@ public abstract class Source { } // Number of images to download at the same time - protected int getNumberOfConcurrentImageDownloads() { + protected int getNumberOfConcurrentPageDownloads() { return 3; } @@ -111,14 +111,10 @@ public abstract class Source { public Observable getRemainingImageUrlsFromPageList(final List pages) { return Observable.from(pages) .filter(page -> page.getImageUrl() == null) - .buffer(getNumberOfConcurrentImageDownloads()) - .concatMap(batchedPages -> { - List> pageObservable = new ArrayList<>(); - for (Page page : batchedPages) { - pageObservable.add(getImageUrlFromPage(page)); - } - return Observable.merge(pageObservable); - }); + .window(getNumberOfConcurrentPageDownloads()) + .concatMap(batchedPages -> + batchedPages.concatMap(this::getImageUrlFromPage) + ); } private Observable getImageUrlFromPage(final Page 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 61fc4363ef..1b3619b07a 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 @@ -6,27 +6,29 @@ import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ProgressBar; -import com.bumptech.glide.Glide; +import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; +import butterknife.Bind; +import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.ui.activity.ReaderActivity; -import eu.kanade.mangafeed.util.MangaImageRegionDecoder; -import eu.kanade.mangafeed.util.PageFileTarget; public class ReaderPageFragment extends Fragment { public static final String URL_ARGUMENT_KEY = "UrlArgumentKey"; - private SubsamplingScaleImageView imageView; + @Bind(R.id.page_image_view) SubsamplingScaleImageView imageView; + @Bind(R.id.progress) ProgressBar progressBar; - private String mUrl; + private String imagePath; public static ReaderPageFragment newInstance(Page page) { ReaderPageFragment newInstance = new ReaderPageFragment(); Bundle arguments = new Bundle(); - arguments.putString(URL_ARGUMENT_KEY, page.getImageUrl()); + arguments.putString(URL_ARGUMENT_KEY, page.getImagePath()); newInstance.setArguments(arguments); return newInstance; } @@ -40,39 +42,42 @@ public class ReaderPageFragment extends Fragment { Bundle arguments = getArguments(); if (arguments != null) { if (arguments.containsKey(URL_ARGUMENT_KEY)) { - mUrl = arguments.getString(URL_ARGUMENT_KEY); + imagePath = arguments.getString(URL_ARGUMENT_KEY); } } } public void setPage(Page page) { - if (!page.getImageUrl().equals(mUrl)) { - mUrl = page.getImageUrl(); + if (!page.getImageUrl().equals(imagePath)) { + imagePath = page.getImagePath(); loadImage(); } } private void loadImage() { - if (mUrl != null) { - Glide.with(getActivity()) - .load(mUrl) - .downloadOnly(new PageFileTarget(imageView)); + if (imagePath != null) { + progressBar.setVisibility(View.GONE); + imageView.setImage(ImageSource.uri(imagePath).tilingDisabled()); } } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - imageView = (SubsamplingScaleImageView)inflater.inflate(R.layout.fragment_page, container, false); + View view = inflater.inflate(R.layout.fragment_page, container, false); + ButterKnife.bind(this, view); + imageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_FIXED); imageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE); - imageView.setOnTouchListener((view, motionEvent) -> + imageView.setOnTouchListener((v, motionEvent) -> ((ReaderActivity) getActivity()).getViewPager().onImageTouch(motionEvent)); + progressBar.setVisibility(View.VISIBLE); + loadImage(); - return imageView; + return view; } } diff --git a/app/src/main/java/eu/kanade/mangafeed/util/PageFileTarget.java b/app/src/main/java/eu/kanade/mangafeed/util/PageFileTarget.java deleted file mode 100644 index 7470daeddb..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/util/PageFileTarget.java +++ /dev/null @@ -1,36 +0,0 @@ -package eu.kanade.mangafeed.util; - -import android.graphics.drawable.Drawable; -import android.net.Uri; - -import com.bumptech.glide.request.animation.GlideAnimation; -import com.bumptech.glide.request.target.ViewTarget; -import com.davemorrissey.labs.subscaleview.ImageSource; -import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; - -import java.io.File; - -import eu.kanade.mangafeed.R; - -public class PageFileTarget extends ViewTarget { - - public PageFileTarget(SubsamplingScaleImageView view) { - super(view); - } - - @Override - public void onLoadCleared(Drawable placeholder) { - view.setImage(ImageSource.resource(R.drawable.ic_action_refresh)); - } - - @Override - public void onLoadStarted(Drawable placeholder) { - view.setImage(ImageSource.resource(R.drawable.ic_action_refresh)); - } - - @Override - public void onResourceReady(File resource, GlideAnimation glideAnimation) { - view.setImage(ImageSource.uri(Uri.fromFile(resource)) - .tilingDisabled()); - } -} diff --git a/app/src/main/res/layout/fragment_page.xml b/app/src/main/res/layout/fragment_page.xml index 1ae04344bf..761a332edb 100644 --- a/app/src/main/res/layout/fragment_page.xml +++ b/app/src/main/res/layout/fragment_page.xml @@ -1,6 +1,21 @@ - \ No newline at end of file + xmlns:android="http://schemas.android.com/apk/res/android"> + + + + + + \ No newline at end of file