Clean up LibraryItem (#9072)

* Move LibraryItem vars to constructor vals

* Convert LibraryItem to data class

Remove redundant equals and hashCode

* Remove unused LibraryItem.displayMode

* Simplify LibraryItem.matches()

* Align types in LibraryItem and LibraryBadges

* fixup! Simplify LibraryItem.matches()
This commit is contained in:
Two-Ai 2023-02-12 15:25:27 -05:00 committed by GitHub
parent f6e6a7ddf1
commit 7b118eba22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 78 deletions

View File

@ -7,7 +7,7 @@ import eu.kanade.presentation.components.Badge
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@Composable @Composable
fun DownloadsBadge(count: Int) { fun DownloadsBadge(count: Long) {
if (count > 0) { if (count > 0) {
Badge( Badge(
text = "$count", text = "$count",
@ -18,7 +18,7 @@ fun DownloadsBadge(count: Int) {
} }
@Composable @Composable
fun UnreadBadge(count: Int) { fun UnreadBadge(count: Long) {
if (count > 0) { if (count > 0) {
Badge(text = "$count") Badge(text = "$count")
} }

View File

@ -46,8 +46,8 @@ fun LibraryComfortableGrid(
lastModified = manga.coverLastModified, lastModified = manga.coverLastModified,
), ),
coverBadgeStart = { coverBadgeStart = {
DownloadsBadge(count = libraryItem.downloadCount.toInt()) DownloadsBadge(count = libraryItem.downloadCount)
UnreadBadge(count = libraryItem.unreadCount.toInt()) UnreadBadge(count = libraryItem.unreadCount)
}, },
coverBadgeEnd = { coverBadgeEnd = {
LanguageBadge( LanguageBadge(

View File

@ -47,8 +47,8 @@ fun LibraryCompactGrid(
lastModified = manga.coverLastModified, lastModified = manga.coverLastModified,
), ),
coverBadgeStart = { coverBadgeStart = {
DownloadsBadge(count = libraryItem.downloadCount.toInt()) DownloadsBadge(count = libraryItem.downloadCount)
UnreadBadge(count = libraryItem.unreadCount.toInt()) UnreadBadge(count = libraryItem.unreadCount)
}, },
coverBadgeEnd = { coverBadgeEnd = {
LanguageBadge( LanguageBadge(

View File

@ -56,8 +56,8 @@ fun LibraryList(
lastModified = manga.coverLastModified, lastModified = manga.coverLastModified,
), ),
badge = { badge = {
DownloadsBadge(count = libraryItem.downloadCount.toInt()) DownloadsBadge(count = libraryItem.downloadCount)
UnreadBadge(count = libraryItem.unreadCount.toInt()) UnreadBadge(count = libraryItem.unreadCount)
LanguageBadge( LanguageBadge(
isLocal = libraryItem.isLocal, isLocal = libraryItem.isLocal,
sourceLanguage = libraryItem.sourceLanguage, sourceLanguage = libraryItem.sourceLanguage,

View File

@ -6,17 +6,14 @@ import tachiyomi.domain.library.model.LibraryManga
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
class LibraryItem( data class LibraryItem(
val libraryManga: LibraryManga, val libraryManga: LibraryManga,
val downloadCount: Long = -1,
val unreadCount: Long = -1,
val isLocal: Boolean = false,
val sourceLanguage: String = "",
private val sourceManager: SourceManager = Injekt.get(), private val sourceManager: SourceManager = Injekt.get(),
) { ) {
var displayMode: Long = -1
var downloadCount: Long = -1
var unreadCount: Long = -1
var isLocal = false
var sourceLanguage = ""
/** /**
* Checks if a query matches the manga * Checks if a query matches the manga
* *
@ -25,73 +22,34 @@ class LibraryItem(
*/ */
fun matches(constraint: String): Boolean { fun matches(constraint: String): Boolean {
val sourceName by lazy { sourceManager.getOrStub(libraryManga.manga.source).getNameForMangaInfo() } val sourceName by lazy { sourceManager.getOrStub(libraryManga.manga.source).getNameForMangaInfo() }
val genres by lazy { libraryManga.manga.genre }
return libraryManga.manga.title.contains(constraint, true) || return libraryManga.manga.title.contains(constraint, true) ||
(libraryManga.manga.author?.contains(constraint, true) ?: false) || (libraryManga.manga.author?.contains(constraint, true) ?: false) ||
(libraryManga.manga.artist?.contains(constraint, true) ?: false) || (libraryManga.manga.artist?.contains(constraint, true) ?: false) ||
(libraryManga.manga.description?.contains(constraint, true) ?: false) || (libraryManga.manga.description?.contains(constraint, true) ?: false) ||
if (constraint.contains(",")) { constraint.split(",").map { it.trim() }.all { subconstraint ->
constraint.split(",").all { containsSourceOrGenre(it.trim(), sourceName, genres) } checkNegatableConstraint(subconstraint) {
} else { sourceName.contains(it, true) ||
containsSourceOrGenre(constraint, sourceName, genres) (libraryManga.manga.genre?.any { genre -> genre.equals(it, true) } ?: false)
}
} }
} }
/** /**
* Filters a manga by checking whether the query is the manga's source OR part of * Checks a predicate on a negatable constraint. If the constraint starts with a minus character,
* the genres of the manga * the minus is stripped and the result of the predicate is inverted.
* Checking for genre is done only if the query isn't part of the source name.
* *
* @param query the query to check * @param constraint the argument to the predicate. Inverts the predicate if it starts with '-'.
* @param sourceName name of the manga's source * @param predicate the check to be run against the constraint.
* @param genres list containing manga's genres * @return !predicate(x) if constraint = "-x", otherwise predicate(constraint)
*/ */
private fun containsSourceOrGenre(query: String, sourceName: String, genres: List<String>?): Boolean { private fun checkNegatableConstraint(
val minus = query.startsWith("-") constraint: String,
val tag = if (minus) { query.substringAfter("-") } else query predicate: (String) -> Boolean,
return when (sourceName.contains(tag, true)) { ): Boolean {
false -> containsGenre(query, genres) return if (constraint.startsWith("-")) {
else -> !minus !predicate(constraint.substringAfter("-").trimStart())
}
}
private fun containsGenre(tag: String, genres: List<String>?): Boolean {
return if (tag.startsWith("-")) {
genres?.find {
it.trim().equals(tag.substringAfter("-"), ignoreCase = true)
} == null
} else { } else {
genres?.find { predicate(constraint)
it.trim().equals(tag, ignoreCase = true)
} != null
} }
} }
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as LibraryItem
if (libraryManga != other.libraryManga) return false
if (sourceManager != other.sourceManager) return false
if (displayMode != other.displayMode) return false
if (downloadCount != other.downloadCount) return false
if (unreadCount != other.unreadCount) return false
if (isLocal != other.isLocal) return false
if (sourceLanguage != other.sourceLanguage) return false
return true
}
override fun hashCode(): Int {
var result = libraryManga.hashCode()
result = 31 * result + sourceManager.hashCode()
result = 31 * result + displayMode.hashCode()
result = 31 * result + downloadCount.toInt()
result = 31 * result + unreadCount.toInt()
result = 31 * result + isLocal.hashCode()
result = 31 * result + sourceLanguage.hashCode()
return result
}
} }

View File

@ -371,20 +371,21 @@ class LibraryScreenModel(
libraryMangaList libraryMangaList
.map { libraryManga -> .map { libraryManga ->
// Display mode based on user preference: take it from global library setting or category // Display mode based on user preference: take it from global library setting or category
LibraryItem(libraryManga).apply { LibraryItem(
libraryManga,
downloadCount = if (prefs.downloadBadge) { downloadCount = if (prefs.downloadBadge) {
downloadManager.getDownloadCount(libraryManga.manga).toLong() downloadManager.getDownloadCount(libraryManga.manga).toLong()
} else { } else {
0 0
} },
unreadCount = libraryManga.unreadCount unreadCount = libraryManga.unreadCount,
isLocal = if (prefs.localBadge) libraryManga.manga.isLocal() else false isLocal = if (prefs.localBadge) libraryManga.manga.isLocal() else false,
sourceLanguage = if (prefs.languageBadge) { sourceLanguage = if (prefs.languageBadge) {
sourceManager.getOrStub(libraryManga.manga.source).lang sourceManager.getOrStub(libraryManga.manga.source).lang
} else { } else {
"" ""
} },
} )
} }
.groupBy { it.libraryManga.category } .groupBy { it.libraryManga.category }
} }