From f98e0858a7577312183da9060d2443d304bb986f Mon Sep 17 00:00:00 2001 From: len Date: Fri, 9 Dec 2016 20:23:48 +0100 Subject: [PATCH] Improve download discovery performance in library updates view --- .../data/download/DownloadManager.kt | 18 +++++++++++++ .../data/download/DownloadProvider.kt | 11 +++++++- .../recent_updates/RecentChaptersPresenter.kt | 25 +++++++++++++++---- build.gradle | 2 +- 4 files changed, 49 insertions(+), 7 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 4338b7d57..2eae39901 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 @@ -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. * @@ -119,6 +128,15 @@ class DownloadManager(context: Context) { 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. * 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 8b7b1f070..79eabc70b 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 @@ -45,6 +45,15 @@ class DownloadProvider(private val context: Context) { .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. * @@ -52,7 +61,7 @@ class DownloadProvider(private val context: Context) { * @param manga the manga to query. */ fun findMangaDir(source: Source, manga: Manga): UniFile? { - val sourceDir = downloadsDir.findFile(getSourceDirName(source)) + val sourceDir = findSourceDir(source) return sourceDir?.findFile(getMangaDirName(manga)) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt index bac1e9eb9..f666e6f2c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt @@ -156,14 +156,29 @@ class RecentChaptersPresenter : BasePresenter() { * @param chapters the list of chapter from the database. */ private fun setDownloadedChapters(chapters: List) { - val cachedDirs = mutableMapOf() + // Cached list of downloaded manga directories. + val mangaDirectories = mutableMapOf?>() - chapters.forEach { chapter -> + // Cached list of downloaded chapter directories for a manga. + val chapterDirectories = mutableMapOf?>() + + for (chapter in chapters) { val manga = chapter.manga - val mangaDir = cachedDirs.getOrPut(manga.id!!) - { downloadManager.findMangaDir(sourceManager.get(manga.source)!!, manga) } + val source = sourceManager.get(manga.source) ?: continue - 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 } } diff --git a/build.gradle b/build.gradle index 319ddb2e7..dcb7393f8 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } 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' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files