重写主题系统

This commit is contained in:
HapeLee
2025-06-07 04:39:02 +08:00
parent 588ac40282
commit 0bb2528fab
56 changed files with 531 additions and 665 deletions
+1 -1
View File
@@ -5,6 +5,7 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.content.pm.ApplicationInfo
import android.content.res.Configuration
@@ -68,7 +69,6 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
DynamicColors.applyToActivitiesIfAvailable(this)
CrashHandler(this)
if (isDebuggable) {
ThreadUtils.setThreadAssertsDisabledForTesting(true)
@@ -17,9 +17,11 @@ import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.google.android.material.appbar.AppBarLayout
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.PreferKey
import io.legato.kazusa.constant.Theme
import io.legato.kazusa.help.config.ThemeConfig
//import io.legado.app.lib.theme.backgroundColor
@@ -27,6 +29,8 @@ import io.legato.kazusa.help.config.ThemeConfig
import io.legato.kazusa.utils.applyOpenTint
import io.legato.kazusa.utils.applyTint
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.toastOnUi
import io.legato.kazusa.utils.windowSize
@@ -70,8 +74,9 @@ abstract class BaseActivity<VB : ViewBinding>(
@SuppressLint("ObsoleteSdkInt")
override fun onCreate(savedInstanceState: Bundle?) {
initTheme()
window.decorView.disableAutoFill()
//initTheme()
super.onCreate(savedInstanceState)
//setupSystemBar()
setContentView(binding.root)
@@ -128,26 +133,10 @@ abstract class BaseActivity<VB : ViewBinding>(
open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item)
open fun initTheme() {
when (theme) {
Theme.Transparent -> setTheme(R.style.AppTheme_Transparent)
Theme.Dark -> {
setTheme(R.style.AppTheme_Dark)
//window.decorView.applyBackgroundTint(backgroundColor)
}
Theme.Light -> {
setTheme(R.style.AppTheme_Light)
//window.decorView.applyBackgroundTint(backgroundColor)
}
else -> {
// if (ColorUtils.isColorLight(primaryColor)) {
// setTheme(R.style.AppTheme_Light)
// } else {
// setTheme(R.style.AppTheme_Dark)
// }
//window.decorView.applyBackgroundTint(backgroundColor)
}
when (getPrefString("app_theme", "0")) {
"0" -> DynamicColors.applyToActivitiesIfAvailable(application)
"1" -> setTheme(R.style.Theme_Base_GR)
"2" -> setTheme(R.style.Theme_Base_WH)
}
}
@@ -179,4 +179,7 @@ object PreferKey {
const val showReadTitleAddition = "showReadTitleAddition"
const val readBarStyleFollowPage = "readBarStyleFollowPage"
const val contentSelectSpeakMod = "contentReadAloudMod"
const val dynamicColors = "dynamicColors"
const val themePref = "themePref"
}
@@ -1,5 +1,5 @@
package io.legato.kazusa.constant
enum class Theme {
Dark, Light, Auto, Transparent, EInk;
Dark, Light, Auto, Transparent, EInk, GR;
}
@@ -100,7 +100,6 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
get() = when (themeMode) {
"1" -> false
"2" -> true
"3" -> false
else -> sysConfiguration.isNightMode
}
set(value) {
@@ -42,7 +42,6 @@ object ThemeConfig {
}
fun getTheme() = when {
AppConfig.isEInkMode -> Theme.EInk
AppConfig.isNightTheme -> Theme.Dark
else -> Theme.Light
}
@@ -216,58 +215,7 @@ object ThemeConfig {
* 更新主题
*/
fun applyTheme(context: Context) = with(context) {
when {
AppConfig.isEInkMode -> {
ThemeStore.editTheme(this)
.primaryColor(Color.WHITE)
.accentColor(Color.BLACK)
.backgroundColor(Color.WHITE)
.bottomBackground(Color.WHITE)
.apply()
}
AppConfig.isNightTheme -> {
val primary =
getPrefInt(PreferKey.cNPrimary, getCompatColor(R.color.md_blue_grey_600))
val accent =
getPrefInt(PreferKey.cNAccent, getCompatColor(R.color.md_deep_orange_800))
var background =
getPrefInt(PreferKey.cNBackground, getCompatColor(R.color.md_grey_900))
if (ColorUtils.isColorLight(background)) {
background = getCompatColor(R.color.md_grey_900)
putPrefInt(PreferKey.cNBackground, background)
}
val bBackground =
getPrefInt(PreferKey.cNBBackground, getCompatColor(R.color.md_grey_850))
ThemeStore.editTheme(this)
.primaryColor(ColorUtils.withAlpha(primary, 1f))
.accentColor(ColorUtils.withAlpha(accent, 1f))
.backgroundColor(ColorUtils.withAlpha(background, 1f))
.bottomBackground(ColorUtils.withAlpha(bBackground, 1f))
.apply()
}
else -> {
val primary =
getPrefInt(PreferKey.cPrimary, getCompatColor(R.color.md_brown_500))
val accent =
getPrefInt(PreferKey.cAccent, getCompatColor(R.color.md_red_600))
var background =
getPrefInt(PreferKey.cBackground, getCompatColor(R.color.md_grey_100))
if (!ColorUtils.isColorLight(background)) {
background = getCompatColor(R.color.md_grey_100)
putPrefInt(PreferKey.cBackground, background)
}
val bBackground =
getPrefInt(PreferKey.cBBackground, getCompatColor(R.color.md_grey_200))
ThemeStore.editTheme(this)
.primaryColor(ColorUtils.withAlpha(primary, 1f))
.accentColor(ColorUtils.withAlpha(accent, 1f))
.backgroundColor(ColorUtils.withAlpha(background, 1f))
.bottomBackground(ColorUtils.withAlpha(bBackground, 1f))
.apply()
}
}
}
@Keep
@@ -6,20 +6,22 @@ import android.widget.TextView
import androidx.preference.ListPreference
import androidx.preference.PreferenceViewHolder
import io.legato.kazusa.R
import androidx.core.content.withStyledAttributes
//import io.legado.app.lib.theme.bottomBackground
//import io.legado.app.lib.theme.getPrimaryTextColor
class NameListPreference(context: Context, attrs: AttributeSet) : ListPreference(context, attrs) {
private val isBottomBackground: Boolean
private var isBottomBackground: Boolean = false
init {
layoutResource = R.layout.view_preference
widgetLayoutResource = R.layout.item_fillet_text
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Preference)
isBottomBackground = typedArray.getBoolean(R.styleable.Preference_isBottomBackground, false)
typedArray.recycle()
context.withStyledAttributes(attrs, R.styleable.Preference) {
isBottomBackground = getBoolean(R.styleable.Preference_isBottomBackground, false)
}
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
@@ -107,7 +107,7 @@ open class Preference(context: Context, attrs: AttributeSet) :
}
final override fun onBindViewHolder(holder: PreferenceViewHolder) {
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
onBindView(holder)
onLongClick?.let { listener ->
@@ -6,32 +6,32 @@ import androidx.appcompat.widget.SwitchCompat
import androidx.preference.PreferenceViewHolder
import androidx.preference.SwitchPreferenceCompat
import io.legato.kazusa.R
import androidx.core.content.withStyledAttributes
//import io.legado.app.lib.theme.accentColor
class SwitchPreference(context: Context, attrs: AttributeSet) :
SwitchPreferenceCompat(context, attrs) {
private val isBottomBackground: Boolean
private var isBottomBackground: Boolean = false
private var onLongClick: ((preference: SwitchPreference) -> Boolean)? = null
init {
layoutResource = R.layout.view_preference
widgetLayoutResource = R.layout.preference_widget_material_switch
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Preference)
isBottomBackground = typedArray.getBoolean(R.styleable.Preference_isBottomBackground, false)
typedArray.recycle()
context.withStyledAttributes(attrs, R.styleable.Preference) {
isBottomBackground = getBoolean(R.styleable.Preference_isBottomBackground, false)
}
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
val v = Preference.bindView<SwitchCompat>(
Preference.bindView<SwitchCompat>(
context, holder, icon, title, summary,
widgetLayoutResource,
androidx.preference.R.id.switchWidget,
isBottomBackground = isBottomBackground
)
if (v is SwitchCompat && !v.isInEditMode) {
//v.applyTint(context.accentColor)
}
super.onBindViewHolder(holder)
onLongClick?.let { listener ->
holder.itemView.setOnLongClickListener {
@@ -52,6 +52,7 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
}
private fun initView() {
setSupportActionBar(binding.topBar)
binding.recyclerView.addItemDecoration(BookmarkDecoration(this,adapter))
binding.recyclerView.adapter = adapter
binding.recyclerView.applyNavigationBarPadding()
@@ -28,7 +28,7 @@ class ConfigActivity : VMBaseActivity<ActivityConfigBinding, ConfigViewModel>()
override fun setTitle(resId: Int) {
super.setTitle(resId)
binding.titleBar.setTitle(resId)
binding.topBar.setTitle(resId)
}
inline fun <reified T : Fragment> replaceFragment(configTag: String) {
@@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import io.legato.kazusa.R
import io.legato.kazusa.base.BaseBottomSheetDialogFragment
import io.legato.kazusa.base.BaseDialogFragment
import io.legato.kazusa.databinding.DialogCoverRuleConfigBinding
//import io.legado.app.lib.theme.primaryColor
@@ -19,13 +20,13 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import splitties.views.onClick
class CoverRuleConfigDialog : BaseDialogFragment(R.layout.dialog_cover_rule_config) {
class CoverRuleConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_cover_rule_config) {
val binding by viewBinding(DialogCoverRuleConfigBinding::bind)
override fun onStart() {
super.onStart()
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
@@ -7,6 +7,7 @@ import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import io.legato.kazusa.R
@@ -22,6 +23,7 @@ import io.legato.kazusa.lib.dialogs.alert
import io.legato.kazusa.lib.dialogs.selector
import io.legato.kazusa.lib.prefs.ColorPreference
import io.legato.kazusa.lib.prefs.NameListPreference
import io.legato.kazusa.lib.prefs.ThemeModePreference
//import io.legado.app.lib.theme.primaryColor
import io.legato.kazusa.ui.widget.number.NumberPickerDialog
import io.legato.kazusa.ui.widget.seekbar.SeekBarChangeListener
@@ -71,6 +73,7 @@ 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())
@@ -95,12 +98,20 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
}
}
}
findPreference<NameListPreference>(PreferKey.themeMode)?.let {
findPreference<ThemeModePreference>(PreferKey.themeMode)?.let {
it.setOnPreferenceChangeListener { _, _ ->
ThemeConfig.applyDayNight(requireContext())
true
}
}
findPreference<ListPreference>(PreferKey.themePref)?.let {
it.setOnPreferenceChangeListener { _, _ ->
val intent = requireActivity().intent
requireActivity().finish()
startActivity(intent)
true
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -126,6 +137,11 @@ 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)
}
PreferKey.cPrimary,
PreferKey.cAccent,
PreferKey.cBackground,
@@ -139,9 +155,11 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
PreferKey.cNBBackground -> {
upTheme(true)
}
PreferKey.themeMode -> {
ThemeConfig.applyDayNight(requireContext())
}
PreferKey.bgImage,
PreferKey.bgImageN -> {
upPreferenceSummary(key, getPrefString(key))
@@ -27,7 +27,7 @@ open class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
finish()
} else {
binding.root.postDelayed(600) { startMainActivity() }
binding.root.postDelayed(100) { startMainActivity() }
}
}