From f6318d83eeed55b5f3e33517b07014726ad2fdfe Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 23 Apr 2020 23:20:45 -0400 Subject: [PATCH] Cleanup/Refactoring of Chapter colors Also using util in recents --- .../tachiyomi/ui/library/LibraryBadge.kt | 25 +++++++------ .../tachiyomi/ui/manga/MangaDetailsAdapter.kt | 9 ----- .../ui/manga/chapter/ChapterHolder.kt | 33 +++++------------ .../tachiyomi/ui/reader/ReaderChapterItem.kt | 36 +++++-------------- .../ui/recent_updates/RecentChapterHolder.kt | 12 +++---- .../tachiyomi/ui/recents/RecentMangaHolder.kt | 14 +++----- .../tachiyomi/util/chapter/ChapterUtil.kt | 32 ++++++++++++++--- .../tachiyomi/util/view/ViewExtensions.kt | 4 --- 8 files changed, 65 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt index 9e38a57ea9..d585d1ea77 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryBadge.kt @@ -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()) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsAdapter.kt index a89bc568a4..d8d44690b4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsAdapter.kt @@ -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 = '.' }) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt index 34c30a508f..6ebe8e5553 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt @@ -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 - } - ) + read.setImageResource( + if (item.read) R.drawable.ic_eye_off_24dp else 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 - } - ) + 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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderChapterItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderChapterItem.kt index d24f6ad0c2..e228c757b3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderChapterItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderChapterItem.kt @@ -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(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) { 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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChapterHolder.kt index b3c6db7e9d..76b86eebf0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChapterHolder.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaHolder.kt index 1485cdb0b8..0290922d6a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentMangaHolder.kt @@ -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 { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterUtil.kt index 4fa70017fe..6f56cf45bf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterUtil.kt @@ -18,12 +18,36 @@ class ChapterUtil { } } - fun readColor(context: Context): Int = context.contextCompatColor(R.color.read_chapter) + 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 unreadColor(context: Context): Int = context.contextCompatColor(R.color.unread_chapter) + fun readColor(context: Context, chapter: Chapter): Int { + return when { + chapter.read -> readColor(context) + else -> unreadColor(context) + } + } - fun bookmarkedColor(context: Context): Int = context.contextCompatColor(R.color.bookmarked_chapter) + fun bookmarkColor(context: Context, chapter: Chapter): Int { + return when { + chapter.bookmark -> bookmarkedColor(context) + else -> readColor(context) + } + } - fun bookmarkedAndReadColor(context: Context): Int = ColorUtils.setAlphaComponent(context.contextCompatColor(R.color.bookmarked_chapter), 150) + 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) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt index 1e442d8725..74314ae63d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt @@ -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 }