Fix issue with picasso

This commit is contained in:
Victor Shcherb 2018-05-16 21:57:16 +02:00
parent 1c2893bdc2
commit 277b51bcee
5 changed files with 46 additions and 45 deletions

View file

@ -13,9 +13,9 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.osmand.plus.OsmandApplication;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
@ -24,33 +24,36 @@ public class PicassoUtils {
private static final String PICASSO_CACHE = "picasso-cache";
private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // 5MB
private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
private static PicassoUtils INSTANCE;
private static Cache diskCache;
private static LruCache memoryCache;
private Cache diskCache;
private LruCache memoryCache;
private static boolean initialized;
private Map<String, Boolean> cached = new HashMap<>();
private PicassoUtils(OsmandApplication app){
File cacheDir = createDefaultCacheDir(app);
private static Map<String, Boolean> cached = new HashMap<>();
diskCache = new Cache(cacheDir, calculateDiskCacheSize(cacheDir));
memoryCache = new LruCache(app);
public static void setupPicasso(@NonNull Context context) {
if (!initialized) {
File cacheDir = createDefaultCacheDir(context);
Picasso picasso = new Picasso.Builder(app)
.downloader(new OkHttp3Downloader(new OkHttpClient.Builder().cache(diskCache).build()))
.memoryCache(memoryCache)
.build();
diskCache = new Cache(cacheDir, calculateDiskCacheSize(cacheDir));
memoryCache = new LruCache(context);
Picasso.setSingletonInstance(picasso);
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(new OkHttpClient.Builder().cache(diskCache).build()))
.memoryCache(memoryCache)
.build();
Picasso.setSingletonInstance(picasso);
initialized = true;
}
}
public static void clearAllPicassoCache() {
public static PicassoUtils getPicasso(@NonNull OsmandApplication app) {
if(INSTANCE == null) {
INSTANCE = new PicassoUtils(app);
}
return INSTANCE;
}
public void clearAllPicassoCache() {
if (memoryCache != null) {
memoryCache.clear();
}
@ -64,19 +67,19 @@ public class PicassoUtils {
cached.clear();
}
public static Boolean isCached(@NonNull String key) {
public Boolean isURLLoaded(@NonNull String key) {
return cached.get(key);
}
public static void setCached(@NonNull String key, boolean val) {
public void setResultLoaded(@NonNull String key, boolean val) {
cached.put(key, val);
}
public static void clearCachedMap() {
public void clearCachedMap() {
cached.clear();
}
public static long getDiskCacheSizeBytes() throws IOException {
public long getDiskCacheSizeBytes() throws IOException {
return diskCache.size();
}

View file

@ -43,6 +43,7 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
private final Drawable readIcon;
private final Drawable deleteIcon;
private PicassoUtils picasso;
public void setListener(Listener listener) {
this.listener = listener;
@ -51,6 +52,7 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
SavedArticlesRvAdapter(OsmandApplication app) {
this.app = app;
this.settings = app.getSettings();
picasso = PicassoUtils.getPicasso(app);
int colorId = settings.isLightContent()
? R.color.wikivoyage_active_light : R.color.wikivoyage_active_dark;
@ -78,28 +80,27 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
final ItemVH holder = (ItemVH) viewHolder;
TravelArticle article = (TravelArticle) getItem(position);
final String url = TravelArticle.getImageUrl(article.getImageTitle(), false);
Boolean cached = PicassoUtils.isCached(url);
Boolean loaded = picasso.isURLLoaded(url);
boolean lastItem = position == getItemCount() - 1;
RequestCreator rc = Picasso.get()
.load(url);
RequestCreator rc = Picasso.get().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);
picasso.setResultLoaded(url, true);
}
@Override
public void onError(Exception e) {
holder.icon.setVisibility(View.GONE);
PicassoUtils.setCached(url, false);
picasso.setResultLoaded(url, false);
}
});
holder.icon.setVisibility(cached != null && cached ? View.VISIBLE : View.GONE);
holder.icon.setVisibility(loaded == null || loaded.booleanValue() ? View.VISIBLE : View.GONE);
holder.title.setText(article.getTitle());
holder.content.setText(article.getContent());
holder.partOf.setText(article.getGeoDescription());

View file

@ -45,13 +45,12 @@ public class WikivoyageExploreDialogFragment extends WikiBaseDialogFragment impl
private ExploreTabFragment exploreTabFragment;
private SavedArticlesTabFragment savedArticlesTabFragment;
private PicassoUtils utils;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getContext();
if (context != null) {
PicassoUtils.setupPicasso(context);
}
utils = PicassoUtils.getPicasso(getMyApplication());
}
@Nullable
@ -145,12 +144,7 @@ public class WikivoyageExploreDialogFragment extends WikiBaseDialogFragment impl
populateData();
}
@Override
public void onDestroy() {
super.onDestroy();
PicassoUtils.clearCachedMap();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

View file

@ -103,7 +103,7 @@ public class WikivoyageOptionsBottomSheetDialogFragment extends MenuBottomSheetD
@Override
public void onClick(View v) {
new WebView(getContext()).clearCache(true);
PicassoUtils.clearAllPicassoCache();
PicassoUtils.getPicasso(getMyApplication()).clearAllPicassoCache();
sendResult(CACHE_CLEARED);
dismiss();
}

View file

@ -31,11 +31,14 @@ public class ArticleTravelCard extends BaseTravelCard {
private FragmentManager fragmentManager;
private boolean isLastItem;
private PicassoUtils picasso;
public ArticleTravelCard(OsmandApplication app, boolean nightMode, TravelArticle article, FragmentManager fragmentManager) {
super(app, nightMode);
this.article = article;
readIcon = getActiveIcon(R.drawable.ic_action_read_article);
this.fragmentManager = fragmentManager;
picasso = PicassoUtils.getPicasso(app);
}
@Override
@ -43,7 +46,7 @@ public class ArticleTravelCard extends BaseTravelCard {
if (viewHolder instanceof ArticleTravelVH) {
final ArticleTravelVH holder = (ArticleTravelVH) viewHolder;
final String url = TravelArticle.getImageUrl(article.getImageTitle(), false);
Boolean cached = PicassoUtils.isCached(url);
Boolean loaded = picasso.isURLLoaded(url);
RequestCreator rc = Picasso.get()
.load(url);
@ -53,17 +56,17 @@ public class ArticleTravelCard extends BaseTravelCard {
@Override
public void onSuccess() {
holder.icon.setVisibility(View.VISIBLE);
PicassoUtils.setCached(url, true);
picasso.setResultLoaded(url, true);
}
@Override
public void onError(Exception e) {
holder.icon.setVisibility(View.GONE);
PicassoUtils.setCached(url, false);
picasso.setResultLoaded(url, false);
}
});
holder.icon.setVisibility(cached != null && cached ? View.VISIBLE : View.GONE);
holder.icon.setVisibility(loaded == null || loaded.booleanValue() ? View.VISIBLE : View.GONE);
holder.title.setText(article.getTitle());
holder.content.setText(WikiArticleHelper.getPartialContent(article.getContent()));
holder.partOf.setText(article.getGeoDescription());