@@ -55,29 +55,27 @@ object ThemeHelper {
5555 * Set the theme, including accent color and night mode
5656 */
5757 fun updateTheme (activity : AppCompatActivity ) {
58- val themeMode = PreferenceHelper .getString(PreferenceKeys .THEME_MODE , " A" )
58+ var accentColor = PreferenceHelper .getString(PreferenceKeys .ACCENT_COLOR , " " )
59+ if (accentColor.isEmpty()) {
60+ accentColor = if (DynamicColors .isDynamicColorAvailable()) " my" else " blue"
61+ PreferenceHelper .putString(PreferenceKeys .ACCENT_COLOR , accentColor)
62+ }
5963
60- updateAccentColor(activity)
61- applyPureThemeIfEnabled(activity)
62- updateThemeMode(themeMode)
64+ activity.setTheme(getTheme(accentColor))
65+ if (accentColor == " my" ) DynamicColors .applyToActivityIfAvailable(activity)
66+
67+ val pureThemeEnabled = PreferenceHelper .getBoolean(
68+ PreferenceKeys .PURE_THEME ,
69+ false
70+ )
71+ if (pureThemeEnabled) activity.theme.applyStyle(R .style.Pure , true )
6372 }
6473
6574 /* *
6675 * Update the accent color of the app and apply dynamic colors if needed
6776 */
68- private fun updateAccentColor (activity : AppCompatActivity ) {
69- var accentColor = PreferenceHelper .getString(PreferenceKeys .ACCENT_COLOR , " " )
70-
71- // automatically choose an accent color on the first app startup
72- if (accentColor.isEmpty()) {
73- accentColor = when (DynamicColors .isDynamicColorAvailable()) {
74- true -> " my"
75- else -> " blue"
76- }
77- PreferenceHelper .putString(PreferenceKeys .ACCENT_COLOR , accentColor)
78- }
79-
80- val theme = when (accentColor) {
77+ private fun getTheme (accentColor : String ): Int {
78+ return when (accentColor) {
8179 // set the accent color, use the pure black/white theme if enabled
8280 " my" -> R .style.BaseTheme
8381 " red" -> R .style.Theme_Red
@@ -89,20 +87,6 @@ object ThemeHelper {
8987 " violet" -> R .style.Theme_Violet
9088 else -> throw IllegalArgumentException ()
9189 }
92- activity.setTheme(theme)
93- // apply dynamic wallpaper based colors
94- if (accentColor == " my" ) DynamicColors .applyToActivityIfAvailable(activity)
95- }
96-
97- /* *
98- * apply the pure black/white theme
99- */
100- private fun applyPureThemeIfEnabled (activity : Activity ) {
101- val pureThemeEnabled = PreferenceHelper .getBoolean(
102- PreferenceKeys .PURE_THEME ,
103- false
104- )
105- if (pureThemeEnabled) activity.theme.applyStyle(R .style.Pure , true )
10690 }
10791
10892 fun applyDialogActivityTheme (activity : Activity ) {
@@ -112,14 +96,13 @@ object ThemeHelper {
11296 /* *
11397 * set the theme mode (light, dark, auto)
11498 */
115- private fun updateThemeMode (themeMode : String ) {
116- val mode = when (themeMode) {
99+ fun getThemeMode (themeMode : String ): Int {
100+ return when (themeMode) {
117101 " A" -> AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM
118102 " L" -> AppCompatDelegate .MODE_NIGHT_NO
119103 " D" -> AppCompatDelegate .MODE_NIGHT_YES
120104 else -> AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM
121105 }
122- AppCompatDelegate .setDefaultNightMode(mode)
123106 }
124107
125108 /* *
0 commit comments