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 cff1863e7..2517cc616 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import io.legado.app.R +import io.legado.app.constant.Theme import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.primaryColor @@ -46,7 +47,7 @@ abstract class BaseActivity( final override fun onCreateOptionsMenu(menu: Menu?): Boolean { return menu?.let { val bool = onCompatCreateOptionsMenu(it) - it.applyTint(this) + it.applyTint(this, theme) bool } ?: super.onCreateOptionsMenu(menu) } @@ -98,6 +99,11 @@ abstract class BaseActivity( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE } ATH.setStatusBarColorAuto(this, fullScreen) + if (theme == Theme.Dark) { + ATH.setLightStatusBar(this, false) + } else if (theme == Theme.Light) { + ATH.setLightStatusBar(this, true) + } } open fun observeLiveBus() { @@ -107,8 +113,4 @@ abstract class BaseActivity( currentFocus?.hideSoftInput() super.finish() } - - enum class Theme { - Dark, Light, Auto - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/base/VMBaseActivity.kt b/app/src/main/java/io/legado/app/base/VMBaseActivity.kt index 65d8e123d..3643588af 100644 --- a/app/src/main/java/io/legado/app/base/VMBaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/VMBaseActivity.kt @@ -1,6 +1,7 @@ package io.legado.app.base import androidx.lifecycle.ViewModel +import io.legado.app.constant.Theme abstract class VMBaseActivity( layoutID: Int, diff --git a/app/src/main/java/io/legado/app/constant/Theme.kt b/app/src/main/java/io/legado/app/constant/Theme.kt new file mode 100644 index 000000000..40467a688 --- /dev/null +++ b/app/src/main/java/io/legado/app/constant/Theme.kt @@ -0,0 +1,16 @@ +package io.legado.app.constant + +import io.legado.app.App +import io.legado.app.utils.isNightTheme + +enum class Theme { + Dark, Light, Auto; + + companion object { + fun getTheme(): Theme { + return if (App.INSTANCE.isNightTheme) { + Dark + } else Light + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index a7f1aad4a..00a2ca315 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -14,6 +14,7 @@ import com.bumptech.glide.request.RequestOptions.bitmapTransform import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.BookType +import io.legado.app.constant.Theme import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookGroup diff --git a/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt index dc06bc77b..517f9dc9d 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/GroupSelectDialog.kt @@ -21,6 +21,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.constant.Theme import io.legado.app.data.entities.BookGroup import io.legado.app.help.ItemTouchCallback import io.legado.app.lib.dialogs.alert @@ -76,7 +77,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private fun initData() { tool_bar.title = getString(R.string.group_select) tool_bar.inflateMenu(R.menu.book_group_manage) - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) tool_bar.menu.findItem(R.id.menu_group_local).isVisible = false tool_bar.menu.findItem(R.id.menu_group_audio).isVisible = false diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt index 59f5e73af..068f6f56f 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt @@ -19,6 +19,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.constant.Theme import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.noButton @@ -60,7 +61,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private fun initData() { tool_bar.title = getString(R.string.group_manage) tool_bar.inflateMenu(R.menu.group_manage) - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) adapter = GroupAdapter(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext()) diff --git a/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt b/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt index 4459bd8cc..b2d72a021 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt @@ -14,6 +14,7 @@ import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R +import io.legado.app.constant.Theme import io.legado.app.lib.theme.ATH import io.legado.app.ui.filechooser.adapter.FileAdapter import io.legado.app.ui.filechooser.adapter.PathAdapter @@ -133,7 +134,7 @@ class FileChooserDialog : DialogFragment(), tool_bar.menu.add(menuTitle) } } - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt index 1f3423853..0d432e437 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt @@ -21,6 +21,7 @@ import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.AppConst +import io.legado.app.constant.Theme import io.legado.app.data.entities.BookGroup import io.legado.app.help.ItemTouchCallback import io.legado.app.lib.dialogs.alert @@ -65,7 +66,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private fun initData() { tool_bar.title = getString(R.string.group_manage) tool_bar.inflateMenu(R.menu.book_group_manage) - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) tool_bar.menu.findItem(R.id.menu_group_local) .isChecked = AppConst.bookGroupLocalShow diff --git a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt index 2b2a84835..2dffe8b8d 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt @@ -19,6 +19,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.constant.Theme import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.noButton @@ -60,7 +61,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private fun initData() { tool_bar.title = getString(R.string.group_manage) tool_bar.inflateMenu(R.menu.group_manage) - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) adapter = GroupAdapter(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext()) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt index 0f14456e8..5470b4c5f 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt @@ -11,6 +11,7 @@ import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment import androidx.lifecycle.Observer import io.legado.app.R +import io.legado.app.constant.Theme import io.legado.app.data.entities.ReplaceRule import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel @@ -53,7 +54,7 @@ class ReplaceEditDialog : DialogFragment(), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) tool_bar.inflateMenu(R.menu.replace_edit) - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) viewModel.replaceRuleData.observe(this, Observer { upReplaceView(it) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt index 27c5fd45f..32ce74791 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt @@ -19,6 +19,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.constant.Theme import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.noButton @@ -60,7 +61,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private fun initData() { tool_bar.title = getString(R.string.group_manage) tool_bar.inflateMenu(R.menu.group_manage) - tool_bar.menu.applyTint(requireContext(), false) + tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.setOnMenuItemClickListener(this) adapter = GroupAdapter(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext()) 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 b92fbea68..d8ecff3e1 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,36 @@ package io.legado.app.utils import android.content.Context -import android.graphics.PorterDuff import android.view.Menu import android.view.MenuItem import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl import androidx.core.view.forEach import io.legado.app.R +import io.legado.app.constant.Theme import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.primaryTextColor import java.lang.reflect.Method import java.util.* -fun Menu.applyTint(context: Context, inPrimary: Boolean = true): Menu = this.let { menu -> +fun Menu.applyTint(context: Context, theme: Theme = Theme.Auto): Menu = this.let { menu -> if (menu is MenuBuilder) { menu.setOptionalIconsVisible(true) } val primaryTextColor = context.primaryTextColor val defaultTextColor = context.getCompatColor(R.color.tv_text_default) + val tintColor = when (theme) { + Theme.Dark -> context.getCompatColor(R.color.md_white_1000) + Theme.Light -> context.getCompatColor(R.color.md_black_1000) + else -> primaryTextColor + } menu.forEach { item -> (item as MenuItemImpl).let { impl -> //overflow:展开的item DrawableUtils.setTint( impl.icon, - if (!inPrimary) defaultTextColor - else if (impl.requiresOverflow()) defaultTextColor - else primaryTextColor + if (impl.requiresOverflow()) defaultTextColor + else tintColor ) } } @@ -47,14 +51,10 @@ fun Menu.applyOpenTint(context: Context) { if (menuItems is ArrayList<*>) { for (menuItem in menuItems) { if (menuItem is MenuItem) { - val drawable = menuItem.icon - if (drawable != null) { - drawable.mutate() - drawable.setColorFilter( - defaultTextColor, - PorterDuff.Mode.SRC_ATOP - ) - } + DrawableUtils.setTint( + menuItem.icon, + defaultTextColor + ) } } }