From cf3db7a76bb9a6606816bb13db24c91d474c0817 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Sat, 5 Sep 2020 21:24:16 +0530 Subject: [PATCH] Code refactor --- .../monkey/retromusic/extensions/ColorExt.kt | 27 ++++++++++++++++--- .../retromusic/extensions/FragmentExt.kt | 11 ++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt b/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt index fc08d6e7..8d2d56e4 100644 --- a/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt +++ b/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt @@ -18,13 +18,16 @@ import android.app.Dialog import android.content.Context import android.content.res.ColorStateList import android.graphics.Color +import android.graphics.drawable.Drawable import android.widget.Button import android.widget.CheckBox import android.widget.SeekBar -import androidx.annotation.AttrRes -import androidx.annotation.ColorInt +import androidx.annotation.* +import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.widget.AppCompatImageView import androidx.appcompat.widget.Toolbar +import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.DrawableCompat import androidx.fragment.app.Fragment import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.util.ATHUtil @@ -163,4 +166,22 @@ fun TextInputEditText.accentColor() { fun AppCompatImageView.accentColor(): Int { return ThemeStore.accentColor(context) -} \ No newline at end of file +} + +@CheckResult +fun Drawable.tint(@ColorInt color: Int): Drawable { + val tintedDrawable = DrawableCompat.wrap(this).mutate() + DrawableCompat.setTint(this, color) + return tintedDrawable +} + +@CheckResult +fun Drawable.tint(context: Context, @ColorRes color: Int): Drawable { + return tint(context.getColorCompat(color)) +} + +@ColorInt +fun Context.getColorCompat(@ColorRes colorRes: Int): Int { + return ContextCompat.getColor(this, colorRes) +} + diff --git a/app/src/main/java/code/name/monkey/retromusic/extensions/FragmentExt.kt b/app/src/main/java/code/name/monkey/retromusic/extensions/FragmentExt.kt index 876e549f..a826d153 100644 --- a/app/src/main/java/code/name/monkey/retromusic/extensions/FragmentExt.kt +++ b/app/src/main/java/code/name/monkey/retromusic/extensions/FragmentExt.kt @@ -2,12 +2,15 @@ package code.name.monkey.retromusic.extensions import android.content.Context import android.content.res.Configuration +import android.graphics.drawable.Drawable import android.os.PowerManager import android.widget.Toast +import androidx.annotation.DrawableRes import androidx.annotation.IdRes import androidx.annotation.IntegerRes import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.content.res.AppCompatResources import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.navigation.fragment.NavHostFragment @@ -73,4 +76,12 @@ fun Fragment.showToast(@StringRes stringRes: Int) { fun Fragment.showToast(message: String) { Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() +} + +fun Context.getDrawableCompat(@DrawableRes drawableRes: Int): Drawable { + return AppCompatResources.getDrawable(this, drawableRes)!! +} + +fun Fragment.getDrawableCompat(@DrawableRes drawableRes: Int): Drawable { + return AppCompatResources.getDrawable(requireContext(), drawableRes)!! } \ No newline at end of file