- Add a new cache to store library covers without relying on Glide's cache, making sure they are always available.
- Use Observable's defer instead of create in NetworkHelper.
- Fix Kissmanga showing only one genre.
This commit is contained in:
inorichi 2015-12-01 14:20:14 +01:00
parent b66809b2b2
commit 04a61edb38
9 changed files with 205 additions and 76 deletions

View file

@ -0,0 +1,129 @@
package eu.kanade.mangafeed.data.cache;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import eu.kanade.mangafeed.util.DiskUtils;
public class CoverCache {
private static final String PARAMETER_CACHE_DIRECTORY = "cover_disk_cache";
private Context context;
private File cacheDir;
public CoverCache(Context context) {
this.context = context;
cacheDir = new File(context.getCacheDir(), PARAMETER_CACHE_DIRECTORY);
createCacheDir();
}
private boolean createCacheDir() {
return !cacheDir.exists() && cacheDir.mkdirs();
}
// Download the cover with Glide (it can avoid repeating requests) and save the file on this cache
public void save(String cover, LazyHeaders headers) {
GlideUrl url = new GlideUrl(cover, headers);
Glide.with(context)
.load(url)
.downloadOnly(new SimpleTarget<File>() {
@Override
public void onResourceReady(File resource, GlideAnimation<? super File> anim) {
try {
add(cover, resource);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
// Copy the cover from Glide's cache to this cache
public void add(String key, File source) throws IOException {
File dest = new File(cacheDir, DiskUtils.hashKeyForDisk(key));
if (dest.exists())
dest.delete();
InputStream in = new FileInputStream(source);
try {
OutputStream out = new FileOutputStream(dest);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
// Get the cover from cache
public File get(String key) {
return new File(cacheDir, DiskUtils.hashKeyForDisk(key));
}
// Delete the cover from cache
public boolean delete(String key) {
File file = new File(cacheDir, DiskUtils.hashKeyForDisk(key));
return file.exists() && file.delete();
}
// Load the cover from cache or network if it doesn't exist
public void loadOrFetchInto(ImageView imageView, String cover, LazyHeaders headers) {
File localCover = get(cover);
if (localCover.exists()) {
loadLocalInto(context, imageView, localCover);
} else {
loadRemoteInto(context, imageView, cover, headers);
}
}
// Load the cover from cache
public static void loadLocalInto(Context context, ImageView imageView, String cover) {
File cacheDir = new File(context.getCacheDir(), PARAMETER_CACHE_DIRECTORY);
File localCover = new File(cacheDir, DiskUtils.hashKeyForDisk(cover));
if (localCover.exists()) {
loadLocalInto(context, imageView, localCover);
}
}
// Load the cover from the cache directory into the specified image view
private static void loadLocalInto(Context context, ImageView imageView, File file) {
Glide.with(context)
.load(file)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.into(imageView);
}
// Load the cover from network into the specified image view. It does NOT save the image in cache
private static void loadRemoteInto(Context context, ImageView imageView, String cover, LazyHeaders headers) {
GlideUrl url = new GlideUrl(cover, headers);
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.centerCrop()
.into(imageView);
}
}

View file

@ -16,83 +16,75 @@ import rx.Observable;
public final class NetworkHelper {
private OkHttpClient mClient;
private OkHttpClient client;
private CookieManager cookieManager;
public final CacheControl NULL_CACHE_CONTROL = new CacheControl.Builder().noCache().build();
public final Headers NULL_HEADERS = new Headers.Builder().build();
public NetworkHelper() {
mClient = new OkHttpClient();
client = new OkHttpClient();
cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
mClient.setCookieHandler(cookieManager);
client.setCookieHandler(cookieManager);
}
public Observable<Response> getResponse(final String url, final Headers headers, final CacheControl cacheControl) {
return Observable.<Response>create(subscriber -> {
return Observable.defer(() -> {
try {
if (!subscriber.isUnsubscribed()) {
Request request = new Request.Builder()
.url(url)
.cacheControl(cacheControl != null ? cacheControl : NULL_CACHE_CONTROL)
.headers(headers != null ? headers : NULL_HEADERS)
.build();
subscriber.onNext(mClient.newCall(request).execute());
}
subscriber.onCompleted();
return Observable.just(client.newCall(request).execute());
} catch (Throwable e) {
subscriber.onError(e);
return Observable.error(e);
}
}).retry(3);
}
public Observable<String> mapResponseToString(final Response response) {
return Observable.create(subscriber -> {
return Observable.defer(() -> {
try {
subscriber.onNext(response.body().string());
subscriber.onCompleted();
return Observable.just(response.body().string());
} catch (Throwable e) {
subscriber.onError(e);
return Observable.error(e);
}
});
}
public Observable<String> getStringResponse(final String url, final Headers headers, final CacheControl cacheControl) {
return getResponse(url, headers, cacheControl)
.flatMap(this::mapResponseToString);
}
public Observable<Response> postData(final String url, final RequestBody formBody, final Headers headers) {
return Observable.create(subscriber -> {
return Observable.defer(() -> {
try {
if (!subscriber.isUnsubscribed()) {
Request request = new Request.Builder()
.url(url)
.post(formBody)
.headers(headers != null ? headers : NULL_HEADERS)
.build();
subscriber.onNext(mClient.newCall(request).execute());
}
subscriber.onCompleted();
return Observable.just(client.newCall(request).execute());
} catch (Throwable e) {
subscriber.onError(e);
return Observable.error(e);
}
});
}).retry(3);
}
public Observable<Response> getProgressResponse(final String url, final Headers headers, final ProgressListener listener) {
return Observable.<Response>create(subscriber -> {
return Observable.defer(() -> {
try {
if (!subscriber.isUnsubscribed()) {
Request request = new Request.Builder()
.url(url)
.cacheControl(NULL_CACHE_CONTROL)
.headers(headers != null ? headers : NULL_HEADERS)
.build();
OkHttpClient progressClient = mClient.clone();
OkHttpClient progressClient = client.clone();
progressClient.networkInterceptors().add(chain -> {
Response originalResponse = chain.proceed(chain.request());
@ -100,11 +92,9 @@ public final class NetworkHelper {
.body(new ProgressResponseBody(originalResponse.body(), listener))
.build();
});
subscriber.onNext(progressClient.newCall(request).execute());
}
subscriber.onCompleted();
return Observable.just(progressClient.newCall(request).execute());
} catch (Throwable e) {
subscriber.onError(e);
return Observable.error(e);
}
}).retry(3);
}

View file

@ -151,7 +151,7 @@ public class Kissmanga extends Source {
Element infoElement = parsedDocument.select("div.barContent").first();
Element titleElement = infoElement.select("a.bigChar").first();
Element authorElement = infoElement.select("p:has(span:contains(Author:)) > a").first();
Element genreElement = infoElement.select("p:has(span:contains(Genres:)) > *:gt(0)").first();
Elements genreElement = infoElement.select("p:has(span:contains(Genres:)) > *:gt(0)");
Elements descriptionElement = infoElement.select("p:has(span:contains(Summary:)) ~ p");
Element thumbnailUrlElement = parsedDocument.select(".rightBox:eq(0) img").first();

View file

@ -7,6 +7,7 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import eu.kanade.mangafeed.data.cache.CacheManager;
import eu.kanade.mangafeed.data.cache.CoverCache;
import eu.kanade.mangafeed.data.chaptersync.ChapterSyncManager;
import eu.kanade.mangafeed.data.database.DatabaseHelper;
import eu.kanade.mangafeed.data.download.DownloadManager;
@ -38,6 +39,12 @@ public class DataModule {
return new CacheManager(app);
}
@Provides
@Singleton
CoverCache provideCoverCache(Application app) {
return new CoverCache(app);
}
@Provides
@Singleton
NetworkHelper provideNetworkHelper() {

View file

@ -67,7 +67,7 @@ public class CatalogueAdapter extends ArrayAdapter<Manga> {
Glide.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.centerCrop()
.into(thumbnail);
} else {

View file

@ -14,6 +14,7 @@ import android.widget.ImageView;
import android.widget.ProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import java.util.List;
@ -182,6 +183,7 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter> {
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.centerCrop()
.into(imageView);
}

View file

@ -4,12 +4,8 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.Objects;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.cache.CoverCache;
import eu.kanade.mangafeed.data.database.models.Manga;
import uk.co.ribot.easyadapter.ItemViewHolder;
import uk.co.ribot.easyadapter.PositionInfo;
@ -43,17 +39,11 @@ public class LibraryHolder extends ItemViewHolder<Manga> {
unreadText.setVisibility(View.GONE);
}
String thumbnail;
if (manga.thumbnail_url != null)
thumbnail = manga.thumbnail_url;
else
thumbnail = "http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg";
Glide.with(getContext())
.load(thumbnail)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.centerCrop()
.into(this.thumbnail);
if (manga.thumbnail_url != null) {
CoverCache.loadLocalInto(getContext(), thumbnail, manga.thumbnail_url);
} else {
thumbnail.setImageResource(android.R.color.transparent);
}
}
}

View file

@ -8,9 +8,6 @@ import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
@ -65,11 +62,9 @@ public class MangaInfoFragment extends BaseRxFragment<MangaInfoPresenter> {
setFavoriteText(manga.favorite);
Glide.with(getActivity())
.load(manga.thumbnail_url)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.centerCrop()
.into(mCover);
getPresenter().coverCache.loadOrFetchInto(mCover,
manga.thumbnail_url, getPresenter().source.getGlideHeaders());
}
public void setChapterCount(int count) {

View file

@ -4,8 +4,11 @@ import android.os.Bundle;
import javax.inject.Inject;
import eu.kanade.mangafeed.data.cache.CoverCache;
import eu.kanade.mangafeed.data.database.DatabaseHelper;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.source.SourceManager;
import eu.kanade.mangafeed.data.source.base.Source;
import eu.kanade.mangafeed.event.ChapterCountEvent;
import eu.kanade.mangafeed.ui.base.presenter.BasePresenter;
import eu.kanade.mangafeed.util.EventBusHook;
@ -14,8 +17,11 @@ import rx.Observable;
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@Inject DatabaseHelper db;
@Inject SourceManager sourceManager;
@Inject CoverCache coverCache;
private Manga manga;
protected Source source;
private int count = -1;
private static final int GET_MANGA = 1;
@ -49,6 +55,7 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@EventBusHook
public void onEventMainThread(Manga manga) {
this.manga = manga;
source = sourceManager.get(manga.source);
start(GET_MANGA);
}
@ -67,7 +74,16 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
public void toggleFavorite() {
manga.favorite = !manga.favorite;
onMangaFavoriteChange(manga.favorite);
db.insertManga(manga).executeAsBlocking();
}
private void onMangaFavoriteChange(boolean isFavorite) {
if (isFavorite) {
coverCache.save(manga.thumbnail_url, source.getGlideHeaders());
} else {
coverCache.delete(manga.thumbnail_url);
}
}
}