Merge pull request #4213 from Bnyro/master

feat: automatically use Material You accent on first app startup when supported
This commit is contained in:
Bnyro 2023-07-11 16:24:46 +02:00 committed by GitHub
commit 55d6f6e5e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 32 deletions

View File

@ -14,6 +14,7 @@ import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.ui.adapters.IconsSheetAdapter
import com.google.android.material.color.DynamicColors
import java.lang.IllegalArgumentException
object ThemeHelper {
@ -24,23 +25,28 @@ object ThemeHelper {
val themeMode = PreferenceHelper.getString(PreferenceKeys.THEME_MODE, "A")
updateAccentColor(activity)
applyDynamicColorsIfEnabled(activity)
applyPureThemeIfEnabled(activity)
updateThemeMode(themeMode)
}
/**
* Update the accent color of the app
* Update the accent color of the app and apply dynamic colors if needed
*/
private fun updateAccentColor(
activity: AppCompatActivity
) {
val theme = when (
PreferenceHelper.getString(
PreferenceKeys.ACCENT_COLOR,
"purple"
)
) {
var accentColor = PreferenceHelper.getString(PreferenceKeys.ACCENT_COLOR, "")
// automatically choose an accent color on the first app startup
if (accentColor.isEmpty()) {
accentColor = when (DynamicColors.isDynamicColorAvailable()) {
true -> "my"
else -> "blue"
}
PreferenceHelper.putString(PreferenceKeys.ACCENT_COLOR, accentColor)
}
val theme = when (accentColor) {
// set the accent color, use the pure black/white theme if enabled
"my" -> R.style.BaseTheme
"red" -> R.style.Theme_Red
@ -50,23 +56,11 @@ object ThemeHelper {
"purple" -> R.style.Theme_Purple
"monochrome" -> R.style.Theme_Monochrome
"violet" -> R.style.Theme_Violet
else -> R.style.Theme_Purple
else -> throw IllegalArgumentException()
}
activity.setTheme(theme)
}
/**
* apply dynamic colors to the activity
*/
private fun applyDynamicColorsIfEnabled(activity: AppCompatActivity) {
if (
PreferenceHelper.getString(
PreferenceKeys.ACCENT_COLOR,
"purple"
) == "my"
) {
DynamicColors.applyToActivityIfAvailable(activity)
}
// apply dynamic wallpaper based colors
if (accentColor == "my") DynamicColors.applyToActivityIfAvailable(activity)
}
/**
@ -110,7 +104,7 @@ object ThemeHelper {
}
// set the class name for the activity alias
val newLogoActivityClass = "com.github.libretube." + newLogoActivityAlias
val newLogoActivityClass = "com.github.libretube.$newLogoActivityAlias"
// Enable New Icon
context.packageManager.setComponentEnabledSetting(
ComponentName(context.packageName, newLogoActivityClass),

View File

@ -1,6 +1,5 @@
package com.github.libretube.ui.preferences
import android.os.Build
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.Preference
@ -13,6 +12,7 @@ import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.NavBarOptionsDialog
import com.github.libretube.ui.dialogs.RequireRestartDialog
import com.github.libretube.ui.sheets.IconsBottomSheet
import com.google.android.material.color.DynamicColors
class AppearanceSettings : BasePreferenceFragment() {
override val titleResourceId: Int = R.string.appearance
@ -78,13 +78,9 @@ class AppearanceSettings : BasePreferenceFragment() {
* Remove material you from accent color option if not available
*/
private fun updateAccentColorValues(pref: ListPreference) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
pref.entries = pref.entries.toMutableList().apply {
removeFirst()
}.toTypedArray()
pref.entryValues = pref.entryValues.toMutableList().apply {
removeFirst()
}.toTypedArray()
if (!DynamicColors.isDynamicColorAvailable()) {
pref.entries = pref.entries.toList().subList(1, pref.entries.size).toTypedArray()
pref.entryValues = pref.entryValues.toList().subList(1, pref.entryValues.size).toTypedArray()
}
}
}