Now using OkHttp Glide module, also improved the artist image loading and caching.
This commit is contained in:
parent
967ef7d824
commit
524273228d
8 changed files with 39 additions and 18 deletions
|
|
@ -4,8 +4,8 @@ import android.content.Context;
|
|||
|
||||
import com.bumptech.glide.Priority;
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
import com.bumptech.glide.load.data.HttpUrlFetcher;
|
||||
import com.bumptech.glide.load.model.GlideUrl;
|
||||
import com.bumptech.glide.load.model.ModelLoader;
|
||||
import com.kabouzeid.gramophone.lastfm.rest.LastFMRestClient;
|
||||
import com.kabouzeid.gramophone.lastfm.rest.model.LastFmArtist;
|
||||
import com.kabouzeid.gramophone.util.LastFMUtil;
|
||||
|
|
@ -18,16 +18,23 @@ import java.io.InputStream;
|
|||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class ArtistImageFetcher implements DataFetcher<InputStream> {
|
||||
public static final String TAG = ArtistImageFetcher.class.getSimpleName();
|
||||
private Context context;
|
||||
private final LastFMRestClient lastFMRestClient;
|
||||
private final ArtistImage model;
|
||||
private HttpUrlFetcher urlFetcher;
|
||||
private ModelLoader<GlideUrl, InputStream> urlLoader;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private volatile boolean isCancelled;
|
||||
private DataFetcher<InputStream> urlFetcher;
|
||||
|
||||
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model) {
|
||||
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height) {
|
||||
this.context = context;
|
||||
this.lastFMRestClient = lastFMRestClient;
|
||||
this.model = model;
|
||||
this.urlLoader = urlLoader;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -42,7 +49,7 @@ public class ArtistImageFetcher implements DataFetcher<InputStream> {
|
|||
|
||||
if (isCancelled) return null;
|
||||
|
||||
urlFetcher = new HttpUrlFetcher(new GlideUrl(LastFMUtil.getLargestArtistImageUrl(lastFmArtist.getArtist().getImage())));
|
||||
urlFetcher = urlLoader.getResourceFetcher(new GlideUrl(LastFMUtil.getLargestArtistImageUrl(lastFmArtist.getArtist().getImage())), width, height);
|
||||
return urlFetcher.loadData(priority);
|
||||
}
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ package com.kabouzeid.gramophone.glide.artistimage;
|
|||
|
||||
import android.content.Context;
|
||||
|
||||
import com.bumptech.glide.integration.okhttp.OkHttpUrlLoader;
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
import com.bumptech.glide.load.model.GenericLoaderFactory;
|
||||
import com.bumptech.glide.load.model.GlideUrl;
|
||||
import com.bumptech.glide.load.model.ModelLoader;
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
||||
import com.bumptech.glide.load.model.stream.StreamModelLoader;
|
||||
|
|
@ -18,20 +20,24 @@ import java.io.InputStream;
|
|||
public class ArtistImageLoader implements StreamModelLoader<ArtistImage> {
|
||||
private Context context;
|
||||
private LastFMRestClient lastFMRestClient;
|
||||
private ModelLoader<GlideUrl, InputStream> urlLoader;
|
||||
|
||||
public ArtistImageLoader(Context context, LastFMRestClient lastFMRestClient) {
|
||||
public ArtistImageLoader(Context context, LastFMRestClient lastFMRestClient, ModelLoader<GlideUrl, InputStream> urlLoader) {
|
||||
this.context = context;
|
||||
this.lastFMRestClient = lastFMRestClient;
|
||||
this.urlLoader = urlLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFetcher<InputStream> getResourceFetcher(ArtistImage model, int width, int height) {
|
||||
return new ArtistImageFetcher(context, lastFMRestClient, model);
|
||||
return new ArtistImageFetcher(context, lastFMRestClient, model, urlLoader, width, height);
|
||||
}
|
||||
|
||||
public static class Factory implements ModelLoaderFactory<ArtistImage, InputStream> {
|
||||
private static volatile LastFMRestClient internalClient;
|
||||
private LastFMRestClient client;
|
||||
private OkHttpUrlLoader.Factory okHttpFactory;
|
||||
|
||||
|
||||
private static LastFMRestClient getInternalClient(Context context) {
|
||||
if (internalClient == null) {
|
||||
|
|
@ -49,15 +55,17 @@ public class ArtistImageLoader implements StreamModelLoader<ArtistImage> {
|
|||
*/
|
||||
public Factory(Context context) {
|
||||
client = getInternalClient(context);
|
||||
okHttpFactory = new OkHttpUrlLoader.Factory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelLoader<ArtistImage, InputStream> build(Context context, GenericLoaderFactory factories) {
|
||||
return new ArtistImageLoader(context, client);
|
||||
return new ArtistImageLoader(context, client, okHttpFactory.build(context, factories));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teardown() {
|
||||
okHttpFactory.teardown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue