From 7e3ea9074c65297177746182a5dfffb5e884c456 Mon Sep 17 00:00:00 2001 From: curche <72807749+curche@users.noreply.github.com> Date: Mon, 28 Jun 2021 21:02:03 +0530 Subject: [PATCH] Extend library search by adding - option (#5387) * extend library search to enable - search library search already allows for search to select manga from a particular source. Similarly, - allows to search for manga that aren't from the said source. TachiyomiSy has this feature but it heavily depends on exh methods A problem when you directly add a negation check is that although it will work, the genre search kicks in adds back every manga since - returns true for all genres of a manga Thus, the solution I decided on was do source search first, and then move to genre check when it fails. A perhaps useful addition is that, now you're able to search like this: , -, - * make if-else statements more readable * refactor containsSourceOrGenre() using `when` * add comment describing the function * remove lazy not really required anymore now that containsSourceOrGenre was rewritten using `when` --- .../tachiyomi/ui/library/LibraryItem.kt | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) 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 d15605f53..eea057b54 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 @@ -101,18 +101,37 @@ class LibraryItem( * @return true if the manga should be included, false otherwise. */ override fun filter(constraint: String): Boolean { + val sourceName by lazy { sourceManager.getOrStub(manga.source).name } + val genres by lazy { manga.getGenres() } return manga.title.contains(constraint, true) || (manga.author?.contains(constraint, true) ?: false) || (manga.artist?.contains(constraint, true) ?: false) || (manga.description?.contains(constraint, true) ?: false) || - sourceManager.getOrStub(manga.source).name.contains(constraint, true) || if (constraint.contains(",")) { - constraint.split(",").all { containsGenre(it.trim(), manga.getGenres()) } + constraint.split(",").all { containsSourceOrGenre(it.trim(), sourceName, genres) } } else { - containsGenre(constraint, manga.getGenres()) + containsSourceOrGenre(constraint, sourceName, genres) } } + /** + * Filters a manga by checking whether the query is the manga's source OR part of + * the genres of the manga + * Checking for genre is done only if the query isn't part of the source name. + * + * @param query the query to check + * @param sourceName name of the manga's source + * @param genres list containing manga's genres + */ + private fun containsSourceOrGenre(query: String, sourceName: String, genres: List?): Boolean { + val minus = query.startsWith("-") + val tag = if (minus) { query.substringAfter("-") } else query + return when (sourceName.contains(tag, true)) { + false -> containsGenre(query, genres) + else -> !minus + } + } + private fun containsGenre(tag: String, genres: List?): Boolean { return if (tag.startsWith("-")) { genres?.find {