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

View File

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