From 477f5cd856b8f4c6ac3b05318b04f768800d210b Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Mon, 3 May 2021 13:21:18 -0400 Subject: [PATCH] Optimize searching in library --- .../tachiyomi/ui/library/LibraryCategoryAdapter.kt | 12 ++++++++++++ .../tachiyomi/ui/library/LibraryController.kt | 4 +++- .../eu/kanade/tachiyomi/ui/library/LibraryItem.kt | 14 +++++--------- .../tachiyomi/util/system/CoroutinesExtensions.kt | 2 ++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index 1b5600f446..c9904eb8b8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.lang.removeArticles import eu.kanade.tachiyomi.util.system.isLTR import eu.kanade.tachiyomi.util.system.timeSpanFromNow +import eu.kanade.tachiyomi.util.system.withDefContext import uy.kohesive.injekt.injectLazy import java.util.Locale @@ -116,6 +117,17 @@ class LibraryCategoryAdapter(val controller: LibraryController) : isLongPressDragEnabled = libraryListener.canDrag() && s.isNullOrBlank() } + suspend fun performFilterAsync() { + val s = getFilter(String::class.java) + if (s.isNullOrBlank()) { + updateDataSet(mangas) + } else { + val filteredManga = withDefContext { mangas.filter { it.filter(s) } } + updateDataSet(filteredManga) + } + isLongPressDragEnabled = libraryListener.canDrag() && s.isNullOrBlank() + } + private fun getFirstLetter(name: String): String { val letter = name.firstOrNull() ?: '#' return if (letter.isLetter()) getFirstChar(name) else "#" diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 33c05a2e7c..ee71edfa7e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -1111,7 +1111,9 @@ class LibraryController( adapter.removeAllScrollableHeaders() } adapter.setFilter(query) - adapter.performFilter() + viewScope.launchUI { + adapter.performFilterAsync() + } return true } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt index 944dc6e88d..c435895279 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt @@ -146,10 +146,7 @@ class LibraryItem( if (manga.isBlank() && manga.title.isBlank()) { return constraint.isEmpty() } - val sourceManager by injectLazy() - val sourceName = if (manga.source == 0L) "Local" else { - sourceManager.getOrStub(manga.source).name - } + val sourceName by lazy { sourceManager.getOrStub(manga.source).name } return manga.title.contains(constraint, true) || (manga.author?.contains(constraint, true) ?: false) || (manga.artist?.contains(constraint, true) ?: false) || @@ -162,16 +159,15 @@ class LibraryItem( private fun containsGenre(tag: String, genres: List?): Boolean { if (tag.trim().isEmpty()) return true + val seriesType by lazy { manga.seriesType(preferences.context, sourceManager) } return if (tag.startsWith("-")) { + val realTag = tag.substringAfter("-") genres?.find { - val realTag = tag.substringAfter("-") - it.trim().equals(realTag, ignoreCase = true) || - manga.seriesType(preferences.context, sourceManager).equals(realTag, true) + it.trim().equals(realTag, ignoreCase = true) || seriesType.equals(realTag, true) } == null } else { genres?.find { - it.trim().equals(tag, ignoreCase = true) || - manga.seriesType(preferences.context, sourceManager).equals(tag, true) + it.trim().equals(tag, ignoreCase = true) || seriesType.equals(tag, true) } != null } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/CoroutinesExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/CoroutinesExtensions.kt index a77e169e5c..08f335668f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/CoroutinesExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/CoroutinesExtensions.kt @@ -26,3 +26,5 @@ fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit): Job = suspend fun withUIContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Main, block) suspend fun withIOContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.IO, block) + +suspend fun withDefContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Default, block)