From b89d6644d869112c4665367631a5b6edcb5c358b Mon Sep 17 00:00:00 2001 From: len Date: Sun, 6 Mar 2016 15:31:21 +0100 Subject: [PATCH] Performance improvements for library filters --- .../data/download/DownloadManager.java | 14 +++-- .../data/preference/PreferencesHelper.kt | 4 ++ .../tachiyomi/ui/library/LibraryPresenter.kt | 54 ++++++++----------- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java index 713c05503..f182f2e4f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java @@ -378,17 +378,21 @@ public class DownloadManager { savePageList(download.source, download.manga, download.chapter, download.pages); } - // Get the absolute path to the chapter directory - public File getAbsoluteChapterDirectory(Source source, Manga manga, Chapter chapter) { + public File getAbsoluteMangaDirectory(Source source, Manga manga) { String chapterRelativePath = source.getName() + File.separator + - manga.title.replaceAll("[^\\sa-zA-Z0-9.-]", "_") + - File.separator + - chapter.name.replaceAll("[^\\sa-zA-Z0-9.-]", "_"); + manga.title.replaceAll("[^\\sa-zA-Z0-9.-]", "_"); return new File(preferences.getDownloadsDirectory(), chapterRelativePath); } + // Get the absolute path to the chapter directory + public File getAbsoluteChapterDirectory(Source source, Manga manga, Chapter chapter) { + String chapterRelativePath = chapter.name.replaceAll("[^\\sa-zA-Z0-9.-]", "_"); + + return new File(getAbsoluteMangaDirectory(source, manga), chapterRelativePath); + } + // Shortcut for the method above private File getAbsoluteChapterDirectory(Download download) { return getAbsoluteChapterDirectory(download.source, download.manga, download.chapter); diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 05d1d7abd..c5adab461 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -11,6 +11,10 @@ import eu.kanade.tachiyomi.data.source.base.Source import java.io.File import java.io.IOException +fun Preference.getOrDefault(): T { + return get() ?: defaultValue()!! +} + class PreferencesHelper(private val context: Context) { private val prefs = PreferenceManager.getDefaultSharedPreferences(context) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index ea4633632..6bc038d23 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -5,11 +5,11 @@ import android.util.Pair import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category -import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.source.SourceManager import eu.kanade.tachiyomi.event.LibraryMangasEvent import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter @@ -62,6 +62,11 @@ class LibraryPresenter : BasePresenter() { */ @Inject lateinit var sourceManager: SourceManager + /** + * Download manager. + */ + @Inject lateinit var downloadManager: DownloadManager + companion object { /** * Id of the restartable that listens for library updates. @@ -153,55 +158,40 @@ class LibraryPresenter : BasePresenter() { * @return filter status */ fun filterLibrary(manga: Manga): Boolean { + val prefFilterDownloaded = preferences.filterDownloaded().getOrDefault() + val prefFilterUnread = preferences.filterUnread().getOrDefault() + // Check if filter option is selected - if (preferences.filterDownloaded().get() as Boolean || preferences.filterUnread().get() as Boolean) { + if (prefFilterDownloaded || prefFilterUnread) { // Does it have downloaded chapters. var hasDownloaded = false // Does it have unread chapters. - var hasUnread = false + val hasUnread = manga.unread > 0 - // Get chapters from database. - val chapters = getChapters(manga) + if (prefFilterDownloaded) { + val mangaDir = downloadManager.getAbsoluteMangaDirectory(sourceManager.get(manga.source), manga) - if (preferences.filterDownloaded().get() as Boolean) { - // Get download manager. - val downloadManager = DownloadManager(context, sourceManager, preferences) - // Loop through chapters and check if library has downloaded manga - chapters?.forEach { chapter -> - if (downloadManager.isChapterDownloaded(sourceManager.get(manga.source), manga, chapter)) { - hasDownloaded = true - } - } - } - if (preferences.filterUnread().get() as Boolean) { - // Loop through chapters and check if library has unread manga - chapters?.forEach { chapter -> - if (!chapter.read) { - hasUnread = true + if (mangaDir.exists()) { + for (file in mangaDir.listFiles()) { + if (file.isDirectory && file.listFiles().isNotEmpty()) { + hasDownloaded = true + break + } } } } // Return correct filter status - if (preferences.filterDownloaded().get() as Boolean && preferences.filterUnread().get() as Boolean) { + if (prefFilterDownloaded && prefFilterUnread) { return (hasDownloaded && hasUnread) } else { return (hasDownloaded || hasUnread) } - } else + } else { return true - } - - /** - * Returns list of chapters belonging to manga - * - * @param manga manga from library - * @return list of chapters belonging to manga - */ - fun getChapters(manga: Manga): MutableList? { - return db.getChapters(manga).executeAsBlocking() + } } /**