Highlighting part of library text when searching in library

Only for comfort grid and list view

Also show artist in comfort gird if it can fit
This commit is contained in:
Jays2Kings 2021-05-03 15:03:38 -04:00
parent f1a6c9eea3
commit 970df326e8
5 changed files with 43 additions and 8 deletions

View File

@ -11,6 +11,7 @@ import coil.size.Scale
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.image.coil.loadLibraryManga import eu.kanade.tachiyomi.data.image.coil.loadLibraryManga
import eu.kanade.tachiyomi.databinding.MangaGridItemBinding import eu.kanade.tachiyomi.databinding.MangaGridItemBinding
import eu.kanade.tachiyomi.util.lang.highlightText
/** /**
* Class used to hold the displayed data of a manga in the library, like the cover or the title. * Class used to hold the displayed data of a manga in the library, like the cover or the title.
@ -55,10 +56,25 @@ class LibraryGridHolder(
override fun onSetValues(item: LibraryItem) { override fun onSetValues(item: LibraryItem) {
// Update the title and subtitle of the manga. // Update the title and subtitle of the manga.
binding.constraintLayout.isVisible = !item.manga.isBlank() binding.constraintLayout.isVisible = !item.manga.isBlank()
binding.title.text = item.manga.title binding.title.text = item.manga.title.highlightText(item.filter, color)
binding.subtitle.text = item.manga.author?.trim() val authorArtist = if (item.manga.author == item.manga.artist || item.manga.artist.isNullOrBlank()) {
item.manga.author?.trim() ?: ""
} else {
listOfNotNull(
item.manga.author?.trim()?.takeIf { it.isNotBlank() },
item.manga.artist?.trim()?.takeIf { it.isNotBlank() }
).joinToString(", ")
}
binding.subtitle.text = authorArtist.highlightText(item.filter, color)
binding.compactTitle.text = binding.title.text binding.compactTitle.text = binding.title.text?.toString()?.highlightText(item.filter, color)
binding.title.post {
val hasAuthorInFilter =
item.filter.isNotBlank() && authorArtist.contains(item.filter, true)
binding.subtitle.isVisible = binding.title.lineCount <= 1 || hasAuthorInFilter
binding.title.maxLines = if (hasAuthorInFilter) 1 else 2
}
setUnreadBadge(binding.unreadDownloadBadge.badgeView, item) setUnreadBadge(binding.unreadDownloadBadge.badgeView, item)
setReadingButton(item) setReadingButton(item)

View File

@ -1,10 +1,12 @@
package eu.kanade.tachiyomi.ui.library package eu.kanade.tachiyomi.ui.library
import android.view.View import android.view.View
import androidx.core.graphics.ColorUtils
import androidx.core.view.isVisible import androidx.core.view.isVisible
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import eu.kanade.tachiyomi.util.system.getResourceColor
/** /**
* Generic class used to hold the displayed data of a manga in the library. * Generic class used to hold the displayed data of a manga in the library.
@ -18,6 +20,8 @@ abstract class LibraryHolder(
val adapter: LibraryCategoryAdapter val adapter: LibraryCategoryAdapter
) : BaseFlexibleViewHolder(view, adapter) { ) : BaseFlexibleViewHolder(view, adapter) {
protected val color = ColorUtils.setAlphaComponent(itemView.context.getResourceColor(R.attr.colorAccent), 75)
/** /**
* Method called from [LibraryCategoryAdapter.onBindViewHolder]. It updates the data for this * Method called from [LibraryCategoryAdapter.onBindViewHolder]. It updates the data for this
* holder with the given manga. * holder with the given manga.

View File

@ -33,6 +33,7 @@ class LibraryItem(
var downloadCount = -1 var downloadCount = -1
var unreadType = 2 var unreadType = 2
var filter = ""
private val sourceManager: SourceManager by injectLazy() private val sourceManager: SourceManager by injectLazy()
private val uniformSize: Boolean private val uniformSize: Boolean
@ -142,6 +143,7 @@ class LibraryItem(
* @return true if the manga should be included, false otherwise. * @return true if the manga should be included, false otherwise.
*/ */
override fun filter(constraint: String): Boolean { override fun filter(constraint: String): Boolean {
filter = constraint
if (manga.isBlank() && manga.title.isBlank()) { if (manga.isBlank() && manga.title.isBlank()) {
return constraint.isEmpty() return constraint.isEmpty()
} }

View File

@ -7,6 +7,7 @@ import coil.clear
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.image.coil.loadLibraryManga import eu.kanade.tachiyomi.data.image.coil.loadLibraryManga
import eu.kanade.tachiyomi.databinding.MangaListItemBinding import eu.kanade.tachiyomi.databinding.MangaListItemBinding
import eu.kanade.tachiyomi.util.lang.highlightText
import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.view.updateLayoutParams import eu.kanade.tachiyomi.util.view.updateLayoutParams
@ -61,14 +62,25 @@ class LibraryListHolder(
binding.title.textAlignment = View.TEXT_ALIGNMENT_TEXT_START binding.title.textAlignment = View.TEXT_ALIGNMENT_TEXT_START
// Update the binding.title of the manga. // Update the binding.title of the manga.
binding.title.text = item.manga.title binding.title.text = item.manga.title.highlightText(item.filter, color)
setUnreadBadge(binding.unreadDownloadBadge.badgeView, item) setUnreadBadge(binding.unreadDownloadBadge.badgeView, item)
binding.subtitle.text = item.manga.author?.trim() val authorArtist = if (item.manga.author == item.manga.artist || item.manga.artist.isNullOrBlank()) {
item.manga.author?.trim() ?: ""
} else {
listOfNotNull(
item.manga.author?.trim()?.takeIf { it.isNotBlank() },
item.manga.artist?.trim()?.takeIf { it.isNotBlank() }
).joinToString(", ")
}
binding.subtitle.text = authorArtist.highlightText(item.filter, color)
binding.title.maxLines = 2
binding.title.post { binding.title.post {
if (binding.title.text == item.manga.title) { val hasAuthorInFilter =
binding.subtitle.isVisible = binding.title.lineCount == 1 && !item.manga.author.isNullOrBlank() item.filter.isNotBlank() && authorArtist.contains(item.filter, true)
} binding.subtitle.isVisible = binding.title.lineCount <= 1 || hasAuthorInFilter
binding.title.maxLines = if (hasAuthorInFilter) 1 else 2
} }
// Update the cover. // Update the cover.

View File

@ -100,6 +100,7 @@ fun CharSequence.tintText(@ColorInt color: Int): Spanned {
fun String.highlightText(highlight: String, @ColorInt color: Int): Spanned { fun String.highlightText(highlight: String, @ColorInt color: Int): Spanned {
val wordToSpan: Spannable = SpannableString(this) val wordToSpan: Spannable = SpannableString(this)
if (highlight.isBlank()) return wordToSpan
indexesOf(highlight).forEach { indexesOf(highlight).forEach {
wordToSpan.setSpan(BackgroundColorSpan(color), it, it + highlight.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) wordToSpan.setSpan(BackgroundColorSpan(color), it, it + highlight.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
} }