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 28, 2024
1 parent 9cb20ff commit bbcf84a
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 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 @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit bbcf84a

Please sign in to comment.