Skip to content

Commit ee21762

Browse files
committed
refactor: rebuild theme setting delegates
1 parent 2205cd4 commit ee21762

17 files changed

+193
-167
lines changed

app/src/main/java/com/osfans/trime/data/prefs/AppPrefs.kt

-28
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class AppPrefs(
4040
val internal = Internal(shared)
4141
val general = General(shared).register()
4242
val keyboard = Keyboard(shared)
43-
val theme = Theme(shared)
4443
val profile = Profile(shared).register()
4544
val clipboard = Clipboard(shared)
4645
val other = Other(shared)
@@ -86,7 +85,6 @@ class AppPrefs(
8685
try {
8786
applicationContext.get()?.let { context ->
8887
PreferenceManager.setDefaultValues(context, R.xml.keyboard_preference, true)
89-
PreferenceManager.setDefaultValues(context, R.xml.theme_color_preference, true)
9088
PreferenceManager.setDefaultValues(context, R.xml.profile_preference, true)
9189
PreferenceManager.setDefaultValues(context, R.xml.other_preference, true)
9290
}
@@ -213,32 +211,6 @@ class AppPrefs(
213211
val position = enum(R.string.candidates_window_position, POSITION, PopupPosition.BOTTOM_LEFT)
214212
}
215213

216-
/**
217-
* Wrapper class of theme and color settings.
218-
*/
219-
class Theme(
220-
shared: SharedPreferences,
221-
) : PreferenceDelegateOwner(shared) {
222-
companion object {
223-
const val SELECTED_THEME = "theme_selected_theme"
224-
const val SELECTED_COLOR = "theme_selected_color"
225-
const val FOLLOW_SYSTEM_DAY_NIGHT = "follow_system_day_night"
226-
const val NAVBAR_BACKGROUND = "navbar_background"
227-
}
228-
229-
val selectedTheme = string(SELECTED_THEME, "trime")
230-
val selectedColor = string(SELECTED_COLOR, "default")
231-
val followSystemDayNight = bool(FOLLOW_SYSTEM_DAY_NIGHT, false)
232-
233-
enum class NavbarBackground {
234-
NONE,
235-
COLOR_ONLY,
236-
FULL,
237-
}
238-
239-
val navbarBackground = enum(NAVBAR_BACKGROUND, NavbarBackground.COLOR_ONLY)
240-
}
241-
242214
/**
243215
* Wrapper class of profile settings.
244216
*/

app/src/main/java/com/osfans/trime/data/prefs/PreferenceDelegateOwner.kt

+18-7
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ abstract class PreferenceDelegateOwner(
3535
protected fun string(
3636
key: String,
3737
defaultValue: String,
38-
): PreferenceDelegate<String> = PreferenceDelegate(sharedPreferences, key, defaultValue)
39-
40-
protected fun stringSet(
41-
key: String,
42-
defaultValue: Set<String>,
43-
) = PreferenceDelegate(sharedPreferences, key, defaultValue)
38+
): PreferenceDelegate<String> = PreferenceDelegate(sharedPreferences, key, defaultValue).apply { register() }
4439

4540
protected fun <T : Any> serializable(
4641
key: String,
@@ -61,6 +56,22 @@ abstract class PreferenceDelegateOwner(
6156
},
6257
)
6358

59+
protected fun string(
60+
@StringRes
61+
title: Int,
62+
key: String,
63+
defaultValue: String,
64+
@StringRes
65+
summary: Int? = null,
66+
enableUiOn: (() -> Boolean)? = null,
67+
): PreferenceDelegate<String> {
68+
val pref = PreferenceDelegate(sharedPreferences, key, defaultValue)
69+
val ui = PreferenceDelegateUi.StringLike(title, key, defaultValue, summary, enableUiOn)
70+
pref.register()
71+
ui.registerUi()
72+
return pref
73+
}
74+
6475
protected fun switch(
6576
@StringRes
6677
title: Int,
@@ -93,7 +104,7 @@ abstract class PreferenceDelegateOwner(
93104
val entryValues = enumValues<T>().toList()
94105
val entryLabels = entryValues.map { it.stringRes }
95106
val pref = serializable(key, defaultValue, serializer)
96-
val ui = PreferenceDelegateUi.StringList(title, key, defaultValue, serializer, entryValues, entryLabels)
107+
val ui = PreferenceDelegateUi.StringList(title, key, defaultValue, serializer, entryValues, entryLabels, enableUiOn)
97108
pref.register()
98109
ui.registerUi()
99110
return pref

app/src/main/java/com/osfans/trime/data/prefs/PreferenceDelegateUi.kt

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ abstract class PreferenceDelegateUi<T : Preference>(
1818

1919
fun isEnabled() = enableUiOn?.invoke() ?: true
2020

21+
class StringLike(
22+
@StringRes
23+
val title: Int,
24+
key: String,
25+
val defaultValue: String,
26+
@StringRes
27+
val summary: Int? = null,
28+
enableUiOn: (() -> Boolean)? = null,
29+
) : PreferenceDelegateUi<Preference>(key, enableUiOn) {
30+
override fun createUi(context: Context) =
31+
Preference(context).apply {
32+
key = this@StringLike.key
33+
isIconSpaceReserved = false
34+
isSingleLineTitle = false
35+
setDefaultValue(defaultValue)
36+
if (this@StringLike.summary != null) {
37+
setSummary(this@StringLike.summary)
38+
}
39+
setTitle(this@StringLike.title)
40+
}
41+
}
42+
2143
class Switch(
2244
@StringRes
2345
val title: Int,

app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.annotation.ColorInt
1212
import androidx.collection.LruCache
1313
import androidx.core.math.MathUtils
1414
import com.osfans.trime.data.base.DataManager
15-
import com.osfans.trime.data.prefs.AppPrefs
1615
import com.osfans.trime.util.ColorUtils
1716
import com.osfans.trime.util.WeakHashSet
1817
import com.osfans.trime.util.bitmapDrawable
@@ -21,8 +20,8 @@ import timber.log.Timber
2120

2221
object ColorManager {
2322
private lateinit var theme: Theme
24-
private val prefs = AppPrefs.defaultInstance().theme
25-
private var prefsSelectedColor by prefs.selectedColor
23+
private val prefs = ThemeManager.prefs
24+
private var normalModeColor by prefs.normalModeColor
2625
private val followSystemDayNight by prefs.followSystemDayNight
2726
private val backgroundFolder get() = theme.generalStyle.backgroundFolder
2827

@@ -39,7 +38,7 @@ object ColorManager {
3938
initialColors["dark_scheme"] ?: lastDarkColorScheme
4039
} else {
4140
initialColors["light_scheme"] ?: lastLightColorScheme
42-
} ?: prefsSelectedColor
41+
} ?: normalModeColor
4342
}
4443

4544
private val defaultFallbackColors =
@@ -124,23 +123,23 @@ object ColorManager {
124123

125124
this.theme = theme
126125

127-
initialColors = theme.presetColorSchemes[prefsSelectedColor] ?: mapOf()
126+
initialColors = theme.presetColorSchemes[normalModeColor] ?: mapOf()
128127
fallbackRules = theme.fallbackColors + defaultFallbackColors
129128

130129
switchNightMode(isNightMode)
131130
}
132131

133132
fun setColorScheme(scheme: String) {
134133
switchColorScheme(scheme)
135-
prefsSelectedColor = scheme
134+
normalModeColor = scheme
136135
}
137136

138137
private fun switchColorScheme(scheme: String) {
139138
if (!theme.presetColorSchemes.containsKey(scheme)) {
140139
Timber.w("Color scheme $scheme not found", scheme)
141140
return
142141
}
143-
Timber.d("switch color scheme from $prefsSelectedColor to $scheme")
142+
Timber.d("switch color scheme from $normalModeColor to $scheme")
144143
// 刷新配色
145144
val isFirst = colorCache.size() == 0 || drawableCache.size() == 0
146145
refreshColorValues(scheme)

app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object ThemeManager {
4444
onChangeListeners.forEach { it.onThemeChange(_activeTheme) }
4545
}
4646

47-
private val prefs = AppPrefs.defaultInstance().theme
47+
val prefs = AppPrefs.defaultInstance().registerProvider(::ThemePrefs)
4848

4949
private fun evaluateActiveTheme(): Theme {
5050
val newTheme = Theme(prefs.selectedTheme.getValue())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015 - 2025 Rime community
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package com.osfans.trime.data.theme
7+
8+
import android.content.SharedPreferences
9+
import android.os.Build
10+
import androidx.core.content.edit
11+
import com.osfans.trime.R
12+
import com.osfans.trime.data.prefs.PreferenceDelegateEnum
13+
import com.osfans.trime.data.prefs.PreferenceDelegateOwner
14+
15+
class ThemePrefs(
16+
sharedPrefs: SharedPreferences,
17+
) : PreferenceDelegateOwner(sharedPrefs, R.string.theme) {
18+
val selectedTheme =
19+
string(
20+
R.string.selected_theme,
21+
SELECTED_THEME,
22+
"trime",
23+
R.string.selected_theme_summary,
24+
)
25+
26+
val normalModeColor =
27+
string(
28+
R.string.normal_mode_color,
29+
NORMAL_MODE_COLOR,
30+
"default",
31+
R.string.normal_mode_color_summary,
32+
)
33+
34+
enum class NavbarBackground(
35+
override val stringRes: Int,
36+
) : PreferenceDelegateEnum {
37+
NONE(R.string.navbar_bkg_none),
38+
COLOR_ONLY(R.string.navbar_bkg_color_only),
39+
FULL(R.string.navbar_bkg_full),
40+
}
41+
42+
val navbarBackground =
43+
enum(
44+
R.string.navbar_background,
45+
NAVBAR_BACKGROUND,
46+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
47+
NavbarBackground.FULL
48+
} else {
49+
NavbarBackground.COLOR_ONLY
50+
},
51+
enableUiOn = { Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM },
52+
).apply {
53+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
54+
sharedPreferences.edit {
55+
remove(this@apply.key)
56+
}
57+
}
58+
}
59+
60+
val followSystemDayNight =
61+
switch(
62+
R.string.follow_system_day_night_color,
63+
FOLLOW_SYSTEM_DAY_NIGHT,
64+
false,
65+
)
66+
67+
companion object {
68+
const val SELECTED_THEME = "selected_theme"
69+
const val NORMAL_MODE_COLOR = "normal_mode_color"
70+
const val FOLLOW_SYSTEM_DAY_NIGHT = "follow_system_day_night"
71+
const val NAVBAR_BACKGROUND = "navbar_background"
72+
}
73+
}

app/src/main/java/com/osfans/trime/ime/core/NavigationBarManager.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import android.os.Build
1010
import android.view.Window
1111
import androidx.annotation.ColorInt
1212
import androidx.core.view.WindowCompat
13-
import com.osfans.trime.data.prefs.AppPrefs
1413
import com.osfans.trime.data.theme.ColorManager
14+
import com.osfans.trime.data.theme.ThemeManager
15+
import com.osfans.trime.data.theme.ThemePrefs
1516
import com.osfans.trime.util.ColorUtils
1617

1718
class NavigationBarManager {
18-
private val navbarBackground by AppPrefs.defaultInstance().theme.navbarBackground
19+
private val navbarBackground by ThemeManager.prefs.navbarBackground
1920

2021
private var shouldUpdateNavbarForeground = false
2122
private var shouldUpdateNavbarBackground = false
@@ -48,19 +49,19 @@ class NavigationBarManager {
4849

4950
fun evaluate(window: Window) {
5051
when (navbarBackground) {
51-
AppPrefs.Theme.NavbarBackground.NONE -> {
52+
ThemePrefs.NavbarBackground.NONE -> {
5253
shouldUpdateNavbarForeground = false
5354
shouldUpdateNavbarBackground = false
5455
window.useSystemNavbarBackground(true)
5556
window.enforceNavbarContrast(true)
5657
}
57-
AppPrefs.Theme.NavbarBackground.COLOR_ONLY -> {
58+
ThemePrefs.NavbarBackground.COLOR_ONLY -> {
5859
shouldUpdateNavbarForeground = true
5960
shouldUpdateNavbarBackground = true
6061
window.useSystemNavbarBackground(true)
6162
window.enforceNavbarContrast(false)
6263
}
63-
AppPrefs.Theme.NavbarBackground.FULL -> {
64+
ThemePrefs.NavbarBackground.FULL -> {
6465
shouldUpdateNavbarForeground = true
6566
shouldUpdateNavbarBackground = false
6667
window.useSystemNavbarBackground(false)

app/src/main/java/com/osfans/trime/ui/fragments/PrefFragment.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class PrefFragment : PaddingPreferenceFragment() {
6969
true
7070
}
7171
get<Preference>("pref_theme_and_color")?.setOnPreferenceClickListener {
72-
findNavController().navigate(R.id.action_prefFragment_to_themeFragment)
72+
findNavController().navigate(R.id.action_prefFragment_to_themeSettingsFragment)
7373
true
7474
}
7575
get<Preference>("pref_clipboard")?.setOnPreferenceClickListener {

app/src/main/java/com/osfans/trime/ui/fragments/ThemeFragment.kt

-43
This file was deleted.

app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object ColorPickerDialog {
2727
return AlertDialog
2828
.Builder(context)
2929
.apply {
30-
setTitle(R.string.looks__selected_color_title)
30+
setTitle(R.string.normal_mode_color)
3131
if (all.isEmpty()) {
3232
setMessage(R.string.no_color_to_select)
3333
} else {

app/src/main/java/com/osfans/trime/ui/main/settings/ThemePickerDialog.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import android.app.AlertDialog
88
import android.content.Context
99
import androidx.lifecycle.LifecycleCoroutineScope
1010
import com.osfans.trime.R
11-
import com.osfans.trime.data.prefs.AppPrefs
1211
import com.osfans.trime.data.theme.ThemeManager
1312
import kotlinx.coroutines.Dispatchers
1413
import kotlinx.coroutines.launch
@@ -24,12 +23,12 @@ object ThemePickerDialog {
2423
withContext(Dispatchers.IO) {
2524
ThemeManager.getAllThemes()
2625
}
27-
val selectedTheme by AppPrefs.defaultInstance().theme.selectedTheme
26+
val selectedTheme by ThemeManager.prefs.selectedTheme
2827
val selectedIndex = allThemes.indexOfFirst { it.configId == selectedTheme }
2928
return AlertDialog
3029
.Builder(context)
3130
.apply {
32-
setTitle(R.string.looks__selected_theme_title)
31+
setTitle(R.string.selected_theme)
3332
if (allThemes.isEmpty()) {
3433
setMessage(R.string.no_theme_to_select)
3534
} else {

0 commit comments

Comments
 (0)