Skip to content

Commit ba7d645

Browse files
authored
Merge pull request #235 from hotwired/fix-window-theme-crash
Fix a potential crash from the Window theme observer
2 parents 1906079 + bf6838d commit ba7d645

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

turbo/src/main/kotlin/dev/hotwire/turbo/observers/TurboWindowThemeObserver.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import dev.hotwire.turbo.nav.TurboNavDestination
1414
import dev.hotwire.turbo.util.animateColorTo
1515

1616
internal class TurboWindowThemeObserver(val destination: TurboNavDestination) : LifecycleObserver {
17-
private val window: Window
18-
get() = destination.fragment.requireActivity().window
17+
private val window: Window?
18+
get() = destination.fragment.activity?.window
1919

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

42-
window.statusBarColor.animateColorTo(statusBarColor) {
43-
window.statusBarColor = it
42+
window?.statusBarColor?.animateColorTo(statusBarColor) {
43+
window?.statusBarColor = it
4444
}
4545

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

54-
window.navigationBarColor.animateColorTo(navigationBarColor) {
55-
window.navigationBarColor = it
54+
window?.navigationBarColor?.animateColorTo(navigationBarColor) {
55+
window?.navigationBarColor = it
5656
}
5757

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

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

9596
@Suppress("DEPRECATION")
9697
private fun updateSystemUiVisibility(useLightSystemBar: Boolean, flag: Int) {
98+
val window = window ?: return
9799
val flags = when (useLightSystemBar) {
98100
true -> window.decorView.systemUiVisibility or flag
99101
else -> window.decorView.systemUiVisibility and flag.inv()

0 commit comments

Comments
 (0)