diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index ef698785e..8b8a6f20a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -105,13 +105,13 @@ class DownloadCache( private val diskCacheFile: File get() = File(context.cacheDir, "dl_index_cache_v3") - private val rootDownloadsDirLock = Mutex() + private val rootDownloadsDirMutex = Mutex() private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory()) init { // Attempt to read cache file scope.launch { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { try { if (diskCacheFile.exists()) { val diskCache = diskCacheFile.inputStream().use { @@ -121,7 +121,7 @@ class DownloadCache( lastRenew = System.currentTimeMillis() } } catch (e: Throwable) { - logcat(LogPriority.ERROR, e) { "Failed to initialize disk cache" } + logcat(LogPriority.ERROR, e) { "Failed to initialize from disk cache" } diskCacheFile.delete() } } @@ -211,7 +211,7 @@ class DownloadCache( * @param manga the manga of the chapter. */ suspend fun addChapter(chapterDirName: String, mangaUniFile: UniFile, manga: Manga) { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { // Retrieve the cached source directory or cache a new one var sourceDir = rootDownloadsDir.sourceDirs[manga.source] if (sourceDir == null) { @@ -243,7 +243,7 @@ class DownloadCache( * @param manga the manga of the chapter. */ suspend fun removeChapter(chapter: Chapter, manga: Manga) { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return val mangaDir = sourceDir.mangaDirs[ provider.getMangaDirName( @@ -262,7 +262,7 @@ class DownloadCache( // SY --> suspend fun removeFolders(folders: List, manga: Manga) { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return val mangaDir = sourceDir.mangaDirs[provider.getMangaDirName(manga.ogTitle)] ?: return folders.forEach { chapter -> @@ -282,7 +282,7 @@ class DownloadCache( * @param manga the manga of the chapter. */ suspend fun removeChapters(chapters: List, manga: Manga) { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return val mangaDir = sourceDir.mangaDirs[ provider.getMangaDirName( @@ -307,7 +307,7 @@ class DownloadCache( * @param manga the manga to remove. */ suspend fun removeManga(manga: Manga) { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { val sourceDir = rootDownloadsDir.sourceDirs[manga.source] ?: return val mangaDirName = provider.getMangaDirName(/* SY --> */ manga.ogTitle /* SY <-- */) if (sourceDir.mangaDirs.containsKey(mangaDirName)) { @@ -319,7 +319,7 @@ class DownloadCache( } suspend fun removeSource(source: Source) { - rootDownloadsDirLock.withLock { + rootDownloadsDirMutex.withLock { rootDownloadsDir.sourceDirs -= source.id } @@ -382,10 +382,10 @@ class DownloadCache( val sourceMap = sources.associate { provider.getSourceDirName(it).lowercase() to it.id } - rootDownloadsDirLock.withLock { - rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory()) + rootDownloadsDirMutex.withLock { + val updatedRootDir = RootDirectory(storageManager.getDownloadsDirectory()) - val sourceDirs = rootDownloadsDir.dir?.listFiles().orEmpty() + updatedRootDir.sourceDirs = updatedRootDir.dir?.listFiles().orEmpty() .filter { it.isDirectory && !it.name.isNullOrBlank() } .mapNotNull { dir -> val sourceId = sourceMap[dir.name!!.lowercase()] @@ -393,36 +393,35 @@ class DownloadCache( } .toMap() - rootDownloadsDir.sourceDirs = sourceDirs + updatedRootDir.sourceDirs.values.map { sourceDir -> + async { + sourceDir.mangaDirs = sourceDir.dir?.listFiles().orEmpty() + .filter { it.isDirectory && !it.name.isNullOrBlank() } + .associate { it.name!! to MangaDirectory(it) } - sourceDirs.values - .map { sourceDir -> - async { - sourceDir.mangaDirs = sourceDir.dir?.listFiles().orEmpty() - .filter { it.isDirectory && !it.name.isNullOrBlank() } - .associate { it.name!! to MangaDirectory(it) } - - sourceDir.mangaDirs.values.forEach { mangaDir -> - val chapterDirs = mangaDir.dir?.listFiles().orEmpty() - .mapNotNull { - when { - // Ignore incomplete downloads - it.name?.endsWith(Downloader.TMP_DIR_SUFFIX) == true -> null - // Folder of images - it.isDirectory -> it.name - // CBZ files - it.isFile && it.extension == "cbz" -> it.nameWithoutExtension - // Anything else is irrelevant - else -> null - } + sourceDir.mangaDirs.values.forEach { mangaDir -> + val chapterDirs = mangaDir.dir?.listFiles().orEmpty() + .mapNotNull { + when { + // Ignore incomplete downloads + it.name?.endsWith(Downloader.TMP_DIR_SUFFIX) == true -> null + // Folder of images + it.isDirectory -> it.name + // CBZ files + it.isFile && it.extension == "cbz" -> it.nameWithoutExtension + // Anything else is irrelevant + else -> null } - .toMutableSet() + } + .toMutableSet() - mangaDir.chapterDirs = chapterDirs - } + mangaDir.chapterDirs = chapterDirs } } + } .awaitAll() + + rootDownloadsDir = updatedRootDir } _isInitializing.emit(false) diff --git a/i18n/src/commonMain/moko-resources/base/strings.xml b/i18n/src/commonMain/moko-resources/base/strings.xml index 0b6c1dff7..bbf15d593 100644 --- a/i18n/src/commonMain/moko-resources/base/strings.xml +++ b/i18n/src/commonMain/moko-resources/base/strings.xml @@ -576,7 +576,7 @@ Reset default user agent string Requires app restart to take effect Cookies cleared - Invalidate downloads index + Reindex downloads Force app to recheck downloaded chapters Downloads index invalidated Clear database