Make extension functions setTintColor and setTintColorAttribute

This commit is contained in:
Jarne Demeulemeester 2022-04-10 14:38:28 +02:00
parent 16f904ae65
commit 6d000882ea
No known key found for this signature in database
GPG key ID: 65C6006F2032DD14
3 changed files with 47 additions and 126 deletions

View file

@ -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

View file

@ -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

View file

@ -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()
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
)
)
}