From c35b7be932a1a813fb399c494869891717e356e5 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 10:55:24 +0800 Subject: [PATCH 1/7] up --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 1 + .../lib/theme/prefs/ATEPreferenceCategory.kt | 15 ++-- .../lib/theme/prefs/ATESwitchPreference.kt | 37 ++++---- .../io/legado/app/ui/config/ConfigActivity.kt | 36 ++++++++ .../io/legado/app/ui/config/ConfigFragment.kt | 15 ++++ .../legado/app/ui/config/ConfigViewModel.kt | 14 +++ .../app/ui/config/ThemeConfigFragment.kt | 15 ++++ .../ui/main/bookshelf/BookshelfFragment.kt | 2 + .../ui/main/booksource/BookSourceFragment.kt | 2 + .../app/ui/main/findbook/FindBookFragment.kt | 2 + .../app/ui/main/myconfig/MyConfigFragment.kt | 37 +++----- .../io/legado/app/utils/MenuExtensions.kt | 30 +++++++ app/src/main/res/layout/activity_config.xml | 21 +++++ app/src/main/res/layout/activity_search.xml | 1 - .../main/res/layout/fragment_my_config.xml | 22 ++++- app/src/main/res/xml/pref_config.xml | 6 ++ app/src/main/res/xml/pref_config_theme.xml | 85 +++++++++++++++++++ 18 files changed, 291 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt create mode 100644 app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt create mode 100644 app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt create mode 100644 app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt create mode 100644 app/src/main/res/layout/activity_config.xml create mode 100644 app/src/main/res/xml/pref_config.xml create mode 100644 app/src/main/res/xml/pref_config_theme.xml diff --git a/app/build.gradle b/app/build.gradle index 66c464856..3a76edf08 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -79,8 +79,9 @@ dependencies { //androidX implementation 'androidx.core:core-ktx:1.2.0-alpha01' implementation 'androidx.appcompat:appcompat:1.1.0-alpha05' + implementation 'androidx.preference:preference:1.1.0-alpha05' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1' - implementation 'com.google.android.material:material:1.1.0-alpha06' + implementation 'com.google.android.material:material:1.1.0-alpha07' //lifecycle def lifecycle_version = '2.0.0' @@ -119,4 +120,7 @@ dependencies { //Glide implementation 'com.github.bumptech.glide:glide:4.9.0' + + //颜色选择 + implementation 'com.jaredrummler:colorpicker:1.1.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cdaf66439..4663ae81d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,6 +41,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/lib/theme/prefs/ATEPreferenceCategory.kt b/app/src/main/java/io/legado/app/lib/theme/prefs/ATEPreferenceCategory.kt index 2029744ed..c2d39f3a7 100644 --- a/app/src/main/java/io/legado/app/lib/theme/prefs/ATEPreferenceCategory.kt +++ b/app/src/main/java/io/legado/app/lib/theme/prefs/ATEPreferenceCategory.kt @@ -2,11 +2,11 @@ package io.legado.app.lib.theme.prefs import android.content.Context import android.os.Build -import android.preference.PreferenceCategory import android.util.AttributeSet -import android.view.View import android.widget.TextView import androidx.annotation.RequiresApi +import androidx.preference.PreferenceCategory +import androidx.preference.PreferenceViewHolder import io.legado.app.lib.theme.ThemeStore @@ -26,10 +26,13 @@ class ATEPreferenceCategory : PreferenceCategory { constructor(context: Context) : super(context) - override fun onBindView(view: View) { - super.onBindView(view) - if (view is TextView) { - view.setTextColor(ThemeStore.accentColor(view.getContext()))//设置title文本的颜色 + override fun onBindViewHolder(holder: PreferenceViewHolder?) { + super.onBindViewHolder(holder) + holder?.let { + val view = it.itemView + if (view is TextView) { + view.setTextColor(ThemeStore.accentColor(view.getContext()))//设置title文本的颜色 + } } } diff --git a/app/src/main/java/io/legado/app/lib/theme/prefs/ATESwitchPreference.kt b/app/src/main/java/io/legado/app/lib/theme/prefs/ATESwitchPreference.kt index 8d3ca5f56..7c4b54ec8 100644 --- a/app/src/main/java/io/legado/app/lib/theme/prefs/ATESwitchPreference.kt +++ b/app/src/main/java/io/legado/app/lib/theme/prefs/ATESwitchPreference.kt @@ -2,17 +2,17 @@ package io.legado.app.lib.theme.prefs import android.content.Context import android.os.Build -import android.preference.SwitchPreference import android.util.AttributeSet -import android.view.View import android.view.ViewGroup import android.widget.Switch import androidx.annotation.RequiresApi +import androidx.preference.PreferenceViewHolder +import androidx.preference.SwitchPreferenceCompat import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import java.util.* -class ATESwitchPreference : SwitchPreference { +class ATESwitchPreference : SwitchPreferenceCompat { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super( @@ -28,25 +28,26 @@ class ATESwitchPreference : SwitchPreference { constructor(context: Context) : super(context) - override fun onBindView(view: View) { - super.onBindView(view) - - if (view is ViewGroup) { - val queue = LinkedList() - queue.add(view) - while (!queue.isEmpty()) { - val current = queue.removeFirst() - for (i in 0 until current.childCount) { - if (current.getChildAt(i) is Switch) { - ATH.setTint(current.getChildAt(i), ThemeStore.accentColor(view.getContext())) - return - } else if (current.getChildAt(i) is ViewGroup) { - queue.addLast(current.getChildAt(i) as ViewGroup) + override fun onBindViewHolder(holder: PreferenceViewHolder?) { + super.onBindViewHolder(holder) + holder?.let { + val view = it.itemView + if (view is ViewGroup) { + val queue = LinkedList() + queue.add(view) + while (!queue.isEmpty()) { + val current = queue.removeFirst() + for (i in 0 until current.childCount) { + if (current.getChildAt(i) is Switch) { + ATH.setTint(current.getChildAt(i), ThemeStore.accentColor(view.getContext())) + return + } else if (current.getChildAt(i) is ViewGroup) { + queue.addLast(current.getChildAt(i) as ViewGroup) + } } } } } - } } diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt new file mode 100644 index 000000000..d4fc7734e --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt @@ -0,0 +1,36 @@ +package io.legado.app.ui.config + +import android.os.Bundle +import io.legado.app.R +import io.legado.app.base.BaseActivity +import io.legado.app.utils.getViewModel +import kotlinx.android.synthetic.main.activity_config.* +import kotlinx.android.synthetic.main.view_titlebar.* + +class ConfigActivity : BaseActivity() { + override val viewModel: ConfigViewModel + get() = getViewModel(ConfigViewModel::class.java) + override val layoutID: Int + get() = R.layout.activity_config + + override fun onViewModelCreated(viewModel: ConfigViewModel, savedInstanceState: Bundle?) { + intent.getIntExtra("configType", -1).let { + if (it != -1) viewModel.configType = it + } + this.setSupportActionBar(toolbar) + + when (viewModel.configType) { + ConfigViewModel.TYPE_CONFIG -> { + title_bar.title = "设置" + supportFragmentManager.beginTransaction().replace(R.id.configFrameLayout, ConfigFragment()).commit() + } + ConfigViewModel.TYPE_THEME_CONFIG -> { + title_bar.title = "主题设置" + supportFragmentManager.beginTransaction().replace(R.id.configFrameLayout, ThemeConfigFragment()) + .commit() + } + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt new file mode 100644 index 000000000..42cecb541 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt @@ -0,0 +1,15 @@ +package io.legado.app.ui.config + +import android.os.Bundle +import androidx.preference.PreferenceFragmentCompat +import io.legado.app.R + + +class ConfigFragment : PreferenceFragmentCompat() { + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + addPreferencesFromResource(R.xml.pref_config) + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt b/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt new file mode 100644 index 000000000..b9a085099 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt @@ -0,0 +1,14 @@ +package io.legado.app.ui.config + +import android.app.Application +import androidx.lifecycle.AndroidViewModel + +class ConfigViewModel(application: Application) : AndroidViewModel(application) { + companion object { + val TYPE_CONFIG = 0 + val TYPE_THEME_CONFIG = 1 + } + + var configType: Int = TYPE_CONFIG + +} \ No newline at end of file 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 new file mode 100644 index 000000000..92e11f0a3 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -0,0 +1,15 @@ +package io.legado.app.ui.config + +import android.os.Bundle +import androidx.preference.PreferenceFragmentCompat +import io.legado.app.R + + +class ThemeConfigFragment : PreferenceFragmentCompat() { + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + addPreferencesFromResource(R.xml.pref_config_theme) + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 7d3d21755..1fe8278d5 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -7,6 +7,7 @@ import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment import io.legado.app.R +import io.legado.app.utils.initIconColor import kotlinx.android.synthetic.main.view_titlebar.* class BookshelfFragment : Fragment(R.layout.fragment_bookshelf), Toolbar.OnMenuItemClickListener { @@ -15,6 +16,7 @@ class BookshelfFragment : Fragment(R.layout.fragment_bookshelf), Toolbar.OnMenuI super.onViewCreated(view, savedInstanceState) Log.e("TAG", "BookshelfFragment") toolbar.inflateMenu(R.menu.bookshelf) + toolbar.menu.initIconColor toolbar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index 7e00b19c9..d3855decb 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -7,6 +7,7 @@ import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment import io.legado.app.R +import io.legado.app.utils.initIconColor import kotlinx.android.synthetic.main.view_titlebar.* class BookSourceFragment : Fragment(R.layout.fragment_book_source), Toolbar.OnMenuItemClickListener { @@ -15,6 +16,7 @@ class BookSourceFragment : Fragment(R.layout.fragment_book_source), Toolbar.OnMe super.onViewCreated(view, savedInstanceState) Log.e("TAG", "BookSourceFragment") toolbar.inflateMenu(R.menu.book_source) + toolbar.menu.initIconColor toolbar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt index 3650f62f9..067da047f 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt @@ -7,6 +7,7 @@ import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment import io.legado.app.R +import io.legado.app.utils.initIconColor import kotlinx.android.synthetic.main.view_titlebar.* class FindBookFragment : Fragment(R.layout.fragment_find_book), Toolbar.OnMenuItemClickListener { @@ -15,6 +16,7 @@ class FindBookFragment : Fragment(R.layout.fragment_find_book), Toolbar.OnMenuIt super.onViewCreated(view, savedInstanceState) Log.e("TAG", "FindBookFragment") toolbar.inflateMenu(R.menu.find_book) + toolbar.menu.initIconColor toolbar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt index 2d431297d..620ee12f8 100644 --- a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt @@ -1,20 +1,17 @@ package io.legado.app.ui.main.myconfig +import android.content.Intent import android.os.Bundle import android.util.Log import android.view.MenuItem import android.view.View -import androidx.appcompat.view.menu.MenuBuilder -import androidx.appcompat.view.menu.MenuItemImpl import androidx.appcompat.widget.Toolbar -import androidx.core.view.forEach import androidx.fragment.app.Fragment import io.legado.app.R -import io.legado.app.lib.theme.ColorUtils -import io.legado.app.lib.theme.DrawableUtils -import io.legado.app.lib.theme.ThemeStore -import io.legado.app.lib.theme.getPrimaryTextColor -import io.legado.app.utils.getCompatColor +import io.legado.app.ui.config.ConfigActivity +import io.legado.app.ui.config.ConfigViewModel +import io.legado.app.utils.initIconColor +import kotlinx.android.synthetic.main.fragment_my_config.* import kotlinx.android.synthetic.main.view_titlebar.* class MyConfigFragment : Fragment(R.layout.fragment_my_config), Toolbar.OnMenuItemClickListener { @@ -23,25 +20,13 @@ class MyConfigFragment : Fragment(R.layout.fragment_my_config), Toolbar.OnMenuIt super.onViewCreated(view, savedInstanceState) Log.e("TAG", "MyConfigFragment") toolbar.inflateMenu(R.menu.my_config) - toolbar.menu?.let { - if (it is MenuBuilder) { - it.setOptionalIconsVisible(true) - } - context?.let { context -> - val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(context))) - val defaultTextColor = getCompatColor(R.color.tv_text_default) - it.forEach { item -> - (item as MenuItemImpl).let { impl -> - //overflow:展开的item - DrawableUtils.setTint( - impl.icon, - if (impl.requiresOverflow()) defaultTextColor else primaryTextColor - ) - } - } - } - } + toolbar.menu.initIconColor toolbar.setOnMenuItemClickListener(this) + tv_theme_config.setOnClickListener(View.OnClickListener { + val intent = Intent(context, ConfigActivity::class.java) + intent.putExtra("configType", ConfigViewModel.TYPE_THEME_CONFIG) + startActivity(intent) + }) } override fun onMenuItemClick(item: MenuItem?): Boolean { diff --git a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt index 656da42d1..f7b1e99d9 100644 --- a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt @@ -1,2 +1,32 @@ package io.legado.app.utils +import android.view.Menu +import androidx.appcompat.view.menu.MenuBuilder +import androidx.appcompat.view.menu.MenuItemImpl +import androidx.core.view.forEach +import io.legado.app.App +import io.legado.app.R +import io.legado.app.lib.theme.ColorUtils +import io.legado.app.lib.theme.DrawableUtils +import io.legado.app.lib.theme.ThemeStore +import io.legado.app.lib.theme.getPrimaryTextColor + +val Menu.initIconColor: Menu + get() = this.let { menu -> + if (menu is MenuBuilder) { + menu.setOptionalIconsVisible(true) + } + val primaryTextColor = + App.INSTANCE.getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(App.INSTANCE))) + val defaultTextColor = App.INSTANCE.getCompatColor(R.color.tv_text_default) + menu.forEach { item -> + (item as MenuItemImpl).let { impl -> + //overflow:展开的item + DrawableUtils.setTint( + impl.icon, + if (impl.requiresOverflow()) defaultTextColor else primaryTextColor + ) + } + } + return menu + } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_config.xml b/app/src/main/res/layout/activity_config.xml new file mode 100644 index 000000000..70d5ad0f7 --- /dev/null +++ b/app/src/main/res/layout/activity_config.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index c09d5b9e7..33b3b9ddb 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -10,7 +10,6 @@ android:id="@+id/title_bar" android:layout_width="match_parent" android:layout_height="wrap_content" - android:fitsSystemWindows="true" app:layout_constraintTop_toTopOf="parent" app:title="搜索"/> diff --git a/app/src/main/res/layout/fragment_my_config.xml b/app/src/main/res/layout/fragment_my_config.xml index 9fe7b70f7..5f568b619 100644 --- a/app/src/main/res/layout/fragment_my_config.xml +++ b/app/src/main/res/layout/fragment_my_config.xml @@ -1,8 +1,9 @@ - + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config.xml b/app/src/main/res/xml/pref_config.xml new file mode 100644 index 000000000..89679212b --- /dev/null +++ b/app/src/main/res/xml/pref_config.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml new file mode 100644 index 000000000..15b3af587 --- /dev/null +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From ac3757429d7c6921771ee1870797f0dc5a9fe00a Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 11:20:49 +0800 Subject: [PATCH 2/7] up --- app/src/main/res/xml/pref_config.xml | 4 +- app/src/main/res/xml/pref_config_theme.xml | 48 ++++++++++++++-------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app/src/main/res/xml/pref_config.xml b/app/src/main/res/xml/pref_config.xml index 89679212b..5d498d953 100644 --- a/app/src/main/res/xml/pref_config.xml +++ b/app/src/main/res/xml/pref_config.xml @@ -1,6 +1,6 @@ - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index 15b3af587..a6ce80b0e 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -1,85 +1,101 @@ - + + android:title="@string/immersion_status_bar" + app:iconSpaceReserved="false"/> + android:title="@string/navigation_bar_color_change" + app:iconSpaceReserved="false"/> + android:title="@string/behavior_main_t" + app:iconSpaceReserved="false"/> + android:title="@string/e_ink_mode" + app:iconSpaceReserved="false"/> + android:title="@string/default_theme" + app:iconSpaceReserved="false"/> - + + app:cpv_dialogType="preset" + app:iconSpaceReserved="false"/> + app:cpv_dialogType="preset" + app:iconSpaceReserved="false"/> + app:cpv_dialogType="preset" + app:iconSpaceReserved="false"/> - + + app:cpv_dialogType="preset" + app:iconSpaceReserved="false"/> + app:cpv_dialogType="preset" + app:iconSpaceReserved="false"/> + app:cpv_dialogType="preset" + app:iconSpaceReserved="false"/> - \ No newline at end of file + \ No newline at end of file From 90c98f2012e917d6bbc7b3d4ad68676e893178b9 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 12:22:48 +0800 Subject: [PATCH 3/7] 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 From d64a7cafb84229445a0bf770e99e7bb56af48c6d Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 12:40:35 +0800 Subject: [PATCH 4/7] up --- .../java/io/legado/app/base/BaseActivity.kt | 22 ++---------- .../ui/main/bookshelf/BookshelfFragment.kt | 5 +-- .../ui/main/booksource/BookSourceFragment.kt | 5 +-- .../app/ui/main/findbook/FindBookFragment.kt | 5 +-- .../app/ui/main/myconfig/MyConfigFragment.kt | 5 +-- .../io/legado/app/utils/MenuExtensions.kt | 36 +++++++++---------- 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 87ec5121e..364327656 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -7,16 +7,11 @@ import android.view.MenuItem import android.view.View import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.view.menu.MenuBuilder -import androidx.appcompat.view.menu.MenuItemImpl -import androidx.core.view.forEach import androidx.lifecycle.ViewModel import io.legado.app.R import io.legado.app.lib.theme.ColorUtils -import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.ThemeStore -import io.legado.app.lib.theme.getPrimaryTextColor -import io.legado.app.utils.getCompatColor +import io.legado.app.utils.setIconColor abstract class BaseActivity : AppCompatActivity() { @@ -37,21 +32,8 @@ abstract class BaseActivity : AppCompatActivity() { final override fun onCreateOptionsMenu(menu: Menu?): Boolean { return menu?.let { - if (it is MenuBuilder) { - it.setOptionalIconsVisible(true) - } val bool = onCompatCreateOptionsMenu(it) - val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(this))) - val defaultTextColor = getCompatColor(R.color.tv_text_default) - menu.forEach { item -> - (item as MenuItemImpl).let { impl -> - //overflow:展开的item - DrawableUtils.setTint( - impl.icon, - if (impl.requiresOverflow()) defaultTextColor else primaryTextColor - ) - } - } + it.setIconColor(this) bool } ?: super.onCreateOptionsMenu(menu) } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 1fe8278d5..fa5963220 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -6,8 +6,9 @@ import android.view.MenuItem import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment +import io.legado.app.App import io.legado.app.R -import io.legado.app.utils.initIconColor +import io.legado.app.utils.setIconColor import kotlinx.android.synthetic.main.view_titlebar.* class BookshelfFragment : Fragment(R.layout.fragment_bookshelf), Toolbar.OnMenuItemClickListener { @@ -16,7 +17,7 @@ class BookshelfFragment : Fragment(R.layout.fragment_bookshelf), Toolbar.OnMenuI super.onViewCreated(view, savedInstanceState) Log.e("TAG", "BookshelfFragment") toolbar.inflateMenu(R.menu.bookshelf) - toolbar.menu.initIconColor + toolbar.menu.setIconColor(App.INSTANCE) toolbar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index d3855decb..e20872cd6 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -6,8 +6,9 @@ import android.view.MenuItem import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment +import io.legado.app.App import io.legado.app.R -import io.legado.app.utils.initIconColor +import io.legado.app.utils.setIconColor import kotlinx.android.synthetic.main.view_titlebar.* class BookSourceFragment : Fragment(R.layout.fragment_book_source), Toolbar.OnMenuItemClickListener { @@ -16,7 +17,7 @@ class BookSourceFragment : Fragment(R.layout.fragment_book_source), Toolbar.OnMe super.onViewCreated(view, savedInstanceState) Log.e("TAG", "BookSourceFragment") toolbar.inflateMenu(R.menu.book_source) - toolbar.menu.initIconColor + toolbar.menu.setIconColor(App.INSTANCE) toolbar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt index 067da047f..8a9da36b5 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt @@ -6,8 +6,9 @@ import android.view.MenuItem import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment +import io.legado.app.App import io.legado.app.R -import io.legado.app.utils.initIconColor +import io.legado.app.utils.setIconColor import kotlinx.android.synthetic.main.view_titlebar.* class FindBookFragment : Fragment(R.layout.fragment_find_book), Toolbar.OnMenuItemClickListener { @@ -16,7 +17,7 @@ class FindBookFragment : Fragment(R.layout.fragment_find_book), Toolbar.OnMenuIt super.onViewCreated(view, savedInstanceState) Log.e("TAG", "FindBookFragment") toolbar.inflateMenu(R.menu.find_book) - toolbar.menu.initIconColor + toolbar.menu.setIconColor(App.INSTANCE) toolbar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt index 620ee12f8..042107d62 100644 --- a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt @@ -7,10 +7,11 @@ import android.view.MenuItem import android.view.View import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment +import io.legado.app.App import io.legado.app.R import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigViewModel -import io.legado.app.utils.initIconColor +import io.legado.app.utils.setIconColor import kotlinx.android.synthetic.main.fragment_my_config.* import kotlinx.android.synthetic.main.view_titlebar.* @@ -20,7 +21,7 @@ class MyConfigFragment : Fragment(R.layout.fragment_my_config), Toolbar.OnMenuIt super.onViewCreated(view, savedInstanceState) Log.e("TAG", "MyConfigFragment") toolbar.inflateMenu(R.menu.my_config) - toolbar.menu.initIconColor + toolbar.menu.setIconColor(App.INSTANCE) toolbar.setOnMenuItemClickListener(this) tv_theme_config.setOnClickListener(View.OnClickListener { val intent = Intent(context, ConfigActivity::class.java) diff --git a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt index f7b1e99d9..cb5f85d20 100644 --- a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt @@ -1,32 +1,30 @@ package io.legado.app.utils +import android.content.Context import android.view.Menu import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl import androidx.core.view.forEach -import io.legado.app.App import io.legado.app.R import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.getPrimaryTextColor -val Menu.initIconColor: Menu - get() = this.let { menu -> - if (menu is MenuBuilder) { - menu.setOptionalIconsVisible(true) +fun Menu.setIconColor(context: Context): Menu = this.let { menu -> + if (menu is MenuBuilder) { + menu.setOptionalIconsVisible(true) + } + val primaryTextColor = context.getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(context))) + val defaultTextColor = context.getCompatColor(R.color.tv_text_default) + menu.forEach { item -> + (item as MenuItemImpl).let { impl -> + //overflow:展开的item + DrawableUtils.setTint( + impl.icon, + if (impl.requiresOverflow()) defaultTextColor else primaryTextColor + ) } - val primaryTextColor = - App.INSTANCE.getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(App.INSTANCE))) - val defaultTextColor = App.INSTANCE.getCompatColor(R.color.tv_text_default) - menu.forEach { item -> - (item as MenuItemImpl).let { impl -> - //overflow:展开的item - DrawableUtils.setTint( - impl.icon, - if (impl.requiresOverflow()) defaultTextColor else primaryTextColor - ) - } - } - return menu - } \ No newline at end of file + } + return menu +} \ No newline at end of file From 8ec1a848c8d47d5a0ae32b3ada3ff7819f1209b2 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 12:41:32 +0800 Subject: [PATCH 5/7] up --- .../java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt index 042107d62..b7d27e01c 100644 --- a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt @@ -23,11 +23,11 @@ class MyConfigFragment : Fragment(R.layout.fragment_my_config), Toolbar.OnMenuIt toolbar.inflateMenu(R.menu.my_config) toolbar.menu.setIconColor(App.INSTANCE) toolbar.setOnMenuItemClickListener(this) - tv_theme_config.setOnClickListener(View.OnClickListener { + tv_theme_config.setOnClickListener { val intent = Intent(context, ConfigActivity::class.java) intent.putExtra("configType", ConfigViewModel.TYPE_THEME_CONFIG) startActivity(intent) - }) + } } override fun onMenuItemClick(item: MenuItem?): Boolean { From 1b860a405734cb4c512a9dfbfa2a3f8fdb12a569 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 14:10:52 +0800 Subject: [PATCH 6/7] up --- app/src/main/java/io/legado/app/ui/widget/TitleBar.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt index 01597a940..b3af419e3 100644 --- a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -14,6 +14,7 @@ import androidx.appcompat.widget.Toolbar import androidx.core.graphics.drawable.DrawableCompat import com.google.android.material.appbar.AppBarLayout import io.legado.app.R +import io.legado.app.lib.theme.ThemeStore import io.legado.app.utils.getStatusBarHeight import org.jetbrains.anko.bottomPadding import org.jetbrains.anko.topPadding @@ -40,6 +41,7 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a init { inflate(context, R.layout.view_titlebar, this) + setBackgroundColor(ThemeStore.primaryColor(context)) toolbar = findViewById(R.id.toolbar) val a = context.obtainStyledAttributes( attrs, R.styleable.TitleBar, From 90acd6842810ddc1c010a1d39f93b718ec8124c4 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 5 Jun 2019 14:18:02 +0800 Subject: [PATCH 7/7] up --- app/src/main/java/io/legado/app/App.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index b24432f92..be31f9309 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -64,14 +64,14 @@ class App : Application() { fun upThemeStore() { if (getPrefBoolean("isNightTheme", false)) { ThemeStore.editTheme(this) - .primaryColor(getPrefInt("colorPrimaryNight", getCompatColor(R.color.md_grey_800))) - .accentColor(getPrefInt("colorAccentNight", getCompatColor(R.color.md_pink_800))) + .primaryColor(getPrefInt("colorPrimaryNight", getCompatColor(R.color.colorPrimary))) + .accentColor(getPrefInt("colorAccentNight", getCompatColor(R.color.colorAccent))) .backgroundColor(getPrefInt("colorBackgroundNight", getCompatColor(R.color.md_grey_800))) .apply() } else { ThemeStore.editTheme(this) - .primaryColor(getPrefInt("colorPrimary", getCompatColor(R.color.md_grey_100))) - .accentColor(getPrefInt("colorAccent", getCompatColor(R.color.md_pink_600))) + .primaryColor(getPrefInt("colorPrimary", getCompatColor(R.color.colorPrimary))) + .accentColor(getPrefInt("colorAccent", getCompatColor(R.color.colorAccent))) .backgroundColor(getPrefInt("colorBackground", getCompatColor(R.color.md_grey_100))) .apply() }