Formatted download cache

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

View File

@ -47,11 +47,9 @@ 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) lastRenew = 0L // invalidate cache
.subscribe { }
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 ->
it.toLowerCase() == file.toLowerCase() provider.getValidChapterDirNames(chapter).any {
} } it.toLowerCase() == file.toLowerCase()
}
}
} }
/** /**
@ -112,41 +112,39 @@ 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) } onlineSources.find { provider.getSourceDirName(it) == entry.key }?.id
.mapNotNullKeys { entry -> }
onlineSources.find { provider.getSourceDirName(it) == entry.key }?.id
}
val db: DatabaseHelper by injectLazy() val db: DatabaseHelper by injectLazy()
val mangas = db.getMangas().executeAsBlocking().groupBy { it.source } val mangas = db.getMangas().executeAsBlocking().groupBy { it.source }
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() val name = it.name ?: return@mapNotNull null
.mapNotNull { name to MangaDirectory(it)
val name = it.name ?: return@mapNotNull null }.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()