Formatted download cache

This commit is contained in:
Jay 2020-05-08 01:31:45 -04:00
parent 4897b721e5
commit 1cc31049c1

View File

@ -47,9 +47,7 @@ class DownloadCache(
private var mangaFiles: MutableMap<Long, MutableSet<String>> = mutableMapOf() private var mangaFiles: MutableMap<Long, MutableSet<String>> = mutableMapOf()
init { init {
preferences.downloadsDirectory().asObservable() preferences.downloadsDirectory().asObservable().skip(1).subscribe {
.skip(1)
.subscribe {
lastRenew = 0L // invalidate cache lastRenew = 0L // invalidate cache
} }
} }
@ -78,9 +76,11 @@ class DownloadCache(
checkRenew() checkRenew()
val files = mangaFiles[manga.id]?.toSet() ?: return false val files = mangaFiles[manga.id]?.toSet() ?: return false
return files.any { file -> provider.getValidChapterDirNames(chapter).any { return files.any { file ->
provider.getValidChapterDirNames(chapter).any {
it.toLowerCase() == file.toLowerCase() it.toLowerCase() == file.toLowerCase()
} } }
}
} }
/** /**
@ -112,10 +112,8 @@ class DownloadCache(
private fun renew() { private fun renew() {
val onlineSources = sourceManager.getOnlineSources() val onlineSources = sourceManager.getOnlineSources()
val sourceDirs = getDirectoryFromPreference().listFiles() val sourceDirs = getDirectoryFromPreference().listFiles().orEmpty()
.orEmpty() .associate { it.name to SourceDirectory(it) }.mapNotNullKeys { entry ->
.associate { it.name to SourceDirectory(it) }
.mapNotNullKeys { entry ->
onlineSources.find { provider.getSourceDirName(it) == entry.key }?.id onlineSources.find { provider.getSourceDirName(it) == entry.key }?.id
} }
@ -124,29 +122,29 @@ class DownloadCache(
sourceDirs.forEach { sourceValue -> sourceDirs.forEach { sourceValue ->
val sourceMangasRaw = mangas[sourceValue.key]?.toMutableSet() ?: return@forEach val sourceMangasRaw = mangas[sourceValue.key]?.toMutableSet() ?: return@forEach
val sourceMangas = arrayOf(sourceMangasRaw.filter { it.favorite }, sourceMangasRaw val sourceMangas = arrayOf(sourceMangasRaw.filter { it.favorite },
.filterNot { it.favorite }) sourceMangasRaw.filterNot { it.favorite })
val sourceDir = sourceValue.value val sourceDir = sourceValue.value
val mangaDirs = sourceDir.dir.listFiles() val mangaDirs = sourceDir.dir.listFiles().orEmpty().mapNotNull {
.orEmpty()
.mapNotNull {
val name = it.name ?: return@mapNotNull null val name = it.name ?: return@mapNotNull null
name to MangaDirectory(it) }.toMap() name to MangaDirectory(it)
}.toMap()
mangaDirs.values.forEach { mangaDir -> mangaDirs.values.forEach { mangaDir ->
val chapterDirs = mangaDir.dir.listFiles() val chapterDirs =
.orEmpty() mangaDir.dir.listFiles().orEmpty().mapNotNull { it.name }.toHashSet()
.mapNotNull { it.name }
.toHashSet()
mangaDir.files = chapterDirs mangaDir.files = chapterDirs
} }
val trueMangaDirs = mangaDirs.mapNotNull { mangaDir -> val trueMangaDirs = mangaDirs.mapNotNull { mangaDir ->
val manga = sourceMangas.firstOrNull()?.find { DiskUtil.buildValidFilename( val manga = sourceMangas.firstOrNull()?.find {
it.title).toLowerCase() == mangaDir.key DiskUtil.buildValidFilename(
.toLowerCase() && it.source == sourceValue.key } it.title
?: sourceMangas.lastOrNull()?.find { DiskUtil.buildValidFilename( ).toLowerCase() == mangaDir.key.toLowerCase() && it.source == sourceValue.key
it.title).toLowerCase() == mangaDir.key } ?: sourceMangas.lastOrNull()?.find {
.toLowerCase() && it.source == sourceValue.key } DiskUtil.buildValidFilename(
it.title
).toLowerCase() == mangaDir.key.toLowerCase() && it.source == sourceValue.key
}
val id = manga?.id ?: return@mapNotNull null val id = manga?.id ?: return@mapNotNull null
id to mangaDir.value.files id to mangaDir.value.files
}.toMap() }.toMap()