浏览代码

Improve download discovery performance in library updates view

len 8 年之前
父节点
当前提交
f98e0858a7

+ 18 - 0
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.
      *

+ 10 - 1
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))
     }
 

+ 20 - 5
app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt

@@ -156,14 +156,29 @@ class RecentChaptersPresenter : BasePresenter<RecentChaptersFragment>() {
      * @param chapters the list of chapter from the database.
      */
     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 mangaDir = cachedDirs.getOrPut(manga.id!!)
-                    { downloadManager.findMangaDir(sourceManager.get(manga.source)!!, manga) }
+            val source = sourceManager.get(manga.source) ?: continue
+
+            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
 
-            if (mangaDir?.findFile(downloadManager.getChapterDirName(chapter)) != null) {
+            val chapterDirName = downloadManager.getChapterDirName(chapter)
+            if (chapterDirs.any { it.name == chapterDirName }) {
                 chapter.status = Download.DOWNLOADED
             }
         }

+ 1 - 1
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