修复主题应用

This commit is contained in:
HapeLee committed 2025-06-11 01:16:11 +08:00
1 parent 32e7e78c69
commit 9c3f185fa7
10 files changed
+59 -78

No files matched your search

@@ -21,6 +21,7 @@ import com.google.android.material.color.DynamicColors
import io.legato.kazusa.R import io.legato.kazusa.R
import io.legato.kazusa.constant.AppConst import io.legato.kazusa.constant.AppConst
import io.legato.kazusa.constant.AppLog import io.legato.kazusa.constant.AppLog
import io.legato.kazusa.constant.EventBus
import io.legato.kazusa.constant.PreferKey import io.legato.kazusa.constant.PreferKey
import io.legato.kazusa.constant.Theme import io.legato.kazusa.constant.Theme
import io.legato.kazusa.help.config.ThemeConfig import io.legato.kazusa.help.config.ThemeConfig
@@ -32,8 +33,10 @@ import io.legato.kazusa.utils.disableAutoFill
import io.legato.kazusa.utils.getPrefBoolean import io.legato.kazusa.utils.getPrefBoolean
import io.legato.kazusa.utils.getPrefString import io.legato.kazusa.utils.getPrefString
import io.legato.kazusa.utils.hideSoftInput import io.legato.kazusa.utils.hideSoftInput
import io.legato.kazusa.utils.observeEvent
import io.legato.kazusa.utils.toastOnUi import io.legato.kazusa.utils.toastOnUi
import io.legato.kazusa.utils.windowSize import io.legato.kazusa.utils.windowSize
import androidx.core.graphics.drawable.toDrawable
abstract class BaseActivity<VB : ViewBinding>( abstract class BaseActivity<VB : ViewBinding>(
@@ -74,7 +77,7 @@ abstract class BaseActivity<VB : ViewBinding>(
@SuppressLint("ObsoleteSdkInt") @SuppressLint("ObsoleteSdkInt")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
initTheme() applyTheme()
window.decorView.disableAutoFill() window.decorView.disableAutoFill()
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -132,7 +135,7 @@ abstract class BaseActivity<VB : ViewBinding>(
open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item) open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item)
open fun initTheme() { open fun applyTheme() {
when (getPrefString("app_theme", "0")) { when (getPrefString("app_theme", "0")) {
"0" -> DynamicColors.applyToActivitiesIfAvailable(application) "0" -> DynamicColors.applyToActivitiesIfAvailable(application)
"1" -> setTheme(R.style.Theme_Base_GR) "1" -> setTheme(R.style.Theme_Base_GR)
@@ -145,7 +148,7 @@ abstract class BaseActivity<VB : ViewBinding>(
if (imageBg) { if (imageBg) {
try { try {
ThemeConfig.getBgImage(this, windowManager.windowSize)?.let { ThemeConfig.getBgImage(this, windowManager.windowSize)?.let {
window.decorView.background = BitmapDrawable(resources, it) window.decorView.background = it.toDrawable(resources)
} }
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
toastOnUi("背景图片太大,内存溢出") toastOnUi("背景图片太大,内存溢出")
@@ -155,21 +158,6 @@ abstract class BaseActivity<VB : ViewBinding>(
} }
} }
// open fun setupSystemBar() {
// if (fullScreen && !isInMultiWindow) {
// fullScreen()
// }
// //val isTransparentStatusBar = AppConfig.isTransparentStatusBar
// //val statusBarColor = ThemeStore.statusBarColor(this, isTransparentStatusBar)
// //setStatusBarColorAuto(statusBarColor, isTransparentStatusBar, fullScreen)
//// if (toolBarTheme == Theme.Dark) {
//// setLightStatusBar(false)
//// } else if (toolBarTheme == Theme.Light) {
//// setLightStatusBar(true)
//// }
// upNavigationBarColor()
// }
open fun upNavigationBarColor() { open fun upNavigationBarColor() {
// if (AppConfig.immNavigationBar) { // if (AppConfig.immNavigationBar) {
// setNavigationBarColorAuto(ThemeStore.navigationBarColor(this)) // setNavigationBarColorAuto(ThemeStore.navigationBarColor(this))
@@ -180,6 +168,9 @@ abstract class BaseActivity<VB : ViewBinding>(
} }
open fun observeLiveBus() { open fun observeLiveBus() {
observeEvent<String>(EventBus.RECREATE) {
recreate()
}
} }
override fun dispatchTouchEvent(ev: MotionEvent): Boolean { override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
@@ -180,6 +180,5 @@ object PreferKey {
const val readBarStyleFollowPage = "readBarStyleFollowPage" const val readBarStyleFollowPage = "readBarStyleFollowPage"
const val contentSelectSpeakMod = "contentReadAloudMod" const val contentSelectSpeakMod = "contentReadAloudMod"
const val dynamicColors = "dynamicColors"
const val themePref = "themePref" const val themePref = "themePref"
} }
@@ -43,9 +43,9 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) { when (key) {
PreferKey.themeMode -> { PreferKey.themeMode -> {
themeMode = appCtx.getPrefString(PreferKey.themeMode, "0") themeMode = appCtx.getPrefString(PreferKey.themeMode, "0")
isEInkMode = themeMode == "3"
} }
PreferKey.clickActionTL -> clickActionTL = PreferKey.clickActionTL -> clickActionTL =
@@ -103,12 +103,9 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
else -> sysConfiguration.isNightMode else -> sysConfiguration.isNightMode
} }
set(value) { set(value) {
if (isNightTheme != value) { val newMode = if (value) "2" else "1"
if (value) { if (themeMode != newMode) {
appCtx.putPrefString(PreferKey.themeMode, "2") appCtx.putPrefString(PreferKey.themeMode, newMode)
} else {
appCtx.putPrefString(PreferKey.themeMode, "1")
}
} }
} }
@@ -51,25 +51,28 @@ object ThemeConfig {
} }
fun applyDayNight(context: Context) { fun applyDayNight(context: Context) {
applyTheme(context)
initNightMode() initNightMode()
BookCover.upDefaultCover() BookCover.upDefaultCover()
postEvent(EventBus.RECREATE, "") postEvent(EventBus.RECREATE, "")
} }
fun applyDayNightInit(context: Context) { fun applyDayNightInit(context: Context) {
applyTheme(context) //applyTheme(context)
initNightMode() initNightMode()
} }
private fun initNightMode() { private fun initNightMode() {
val targetMode = when (appCtx.getPrefString(PreferKey.themeMode, "0")) {
if (AppConfig.isNightTheme) { "1" -> {
AppCompatDelegate.MODE_NIGHT_YES AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
} else { }
AppCompatDelegate.MODE_NIGHT_NO "2" -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
} }
AppCompatDelegate.setDefaultNightMode(targetMode)
} }
fun getBgImage(context: Context, metrics: DisplayMetrics): Bitmap? { fun getBgImage(context: Context, metrics: DisplayMetrics): Bitmap? {
@@ -214,9 +217,9 @@ object ThemeConfig {
/** /**
* 更新主题 * 更新主题
*/ */
fun applyTheme(context: Context) = with(context) { // fun applyTheme(context: Context) = with(context) {
//
} // }
@Keep @Keep
data class Config( data class Config(
@@ -16,15 +16,15 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView import com.google.android.material.card.MaterialCardView
import io.legato.kazusa.R import io.legato.kazusa.R
import androidx.core.graphics.toColorInt import androidx.core.graphics.toColorInt
import io.legato.kazusa.constant.EventBus
import io.legato.kazusa.utils.postEvent
@SuppressLint("ResourceType") @SuppressLint("ResourceType")
class ThemeCardPreference( class ThemeCardPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
context: Context,
attrs: AttributeSet
) : Preference(context, attrs) {
private var entries: Array<CharSequence> = context.resources.getTextArray(R.array.themes_item) private var entries: Array<CharSequence> = context.resources.getTextArray(R.array.themes_item)
private var entryValues: Array<CharSequence> = context.resources.getTextArray(R.array.themes_value) private var entryValues: Array<CharSequence> = context.resources.getTextArray(R.array.themes_value).takeIf { it.isNotEmpty() }
?: arrayOf("0")
private var currentValue: String? = null private var currentValue: String? = null
init { init {
@@ -39,10 +39,8 @@ class ThemeCardPreference(
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
//val titleView = holder.findViewById(R.id.title) as TextView
val recyclerView = holder.findViewById(R.id.recyclerView) as RecyclerView val recyclerView = holder.findViewById(R.id.recyclerView) as RecyclerView
//titleView.text = title
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = ThemeAdapter() recyclerView.adapter = ThemeAdapter()
} }
@@ -80,6 +78,7 @@ class ThemeCardPreference(
persistString(value) persistString(value)
callChangeListener(value) callChangeListener(value)
notifyDataSetChanged() notifyDataSetChanged()
postEvent(EventBus.RECREATE, "")
} }
} }
} }
@@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import com.google.android.material.button.MaterialButtonToggleGroup import com.google.android.material.button.MaterialButtonToggleGroup
import io.legato.kazusa.R import io.legato.kazusa.R
import io.legato.kazusa.help.config.ThemeConfig
class ThemeModePreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) { class ThemeModePreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
@@ -41,7 +42,6 @@ class ThemeModePreference(context: Context, attrs: AttributeSet) : Preference(co
"2" -> toggleGroup.check(R.id.btn_dark) "2" -> toggleGroup.check(R.id.btn_dark)
} }
// 监听选择变化
toggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked -> toggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (isChecked) { if (isChecked) {
val newValue = when (checkedId) { val newValue = when (checkedId) {
@@ -54,6 +54,8 @@ class ThemeModePreference(context: Context, attrs: AttributeSet) : Preference(co
if (newValue != null && callChangeListener(newValue)) { if (newValue != null && callChangeListener(newValue)) {
currentValue = newValue currentValue = newValue
persistString(newValue) persistString(newValue)
callChangeListener(newValue)
ThemeConfig.applyDayNight(context)
} }
} }
} }
@@ -66,4 +68,5 @@ class ThemeModePreference(context: Context, attrs: AttributeSet) : Preference(co
override fun shouldDisableDependents(): Boolean { override fun shouldDisableDependents(): Boolean {
return currentValue == null || super.shouldDisableDependents() return currentValue == null || super.shouldDisableDependents()
} }
} }
@@ -40,12 +40,4 @@ class ConfigActivity : VMBaseActivity<ActivityConfigBinding, ConfigViewModel>()
.replace(R.id.configFrameLayout, configFragment, configTag) .replace(R.id.configFrameLayout, configFragment, configTag)
.commit() .commit()
} }
override fun observeLiveBus() {
super.observeLiveBus()
observeEvent<String>(EventBus.RECREATE) {
recreate()
}
}
} }
@@ -1,6 +1,7 @@
package io.legato.kazusa.ui.config package io.legato.kazusa.ui.config
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
@@ -74,11 +75,11 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
if (Build.VERSION.SDK_INT < 26) { if (Build.VERSION.SDK_INT < 26) {
preferenceScreen.removePreferenceRecursively(PreferKey.launcherIcon) preferenceScreen.removePreferenceRecursively(PreferKey.launcherIcon)
} }
upPreferenceSummary(PreferKey.themePref)
upPreferenceSummary(PreferKey.bgImage, getPrefString(PreferKey.bgImage)) upPreferenceSummary(PreferKey.bgImage, getPrefString(PreferKey.bgImage))
upPreferenceSummary(PreferKey.bgImageN, getPrefString(PreferKey.bgImageN)) upPreferenceSummary(PreferKey.bgImageN, getPrefString(PreferKey.bgImageN))
//upPreferenceSummary(PreferKey.barElevation, AppConfig.elevation.toString())
upPreferenceSummary(PreferKey.fontScale) upPreferenceSummary(PreferKey.fontScale)
findPreference<ColorPreference>(PreferKey.cBackground)?.let { findPreference<ColorPreference>(PreferKey.cBackground)?.let {
it.onSaveColor = { color -> it.onSaveColor = { color ->
if (!ColorUtils.isColorLight(color)) { if (!ColorUtils.isColorLight(color)) {
@@ -89,6 +90,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
} }
} }
} }
findPreference<ColorPreference>(PreferKey.cNBackground)?.let { findPreference<ColorPreference>(PreferKey.cNBackground)?.let {
it.onSaveColor = { color -> it.onSaveColor = { color ->
if (ColorUtils.isColorLight(color)) { if (ColorUtils.isColorLight(color)) {
@@ -99,17 +101,15 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
} }
} }
} }
findPreference<ThemeModePreference>(PreferKey.themeMode)?.let { findPreference<ThemeModePreference>(PreferKey.themeMode)?.let {
it.setOnPreferenceChangeListener { _, _ -> it.setOnPreferenceChangeListener { _, _ ->
ThemeConfig.applyDayNight(requireContext())
true true
} }
} }
findPreference<ThemeCardPreference>(PreferKey.themePref)?.let { findPreference<ThemeCardPreference>(PreferKey.themePref)?.let {
it.setOnPreferenceChangeListener { _, _ -> it.setOnPreferenceChangeListener { _, _ ->
val intent = requireActivity().intent
requireActivity().finish()
startActivity(intent)
true true
} }
} }
@@ -138,11 +138,12 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
PreferKey.launcherIcon -> LauncherIconHelp.changeIcon(getPrefString(key)) PreferKey.launcherIcon -> LauncherIconHelp.changeIcon(getPrefString(key))
//PreferKey.transparentStatusBar -> recreateActivities() //PreferKey.transparentStatusBar -> recreateActivities()
//PreferKey.immNavigationBar -> recreateActivities() //PreferKey.immNavigationBar -> recreateActivities()
PreferKey.themePref -> { PreferKey.themePref -> {
val intent = requireActivity().intent ThemeConfig.applyDayNight(requireContext())
requireActivity().finish() recreateActivities()
startActivity(intent)
} }
PreferKey.cPrimary, PreferKey.cPrimary,
PreferKey.cAccent, PreferKey.cAccent,
PreferKey.cBackground, PreferKey.cBackground,
@@ -158,7 +159,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
} }
PreferKey.themeMode -> { PreferKey.themeMode -> {
ThemeConfig.applyDayNight(requireContext()) recreateActivities()
} }
PreferKey.bgImage, PreferKey.bgImage,
@@ -291,7 +292,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
private fun upTheme(isNightTheme: Boolean) { private fun upTheme(isNightTheme: Boolean) {
if (AppConfig.isNightTheme == isNightTheme) { if (AppConfig.isNightTheme == isNightTheme) {
listView.post { listView.post {
ThemeConfig.applyTheme(requireContext()) //ThemeConfig.applyTheme(requireContext())
recreateActivities() recreateActivities()
} }
} }
@@ -56,6 +56,7 @@ import kotlinx.coroutines.withContext
import splitties.views.bottomPadding import splitties.views.bottomPadding
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
import androidx.core.view.get
/** /**
* 主界面 * 主界面
@@ -197,9 +198,6 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
//bottomNavigationView.elevation = elevation //bottomNavigationView.elevation = elevation
bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity) bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity)
bottomNavigationView.setOnNavigationItemReselectedListener(this@MainActivity) bottomNavigationView.setOnNavigationItemReselectedListener(this@MainActivity)
if (AppConfig.isEInkMode) {
bottomNavigationView.setBackgroundResource(R.drawable.bg_eink_border_top)
}
bottomNavigationView.setOnApplyWindowInsetsListenerCompat { view, windowInsets -> bottomNavigationView.setOnApplyWindowInsetsListenerCompat { view, windowInsets ->
val height = windowInsets.navigationBarHeight val height = windowInsets.navigationBarHeight
view.bottomPadding = height view.bottomPadding = height
@@ -411,10 +409,8 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
override fun onPageSelected(position: Int) { override fun onPageSelected(position: Int) {
pagePosition = position pagePosition = position
binding.bottomNavigationView.menu binding.bottomNavigationView.menu[realPositions[position]].isChecked = true
.getItem(realPositions[position]).isChecked = true
} }
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
+8 -8
View File
@@ -2,15 +2,15 @@
//**************************************************************Theme******************************************************************************// //**************************************************************Theme******************************************************************************//
<!-- 亮色主题 --> <!-- &lt;!&ndash; 亮色主题 &ndash;&gt;-->
<style name="AppTheme.Light" parent="Base.AppTheme"> <!-- <style name="AppTheme.Light" parent="Base.AppTheme">-->
<item name="android:windowLightStatusBar">true</item> <!-- <item name="android:windowLightStatusBar">true</item>-->
</style> <!-- </style>-->
<!-- 暗色主题 --> <!-- &lt;!&ndash; 暗色主题 &ndash;&gt;-->
<style name="AppTheme.Dark" parent="Base.AppTheme"> <!-- <style name="AppTheme.Dark" parent="Base.AppTheme">-->
<item name="android:windowLightStatusBar">false</item> <!-- <item name="android:windowLightStatusBar">false</item>-->
</style> <!-- </style>-->
<!-- 透明主题 --> <!-- 透明主题 -->
<style name="AppTheme.Transparent" parent="Base.AppTheme"> <style name="AppTheme.Transparent" parent="Base.AppTheme">