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.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
)
}

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.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))

View File

@ -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<PreferencesHelper>().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? =