From 658a1a996be083ad76849973cea0a9f3de198b9b Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 26 Apr 2020 00:52:12 -0400 Subject: [PATCH] Using context extension for night mode checking --- .../ui/manga/MangaDetailsController.kt | 34 +++++++++---------- .../tachiyomi/ui/webview/WebViewActivity.kt | 8 ++--- .../tachiyomi/util/view/ViewExtensions.kt | 5 ++- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt index a8f19f0f32..2f72489f2b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt @@ -95,6 +95,7 @@ import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.getResourceColor +import eu.kanade.tachiyomi.util.system.isInNightMode import eu.kanade.tachiyomi.util.system.launchUI import eu.kanade.tachiyomi.util.system.pxToDp import eu.kanade.tachiyomi.util.system.toast @@ -397,17 +398,15 @@ class MangaDetailsController : BaseController, coverDrawable = resource val bitmapCover = resource as? BitmapDrawable ?: return Palette.from(bitmapCover.bitmap).generate { - if (recycler == null) return@generate - val currentNightMode = - recycler.resources!!.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + if (recycler == null || it == null) return@generate val colorBack = view.context.getResourceColor( android.R.attr.colorBackground ) - val backDropColor = - (if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) it?.getLightVibrantColor( - colorBack - ) - else it?.getDarkVibrantColor(colorBack)) ?: colorBack + val backDropColor = if (!view.context.isInNightMode()) { + it.getLightVibrantColor(colorBack) + } else { + it.getDarkVibrantColor(colorBack) + } coverColor = backDropColor getHeader()?.setBackDrop(backDropColor) if (toolbarIsColored) { @@ -424,10 +423,9 @@ class MangaDetailsController : BaseController, /** Set toolbar theme for themes that are inverted (ie. light blue theme) */ private fun setActionBar(forThis: Boolean) { - val currentNightMode = - activity!!.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + val activity = activity ?: return // if the theme is using inverted toolbar color - if (currentNightMode == Configuration.UI_MODE_NIGHT_NO && ThemeUtil.isBlueTheme( + if (!activity.isInNightMode() && ThemeUtil.isBlueTheme( presenter.preferences.theme() ) ) { @@ -442,15 +440,15 @@ class MangaDetailsController : BaseController, if (forThis) android.R.attr.textColorPrimary else R.attr.actionBarTintColor ) ?: Color.BLACK - (activity as MainActivity).toolbar.setTitleTextColor(iconPrimary) - (activity as MainActivity).drawerArrow?.color = iconPrimary - (activity as MainActivity).toolbar.overflowIcon?.setTint(iconPrimary) - if (forThis) activity!!.main_content.systemUiVisibility = - activity!!.main_content.systemUiVisibility.or( + activity.toolbar.setTitleTextColor(iconPrimary) + activity.drawerArrow?.color = iconPrimary + activity.toolbar.overflowIcon?.setTint(iconPrimary) + if (forThis) activity.main_content.systemUiVisibility = + activity.main_content.systemUiVisibility.or( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ) - else activity!!.main_content.systemUiVisibility = - activity!!.main_content.systemUiVisibility.rem( + else activity.main_content.systemUiVisibility = + activity.main_content.systemUiVisibility.rem( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt index 91fd2df6a6..3fe7ed9cca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -23,6 +23,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.getResourceColor +import eu.kanade.tachiyomi.util.system.isInNightMode import eu.kanade.tachiyomi.util.system.openInBrowser import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.invisible @@ -32,7 +33,6 @@ import eu.kanade.tachiyomi.util.view.updateLayoutParams import eu.kanade.tachiyomi.util.view.updatePadding import eu.kanade.tachiyomi.util.view.visible import kotlinx.android.synthetic.main.webview_activity.* -import kotlinx.android.synthetic.main.webview_activity.swipe_refresh import uy.kohesive.injekt.injectLazy class WebViewActivity : BaseActivity() { @@ -120,8 +120,7 @@ class WebViewActivity : BaseActivity() { } v.setPadding(insets.systemWindowInsetLeft, insets.systemWindowInsetTop, insets.systemWindowInsetRight, 0) - val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - if (Build.VERSION.SDK_INT >= 26 && currentNightMode == Configuration.UI_MODE_NIGHT_NO) { + if (Build.VERSION.SDK_INT >= 26 && !isInNightMode()) { content.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR } insets @@ -191,8 +190,7 @@ class WebViewActivity : BaseActivity() { override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - val lightMode = currentNightMode == Configuration.UI_MODE_NIGHT_NO + val lightMode = !isInNightMode() window.statusBarColor = ColorUtils.setAlphaComponent(getResourceColor(R.attr .colorSecondary), 255) toolbar.setBackgroundColor(getResourceColor(R.attr.colorSecondary)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt index a47ba5a5a1..0a9f30b3a5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt @@ -5,7 +5,6 @@ package eu.kanade.tachiyomi.util.view import android.annotation.SuppressLint import android.app.Activity import android.content.res.ColorStateList -import android.content.res.Configuration import android.graphics.Color import android.graphics.Point import android.os.Build @@ -33,6 +32,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.contextCompatColor import eu.kanade.tachiyomi.util.system.getResourceColor +import eu.kanade.tachiyomi.util.system.isInNightMode import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -61,8 +61,7 @@ fun View.snack( snack.f() } val theme = Injekt.get().theme() - val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - if (ThemeUtil.isAMOLEDTheme(theme) && currentNightMode == Configuration.UI_MODE_NIGHT_YES) { + if (ThemeUtil.isAMOLEDTheme(theme) && context.isInNightMode()) { val textView: TextView = snack.view.findViewById(com.google.android.material.R.id.snackbar_text) val button: Button? =