Using context extension for night mode checking

This commit is contained in:
Jay 2020-04-26 00:52:12 -04:00
parent 52892da065
commit 658a1a996b
3 changed files with 21 additions and 26 deletions

View file

@ -95,6 +95,7 @@ import eu.kanade.tachiyomi.util.storage.getUriCompat
import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.ThemeUtil
import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.system.getResourceColor 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.launchUI
import eu.kanade.tachiyomi.util.system.pxToDp import eu.kanade.tachiyomi.util.system.pxToDp
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
@ -397,17 +398,15 @@ class MangaDetailsController : BaseController,
coverDrawable = resource coverDrawable = resource
val bitmapCover = resource as? BitmapDrawable ?: return val bitmapCover = resource as? BitmapDrawable ?: return
Palette.from(bitmapCover.bitmap).generate { Palette.from(bitmapCover.bitmap).generate {
if (recycler == null) return@generate if (recycler == null || it == null) return@generate
val currentNightMode =
recycler.resources!!.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val colorBack = view.context.getResourceColor( val colorBack = view.context.getResourceColor(
android.R.attr.colorBackground android.R.attr.colorBackground
) )
val backDropColor = val backDropColor = if (!view.context.isInNightMode()) {
(if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) it?.getLightVibrantColor( it.getLightVibrantColor(colorBack)
colorBack } else {
) it.getDarkVibrantColor(colorBack)
else it?.getDarkVibrantColor(colorBack)) ?: colorBack }
coverColor = backDropColor coverColor = backDropColor
getHeader()?.setBackDrop(backDropColor) getHeader()?.setBackDrop(backDropColor)
if (toolbarIsColored) { if (toolbarIsColored) {
@ -424,10 +423,9 @@ class MangaDetailsController : BaseController,
/** Set toolbar theme for themes that are inverted (ie. light blue theme) */ /** Set toolbar theme for themes that are inverted (ie. light blue theme) */
private fun setActionBar(forThis: Boolean) { private fun setActionBar(forThis: Boolean) {
val currentNightMode = val activity = activity ?: return
activity!!.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
// if the theme is using inverted toolbar color // 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() presenter.preferences.theme()
) )
) { ) {
@ -442,15 +440,15 @@ class MangaDetailsController : BaseController,
if (forThis) android.R.attr.textColorPrimary if (forThis) android.R.attr.textColorPrimary
else R.attr.actionBarTintColor else R.attr.actionBarTintColor
) ?: Color.BLACK ) ?: Color.BLACK
(activity as MainActivity).toolbar.setTitleTextColor(iconPrimary) activity.toolbar.setTitleTextColor(iconPrimary)
(activity as MainActivity).drawerArrow?.color = iconPrimary activity.drawerArrow?.color = iconPrimary
(activity as MainActivity).toolbar.overflowIcon?.setTint(iconPrimary) activity.toolbar.overflowIcon?.setTint(iconPrimary)
if (forThis) activity!!.main_content.systemUiVisibility = if (forThis) activity.main_content.systemUiVisibility =
activity!!.main_content.systemUiVisibility.or( activity.main_content.systemUiVisibility.or(
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
) )
else activity!!.main_content.systemUiVisibility = else activity.main_content.systemUiVisibility =
activity!!.main_content.systemUiVisibility.rem( activity.main_content.systemUiVisibility.rem(
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
) )
} }

View file

@ -23,6 +23,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.WebViewClientCompat
import eu.kanade.tachiyomi.util.system.getResourceColor 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.openInBrowser
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.invisible 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.updatePadding
import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.util.view.visible
import kotlinx.android.synthetic.main.webview_activity.* import kotlinx.android.synthetic.main.webview_activity.*
import kotlinx.android.synthetic.main.webview_activity.swipe_refresh
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
class WebViewActivity : BaseActivity() { class WebViewActivity : BaseActivity() {
@ -120,8 +120,7 @@ class WebViewActivity : BaseActivity() {
} }
v.setPadding(insets.systemWindowInsetLeft, insets.systemWindowInsetTop, v.setPadding(insets.systemWindowInsetLeft, insets.systemWindowInsetTop,
insets.systemWindowInsetRight, 0) insets.systemWindowInsetRight, 0)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK if (Build.VERSION.SDK_INT >= 26 && !isInNightMode()) {
if (Build.VERSION.SDK_INT >= 26 && currentNightMode == Configuration.UI_MODE_NIGHT_NO) {
content.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR content.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} }
insets insets
@ -191,8 +190,7 @@ class WebViewActivity : BaseActivity() {
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK val lightMode = !isInNightMode()
val lightMode = currentNightMode == Configuration.UI_MODE_NIGHT_NO
window.statusBarColor = ColorUtils.setAlphaComponent(getResourceColor(R.attr window.statusBarColor = ColorUtils.setAlphaComponent(getResourceColor(R.attr
.colorSecondary), 255) .colorSecondary), 255)
toolbar.setBackgroundColor(getResourceColor(R.attr.colorSecondary)) toolbar.setBackgroundColor(getResourceColor(R.attr.colorSecondary))

View file

@ -5,7 +5,6 @@ package eu.kanade.tachiyomi.util.view
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
import android.graphics.Point import android.graphics.Point
import android.os.Build 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.ThemeUtil
import eu.kanade.tachiyomi.util.system.contextCompatColor import eu.kanade.tachiyomi.util.system.contextCompatColor
import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.isInNightMode
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -61,8 +61,7 @@ fun View.snack(
snack.f() snack.f()
} }
val theme = Injekt.get<PreferencesHelper>().theme() val theme = Injekt.get<PreferencesHelper>().theme()
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK if (ThemeUtil.isAMOLEDTheme(theme) && context.isInNightMode()) {
if (ThemeUtil.isAMOLEDTheme(theme) && currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
val textView: TextView = val textView: TextView =
snack.view.findViewById(com.google.android.material.R.id.snackbar_text) snack.view.findViewById(com.google.android.material.R.id.snackbar_text)
val button: Button? = val button: Button? =