Fix clearing caches

master
Zakhar Timoshenko 2 years ago
parent 2a6edad196
commit cdd4ff4eb4
Signed by: Xtimms
SSH Key Fingerprint: SHA256:wH6spYepK/A5erBh7ZyAnr1ru9H4eaMVBEuiw6DSpxI

@ -2,6 +2,8 @@ package org.xtimms.tokusho.sections.settings.storage
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.runInterruptible
@ -21,17 +23,18 @@ class StorageViewModel @Inject constructor(
val httpCacheSize = MutableStateFlow(-1L) val httpCacheSize = MutableStateFlow(-1L)
val cacheSizes = EnumMap<CacheDir, MutableStateFlow<Long>>(CacheDir::class.java) val cacheSizes = EnumMap<CacheDir, MutableStateFlow<Long>>(CacheDir::class.java)
private var storageUsageJob: Job? = null
init { init {
launchJob(Dispatchers.Default) { val prevJob = storageUsageJob
setLoading(true) storageUsageJob = launchJob(Dispatchers.Default) {
httpCacheSize.value = runInterruptible { httpCache.size() } prevJob?.cancelAndJoin()
mutableUiState.update { mutableUiState.update {
it.copy( it.copy(
availableSpace = storageManager.computeAvailableSize(), availableSpace = storageManager.computeAvailableSize(),
pagesCache = storageManager.computeCacheSize(CacheDir.PAGES), pagesCache = storageManager.computeCacheSize(CacheDir.PAGES),
thumbnailsCache = storageManager.computeCacheSize(CacheDir.THUMBS), thumbnailsCache = storageManager.computeCacheSize(CacheDir.THUMBS),
httpCacheSize = httpCacheSize.value, httpCacheSize = runInterruptible { httpCache.size() }
isLoading = false
) )
} }
} }
@ -41,13 +44,13 @@ class StorageViewModel @Inject constructor(
launchJob(Dispatchers.Default) { launchJob(Dispatchers.Default) {
try { try {
storageManager.clearCache(cache) storageManager.clearCache(cache)
checkNotNull(cacheSizes[cache]).value = storageManager.computeCacheSize(cache) storageManager.computeCacheSize(cache)
mutableUiState.update { mutableUiState.update {
it.copy( it.copy(
availableSpace = storageManager.computeAvailableSize(), availableSpace = storageManager.computeAvailableSize(),
pagesCache = storageManager.computeCacheSize(CacheDir.PAGES), pagesCache = storageManager.computeCacheSize(CacheDir.PAGES),
thumbnailsCache = storageManager.computeCacheSize(CacheDir.THUMBS), thumbnailsCache = storageManager.computeCacheSize(CacheDir.THUMBS),
httpCacheSize = httpCacheSize.value, httpCacheSize = runInterruptible { httpCache.size() },
isLoading = false isLoading = false
) )
} }
@ -64,13 +67,12 @@ class StorageViewModel @Inject constructor(
httpCache.evictAll() httpCache.evictAll()
httpCache.size() httpCache.size()
} }
httpCacheSize.value = size
mutableUiState.update { mutableUiState.update {
it.copy( it.copy(
availableSpace = storageManager.computeAvailableSize(), availableSpace = storageManager.computeAvailableSize(),
pagesCache = storageManager.computeCacheSize(CacheDir.PAGES), pagesCache = storageManager.computeCacheSize(CacheDir.PAGES),
thumbnailsCache = storageManager.computeCacheSize(CacheDir.THUMBS), thumbnailsCache = storageManager.computeCacheSize(CacheDir.THUMBS),
httpCacheSize = httpCacheSize.value, httpCacheSize = size,
isLoading = false isLoading = false
) )
} }

Loading…
Cancel
Save