diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadCache.kt index 3e24548807..cf881ee54f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadCache.kt @@ -356,67 +356,69 @@ class AnimeDownloadCache( _isInitializing.emit(true) } - // Try to wait until extensions and sources have loaded - var sources = emptyList() - withTimeoutOrNull(30.seconds) { - extensionManager.isInitialized.first { it } - sourceManager.isInitialized.first { it } + try { + // Try to wait until extensions and sources have loaded + var sources = emptyList() + withTimeoutOrNull(30.seconds) { + extensionManager.isInitialized.first { it } + sourceManager.isInitialized.first { it } - sources = getSources() - } + sources = getSources() + } - val sourceMap = sources.associate { - provider.getSourceDirName(it).lowercase() to it.id - } + val sourceMap = sources.associate { + provider.getSourceDirName(it).lowercase() to it.id + } - rootDownloadsDirMutex.withLock { - val updatedRootDir = RootDirectory(storageManager.getDownloadsDirectory()) + rootDownloadsDirMutex.withLock { + val updatedRootDir = RootDirectory(storageManager.getDownloadsDirectory()) - updatedRootDir.sourceDirs = updatedRootDir.dir?.listFiles().orEmpty() - .filter { it.isDirectory && !it.name.isNullOrBlank() } - .mapNotNull { dir -> - val sourceId = sourceMap[dir.name!!.lowercase()] - sourceId?.let { it to SourceDirectory(dir) } - } - .toMap() - - updatedRootDir.sourceDirs.values.map { sourceDir -> - async { - sourceDir.animeDirs = sourceDir.dir?.listFiles().orEmpty() - .filter { it.isDirectory && !it.name.isNullOrBlank() } - .associate { it.name!! to AnimeDirectory(it) } - sourceDir.animeDirs.values.forEach { animeDir -> - val episodeDirs = animeDir.dir?.listFiles().orEmpty() - .mapNotNull { - when { - // Ignore incomplete downloads - it.name?.endsWith(AnimeDownloader.TMP_DIR_SUFFIX) == true -> null - - // Folder of videos - it.isDirectory -> it.name - - // MP4 files - it.isFile && it.extension == "mp4" -> it.nameWithoutExtension - - // MKV files - it.isFile && it.extension == "mkv" -> it.nameWithoutExtension - - // Anything else is irrelevant - else -> null + updatedRootDir.sourceDirs = updatedRootDir.dir?.listFiles().orEmpty() + .filter { it.isDirectory && !it.name.isNullOrBlank() } + .mapNotNull { dir -> + val sourceId = sourceMap[dir.name!!.lowercase()] + sourceId?.let { it to SourceDirectory(dir) } + } + .toMap() + + updatedRootDir.sourceDirs.values.map { sourceDir -> + async { + sourceDir.animeDirs = sourceDir.dir?.listFiles().orEmpty() + .filter { it.isDirectory && !it.name.isNullOrBlank() } + .associate { it.name!! to AnimeDirectory(it) } + sourceDir.animeDirs.values.forEach { animeDir -> + val episodeDirs = animeDir.dir?.listFiles().orEmpty() + .mapNotNull { + when { + // Ignore incomplete downloads + it.name?.endsWith(AnimeDownloader.TMP_DIR_SUFFIX) == true -> null + + // Folder of videos + it.isDirectory -> it.name + + // MP4 files + it.isFile && it.extension == "mp4" -> it.nameWithoutExtension + + // MKV files + it.isFile && it.extension == "mkv" -> it.nameWithoutExtension + + // Anything else is irrelevant + else -> null + } } - } - .toMutableSet() + .toMutableSet() - animeDir.episodeDirs = episodeDirs + animeDir.episodeDirs = episodeDirs + } } } - } - .awaitAll() + .awaitAll() - rootDownloadsDir = updatedRootDir + rootDownloadsDir = updatedRootDir + } + } finally { + _isInitializing.value = false } - - _isInitializing.emit(false) }.also { it.invokeOnCompletion(onCancelling = true) { exception -> if (exception != null && exception !is CancellationException) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadCache.kt index 17857c7aed..639c110023 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadCache.kt @@ -358,62 +358,64 @@ class MangaDownloadCache( _isInitializing.emit(true) } - // Try to wait until extensions and sources have loaded - var sources = emptyList() - withTimeoutOrNull(30.seconds) { - extensionManager.isInitialized.first { it } - sourceManager.isInitialized.first { it } + try { + // Try to wait until extensions and sources have loaded + var sources = emptyList() + withTimeoutOrNull(30.seconds) { + extensionManager.isInitialized.first { it } + sourceManager.isInitialized.first { it } - sources = getSources() - } + sources = getSources() + } - val sourceMap = sources.associate { provider.getSourceDirName(it).lowercase() to it.id } + val sourceMap = sources.associate { provider.getSourceDirName(it).lowercase() to it.id } - rootDownloadsDirMutex.withLock { - val updatedRootDir = RootDirectory(storageManager.getDownloadsDirectory()) + rootDownloadsDirMutex.withLock { + val updatedRootDir = RootDirectory(storageManager.getDownloadsDirectory()) - updatedRootDir.sourceDirs = updatedRootDir.dir?.listFiles().orEmpty() - .filter { it.isDirectory && !it.name.isNullOrBlank() } - .mapNotNull { dir -> - val sourceId = sourceMap[dir.name!!.lowercase()] - sourceId?.let { it to SourceDirectory(dir) } - } - .toMap() - - updatedRootDir.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(MangaDownloader.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 + updatedRootDir.sourceDirs = updatedRootDir.dir?.listFiles().orEmpty() + .filter { it.isDirectory && !it.name.isNullOrBlank() } + .mapNotNull { dir -> + val sourceId = sourceMap[dir.name!!.lowercase()] + sourceId?.let { it to SourceDirectory(dir) } + } + .toMap() + + updatedRootDir.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(MangaDownloader.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() + .awaitAll() - rootDownloadsDir = updatedRootDir + rootDownloadsDir = updatedRootDir + } + } finally { + _isInitializing.value = false } - - _isInitializing.emit(false) }.also { it.invokeOnCompletion(onCancelling = true) { exception -> if (exception != null && exception !is CancellationException) {