Cleanup/Refactoring of Chapter colors

Also using util in recents
This commit is contained in:
Jay 2020-04-23 23:20:45 -04:00
parent 1519395267
commit f6318d83ee
8 changed files with 65 additions and 100 deletions

View File

@ -2,13 +2,11 @@ package eu.kanade.tachiyomi.ui.library
import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.google.android.material.card.MaterialCardView
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.contextCompatColor
import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.isNotGone
import eu.kanade.tachiyomi.util.view.isVisible
import eu.kanade.tachiyomi.util.view.updatePaddingRelative
import eu.kanade.tachiyomi.util.view.visibleIf
@ -20,20 +18,20 @@ class LibraryBadge @JvmOverloads constructor(context: Context, attrs: AttributeS
fun setUnreadDownload(unread: Int, downloads: Int, showTotalChapters: Boolean) {
// Update the unread count and its visibility.
val unreadBadgeBackground = when (showTotalChapters) {
true -> context.contextCompatColor(R.color.total_badge)
false -> context.contextCompatColor(R.color.unread_badge)
}
val unreadBadgeBackground = context.contextCompatColor(
if (showTotalChapters) R.color.total_badge else R.color.unread_badge
)
with(unread_text) {
visibleIf(unread > 0 || unread == -1 || showTotalChapters)
if (!isVisible()) { return@with }
text = if (unread == -1) "0" else unread.toString()
setTextColor(
when (unread == -1 && !showTotalChapters) {
true -> context.contextCompatColor(R.color.unread_badge) // hide the 0 in the badge
false -> context.contextCompatColor(R.color.unread_badge_text)
}
context.contextCompatColor(
// hide the badge text when preference is only show badge
if (unread == -1 && !showTotalChapters) R.color.unread_badge
else R.color.unread_badge_text
)
)
setBackgroundColor(unreadBadgeBackground)
}
@ -41,6 +39,7 @@ class LibraryBadge @JvmOverloads constructor(context: Context, attrs: AttributeS
// Update the download count or local status and its visibility.
with(download_text) {
visibleIf(downloads == -2 || downloads > 0)
if (!isVisible()) { return@with }
text = if (downloads == -2) {
resources.getString(R.string.local)
} else {
@ -49,10 +48,10 @@ class LibraryBadge @JvmOverloads constructor(context: Context, attrs: AttributeS
}
// Show the badge card if unread or downloads exists
this.visibleIf(download_text.isVisible() || unread_text.isNotGone())
visibleIf(download_text.isVisible() || unread_text.isVisible())
// Show the angles divider if both unread and downloads exists
unread_angle.visibleIf(download_text.isVisible() && unread_text.isNotGone())
unread_angle.visibleIf(download_text.isVisible() && unread_text.isVisible())
unread_angle.setColorFilter(unreadBadgeBackground)
if (unread_angle.isVisible()) {

View File

@ -8,7 +8,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.manga.chapter.BaseChapterAdapter
import eu.kanade.tachiyomi.ui.manga.chapter.ChapterItem
import eu.kanade.tachiyomi.util.chapter.ChapterUtil
import uy.kohesive.injekt.injectLazy
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
@ -25,14 +24,6 @@ class MangaDetailsAdapter(
val delegate: MangaDetailsInterface = controller
val presenter = controller.presenter
val readColor = ChapterUtil.readColor(context)
val unreadColor = ChapterUtil.unreadColor(context)
val bookmarkedColor = ChapterUtil.bookmarkedColor(context)
val bookmarkedAndReadColor = ChapterUtil.bookmarkedAndReadColor(context)
val decimalFormat = DecimalFormat("#.###", DecimalFormatSymbols()
.apply { decimalSeparator = '.' })

View File

@ -7,7 +7,6 @@ import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.manga.MangaDetailsAdapter
import eu.kanade.tachiyomi.util.chapter.ChapterUtil
import eu.kanade.tachiyomi.util.system.contextCompatDrawable
import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.visibleIf
import kotlinx.android.synthetic.main.chapters_item.*
@ -41,13 +40,7 @@ class ChapterHolder(
localSource = manga.source == LocalSource.ID
download_button.visibleIf(!localSource && !isLocked)
var chapterColor = when {
isLocked -> adapter.unreadColor
chapter.bookmark && chapter.read -> adapter.bookmarkedAndReadColor
chapter.bookmark -> adapter.bookmarkedColor
chapter.read -> adapter.readColor
else -> adapter.unreadColor
}
val chapterColor = ChapterUtil.chapterColor(itemView.context, item, isLocked)
// Set correct text color
chapter_title.setTextColor(chapterColor)
@ -75,30 +68,20 @@ class ChapterHolder(
chapter.scanlator?.isNotBlank()?.let { statuses.add(chapter.scanlator!!) }
if (front_view.translationX == 0f) {
read.setImageDrawable(
read.context.contextCompatDrawable(
when (item.read) {
true -> R.drawable.ic_eye_off_24dp
false -> R.drawable.ic_eye_24dp
}
)
)
bookmark.setImageDrawable(
read.context.contextCompatDrawable(
when (item.bookmark) {
true -> R.drawable.ic_bookmark_off_24dp
false -> R.drawable.ic_bookmark_24dp
}
read.setImageResource(
if (item.read) R.drawable.ic_eye_off_24dp else R.drawable.ic_eye_24dp
)
bookmark.setImageResource(
if (item.bookmark) R.drawable.ic_bookmark_off_24dp else R.drawable.ic_bookmark_24dp
)
}
// this will color the scanlator the same bookmarks
chapter_scanlator.setTextColor(chapterColor)
chapter_scanlator.text = statuses.joinToString("")
val status = when (adapter.isSelected(adapterPosition)) {
true -> Download.CHECKED
false -> item.status
val status = when {
adapter.isSelected(adapterPosition) -> Download.CHECKED
else -> item.status
}
notifyStatus(status, item.isLocked, item.progress)

View File

@ -12,7 +12,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.util.chapter.ChapterUtil
import eu.kanade.tachiyomi.util.system.contextCompatDrawable
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
@ -38,29 +37,16 @@ class ReaderChapterItem(val chapter: Chapter, val manga: Manga, val isCurrent: B
}
class ViewHolder(view: View) : FastAdapter.ViewHolder<ReaderChapterItem>(view) {
var chapterTitle: TextView = view.findViewById(R.id.chapter_title)
var chapterSubtitle: TextView = view.findViewById(R.id.chapter_scanlator)
private var chapterTitle: TextView = view.findViewById(R.id.chapter_title)
private var chapterSubtitle: TextView = view.findViewById(R.id.chapter_scanlator)
var bookmarkButton: FrameLayout = view.findViewById(R.id.bookmark_layout)
var bookmarkImage: ImageView = view.findViewById(R.id.bookmark_image)
private var readColor = ChapterUtil.readColor(view.context)
private var unreadColor = ChapterUtil.unreadColor(view.context)
private var bookmarkColor = ChapterUtil.bookmarkedColor(view.context)
private var bookmarkReadColor = ChapterUtil.bookmarkedAndReadColor(view.context)
private var unbookmark = view.context.contextCompatDrawable(R.drawable.ic_bookmark_border_24dp)
private var bookmark = view.context.contextCompatDrawable(R.drawable.ic_bookmark_24dp)
private var bookmarkImage: ImageView = view.findViewById(R.id.bookmark_image)
override fun bindView(item: ReaderChapterItem, payloads: List<Any>) {
val chapter = item.chapter
val manga = item.manga
var chapterColor = when {
chapter.bookmark && chapter.read -> bookmarkReadColor
chapter.bookmark -> bookmarkColor
chapter.read && !item.isCurrent -> readColor
else -> unreadColor
}
val chapterColor = ChapterUtil.chapterColor(itemView.context, item.chapter)
chapterTitle.setTextColor(chapterColor)
chapterTitle.text = when (manga.displayMode) {
@ -86,16 +72,12 @@ class ReaderChapterItem(val chapter: Chapter, val manga: Manga, val isCurrent: B
// match color of the chapter title
chapterSubtitle.setTextColor(chapterColor)
bookmarkImage.setImageDrawable(when (chapter.bookmark) {
true -> bookmark
false -> unbookmark
})
bookmarkImage.setImageResource(
if (chapter.bookmark) R.drawable.ic_bookmark_border_24dp
else R.drawable.ic_bookmark_24dp
)
val drawableColor = when {
chapter.bookmark && chapter.read -> bookmarkReadColor
chapter.bookmark -> bookmarkColor
else -> readColor
}
val drawableColor = ChapterUtil.bookmarkColor(itemView.context, chapter)
DrawableCompat.setTint(bookmarkImage.drawable, drawableColor)

View File

@ -6,6 +6,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.ui.manga.chapter.BaseChapterHolder
import eu.kanade.tachiyomi.util.chapter.ChapterUtil
import eu.kanade.tachiyomi.util.system.getResourceColor
import kotlinx.android.synthetic.main.download_button.*
import kotlinx.android.synthetic.main.recent_chapters_item.*
@ -77,14 +78,9 @@ class RecentChapterHolder(private val view: View, private val adapter: RecentCha
.into(manga_cover)
}
// Check if chapter is read and set correct color
if (item.chapter.read) {
chapter_title.setTextColor(readColor)
title.setTextColor(readColor)
} else {
chapter_title.setTextColor(unreadColor)
title.setTextColor(unreadColor)
}
val chapterColor = ChapterUtil.chapterColor(itemView.context, item)
chapter_title.setTextColor(chapterColor)
title.setTextColor(chapterColor)
// Set chapter status
notifyStatus(item.status, item.progress)

View File

@ -10,7 +10,7 @@ import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.manga.chapter.BaseChapterHolder
import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.chapter.ChapterUtil
import eu.kanade.tachiyomi.util.view.visibleIf
import kotlinx.android.synthetic.main.download_button.*
import kotlinx.android.synthetic.main.recent_manga_item.*
@ -38,20 +38,14 @@ class RecentMangaHolder(
fun bind(item: RecentMangaItem) {
download_button.visibleIf(item.mch.manga.source != LocalSource.ID)
title.apply {
text = item.chapter.name
setTextColor(when {
item.chapter.bookmark -> context.getResourceColor(R.attr.colorAccent)
item.chapter.read -> context.getResourceColor(android.R.attr.textColorHint)
else -> context.getResourceColor(android.R.attr.textColorPrimary)
})
setTextColor(ChapterUtil.chapterColor(context, item))
}
subtitle.apply {
text = item.mch.manga.title
setTextColor(when {
item.chapter.read -> context.getResourceColor(android.R.attr.textColorHint)
else -> context.getResourceColor(android.R.attr.textColorPrimary)
})
setTextColor(ChapterUtil.readColor(context, item))
}
val notValidNum = item.mch.chapter.chapter_number <= 0
body.text = when {

View File

@ -18,12 +18,36 @@ class ChapterUtil {
}
}
fun readColor(context: Context): Int = context.contextCompatColor(R.color.read_chapter)
fun unreadColor(context: Context): Int = context.contextCompatColor(R.color.unread_chapter)
fun bookmarkedColor(context: Context): Int = context.contextCompatColor(R.color.bookmarked_chapter)
fun bookmarkedAndReadColor(context: Context): Int = ColorUtils.setAlphaComponent(context.contextCompatColor(R.color.bookmarked_chapter), 150)
fun chapterColor(context: Context, chapter: Chapter, hideStatus: Boolean = false): Int {
return when {
hideStatus -> unreadColor(context)
chapter.bookmark && chapter.read -> bookmarkedAndReadColor(context)
chapter.bookmark -> bookmarkedColor(context)
chapter.read -> readColor(context)
else -> unreadColor(context)
}
}
fun readColor(context: Context, chapter: Chapter): Int {
return when {
chapter.read -> readColor(context)
else -> unreadColor(context)
}
}
fun bookmarkColor(context: Context, chapter: Chapter): Int {
return when {
chapter.bookmark -> bookmarkedColor(context)
else -> readColor(context)
}
}
private fun readColor(context: Context): Int = context.contextCompatColor(R.color.read_chapter)
private fun unreadColor(context: Context): Int = context.contextCompatColor(R.color.unread_chapter)
private fun bookmarkedColor(context: Context): Int = context.contextCompatColor(R.color.bookmarked_chapter)
private fun bookmarkedAndReadColor(context: Context): Int = ColorUtils.setAlphaComponent(context.contextCompatColor(R.color.bookmarked_chapter), 150)
}
}

View File

@ -101,10 +101,6 @@ inline fun View.isVisible(): Boolean {
return visibility == View.VISIBLE
}
inline fun View.isNotGone(): Boolean {
return visibility != View.GONE
}
inline fun View.visibleIf(show: Boolean) {
visibility = if (show) View.VISIBLE else View.GONE
}