Skip to content

Commit 738da2b

Browse files
committed
refactor: simplify theme and dark/light mode logic
1 parent 9ceb258 commit 738da2b

2 files changed

Lines changed: 22 additions & 34 deletions

File tree

app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import android.content.pm.ActivityInfo
55
import android.content.res.Configuration
66
import android.os.Bundle
77
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.appcompat.app.AppCompatDelegate
89
import com.github.libretube.R
910
import com.github.libretube.constants.PreferenceKeys
1011
import com.github.libretube.helpers.LocaleHelper
1112
import com.github.libretube.helpers.PreferenceHelper
1213
import com.github.libretube.helpers.ThemeHelper
14+
import com.github.libretube.helpers.ThemeHelper.getThemeMode
1315
import com.github.libretube.helpers.WindowHelper
1416
import java.util.Locale
1517

@@ -62,6 +64,9 @@ open class BaseActivity : AppCompatActivity() {
6264

6365
val configuration = Configuration().apply {
6466
setLocale(locale)
67+
68+
val uiPref = PreferenceHelper.getString(PreferenceKeys.THEME_MODE, "A")
69+
AppCompatDelegate.setDefaultNightMode(getThemeMode(uiPref))
6570
}
6671

6772
applyOverrideConfiguration(configuration)

0 commit comments

Comments
 (0)