From 71c6c71081c2a805840386a705f0c513266564ce Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Jul 2020 12:54:17 -0400 Subject: [PATCH] Lazily find chapter directories --- .../data/download/DownloadManager.kt | 7 ++++-- .../data/download/DownloadProvider.kt | 22 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index da277fae5..240dbc2c7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -255,8 +255,11 @@ class DownloadManager(private val context: Context) { val newName = provider.getChapterDirName(newChapter) val mangaDir = provider.getMangaDir(manga, source) - // There should only be one folder with the chapter - val oldFolder = oldNames.mapNotNull { mangaDir.findFile(it) }.firstOrNull() + // Assume there's only 1 version of the chapter name formats present + val oldFolder = oldNames.asSequence() + .mapNotNull { mangaDir.findFile(it) } + .firstOrNull() + if (oldFolder?.renameTo(newName) == true) { cache.removeChapter(oldChapter, manga) cache.addChapter(newName, mangaDir, manga) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index 7845dc2e9..82852f41e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -88,7 +88,9 @@ class DownloadProvider(private val context: Context) { */ fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? { val mangaDir = findMangaDir(manga, source) - return getValidChapterDirNames(chapter).mapNotNull { mangaDir?.findFile(it) }.firstOrNull() + return getValidChapterDirNames(chapter).asSequence() + .mapNotNull { mangaDir?.findFile(it) } + .firstOrNull() } /** @@ -100,8 +102,10 @@ class DownloadProvider(private val context: Context) { */ fun findChapterDirs(chapters: List, manga: Manga, source: Source): List { val mangaDir = findMangaDir(manga, source) ?: return emptyList() - return chapters.mapNotNull { chp -> - getValidChapterDirNames(chp).mapNotNull { mangaDir.findFile(it) }.firstOrNull() + return chapters.mapNotNull { chapter -> + getValidChapterDirNames(chapter).asSequence() + .mapNotNull { mangaDir.findFile(it) } + .firstOrNull() } } @@ -130,8 +134,10 @@ class DownloadProvider(private val context: Context) { */ fun getChapterDirName(chapter: Chapter): String { return DiskUtil.buildValidFilename( - if (chapter.scanlator != null) "${chapter.scanlator}_${chapter.name}" - else chapter.name + when { + chapter.scanlator != null -> "${chapter.scanlator}_${chapter.name}" + else -> chapter.name + } ) } @@ -142,10 +148,10 @@ class DownloadProvider(private val context: Context) { */ fun getValidChapterDirNames(chapter: Chapter): List { return listOf( + getChapterDirName(chapter), + // Legacy chapter directory name used in v0.9.2 and before - DiskUtil.buildValidFilename(chapter.name), - // New chapter chapter directory name - getChapterDirName(chapter) + DiskUtil.buildValidFilename(chapter.name) ) } }