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