Lazily find chapter directories

This commit is contained in:
arkon 2020-07-25 12:54:17 -04:00
parent d2b14bcfc4
commit 71c6c71081
2 changed files with 19 additions and 10 deletions

View File

@ -255,8 +255,11 @@ class DownloadManager(private val context: Context) {
val newName = provider.getChapterDirName(newChapter) val newName = provider.getChapterDirName(newChapter)
val mangaDir = provider.getMangaDir(manga, source) val mangaDir = provider.getMangaDir(manga, source)
// There should only be one folder with the chapter // Assume there's only 1 version of the chapter name formats present
val oldFolder = oldNames.mapNotNull { mangaDir.findFile(it) }.firstOrNull() val oldFolder = oldNames.asSequence()
.mapNotNull { mangaDir.findFile(it) }
.firstOrNull()
if (oldFolder?.renameTo(newName) == true) { if (oldFolder?.renameTo(newName) == true) {
cache.removeChapter(oldChapter, manga) cache.removeChapter(oldChapter, manga)
cache.addChapter(newName, mangaDir, manga) cache.addChapter(newName, mangaDir, manga)

View File

@ -88,7 +88,9 @@ class DownloadProvider(private val context: Context) {
*/ */
fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? { fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? {
val mangaDir = findMangaDir(manga, source) 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<Chapter>, manga: Manga, source: Source): List<UniFile> { fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): List<UniFile> {
val mangaDir = findMangaDir(manga, source) ?: return emptyList() val mangaDir = findMangaDir(manga, source) ?: return emptyList()
return chapters.mapNotNull { chp -> return chapters.mapNotNull { chapter ->
getValidChapterDirNames(chp).mapNotNull { mangaDir.findFile(it) }.firstOrNull() getValidChapterDirNames(chapter).asSequence()
.mapNotNull { mangaDir.findFile(it) }
.firstOrNull()
} }
} }
@ -130,8 +134,10 @@ class DownloadProvider(private val context: Context) {
*/ */
fun getChapterDirName(chapter: Chapter): String { fun getChapterDirName(chapter: Chapter): String {
return DiskUtil.buildValidFilename( return DiskUtil.buildValidFilename(
if (chapter.scanlator != null) "${chapter.scanlator}_${chapter.name}" when {
else chapter.name 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<String> { fun getValidChapterDirNames(chapter: Chapter): List<String> {
return listOf( return listOf(
getChapterDirName(chapter),
// Legacy chapter directory name used in v0.9.2 and before // Legacy chapter directory name used in v0.9.2 and before
DiskUtil.buildValidFilename(chapter.name), DiskUtil.buildValidFilename(chapter.name)
// New chapter chapter directory name
getChapterDirName(chapter)
) )
} }
} }