修复主题应用

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