From 6d000882ea78e5e3f675951f0c16b1d4ec4d1d99 Mon Sep 17 00:00:00 2001 From: Jarne Demeulemeester Date: Sun, 10 Apr 2022 14:38:28 +0200 Subject: [PATCH] Make extension functions setTintColor and setTintColorAttribute --- .../fragments/EpisodeBottomSheetFragment.kt | 76 +++---------------- .../jellyfin/fragments/MediaInfoFragment.kt | 69 +++-------------- .../dev/jdtech/jellyfin/utils/extensions.kt | 28 ++++++- 3 files changed, 47 insertions(+), 126 deletions(-) diff --git a/app/src/main/java/dev/jdtech/jellyfin/fragments/EpisodeBottomSheetFragment.kt b/app/src/main/java/dev/jdtech/jellyfin/fragments/EpisodeBottomSheetFragment.kt index 851420a5..c759a00e 100644 --- a/app/src/main/java/dev/jdtech/jellyfin/fragments/EpisodeBottomSheetFragment.kt +++ b/app/src/main/java/dev/jdtech/jellyfin/fragments/EpisodeBottomSheetFragment.kt @@ -1,6 +1,5 @@ package dev.jdtech.jellyfin.fragments -import android.content.res.ColorStateList import android.os.Bundle import android.util.TypedValue import android.view.LayoutInflater @@ -23,6 +22,8 @@ import dev.jdtech.jellyfin.bindBaseItemImage import dev.jdtech.jellyfin.databinding.EpisodeBottomSheetBinding import dev.jdtech.jellyfin.dialogs.ErrorDialogFragment import dev.jdtech.jellyfin.models.PlayerItem +import dev.jdtech.jellyfin.utils.setTintColor +import dev.jdtech.jellyfin.utils.setTintColorAttribute import dev.jdtech.jellyfin.viewmodels.EpisodeBottomSheetViewModel import dev.jdtech.jellyfin.viewmodels.PlayerViewModel import kotlinx.coroutines.launch @@ -84,23 +85,11 @@ class EpisodeBottomSheetFragment : BottomSheetDialogFragment() { when (viewModel.played) { true -> { viewModel.markAsUnplayed(episodeId) - val typedValue = TypedValue() - requireActivity().theme.resolveAttribute(R.attr.colorOnSecondaryContainer, typedValue, true) - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - typedValue.resourceId, - requireActivity().theme - ) - ) + binding.checkButton.setTintColorAttribute(R.attr.colorOnSecondaryContainer, requireActivity().theme) } false -> { viewModel.markAsPlayed(episodeId) - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + binding.checkButton.setTintColor(R.color.red, requireActivity().theme) } } } @@ -110,24 +99,12 @@ class EpisodeBottomSheetFragment : BottomSheetDialogFragment() { true -> { viewModel.unmarkAsFavorite(episodeId) binding.favoriteButton.setImageResource(R.drawable.ic_heart) - val typedValue = TypedValue() - requireActivity().theme.resolveAttribute(R.attr.colorOnSecondaryContainer, typedValue, true) - binding.favoriteButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - typedValue.resourceId, - requireActivity().theme - ) - ) + binding.favoriteButton.setTintColorAttribute(R.attr.colorOnSecondaryContainer, requireActivity().theme) } false -> { viewModel.markAsFavorite(episodeId) binding.favoriteButton.setImageResource(R.drawable.ic_heart_filled) - binding.favoriteButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + binding.favoriteButton.setTintColor(R.color.red, requireActivity().theme) } } } @@ -135,12 +112,7 @@ class EpisodeBottomSheetFragment : BottomSheetDialogFragment() { binding.downloadButton.setOnClickListener { binding.downloadButton.isEnabled = false viewModel.loadDownloadRequestItem(episodeId) - binding.downloadButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + binding.downloadButton.setTintColor(R.color.red, requireActivity().theme) } viewModel.loadEpisode(episodeId) @@ -187,24 +159,8 @@ class EpisodeBottomSheetFragment : BottomSheetDialogFragment() { // Check icon when (played) { - true -> { - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) - } - false -> { - val typedValue = TypedValue() - requireActivity().theme.resolveAttribute(R.attr.colorOnSecondaryContainer, typedValue, true) - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - typedValue.resourceId, - requireActivity().theme - ) - ) - } + true -> binding.checkButton.setTintColor(R.color.red, requireActivity().theme) + false -> binding.checkButton.setTintColorAttribute(R.attr.colorOnSecondaryContainer, requireActivity().theme) } // Favorite icon @@ -213,24 +169,14 @@ class EpisodeBottomSheetFragment : BottomSheetDialogFragment() { false -> R.drawable.ic_heart } binding.favoriteButton.setImageResource(favoriteDrawable) - if (favorite) binding.favoriteButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + if (favorite) binding.favoriteButton.setTintColor(R.color.red, requireActivity().theme) when (canDownload) { true -> { binding.downloadButtonWrapper.isVisible = true binding.downloadButton.isEnabled = !downloaded - if (downloaded) binding.downloadButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + if (downloaded) binding.downloadButton.setTintColor(R.color.red, requireActivity().theme) } false -> { binding.downloadButtonWrapper.isVisible = false diff --git a/app/src/main/java/dev/jdtech/jellyfin/fragments/MediaInfoFragment.kt b/app/src/main/java/dev/jdtech/jellyfin/fragments/MediaInfoFragment.kt index 766f61f3..978875d3 100644 --- a/app/src/main/java/dev/jdtech/jellyfin/fragments/MediaInfoFragment.kt +++ b/app/src/main/java/dev/jdtech/jellyfin/fragments/MediaInfoFragment.kt @@ -4,7 +4,6 @@ import android.content.Intent import android.content.res.ColorStateList import android.net.Uri import android.os.Bundle -import android.util.TypedValue import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -29,6 +28,8 @@ import dev.jdtech.jellyfin.dialogs.ErrorDialogFragment import dev.jdtech.jellyfin.dialogs.VideoVersionDialogFragment import dev.jdtech.jellyfin.models.PlayerItem import dev.jdtech.jellyfin.utils.checkIfLoginRequired +import dev.jdtech.jellyfin.utils.setTintColor +import dev.jdtech.jellyfin.utils.setTintColorAttribute import dev.jdtech.jellyfin.viewmodels.MediaInfoViewModel import dev.jdtech.jellyfin.viewmodels.PlayerViewModel import kotlinx.coroutines.launch @@ -148,23 +149,11 @@ class MediaInfoFragment : Fragment() { when (viewModel.played) { true -> { viewModel.markAsUnplayed(args.itemId) - val typedValue = TypedValue() - requireActivity().theme.resolveAttribute(R.attr.colorOnSecondaryContainer, typedValue, true) - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - typedValue.resourceId, - requireActivity().theme - ) - ) + binding.checkButton.setTintColorAttribute(R.attr.colorOnSecondaryContainer, requireActivity().theme) } false -> { viewModel.markAsPlayed(args.itemId) - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + binding.checkButton.setTintColor(R.color.red, requireActivity().theme) } } } @@ -174,24 +163,12 @@ class MediaInfoFragment : Fragment() { true -> { viewModel.unmarkAsFavorite(args.itemId) binding.favoriteButton.setImageResource(R.drawable.ic_heart) - val typedValue = TypedValue() - requireActivity().theme.resolveAttribute(R.attr.colorOnSecondaryContainer, typedValue, true) - binding.favoriteButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - typedValue.resourceId, - requireActivity().theme - ) - ) + binding.favoriteButton.setTintColorAttribute(R.attr.colorOnSecondaryContainer, requireActivity().theme) } false -> { viewModel.markAsFavorite(args.itemId) binding.favoriteButton.setImageResource(R.drawable.ic_heart_filled) - binding.favoriteButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + binding.favoriteButton.setTintColor(R.color.red, requireActivity().theme) } } } @@ -234,24 +211,8 @@ class MediaInfoFragment : Fragment() { // Check icon when (played) { - true -> { - if (played) binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) - } - false -> { - val typedValue = TypedValue() - requireActivity().theme.resolveAttribute(R.attr.colorOnSecondaryContainer, typedValue, true) - binding.checkButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - typedValue.resourceId, - requireActivity().theme - ) - ) - } + true -> binding.checkButton.setTintColor(R.color.red, requireActivity().theme) + false -> binding.checkButton.setTintColorAttribute(R.attr.colorOnSecondaryContainer, requireActivity().theme) } @@ -261,12 +222,7 @@ class MediaInfoFragment : Fragment() { false -> R.drawable.ic_heart } binding.favoriteButton.setImageResource(favoriteDrawable) - if (favorite) binding.favoriteButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + if (favorite) binding.favoriteButton.setTintColor(R.color.red, requireActivity().theme) binding.downloadButton.isEnabled = !downloaded @@ -275,12 +231,7 @@ class MediaInfoFragment : Fragment() { binding.downloadButton.isVisible = true binding.downloadButton.isEnabled = !downloaded - if (downloaded) binding.downloadButton.imageTintList = ColorStateList.valueOf( - resources.getColor( - R.color.red, - requireActivity().theme - ) - ) + if (downloaded) binding.downloadButton.setTintColor(R.color.red, requireActivity().theme) } false -> { binding.downloadButton.isVisible = false diff --git a/app/src/main/java/dev/jdtech/jellyfin/utils/extensions.kt b/app/src/main/java/dev/jdtech/jellyfin/utils/extensions.kt index 6eb182ba..4de32c1c 100644 --- a/app/src/main/java/dev/jdtech/jellyfin/utils/extensions.kt +++ b/app/src/main/java/dev/jdtech/jellyfin/utils/extensions.kt @@ -1,8 +1,13 @@ package dev.jdtech.jellyfin.utils import android.content.Context +import android.content.res.ColorStateList import android.content.res.Resources +import android.util.TypedValue +import android.widget.ImageButton import android.widget.Toast +import androidx.annotation.AttrRes +import androidx.annotation.ColorRes import androidx.annotation.StringRes import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController @@ -34,9 +39,28 @@ fun Fragment.checkIfLoginRequired(error: String) { } } - inline fun Context.toast(@StringRes text: Int, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, text, duration).show() -inline fun Resources.dip(px: Int) = (px * displayMetrics.density).toInt() \ No newline at end of file +inline fun Resources.dip(px: Int) = (px * displayMetrics.density).toInt() + +fun ImageButton.setTintColor(@ColorRes colorId: Int, theme: Resources.Theme) { + this.imageTintList = ColorStateList.valueOf( + resources.getColor( + colorId, + theme + ) + ) +} + +fun ImageButton.setTintColorAttribute(@AttrRes attributeId: Int, theme: Resources.Theme) { + val typedValue = TypedValue() + theme.resolveAttribute(attributeId, typedValue, true) + this.imageTintList = ColorStateList.valueOf( + resources.getColor( + typedValue.resourceId, + theme + ) + ) +} \ No newline at end of file