mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-11-04 08:08:55 +01:00 
			
		
		
		
	Performance improvements for library filters
This commit is contained in:
		@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,10 @@ import eu.kanade.tachiyomi.data.source.base.Source
 | 
			
		||||
import java.io.File
 | 
			
		||||
import java.io.IOException
 | 
			
		||||
 | 
			
		||||
fun <T> Preference<T>.getOrDefault(): T {
 | 
			
		||||
    return get() ?: defaultValue()!!
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class PreferencesHelper(private val context: Context) {
 | 
			
		||||
 | 
			
		||||
    private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
 | 
			
		||||
 
 | 
			
		||||
@@ -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<LibraryFragment>() {
 | 
			
		||||
     */
 | 
			
		||||
    @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<LibraryFragment>() {
 | 
			
		||||
     * @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<Chapter>? {
 | 
			
		||||
        return db.getChapters(manga).executeAsBlocking()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user