@ -1,8 +1,12 @@
package io.legado.app.ui.config
package io.legado.app.ui.config
import android.annotation.SuppressLint
import android.content.SharedPreferences
import android.content.SharedPreferences
import android.os.Build
import android.os.Build
import android.os.Bundle
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.View
import androidx.preference.Preference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceFragmentCompat
@ -22,7 +26,8 @@ import io.legado.app.ui.widget.prefs.IconListPreference
import io.legado.app.utils.*
import io.legado.app.utils.*
class ThemeConfigFragment : PreferenceFragmentCompat ( ) , SharedPreferences . OnSharedPreferenceChangeListener {
class ThemeConfigFragment : PreferenceFragmentCompat ( ) ,
SharedPreferences . OnSharedPreferenceChangeListener {
val items = arrayListOf ( " 极简 " , " 曜夜 " , " 经典 " , " 黑白 " , " A屏黑 " )
val items = arrayListOf ( " 极简 " , " 曜夜 " , " 经典 " , " 黑白 " , " A屏黑 " )
@ -33,12 +38,13 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
preferenceScreen . removePreference ( it )
preferenceScreen . removePreference ( it )
}
}
}
}
upPreferenceSummary ( " barElevation " , AppConfig . elevation . toString ( ) )
upPreferenceSummary ( PreferKey . barElevation , AppConfig . elevation . toString ( ) )
}
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
super . onViewCreated ( view , savedInstanceState )
ATH . applyEdgeEffectColor ( listView )
ATH . applyEdgeEffectColor ( listView )
setHasOptionsMenu ( true )
}
}
override fun onResume ( ) {
override fun onResume ( ) {
@ -51,22 +57,37 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
super . onPause ( )
super . onPause ( )
}
}
override fun onCreateOptionsMenu ( menu : Menu , inflater : MenuInflater ) {
super . onCreateOptionsMenu ( menu , inflater )
inflater . inflate ( R . menu . theme _config , menu )
}
override fun onOptionsItemSelected ( item : MenuItem ) : Boolean {
when ( item . itemId ) {
R . id . menu _theme _mode -> {
AppConfig . isNightTheme = ! AppConfig . isNightTheme
App . INSTANCE . applyDayNight ( )
}
}
return super . onOptionsItemSelected ( item )
}
override fun onSharedPreferenceChanged ( sharedPreferences : SharedPreferences ? , key : String ? ) {
override fun onSharedPreferenceChanged ( sharedPreferences : SharedPreferences ? , key : String ? ) {
sharedPreferences ?: return
sharedPreferences ?: return
when ( key ) {
when ( key ) {
PreferKey . launcherIcon -> LauncherIconHelp . changeIcon ( getPrefString ( key ) )
PreferKey . launcherIcon -> LauncherIconHelp . changeIcon ( getPrefString ( key ) )
" transparentStatusBar " -> recreateActivities ( )
PreferKey . transparentStatusBar -> recreateActivities ( )
" colorPrimary " ,
PreferKey . cPrimary ,
" colorAccent " ,
PreferKey . cAccent ,
" colorBackground " ,
PreferKey . cBackground ,
" colorBottomBackground " -> {
PreferKey . cBBackground -> {
if ( backgroundIsDark ( sharedPreferences ) ) {
if ( backgroundIsDark ( sharedPreferences ) ) {
alert {
alert {
title = " 白天背景太暗 "
title = " 白天背景太暗 "
message = " 将会恢复默认背景? "
message = " 将会恢复默认背景? "
yesButton {
yesButton {
putPrefInt (
putPrefInt (
" colorBackground " ,
PreferKey . cBackground ,
getCompatColor ( R . color . md _grey _100 )
getCompatColor ( R . color . md _grey _100 )
)
)
upTheme ( false )
upTheme ( false )
@ -80,17 +101,17 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
upTheme ( false )
upTheme ( false )
}
}
}
}
" colorPrimaryNight " ,
PreferKey . cNPrimary ,
" colorAccentNight " ,
PreferKey . cNAccent ,
" colorBackgroundNight " ,
PreferKey . cNBackground ,
" colorBottomBackgroundNight " -> {
PreferKey . cNBBackground -> {
if ( backgroundIsLight ( sharedPreferences ) ) {
if ( backgroundIsLight ( sharedPreferences ) ) {
alert {
alert {
title = " 夜间背景太亮 "
title = " 夜间背景太亮 "
message = " 将会恢复默认背景? "
message = " 将会恢复默认背景? "
yesButton {
yesButton {
putPrefInt (
putPrefInt (
" colorBackgroundNight " ,
PreferKey . cNBackground ,
getCompatColor ( R . color . md _grey _800 )
getCompatColor ( R . color . md _grey _800 )
)
)
upTheme ( true )
upTheme ( true )
@ -108,45 +129,65 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
}
}
@SuppressLint ( " PrivateResource " )
override fun onPreferenceTreeClick ( preference : Preference ? ) : Boolean {
override fun onPreferenceTreeClick ( preference : Preference ? ) : Boolean {
when ( preference ?. key ) {
when ( preference ?. key ) {
" defaultTheme " -> alert ( title = " 切换默认主题 " ) {
" defaultTheme " -> changeTheme ( )
PreferKey . barElevation -> NumberPickerDialog ( requireContext ( ) )
. setTitle ( getString ( R . string . bar _elevation ) )
. setMaxValue ( 32 )
. setMinValue ( 0 )
. setValue ( AppConfig . elevation )
. setCustomButton ( ( R . string . btn _default _s ) ) {
AppConfig . elevation =
App . INSTANCE . resources . getDimension ( R . dimen . design _appbar _elevation ) . toInt ( )
recreateActivities ( )
}
. show {
AppConfig . elevation = it
recreateActivities ( )
}
}
return super . onPreferenceTreeClick ( preference )
}
private fun changeTheme ( ) {
alert ( title = " 切换默认主题 " ) {
items ( items ) { _ , which ->
items ( items ) { _ , which ->
when ( which ) {
when ( which ) {
0 -> {
0 -> {
putPrefInt ( " colorPrimary " , getCompatColor ( R . color . md _grey _100 ) )
putPrefInt ( PreferKey . cPrimary , getCompatColor ( R . color . md _grey _100 ) )
putPrefInt ( " colorAccent " , getCompatColor ( R . color . lightBlue _color ) )
putPrefInt ( PreferKey . cAccent , getCompatColor ( R . color . lightBlue _color ) )
putPrefInt ( " colorBackground " , getCompatColor ( R . color . md _grey _100 ) )
putPrefInt ( PreferKey . cBackground , getCompatColor ( R . color . md _grey _100 ) )
putPrefInt ( PreferKey . cBBackground , getCompatColor ( R . color . md _grey _200 ) )
AppConfig . isNightTheme = false
AppConfig . isNightTheme = false
}
}
1 -> {
1 -> {
putPrefInt ( " colorPrimaryNight " , getCompatColor ( R . color . shine _color ) )
putPrefInt ( PreferKey . cNPrimary , getCompatColor ( R . color . md _grey _900 ) )
putPrefInt ( " colorAccentNight " , getCompatColor ( R . color . lightBlue _color ) )
putPrefInt ( PreferKey . cNAccent , getCompatColor ( R . color . lightBlue _color ) )
putPrefInt ( " colorBackgroundNight " , getCompatColor ( R . color . shine _color ) )
putPrefInt ( PreferKey . cNBackground , getCompatColor ( R . color . md _grey _900 ) )
putPrefInt ( PreferKey . cNBBackground , getCompatColor ( R . color . md _grey _900 ) )
AppConfig . isNightTheme = true
AppConfig . isNightTheme = true
}
}
2 -> {
2 -> {
putPrefInt ( " colorPrimary " , getCompatColor ( R . color . md _light _blue _500 ) )
putPrefInt ( PreferKey . cPrimary , getCompatColor ( R . color . md _light _blue _500 ) )
putPrefInt ( " colorAccent " , getCompatColor ( R . color . md _pink _800 ) )
putPrefInt ( PreferKey . cAccent , getCompatColor ( R . color . md _pink _800 ) )
putPrefInt ( " colorBackground " , getCompatColor ( R . color . md _grey _100 ) )
putPrefInt ( PreferKey . cBackground , getCompatColor ( R . color . md _grey _100 ) )
putPrefInt ( PreferKey . cBBackground , getCompatColor ( R . color . md _grey _200 ) )
AppConfig . isNightTheme = false
AppConfig . isNightTheme = false
}
}
3 -> {
3 -> {
putPrefInt ( " colorPrimary " , getCompatColor ( R . color . white ) )
putPrefInt ( PreferKey . cPrimary , getCompatColor ( R . color . white ) )
putPrefInt ( " colorAccent " , getCompatColor ( R . color . black ) )
putPrefInt ( PreferKey . cAccent , getCompatColor ( R . color . black ) )
putPrefInt ( " colorBackground " , getCompatColor ( R . color . white ) )
putPrefInt ( PreferKey . cBackground , getCompatColor ( R . color . white ) )
putPrefInt ( PreferKey . cBBackground , getCompatColor ( R . color . white ) )
AppConfig . isNightTheme = false
AppConfig . isNightTheme = false
}
}
4 -> {
4 -> {
putPrefInt ( " colorPrimaryNight " , getCompatColor ( R . color . black ) )
putPrefInt ( PreferKey . cNPrimary , getCompatColor ( R . color . black ) )
putPrefInt (
putPrefInt ( PreferKey . cNAccent , getCompatColor ( R . color . md _grey _500 ) )
" colorAccentNight " ,
putPrefInt ( PreferKey . cNBackground , getCompatColor ( R . color . black ) )
getCompatColor ( R . color . md _grey _600 )
putPrefInt ( PreferKey . cNBBackground , getCompatColor ( R . color . black ) )
)
putPrefInt (
" colorBackgroundNight " ,
getCompatColor ( R . color . black )
)
AppConfig . isNightTheme = true
AppConfig . isNightTheme = true
}
}
}
}
@ -154,27 +195,12 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
recreateActivities ( )
recreateActivities ( )
}
}
} . show ( ) . applyTint ( )
} . show ( ) . applyTint ( )
" barElevation " -> NumberPickerDialog ( requireContext ( ) )
. setTitle ( getString ( R . string . bar _elevation ) )
. setMaxValue ( 32 )
. setMinValue ( 0 )
. setValue ( AppConfig . elevation )
. setCustomButton ( ( R . string . btn _default _s ) ) {
AppConfig . elevation = App . INSTANCE . resources . getDimension ( R . dimen . design _appbar _elevation ) . toInt ( )
recreateActivities ( )
}
. show {
AppConfig . elevation = it
recreateActivities ( )
}
}
return super . onPreferenceTreeClick ( preference )
}
}
private fun backgroundIsDark ( sharedPreferences : SharedPreferences ) : Boolean {
private fun backgroundIsDark ( sharedPreferences : SharedPreferences ) : Boolean {
return ! ColorUtils . isColorLight (
return ! ColorUtils . isColorLight (
sharedPreferences . getInt (
sharedPreferences . getInt (
" colorBackground " ,
PreferKey . cBackground ,
getCompatColor ( R . color . md _grey _100 )
getCompatColor ( R . color . md _grey _100 )
)
)
)
)
@ -183,7 +209,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
private fun backgroundIsLight ( sharedPreferences : SharedPreferences ) : Boolean {
private fun backgroundIsLight ( sharedPreferences : SharedPreferences ) : Boolean {
return ColorUtils . isColorLight (
return ColorUtils . isColorLight (
sharedPreferences . getInt (
sharedPreferences . getInt (
" colorBackgroundNight " ,
PreferKey . cNBackground ,
getCompatColor ( R . color . md _grey _800 )
getCompatColor ( R . color . md _grey _800 )
)
)
)
)
@ -205,7 +231,8 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
private fun upPreferenceSummary ( preferenceKey : String , value : String ? ) {
private fun upPreferenceSummary ( preferenceKey : String , value : String ? ) {
val preference = findPreference < Preference > ( preferenceKey ) ?: return
val preference = findPreference < Preference > ( preferenceKey ) ?: return
when ( preferenceKey ) {
when ( preferenceKey ) {
" barElevation " -> preference . summary = getString ( R . string . bar _elevation _s , value )
PreferKey . barElevation -> preference . summary =
getString ( R . string . bar _elevation _s , value )
}
}
}
}
}
}