Performance improvements for library filters

This commit is contained in:
len 2016-03-06 15:31:21 +01:00 committed by NoodleMage
parent 8fbef4b4bb
commit b89d6644d8
3 changed files with 35 additions and 37 deletions

View File

@ -378,17 +378,21 @@ public class DownloadManager {
savePageList(download.source, download.manga, download.chapter, download.pages); savePageList(download.source, download.manga, download.chapter, download.pages);
} }
// Get the absolute path to the chapter directory public File getAbsoluteMangaDirectory(Source source, Manga manga) {
public File getAbsoluteChapterDirectory(Source source, Manga manga, Chapter chapter) {
String chapterRelativePath = source.getName() + String chapterRelativePath = source.getName() +
File.separator + File.separator +
manga.title.replaceAll("[^\\sa-zA-Z0-9.-]", "_") + manga.title.replaceAll("[^\\sa-zA-Z0-9.-]", "_");
File.separator +
chapter.name.replaceAll("[^\\sa-zA-Z0-9.-]", "_");
return new File(preferences.getDownloadsDirectory(), chapterRelativePath); 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 // Shortcut for the method above
private File getAbsoluteChapterDirectory(Download download) { private File getAbsoluteChapterDirectory(Download download) {
return getAbsoluteChapterDirectory(download.source, download.manga, download.chapter); return getAbsoluteChapterDirectory(download.source, download.manga, download.chapter);

View File

@ -11,6 +11,10 @@ import eu.kanade.tachiyomi.data.source.base.Source
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
fun <T> Preference<T>.getOrDefault(): T {
return get() ?: defaultValue()!!
}
class PreferencesHelper(private val context: Context) { class PreferencesHelper(private val context: Context) {
private val prefs = PreferenceManager.getDefaultSharedPreferences(context) private val prefs = PreferenceManager.getDefaultSharedPreferences(context)

View File

@ -5,11 +5,11 @@ import android.util.Pair
import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category 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.Manga
import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.database.models.MangaCategory
import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper 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.data.source.SourceManager
import eu.kanade.tachiyomi.event.LibraryMangasEvent import eu.kanade.tachiyomi.event.LibraryMangasEvent
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
@ -62,6 +62,11 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
*/ */
@Inject lateinit var sourceManager: SourceManager @Inject lateinit var sourceManager: SourceManager
/**
* Download manager.
*/
@Inject lateinit var downloadManager: DownloadManager
companion object { companion object {
/** /**
* Id of the restartable that listens for library updates. * Id of the restartable that listens for library updates.
@ -153,55 +158,40 @@ class LibraryPresenter : BasePresenter<LibraryFragment>() {
* @return filter status * @return filter status
*/ */
fun filterLibrary(manga: Manga): Boolean { fun filterLibrary(manga: Manga): Boolean {
val prefFilterDownloaded = preferences.filterDownloaded().getOrDefault()
val prefFilterUnread = preferences.filterUnread().getOrDefault()
// Check if filter option is selected // 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. // Does it have downloaded chapters.
var hasDownloaded = false var hasDownloaded = false
// Does it have unread chapters. // Does it have unread chapters.
var hasUnread = false val hasUnread = manga.unread > 0
// Get chapters from database. if (prefFilterDownloaded) {
val chapters = getChapters(manga) val mangaDir = downloadManager.getAbsoluteMangaDirectory(sourceManager.get(manga.source), manga)
if (preferences.filterDownloaded().get() as Boolean) { if (mangaDir.exists()) {
// Get download manager. for (file in mangaDir.listFiles()) {
val downloadManager = DownloadManager(context, sourceManager, preferences) if (file.isDirectory && file.listFiles().isNotEmpty()) {
// Loop through chapters and check if library has downloaded manga hasDownloaded = true
chapters?.forEach { chapter -> break
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
} }
} }
} }
// Return correct filter status // Return correct filter status
if (preferences.filterDownloaded().get() as Boolean && preferences.filterUnread().get() as Boolean) { if (prefFilterDownloaded && prefFilterUnread) {
return (hasDownloaded && hasUnread) return (hasDownloaded && hasUnread)
} else { } else {
return (hasDownloaded || hasUnread) return (hasDownloaded || hasUnread)
} }
} else } else {
return true 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<Chapter>? {
return db.getChapters(manga).executeAsBlocking()
} }
/** /**