Don't let the stored download index overwrite a startup rescan (#3968)
Closes #3968 Fixes #3526 Fixes #2808 Fixes #1429 Co-authored-by: Chopper <156493704+choppeh@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
package eu.kanade.tachiyomi.data.download
|
package eu.kanade.tachiyomi.data.download
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.AtomicFile
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
|
import androidx.core.util.writeBytes
|
||||||
import com.hippo.unifile.UniFile
|
import com.hippo.unifile.UniFile
|
||||||
import dev.zacsweers.metro.AppScope
|
import dev.zacsweers.metro.AppScope
|
||||||
import dev.zacsweers.metro.Inject
|
import dev.zacsweers.metro.Inject
|
||||||
@@ -25,7 +27,6 @@ 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.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
|
||||||
@@ -88,25 +89,26 @@ class DownloadCache(
|
|||||||
*/
|
*/
|
||||||
private var lastRenew = 0L
|
private var lastRenew = 0L
|
||||||
private var renewalJob: Job? = null
|
private var renewalJob: Job? = null
|
||||||
|
private val initJob: Job
|
||||||
|
|
||||||
private val _isInitializing = MutableStateFlow(false)
|
private val _isInitializing = MutableStateFlow(false)
|
||||||
val isInitializing = _isInitializing
|
val isInitializing = _isInitializing
|
||||||
.debounce(1.seconds) // Don't notify if it finishes quickly enough
|
.debounce(1.seconds) // Don't notify if it finishes quickly enough
|
||||||
.stateIn(scope, SharingStarted.WhileSubscribed(), false)
|
.stateIn(scope, SharingStarted.WhileSubscribed(), false)
|
||||||
|
|
||||||
private val diskCacheFile: File
|
private val diskCacheFile: AtomicFile
|
||||||
get() = File(context.cacheDir, "dl_index_cache_v3")
|
get() = AtomicFile(File(context.cacheDir, "dl_index_cache_v3"))
|
||||||
|
|
||||||
private val rootDownloadsDirMutex = Mutex()
|
private val rootDownloadsDirMutex = Mutex()
|
||||||
private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory())
|
private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// Attempt to read cache file
|
// Attempt to read cache file
|
||||||
scope.launch {
|
initJob = scope.launchIO {
|
||||||
rootDownloadsDirMutex.withLock {
|
rootDownloadsDirMutex.withLock {
|
||||||
try {
|
try {
|
||||||
if (diskCacheFile.exists()) {
|
if (diskCacheFile.baseFile.exists()) {
|
||||||
val diskCache = diskCacheFile.inputStream().use {
|
val diskCache = diskCacheFile.openRead().use {
|
||||||
ProtoBuf.decodeFromByteArray<RootDirectory>(it.readBytes())
|
ProtoBuf.decodeFromByteArray<RootDirectory>(it.readBytes())
|
||||||
}
|
}
|
||||||
rootDownloadsDir = diskCache
|
rootDownloadsDir = diskCache
|
||||||
@@ -360,6 +362,9 @@ class DownloadCache(
|
|||||||
}
|
}
|
||||||
|
|
||||||
renewalJob = scope.launchIO {
|
renewalJob = scope.launchIO {
|
||||||
|
// The disk index is older than this scan and would replace it if read afterwards
|
||||||
|
initJob.join()
|
||||||
|
|
||||||
if (lastRenew == 0L) {
|
if (lastRenew == 0L) {
|
||||||
_isInitializing.emit(true)
|
_isInitializing.emit(true)
|
||||||
}
|
}
|
||||||
@@ -445,8 +450,9 @@ class DownloadCache(
|
|||||||
updateDiskCacheJob?.cancel()
|
updateDiskCacheJob?.cancel()
|
||||||
updateDiskCacheJob = scope.launchIO {
|
updateDiskCacheJob = scope.launchIO {
|
||||||
delay(1.seconds)
|
delay(1.seconds)
|
||||||
ensureActive()
|
val bytes = rootDownloadsDirMutex.withLock {
|
||||||
val bytes = ProtoBuf.encodeToByteArray(rootDownloadsDir)
|
ProtoBuf.encodeToByteArray(rootDownloadsDir)
|
||||||
|
}
|
||||||
ensureActive()
|
ensureActive()
|
||||||
try {
|
try {
|
||||||
diskCacheFile.writeBytes(bytes)
|
diskCacheFile.writeBytes(bytes)
|
||||||
|
|||||||
Reference in New Issue
Block a user