From bbcf84a8fcc56b3a51711488c33afe677411286e Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Thu, 27 Jun 2024 18:47:19 +0200 Subject: [PATCH] Keep icons in cache when refreshing Sitemap When the reload is triggered via the pull-down, don't remove icons from the cache. Especially over myopenhab.org it takes some seconds until all icons have been loaded. Signed-off-by: mueller-ma --- .../org/openhab/habdroid/util/CacheManager.kt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/util/CacheManager.kt b/mobile/src/main/java/org/openhab/habdroid/util/CacheManager.kt index 9248d1669f..d47b6aeb1c 100644 --- a/mobile/src/main/java/org/openhab/habdroid/util/CacheManager.kt +++ b/mobile/src/main/java/org/openhab/habdroid/util/CacheManager.kt @@ -24,6 +24,7 @@ import java.io.IOException import java.io.InputStream import okhttp3.Cache import okhttp3.HttpUrl +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import org.openhab.habdroid.model.IconFormat class CacheManager private constructor(appContext: Context) { @@ -53,13 +54,15 @@ class CacheManager private constructor(appContext: Context) { } private fun targetCache(url: HttpUrl): BitmapCache { - return if (url.pathSegments.firstOrNull() == "icon" && url.pathSegments[1].isNotEmpty()) { + return if (url.isIconUrl()) { iconBitmapCache } else { temporaryBitmapCache } } + private fun HttpUrl.isIconUrl() = pathSegments.firstOrNull() == "icon" && pathSegments[1].isNotEmpty() + fun isBitmapCached(url: HttpUrl, @ColorInt fallbackColor: Int): Boolean { return getCachedBitmap(url, fallbackColor) != null } @@ -87,14 +90,26 @@ class CacheManager private constructor(appContext: Context) { fun clearCache(alsoClearIcons: Boolean) { temporaryBitmapCache.evictAll() - try { - httpCache.evictAll() - } catch (ignored: IOException) { - // ignored - } if (alsoClearIcons) { + try { + httpCache.evictAll() + } catch (ignored: IOException) { + // ignored + } widgetIconDirectory?.listFiles()?.forEach { f -> f.delete() } iconBitmapCache.evictAll() + } else { + // Don't evict icons from httpCache + try { + val urlIterator = httpCache.urls() + while (urlIterator.hasNext()) { + if (urlIterator.next().toHttpUrlOrNull()?.isIconUrl() == false) { + urlIterator.remove() + } + } + } catch (ignored: IOException) { + // ignored + } } } @@ -118,6 +133,7 @@ class CacheManager private constructor(appContext: Context) { ) companion object { + private val TAG = CacheManager::class.java.simpleName private var instance: CacheManager? = null fun getInstance(context: Context): CacheManager {