Skip to content

Commit

Permalink
Keep icons in cache when refreshing Sitemap
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mueller-ma committed Jun 27, 2024
1 parent 9cb20ff commit 9f2f647
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions mobile/src/main/java/org/openhab/habdroid/util/CacheManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package org.openhab.habdroid.util

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import android.util.LruCache
import androidx.annotation.ColorInt
import java.io.File
Expand All @@ -24,6 +25,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) {
Expand Down Expand Up @@ -53,13 +55,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
}
Expand All @@ -86,15 +90,29 @@ class CacheManager private constructor(appContext: Context) {
}

fun clearCache(alsoClearIcons: Boolean) {
Log.d(TAG, "httpCache hitCount: ${httpCache.hitCount()}, networkCount: ${httpCache.networkCount()}, " +
"requestCount: ${httpCache.requestCount()}")
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
}
}
}

Expand All @@ -118,6 +136,7 @@ class CacheManager private constructor(appContext: Context) {
)

companion object {
private var TAG = CacheManager::class.java.simpleName
private var instance: CacheManager? = null

fun getInstance(context: Context): CacheManager {
Expand Down

0 comments on commit 9f2f647

Please sign in to comment.