From 85f9f01cb80731747e6396a9d10fcb6d31616ae0 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 21 Jan 2025 12:20:48 -0500 Subject: [PATCH] Use fun setWindowBarColor to set either status or navBar colors --- .../util/extensions/WindowExtensions.kt | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/util/extensions/WindowExtensions.kt b/WordPress/src/main/java/org/wordpress/android/util/extensions/WindowExtensions.kt index 30c52f0d7e57..825b9493d6cd 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/extensions/WindowExtensions.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/extensions/WindowExtensions.kt @@ -12,44 +12,48 @@ fun Window.setEdgeToEdgeContentDisplay(isEnabled: Boolean) { WindowCompat.setDecorFitsSystemWindows(this, decorFitsSystemWindows) } -@Suppress("DEPRECATION") fun Window.setWindowStatusBarColor(color: Int) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { - // Android 15+ - decorView.setOnApplyWindowInsetsListener { view, insets -> - val statusBarInsets = insets.getInsets(WindowInsets.Type.statusBars()) - view.setBackgroundColor(color) - // Adjust padding to avoid overlap - view.setPadding(0, statusBarInsets.top, 0, 0) - insets - } - } else { - // Android 14- - statusBarColor = color - val windowInsetsController = WindowInsetsControllerCompat(this, decorView) - windowInsetsController.isAppearanceLightStatusBars = ColorUtils.isColorLight(statusBarColor) + setWindowBarColor(color, InsetsType.STATUS_BAR) +} - // we need to set the light navigation appearance here because, for some reason, changing the status bar also - // changes the navigation bar appearance but this method is supposed to only change the status bar - windowInsetsController.isAppearanceLightNavigationBars = ColorUtils.isColorLight(navigationBarColor) - } +fun Window.setWindowNavigationBarColor(color: Int) { + setWindowBarColor(color, InsetsType.NAVIGATION_BAR) } +/** + * Sets the status bar or navigation bar color + * TODO Setting both the status bar color and navigation bar color causes the insets to be set twice + */ @Suppress("DEPRECATION") -fun Window.setWindowNavigationBarColor(color: Int) { +private fun Window.setWindowBarColor(color: Int, insetsType: InsetsType) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { // Android 15+ decorView.setOnApplyWindowInsetsListener { view, insets -> view.setBackgroundColor(color) + val barInsets = insets.getInsets( + when (insetsType) { + InsetsType.STATUS_BAR -> WindowInsets.Type.statusBars() + InsetsType.NAVIGATION_BAR -> WindowInsets.Type.navigationBars() + } + ) // Adjust padding to avoid overlap - val navBarInsets = insets.getInsets(WindowInsets.Type.navigationBars()) - view.setPadding(0, navBarInsets.top, 0, 0) + view.setPadding(0, barInsets.top, 0, 0) insets } } else { - // Android 14- + when(insetsType) { + InsetsType.STATUS_BAR -> statusBarColor = color + InsetsType.NAVIGATION_BAR -> navigationBarColor = color + } val windowInsetsController = WindowInsetsControllerCompat(this, decorView) - navigationBarColor = color + if (insetsType == InsetsType.STATUS_BAR) { + windowInsetsController.isAppearanceLightStatusBars = ColorUtils.isColorLight(statusBarColor) + } windowInsetsController.isAppearanceLightNavigationBars = ColorUtils.isColorLight(navigationBarColor) } } + +private enum class InsetsType { + STATUS_BAR, + NAVIGATION_BAR +}