Adding advanced glide management

This commit is contained in:
Shinokuni 2019-02-03 12:58:36 +00:00
parent 5941e9708f
commit ff8b9156db
8 changed files with 74 additions and 27 deletions

View File

@ -40,6 +40,7 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
implementation "com.github.bumptech.glide:okhttp3-integration:4.8.0"
implementation ("com.github.bumptech.glide:recyclerview-integration:4.8.0") {
// Excludes the support library because it's already included by Glide.
transitive = false

View File

@ -4,6 +4,7 @@
package="com.readrops.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"

View File

@ -20,11 +20,13 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
import com.readrops.app.database.ItemWithFeed;
import com.readrops.app.database.entities.Item;
import com.readrops.app.utils.GlideApp;
import com.readrops.readropslibrary.ParsingResult;
@ -120,11 +122,16 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S
recyclerView = findViewById(R.id.items_recycler_view);
ViewPreloadSizeProvider preloadSizeProvider = new ViewPreloadSizeProvider();
adapter = new MainItemListAdapter(Glide.with(this), preloadSizeProvider);
adapter = new MainItemListAdapter(GlideApp.with(this), preloadSizeProvider);
RecyclerViewPreloader<String> preloader = new RecyclerViewPreloader<String>(Glide.with(this), adapter, preloadSizeProvider, 10);
recyclerView.addOnScrollListener(preloader);
recyclerView.setRecyclerListener(viewHolder -> {
MainItemListAdapter.ItemViewHolder vh = (MainItemListAdapter.ItemViewHolder) viewHolder;
GlideApp.with(this).clear(vh.getItemImage());
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
@ -162,8 +169,6 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S
}).attachToRecyclerView(recyclerView);
}
private void updateList() {

View File

@ -1,5 +1,6 @@
package com.readrops.app;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -14,7 +15,7 @@ import android.widget.TextView;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
@ -23,20 +24,24 @@ import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
import com.readrops.app.database.ItemWithFeed;
import com.readrops.app.database.entities.Item;
import com.readrops.app.utils.DateUtils;
import com.readrops.app.utils.GlideRequests;
import java.util.Collections;
import java.util.List;
import static com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions.withCrossFade;
public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListAdapter.ItemViewHolder> implements ListPreloader.PreloadModelProvider<String> {
private RequestManager manager;
private GlideRequests glideRequests;
private OnItemClickListener listener;
private ViewPreloadSizeProvider preloadSizeProvider;
public MainItemListAdapter(RequestManager manager, ViewPreloadSizeProvider preloadSizeProvider) {
public MainItemListAdapter(GlideRequests glideRequests, ViewPreloadSizeProvider preloadSizeProvider) {
super(DIFF_CALLBACK);
this.manager = manager;
this.glideRequests = glideRequests;
this.preloadSizeProvider = preloadSizeProvider;
}
@ -58,13 +63,18 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
private static final DrawableCrossFadeFactory FADE_FACTORY = new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
private static final RequestOptions requestOptions = new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(16));
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.image_item, viewGroup, false);
return new ItemViewHolder(view);
ItemViewHolder viewHolder = new ItemViewHolder(view);
preloadSizeProvider.setView(viewHolder.itemImage);
return viewHolder;
}
@Override
@ -72,39 +82,55 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
ItemWithFeed itemWithFeed = getItem(i);
viewHolder.bind(itemWithFeed);
preloadSizeProvider.setView(viewHolder.itemImage);
// displaying image with some round corners
RequestOptions requestOptions = new RequestOptions();
requestOptions = requestOptions.transforms(new CenterCrop(), new RoundedCorners(16));
if (itemWithFeed.getItem().getImageLink() != null) {
if (itemWithFeed.getItem().hasImage()) {
viewHolder.itemImage.setVisibility(View.VISIBLE);
manager.load(itemWithFeed.getItem().getImageLink())
glideRequests
.load(itemWithFeed.getItem().getImageLink())
.apply(requestOptions)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.transition(DrawableTransitionOptions.withCrossFade(FADE_FACTORY))
.into(viewHolder.itemImage);
}
} else
viewHolder.itemImage.setVisibility(View.GONE);
if (itemWithFeed.getFeedIconUrl() != null) {
manager.load(itemWithFeed.getFeedIconUrl())
glideRequests.load(itemWithFeed.getFeedIconUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.ic_rss_feed)
.into(viewHolder.feedIcon);
}
} else
viewHolder.feedIcon.setImageResource(R.drawable.ic_rss_feed);
if (itemWithFeed.getColor() != 0)
viewHolder.feedName.setTextColor(itemWithFeed.getColor());
}
@Override
public long getItemId(int position) {
return getItem(position).getItem().getId();
}
@NonNull
@Override
public List<String> getPreloadItems(int position) {
String url = getItem(position).getItem().getImageLink();
if (getItem(position).getItem().hasImage()) {
String url = getItem(position).getItem().getImageLink();
return Collections.singletonList(url);
} else {
return Collections.emptyList();
}
return Collections.singletonList(url);
}
@Nullable
@Override
public RequestBuilder<Drawable> getPreloadRequestBuilder(@NonNull String url) {
return manager.load(url);
return glideRequests
.load(url)
.apply(requestOptions)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.transition(DrawableTransitionOptions.withCrossFade(FADE_FACTORY));
}
public interface OnItemClickListener {
@ -149,9 +175,10 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
date.setText(DateUtils.formatedDateByLocal(item.getPubDate()));
feedName.setText(itemWithFeed.getFeedName());
itemDescription.setText(item.getDescription());
}
if (itemWithFeed.getColor() != 0)
feedName.setTextColor(itemWithFeed.getColor());
public ImageView getItemImage() {
return itemImage;
}
}
}

View File

@ -2,13 +2,12 @@ package com.readrops.app.database.entities;
import android.arch.persistence.room.*;
import com.readrops.app.DateUtils;
import com.readrops.app.utils.DateUtils;
import com.readrops.readropslibrary.localfeed.atom.ATOMEntry;
import com.readrops.readropslibrary.localfeed.json.JSONItem;
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
import org.joda.time.LocalDateTime;
import org.jsoup.Jsoup;
import java.text.ParseException;
import java.util.ArrayList;

View File

@ -1,4 +1,4 @@
package com.readrops.app;
package com.readrops.app.utils;
import java.text.DateFormat;
import java.text.ParseException;

View File

@ -0,0 +1,14 @@
package com.readrops.app.utils;
import android.content.Context;
import android.support.annotation.NonNull;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;
import com.readrops.app.R;
@com.bumptech.glide.annotation.GlideModule
public class GlideModule extends AppGlideModule {
}