mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-10 12:47:26 +01:00
DownloadCache: Fix freezing on initial loading of cache file (#9523)
This commit is contained in:
parent
c27bf4e866
commit
73118d4af7
@ -26,7 +26,7 @@ import kotlinx.coroutines.flow.onStart
|
|||||||
import kotlinx.coroutines.flow.receiveAsFlow
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||||||
import kotlinx.coroutines.flow.shareIn
|
import kotlinx.coroutines.flow.shareIn
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withTimeoutOrNull
|
import kotlinx.coroutines.withTimeoutOrNull
|
||||||
@ -96,7 +96,7 @@ class DownloadCache(
|
|||||||
get() = File(context.cacheDir, "dl_index_cache")
|
get() = File(context.cacheDir, "dl_index_cache")
|
||||||
|
|
||||||
private val rootDownloadsDirLock = Mutex()
|
private val rootDownloadsDirLock = Mutex()
|
||||||
private var rootDownloadsDir: RootDirectory
|
private var rootDownloadsDir = RootDirectory(getDirectoryFromPreference())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
downloadPreferences.downloadsDirectory().changes()
|
downloadPreferences.downloadsDirectory().changes()
|
||||||
@ -106,20 +106,20 @@ class DownloadCache(
|
|||||||
}
|
}
|
||||||
.launchIn(scope)
|
.launchIn(scope)
|
||||||
|
|
||||||
rootDownloadsDir = runBlocking(Dispatchers.IO) {
|
// Attempt to read cache file
|
||||||
try {
|
scope.launch {
|
||||||
val diskCache = diskCacheFile.inputStream().use {
|
rootDownloadsDirLock.withLock {
|
||||||
ProtoBuf.decodeFromByteArray<RootDirectory>(it.readBytes())
|
try {
|
||||||
|
val diskCache = diskCacheFile.inputStream().use {
|
||||||
|
ProtoBuf.decodeFromByteArray<RootDirectory>(it.readBytes())
|
||||||
|
}
|
||||||
|
rootDownloadsDir = diskCache
|
||||||
|
lastRenew = System.currentTimeMillis()
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
diskCacheFile.delete()
|
||||||
}
|
}
|
||||||
lastRenew = 1 // Just so that the banner won't show up
|
|
||||||
diskCache
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
diskCacheFile.delete()
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
} ?: RootDirectory(getDirectoryFromPreference())
|
}
|
||||||
|
|
||||||
notifyChanges()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user