From ad2e14bf1a98bb9d195ea00e2bce3658f8fbcdb7 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 25 Apr 2020 18:18:13 -0400 Subject: [PATCH] Created setTextColorRes Extension method --- .../tachiyomi/ui/library/LibraryBadge.kt | 19 +++++++++++-------- .../tachiyomi/util/view/ViewExtensions.kt | 6 ++++++ 2 files changed, 17 insertions(+), 8 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 adee395af8..2126dd588b 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 @@ -8,6 +8,7 @@ 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.isVisible +import eu.kanade.tachiyomi.util.view.setTextColorRes import eu.kanade.tachiyomi.util.view.updatePaddingRelative import eu.kanade.tachiyomi.util.view.visibleIf import kotlinx.android.synthetic.main.unread_download_badge.view.* @@ -24,15 +25,17 @@ class LibraryBadge @JvmOverloads constructor(context: Context, attrs: AttributeS with(unread_text) { visibleIf(unread > 0 || unread == -1 || showTotalChapters) - if (!isVisible()) { return@with } + if (!isVisible()) { + return@with + } text = if (unread == -1) "0" else unread.toString() - setTextColor( - context.contextCompatColor( - // hide the badge text when preference is only show badge - if (unread == -1 && !showTotalChapters) R.color.unread_badge - else if (showTotalChapters) R.color.total_badge_text - else R.color.unread_badge_text - ) + setTextColorRes( + // hide the badge text when preference is only show badge + when { + unread == -1 && !showTotalChapters -> R.color.unread_badge + showTotalChapters -> R.color.total_badge_text + else -> R.color.unread_badge_text + } ) setBackgroundColor(unreadBadgeBackground) } 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 566ee6129c..9cc964393e 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 @@ -18,6 +18,7 @@ import android.widget.Button import android.widget.FrameLayout import android.widget.ImageView import android.widget.TextView +import androidx.annotation.ColorRes import androidx.annotation.IdRes import androidx.annotation.Px import androidx.appcompat.view.menu.MenuBuilder @@ -34,6 +35,7 @@ import com.google.android.material.snackbar.Snackbar import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.ThemeUtil +import eu.kanade.tachiyomi.util.system.contextCompatColor import eu.kanade.tachiyomi.util.system.getResourceColor import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -330,6 +332,10 @@ fun MaterialButton.resetStrokeColor() { ) } +fun TextView.setTextColorRes(@ColorRes id: Int) { + setTextColor(context.contextCompatColor(id)) +} + @SuppressLint("RestrictedApi") fun BottomNavigationView.getItemView(@IdRes id: Int): BottomNavigationItemView? { val order = (menu as MenuBuilder).findItemIndex(id)