diff --git a/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt b/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt index 00c35b8ed..d3b712009 100644 --- a/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt +++ b/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt @@ -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( @@ -74,7 +77,7 @@ abstract class BaseActivity( @SuppressLint("ObsoleteSdkInt") override fun onCreate(savedInstanceState: Bundle?) { - initTheme() + applyTheme() window.decorView.disableAutoFill() super.onCreate(savedInstanceState) @@ -132,7 +135,7 @@ abstract class BaseActivity( 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( 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( } } -// 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( } open fun observeLiveBus() { + observeEvent(EventBus.RECREATE) { + recreate() + } } override fun dispatchTouchEvent(ev: MotionEvent): Boolean { diff --git a/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt b/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt index 4ae16c9bf..7bbc5eedf 100644 --- a/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt +++ b/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt @@ -180,6 +180,5 @@ object PreferKey { const val readBarStyleFollowPage = "readBarStyleFollowPage" const val contentSelectSpeakMod = "contentReadAloudMod" - const val dynamicColors = "dynamicColors" const val themePref = "themePref" } diff --git a/app/src/main/java/io/legato/kazusa/help/config/AppConfig.kt b/app/src/main/java/io/legato/kazusa/help/config/AppConfig.kt index 260ec9c48..26e8fc5f9 100644 --- a/app/src/main/java/io/legato/kazusa/help/config/AppConfig.kt +++ b/app/src/main/java/io/legato/kazusa/help/config/AppConfig.kt @@ -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) } } diff --git a/app/src/main/java/io/legato/kazusa/help/config/ThemeConfig.kt b/app/src/main/java/io/legato/kazusa/help/config/ThemeConfig.kt index 08a0aabb8..f17930a22 100644 --- a/app/src/main/java/io/legato/kazusa/help/config/ThemeConfig.kt +++ b/app/src/main/java/io/legato/kazusa/help/config/ThemeConfig.kt @@ -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( diff --git a/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeCardPreference.kt b/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeCardPreference.kt index 5e867a066..10601c09b 100644 --- a/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeCardPreference.kt +++ b/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeCardPreference.kt @@ -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 = context.resources.getTextArray(R.array.themes_item) - private var entryValues: Array = context.resources.getTextArray(R.array.themes_value) + private var entryValues: Array = 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, "") } } } diff --git a/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeModePreference.kt b/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeModePreference.kt index f8cd36bc0..680cd5c81 100644 --- a/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeModePreference.kt +++ b/app/src/main/java/io/legato/kazusa/lib/prefs/ThemeModePreference.kt @@ -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() } + } \ No newline at end of file diff --git a/app/src/main/java/io/legato/kazusa/ui/config/ConfigActivity.kt b/app/src/main/java/io/legato/kazusa/ui/config/ConfigActivity.kt index 54833392e..273d56484 100644 --- a/app/src/main/java/io/legato/kazusa/ui/config/ConfigActivity.kt +++ b/app/src/main/java/io/legato/kazusa/ui/config/ConfigActivity.kt @@ -40,12 +40,4 @@ class ConfigActivity : VMBaseActivity() .replace(R.id.configFrameLayout, configFragment, configTag) .commit() } - - override fun observeLiveBus() { - super.observeLiveBus() - observeEvent(EventBus.RECREATE) { - recreate() - } - } - } \ No newline at end of file diff --git a/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt index b0ef18c76..7a4fa6547 100644 --- a/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt @@ -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(PreferKey.cBackground)?.let { it.onSaveColor = { color -> if (!ColorUtils.isColorLight(color)) { @@ -89,6 +90,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), } } } + findPreference(PreferKey.cNBackground)?.let { it.onSaveColor = { color -> if (ColorUtils.isColorLight(color)) { @@ -99,17 +101,15 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), } } } + findPreference(PreferKey.themeMode)?.let { it.setOnPreferenceChangeListener { _, _ -> - ThemeConfig.applyDayNight(requireContext()) true } } + findPreference(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() } } diff --git a/app/src/main/java/io/legato/kazusa/ui/main/MainActivity.kt b/app/src/main/java/io/legato/kazusa/ui/main/MainActivity.kt index 754c4041f..ff59e2e00 100644 --- a/app/src/main/java/io/legato/kazusa/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legato/kazusa/ui/main/MainActivity.kt @@ -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(), //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(), override fun onPageSelected(position: Int) { pagePosition = position - binding.bottomNavigationView.menu - .getItem(realPositions[position]).isChecked = true + binding.bottomNavigationView.menu[realPositions[position]].isChecked = true } - } @Suppress("DEPRECATION") diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 9607879fa..ff63cf76f 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -2,15 +2,15 @@ //**************************************************************Theme******************************************************************************// - - + + + + - - + + + +