Optimize searching in library

This commit is contained in:
Jays2Kings 2021-05-03 13:21:18 -04:00
parent 6dc79ed9e0
commit 477f5cd856
4 changed files with 22 additions and 10 deletions

View File

@ -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 "#"

View File

@ -1111,7 +1111,9 @@ class LibraryController(
adapter.removeAllScrollableHeaders()
}
adapter.setFilter(query)
adapter.performFilter()
viewScope.launchUI {
adapter.performFilterAsync()
}
return true
}

View File

@ -146,10 +146,7 @@ class LibraryItem(
if (manga.isBlank() && manga.title.isBlank()) {
return constraint.isEmpty()
}
val sourceManager by injectLazy<SourceManager>()
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<String>?): Boolean {
if (tag.trim().isEmpty()) return true
val seriesType by lazy { manga.seriesType(preferences.context, sourceManager) }
return if (tag.startsWith("-")) {
genres?.find {
val realTag = tag.substringAfter("-")
it.trim().equals(realTag, ignoreCase = true) ||
manga.seriesType(preferences.context, sourceManager).equals(realTag, true)
genres?.find {
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
}
}

View File

@ -26,3 +26,5 @@ fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit): Job =
suspend fun <T> withUIContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Main, block)
suspend fun <T> withIOContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.IO, block)
suspend fun <T> withDefContext(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Default, block)