From 5b1f4ad4a29da5754c1c06ae6049b90138c7778e Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 9 Jun 2019 09:45:37 +0800 Subject: [PATCH] reformat code --- .../app/ui/config/ThemeConfigFragment.kt | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) 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 8f9ded816..7477c69ae 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 @@ -4,6 +4,7 @@ import android.content.SharedPreferences import android.os.Bundle import android.os.Handler import androidx.appcompat.app.AlertDialog +import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.jeremyliao.liveeventbus.LiveEventBus import io.legado.app.App @@ -34,6 +35,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { sharedPreferences ?: return + activity ?: return when (key) { "colorPrimary", "colorAccent", "colorBackground" -> if (!ColorUtils.isColorLight( @@ -43,7 +45,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar ) ) ) { - AlertDialog.Builder(App.INSTANCE) + AlertDialog.Builder(activity!!) .setTitle("白天背景太暗") .setMessage("将会恢复默认背景?") .setPositiveButton(R.string.ok) { _, _ -> @@ -63,7 +65,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar ) ) ) { - AlertDialog.Builder(App.INSTANCE) + AlertDialog.Builder(activity!!) .setTitle("夜间背景太亮") .setMessage("将会恢复默认背景?") .setPositiveButton(R.string.ok) { _, _ -> @@ -82,6 +84,33 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar } + override fun onPreferenceTreeClick(preference: Preference?): Boolean { + when (preference?.key) { + "defaultTheme" -> { + AlertDialog.Builder(activity!!) + .setTitle("恢复默认主题") + .setMessage("是否确认恢复?") + .setPositiveButton(R.string.ok) { _, _ -> + preferenceManager.sharedPreferences.edit() + .putInt("colorPrimary", App.INSTANCE.getCompatColor(R.color.md_grey_100)) + .putInt("colorAccent", App.INSTANCE.getCompatColor(R.color.md_pink_600)) + .putInt("colorBackground", App.INSTANCE.getCompatColor(R.color.md_grey_100)) + .putInt("colorPrimaryNight", App.INSTANCE.getCompatColor(R.color.md_grey_800)) + .putInt("colorAccentNight", App.INSTANCE.getCompatColor(R.color.md_pink_800)) + .putInt("colorBackgroundNight", App.INSTANCE.getCompatColor(R.color.md_grey_800)) + .apply() + App.INSTANCE.upThemeStore() + LiveEventBus.get().with(Bus.recreate).post("") + Handler().postDelayed({ activity?.recreate() }, 100) + } + .setNegativeButton(R.string.cancel, null) + .show() + .upTint + } + } + return super.onPreferenceTreeClick(preference) + } + private fun upTheme(isNightTheme: Boolean) { if (App.INSTANCE.getPrefBoolean("isNightTheme", false) == isNightTheme) { App.INSTANCE.upThemeStore()