Improve download discovery performance in library updates view

This commit is contained in:
len 2016-12-09 20:23:48 +01:00
parent 8b60d5bfcb
commit f98e0858a7
4 changed files with 49 additions and 7 deletions

View File

@ -110,6 +110,15 @@ class DownloadManager(context: Context) {
} }
} }
/**
* Returns the directory name for a manga.
*
* @param manga the manga to query.
*/
fun getMangaDirName(manga: Manga): String {
return provider.getMangaDirName(manga)
}
/** /**
* Returns the directory name for the given chapter. * Returns the directory name for the given chapter.
* *
@ -119,6 +128,15 @@ class DownloadManager(context: Context) {
return provider.getChapterDirName(chapter) return provider.getChapterDirName(chapter)
} }
/**
* Returns the download directory for a source if it exists.
*
* @param source the source to query.
*/
fun findSourceDir(source: Source): UniFile? {
return provider.findSourceDir(source)
}
/** /**
* Returns the directory for the given manga, if it exists. * Returns the directory for the given manga, if it exists.
* *

View File

@ -45,6 +45,15 @@ class DownloadProvider(private val context: Context) {
.createDirectory(getMangaDirName(manga)) .createDirectory(getMangaDirName(manga))
} }
/**
* Returns the download directory for a source if it exists.
*
* @param source the source to query.
*/
fun findSourceDir(source: Source): UniFile? {
return downloadsDir.findFile(getSourceDirName(source))
}
/** /**
* Returns the download directory for a manga if it exists. * Returns the download directory for a manga if it exists.
* *
@ -52,7 +61,7 @@ class DownloadProvider(private val context: Context) {
* @param manga the manga to query. * @param manga the manga to query.
*/ */
fun findMangaDir(source: Source, manga: Manga): UniFile? { fun findMangaDir(source: Source, manga: Manga): UniFile? {
val sourceDir = downloadsDir.findFile(getSourceDirName(source)) val sourceDir = findSourceDir(source)
return sourceDir?.findFile(getMangaDirName(manga)) return sourceDir?.findFile(getMangaDirName(manga))
} }

View File

@ -156,14 +156,29 @@ class RecentChaptersPresenter : BasePresenter<RecentChaptersFragment>() {
* @param chapters the list of chapter from the database. * @param chapters the list of chapter from the database.
*/ */
private fun setDownloadedChapters(chapters: List<RecentChapter>) { private fun setDownloadedChapters(chapters: List<RecentChapter>) {
val cachedDirs = mutableMapOf<Long, UniFile?>() // Cached list of downloaded manga directories.
val mangaDirectories = mutableMapOf<Int, Array<UniFile>?>()
chapters.forEach { chapter -> // Cached list of downloaded chapter directories for a manga.
val chapterDirectories = mutableMapOf<Long, Array<UniFile>?>()
for (chapter in chapters) {
val manga = chapter.manga val manga = chapter.manga
val mangaDir = cachedDirs.getOrPut(manga.id!!) val source = sourceManager.get(manga.source) ?: continue
{ downloadManager.findMangaDir(sourceManager.get(manga.source)!!, manga) }
if (mangaDir?.findFile(downloadManager.getChapterDirName(chapter)) != null) { val mangaDirs = mangaDirectories.getOrPut(source.id) {
downloadManager.findSourceDir(source)?.listFiles()
} ?: continue
val mangaDirName = downloadManager.getMangaDirName(manga)
val mangaDir = mangaDirs.find { it.name == mangaDirName } ?: continue
val chapterDirs = chapterDirectories.getOrPut(manga.id!!) {
mangaDir.listFiles()
} ?: continue
val chapterDirName = downloadManager.getChapterDirName(chapter)
if (chapterDirs.any { it.name == chapterDirName }) {
chapter.status = Download.DOWNLOADED chapter.status = Download.DOWNLOADED
} }
} }

View File

@ -6,7 +6,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.2.2' classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0' classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files