style: simplify ThemeHelper#getThemeColor

This commit is contained in:
Bnyro 2023-08-25 19:50:53 +02:00
parent 3f095da89f
commit dbb7126dfb
2 changed files with 7 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Color
import android.text.Spanned import android.text.Spanned
import android.util.TypedValue import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -15,6 +16,7 @@ import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.ui.adapters.IconsSheetAdapter import com.github.libretube.ui.adapters.IconsSheetAdapter
import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColors
import com.google.android.material.color.MaterialColors
object ThemeHelper { object ThemeHelper {
@ -114,13 +116,9 @@ object ThemeHelper {
} }
/** /**
* Get a color by a resource code * Get a color by a color resource attr
*/ */
fun getThemeColor(context: Context, colorCode: Int): Int { fun getThemeColor(context: Context, colorCode: Int) = MaterialColors.getColor(context, colorCode, Color.TRANSPARENT)
val value = TypedValue()
context.theme.resolveAttribute(colorCode, value, true)
return value.data
}
/** /**
* Get the styled app name * Get the styled app name

View File

@ -9,7 +9,6 @@ import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.helpers.ThemeHelper import com.github.libretube.helpers.ThemeHelper
import com.google.android.material.color.MaterialColors
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
/** /**
@ -21,15 +20,12 @@ fun View.setWatchProgressLength(videoId: String, duration: Long) {
updateLayoutParams<ConstraintLayout.LayoutParams> { updateLayoutParams<ConstraintLayout.LayoutParams> {
matchConstraintPercentWidth = 0f matchConstraintPercentWidth = 0f
} }
var backgroundColor = MaterialColors.getColor( var backgroundColor = ThemeHelper.getThemeColor(
this, context,
com.google.android.material.R.attr.colorPrimaryDark com.google.android.material.R.attr.colorPrimaryDark
) )
// increase the brightness for better contrast in light mode // increase the brightness for better contrast in light mode
if (!ThemeHelper.isDarkMode( if (!ThemeHelper.isDarkMode(context)) {
context
)
) {
backgroundColor = ColorUtils.blendARGB(backgroundColor, Color.WHITE, 0.4f) backgroundColor = ColorUtils.blendARGB(backgroundColor, Color.WHITE, 0.4f)
} }
setBackgroundColor(backgroundColor) setBackgroundColor(backgroundColor)