Check if icon is already cached

This commit is contained in:
Alex Sytnyk 2018-04-30 18:23:20 +03:00
parent ad4d3e3603
commit 4d5d60b86c
4 changed files with 41 additions and 2 deletions

View file

@ -10,6 +10,8 @@ import com.squareup.picasso.Picasso;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
@ -28,6 +30,8 @@ public class PicassoUtils {
private static boolean initialized;
private static Map<String, Boolean> cached = new HashMap<>();
public static void setupPicasso(@NonNull Context context) {
if (!initialized) {
File cacheDir = createDefaultCacheDir(context);
@ -57,6 +61,19 @@ public class PicassoUtils {
e.printStackTrace();
}
}
cached.clear();
}
public static Boolean isCached(@NonNull String key) {
return cached.get(key);
}
public static void setCached(@NonNull String key, boolean val) {
cached.put(key, val);
}
public static void clearCachedMap() {
cached.clear();
}
public static long getDiskCacheSizeBytes() throws IOException {

View file

@ -16,6 +16,7 @@ import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import net.osmand.AndroidUtils;
import net.osmand.PicassoUtils;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -76,22 +77,29 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
} else {
final ItemVH holder = (ItemVH) viewHolder;
TravelArticle article = (TravelArticle) getItem(position);
final String url = TravelArticle.getImageUrl(article.getImageTitle(), false);
Boolean cached = PicassoUtils.isCached(url);
boolean lastItem = position == getItemCount() - 1;
RequestCreator rc = Picasso.get()
.load(TravelArticle.getImageUrl(article.getImageTitle(), false));
.load(url);
WikivoyageUtils.setupNetworkPolicy(settings, rc);
rc.transform(new CropCircleTransformation())
.into(holder.icon, new Callback() {
@Override
public void onSuccess() {
holder.icon.setVisibility(View.VISIBLE);
PicassoUtils.setCached(url, true);
}
@Override
public void onError(Exception e) {
holder.icon.setVisibility(View.GONE);
PicassoUtils.setCached(url, false);
}
});
holder.icon.setVisibility(cached != null && cached ? View.VISIBLE : View.GONE);
holder.title.setText(article.getTitle());
holder.content.setText(article.getContent());
holder.partOf.setText(article.getGeoDescription());

View file

@ -143,6 +143,12 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen
return mainView;
}
@Override
public void onDestroy() {
super.onDestroy();
PicassoUtils.clearCachedMap();
}
protected void onDataLoaded() {
mainView.findViewById(R.id.progress_bar).setVisibility(View.GONE);
updateSearchVisibility();

View file

@ -12,6 +12,7 @@ import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import net.osmand.PicassoUtils;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.widgets.tools.CropCircleTransformation;
@ -40,21 +41,28 @@ public class ArticleTravelCard extends BaseTravelCard {
public void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof ArticleTravelVH) {
final ArticleTravelVH holder = (ArticleTravelVH) viewHolder;
final String url = TravelArticle.getImageUrl(article.getImageTitle(), false);
Boolean cached = PicassoUtils.isCached(url);
RequestCreator rc = Picasso.get()
.load(TravelArticle.getImageUrl(article.getImageTitle(), false));
.load(url);
WikivoyageUtils.setupNetworkPolicy(app.getSettings(), rc);
rc.transform(new CropCircleTransformation())
.into(holder.icon, new Callback() {
@Override
public void onSuccess() {
holder.icon.setVisibility(View.VISIBLE);
PicassoUtils.setCached(url, true);
}
@Override
public void onError(Exception e) {
holder.icon.setVisibility(View.GONE);
PicassoUtils.setCached(url, false);
}
});
holder.icon.setVisibility(cached != null && cached ? View.VISIBLE : View.GONE);
holder.title.setText(article.getTitle());
holder.content.setText(article.getPartialContent());
holder.partOf.setText(article.getGeoDescription());