Skip to content

Commit

Permalink
Merge pull request #235 from hotwired/fix-window-theme-crash
Browse files Browse the repository at this point in the history
Fix a potential crash from the Window theme observer
  • Loading branch information
jayohms authored Jul 8, 2022
2 parents 1906079 + bf6838d commit ba7d645
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import dev.hotwire.turbo.nav.TurboNavDestination
import dev.hotwire.turbo.util.animateColorTo

internal class TurboWindowThemeObserver(val destination: TurboNavDestination) : LifecycleObserver {
private val window: Window
get() = destination.fragment.requireActivity().window
private val window: Window?
get() = destination.fragment.activity?.window

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun updateSystemBarColors() {
Expand All @@ -39,8 +39,8 @@ internal class TurboWindowThemeObserver(val destination: TurboNavDestination) :
val statusBarColor = colorAttribute(theme, android.R.attr.statusBarColor)
val useLightStatusBar = booleanAttribute(theme, android.R.attr.windowLightStatusBar)

window.statusBarColor.animateColorTo(statusBarColor) {
window.statusBarColor = it
window?.statusBarColor?.animateColorTo(statusBarColor) {
window?.statusBarColor = it
}

@Suppress("DEPRECATION")
Expand All @@ -51,8 +51,8 @@ internal class TurboWindowThemeObserver(val destination: TurboNavDestination) :
private fun updateNavigationBar(theme: Theme) {
val navigationBarColor = colorAttribute(theme, android.R.attr.navigationBarColor)

window.navigationBarColor.animateColorTo(navigationBarColor) {
window.navigationBarColor = it
window?.navigationBarColor?.animateColorTo(navigationBarColor) {
window?.navigationBarColor = it
}

// Light navigation bars are only available in API 27+
Expand All @@ -76,6 +76,7 @@ internal class TurboWindowThemeObserver(val destination: TurboNavDestination) :

@RequiresApi(Build.VERSION_CODES.R)
private fun updateSystemBarsAppearance(useLightSystemBars: Boolean) {
val window = window ?: return
val appearance = when (useLightSystemBars) {
true -> APPEARANCE_LIGHT_STATUS_BARS
else -> 0
Expand All @@ -94,6 +95,7 @@ internal class TurboWindowThemeObserver(val destination: TurboNavDestination) :

@Suppress("DEPRECATION")
private fun updateSystemUiVisibility(useLightSystemBar: Boolean, flag: Int) {
val window = window ?: return
val flags = when (useLightSystemBar) {
true -> window.decorView.systemUiVisibility or flag
else -> window.decorView.systemUiVisibility and flag.inv()
Expand Down

0 comments on commit ba7d645

Please sign in to comment.