From 6120072730692d4d068b042e1d12aea567d9c3ac Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Fri, 6 Apr 2018 22:02:46 +0200 Subject: [PATCH] add disk cache for images --- .../keylesspalace/tusky/TuskyApplication.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java index 11f16e81d..db04465bf 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java +++ b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java @@ -39,6 +39,8 @@ import javax.inject.Inject; import dagger.android.AndroidInjector; import dagger.android.DispatchingAndroidInjector; import dagger.android.HasActivityInjector; +import okhttp3.Cache; +import okhttp3.OkHttpClient; public class TuskyApplication extends Application implements HasActivityInjector { public static final String APP_THEME_DEFAULT = ThemeUtils.THEME_NIGHT; @@ -102,15 +104,19 @@ public class TuskyApplication extends Application implements HasActivityInjector // Initialize Picasso configuration Picasso.Builder builder = new Picasso.Builder(this); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - builder.downloader(new OkHttp3Downloader(OkHttpUtils.getCompatibleClient(preferences))); + + OkHttpClient.Builder okHttpBuilder = OkHttpUtils.getCompatibleClientBuilder(preferences); + + int cacheSize = 10*1024*1024; // 10 MiB + + okHttpBuilder.cache(new Cache(getCacheDir(), cacheSize)); + + builder.downloader(new OkHttp3Downloader(okHttpBuilder.build())); if (BuildConfig.DEBUG) { builder.listener((picasso, uri, exception) -> exception.printStackTrace()); } - try { - Picasso.setSingletonInstance(builder.build()); - } catch (IllegalStateException e) { - throw new RuntimeException(e); - } + + Picasso.setSingletonInstance(builder.build()); } public ServiceLocator getServiceLocator() {