From 90c98f2012e917d6bbc7b3d4ad68676e893178b9 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 12:22:48 +0800 Subject: [PATCH] up --- app/src/main/java/io/legado/app/App.kt | 13 ++-- .../app/ui/config/ThemeConfigFragment.kt | 73 ++++++++++++++++++- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 868748389..b24432f92 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -13,6 +13,7 @@ import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.data.AppDatabase import io.legado.app.lib.theme.ThemeStore +import io.legado.app.utils.getCompatColor import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefInt import java.util.* @@ -63,15 +64,15 @@ class App : Application() { fun upThemeStore() { if (getPrefBoolean("isNightTheme", false)) { ThemeStore.editTheme(this) - .primaryColor(getPrefInt("colorPrimaryNight", resources.getColor(R.color.md_grey_800))) - .accentColor(getPrefInt("colorAccentNight", resources.getColor(R.color.md_pink_800))) - .backgroundColor(getPrefInt("colorBackgroundNight", resources.getColor(R.color.md_grey_800))) + .primaryColor(getPrefInt("colorPrimaryNight", getCompatColor(R.color.md_grey_800))) + .accentColor(getPrefInt("colorAccentNight", getCompatColor(R.color.md_pink_800))) + .backgroundColor(getPrefInt("colorBackgroundNight", getCompatColor(R.color.md_grey_800))) .apply() } else { ThemeStore.editTheme(this) - .primaryColor(getPrefInt("colorPrimary", resources.getColor(R.color.md_grey_100))) - .accentColor(getPrefInt("colorAccent", resources.getColor(R.color.md_pink_600))) - .backgroundColor(getPrefInt("colorBackground", resources.getColor(R.color.md_grey_100))) + .primaryColor(getPrefInt("colorPrimary", getCompatColor(R.color.md_grey_100))) + .accentColor(getPrefInt("colorAccent", getCompatColor(R.color.md_pink_600))) + .backgroundColor(getPrefInt("colorBackground", getCompatColor(R.color.md_grey_100))) .apply() } } diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 92e11f0a3..a6a2b451c 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -1,15 +1,86 @@ package io.legado.app.ui.config +import android.content.SharedPreferences import android.os.Bundle +import androidx.appcompat.app.AlertDialog import androidx.preference.PreferenceFragmentCompat +import io.legado.app.App import io.legado.app.R +import io.legado.app.lib.theme.ColorUtils +import io.legado.app.utils.getCompatColor +import io.legado.app.utils.getPrefBoolean +import io.legado.app.utils.putPrefInt +import io.legado.app.utils.upTint -class ThemeConfigFragment : PreferenceFragmentCompat() { +class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) } + override fun onResume() { + super.onResume() + preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) + } + + override fun onPause() { + preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this) + super.onPause() + } + + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { + sharedPreferences ?: return + when (key) { + "colorPrimary", "colorAccent", "colorBackground" -> if (!ColorUtils.isColorLight( + sharedPreferences.getInt( + "colorBackground", + App.INSTANCE.getCompatColor(R.color.md_grey_100) + ) + ) + ) { + AlertDialog.Builder(App.INSTANCE) + .setTitle("白天背景太暗") + .setMessage("将会恢复默认背景?") + .setPositiveButton(R.string.ok) { dialog, which -> + App.INSTANCE.putPrefInt("colorBackground", App.INSTANCE.getCompatColor(R.color.md_grey_100)) + upTheme(false) + } + .setNegativeButton(R.string.cancel) { dialogInterface, i -> upTheme(false) } + .show().upTint + } else { + upTheme(false) + } + "colorPrimaryNight", "colorAccentNight", "colorBackgroundNight" -> if (ColorUtils.isColorLight( + sharedPreferences.getInt( + "colorBackgroundNight", + App.INSTANCE.getCompatColor(R.color.md_grey_800) + ) + ) + ) { + AlertDialog.Builder(App.INSTANCE) + .setTitle("夜间背景太亮") + .setMessage("将会恢复默认背景?") + .setPositiveButton(R.string.ok) { dialog, which -> + App.INSTANCE.putPrefInt( + "colorBackgroundNight", + App.INSTANCE.getCompatColor(R.color.md_grey_800) + ) + upTheme(true) + } + .setNegativeButton(R.string.cancel) { dialogInterface, i -> upTheme(true) } + .show() + } else { + upTheme(true) + } + } + + } + + private fun upTheme(isNightTheme: Boolean) { + if (App.INSTANCE.getPrefBoolean("isNightTheme", false) == isNightTheme) { + App.INSTANCE.upThemeStore() + } + } } \ No newline at end of file