From 47eb5c30b64f6a20415e75cd9c4c8c1e24a91f9c Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Thu, 29 Feb 2024 09:49:58 +0100 Subject: [PATCH] rewrite SystemUiController to edgeToEdge --- .github/renovate.json5 | 3 +- catalog/build.gradle.kts | 2 +- catalog/src/main/AndroidManifest.xml | 1 + .../compose/catalog/CatalogApplication.kt | 19 +++------- .../orbit/compose/catalog/MainActivity.kt | 37 ++++++++++++++++--- catalog/src/main/res/values-night/themes.xml | 4 +- catalog/src/main/res/values/themes.xml | 2 +- gradle/libs.versions.toml | 2 +- 8 files changed, 43 insertions(+), 27 deletions(-) diff --git a/.github/renovate.json5 b/.github/renovate.json5 index d788e54bf..bc5ea62e3 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -32,10 +32,9 @@ ], }, { - "groupName": "Compose & Accompanist", + "groupName": "Compose", "matchPackageNames": [ "androidx.compose", - "com.google.accompanist", ], }, { diff --git a/catalog/build.gradle.kts b/catalog/build.gradle.kts index 586fed651..47d17b8e2 100644 --- a/catalog/build.gradle.kts +++ b/catalog/build.gradle.kts @@ -137,6 +137,7 @@ dependencies { implementation(projects.catalog.semantics) implementation(libs.androidx.core) + implementation(libs.androidx.appCompat) implementation(libs.androidx.activityCompose) implementation(libs.androidx.activityComposeCatalog) @@ -155,7 +156,6 @@ dependencies { implementation(libs.kotlin.stdlib) implementation(libs.coil) - implementation(libs.accompanist.systemController) implementation(libs.kiwi.navigationComposeTyped) baselineProfile(projects.baselineprofile) diff --git a/catalog/src/main/AndroidManifest.xml b/catalog/src/main/AndroidManifest.xml index f835e843d..f1ee3de7e 100644 --- a/catalog/src/main/AndroidManifest.xml +++ b/catalog/src/main/AndroidManifest.xml @@ -15,6 +15,7 @@ diff --git a/catalog/src/main/java/kiwi/orbit/compose/catalog/CatalogApplication.kt b/catalog/src/main/java/kiwi/orbit/compose/catalog/CatalogApplication.kt index 24ad4373b..c80ee8414 100644 --- a/catalog/src/main/java/kiwi/orbit/compose/catalog/CatalogApplication.kt +++ b/catalog/src/main/java/kiwi/orbit/compose/catalog/CatalogApplication.kt @@ -11,7 +11,6 @@ import androidx.compose.animation.slideInHorizontally import androidx.compose.animation.slideOutHorizontally import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.runtime.Composable -import androidx.compose.runtime.SideEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable @@ -19,7 +18,6 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.testTagsAsResourceId @@ -27,7 +25,6 @@ import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.dp import androidx.navigation.compose.NavHost import androidx.navigation.compose.rememberNavController -import com.google.accompanist.systemuicontroller.rememberSystemUiController import com.kiwi.navigationcompose.typed.composable import com.kiwi.navigationcompose.typed.createRoutePattern import com.kiwi.navigationcompose.typed.dialog @@ -77,22 +74,16 @@ import kiwi.orbit.compose.catalog.screens.topAppBarNavigation import kotlinx.serialization.ExperimentalSerializationApi @Composable -fun CatalogApplication() { - val systemUiController = rememberSystemUiController() - +fun CatalogApplication(activity: MainActivity) { var isLightThemeUser by rememberSaveable { mutableStateOf(null) } val isLightThemeFinal = isLightThemeUser ?: !isSystemInDarkTheme() - SideEffect { - systemUiController.setSystemBarsColor( - color = Color.Transparent, - darkIcons = isLightThemeFinal, - ) - } - AnimatedAppTheme( isLightTheme = isLightThemeFinal, - onThemeToggle = { isLightThemeUser = it }, + onThemeToggle = { isLight -> + isLightThemeUser = isLight + activity.setUiMode(isLight) + }, ) { onThemeToggle -> NavGraph(onThemeToggle = onThemeToggle) } diff --git a/catalog/src/main/java/kiwi/orbit/compose/catalog/MainActivity.kt b/catalog/src/main/java/kiwi/orbit/compose/catalog/MainActivity.kt index f6207c7c0..20e3c8e9c 100644 --- a/catalog/src/main/java/kiwi/orbit/compose/catalog/MainActivity.kt +++ b/catalog/src/main/java/kiwi/orbit/compose/catalog/MainActivity.kt @@ -1,18 +1,43 @@ package kiwi.orbit.compose.catalog +import android.app.UiModeManager +import android.graphics.Color +import android.os.Build import android.os.Bundle -import androidx.activity.ComponentActivity +import androidx.activity.SystemBarStyle import androidx.activity.compose.setContent -import androidx.core.view.WindowCompat +import androidx.activity.enableEdgeToEdge +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.app.AppCompatDelegate +import androidx.core.content.getSystemService -class MainActivity : ComponentActivity() { +class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - WindowCompat.setDecorFitsSystemWindows(window, false) - + enableEdgeToEdge() setContent { - CatalogApplication() + CatalogApplication(this) } } + + internal fun setUiMode(isLight: Boolean) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + val uiModeManager = getSystemService()!! + uiModeManager.setApplicationNightMode( + if (isLight) UiModeManager.MODE_NIGHT_NO else UiModeManager.MODE_NIGHT_YES, + ) + } else { + AppCompatDelegate.setDefaultNightMode( + if (isLight) AppCompatDelegate.MODE_NIGHT_NO else AppCompatDelegate.MODE_NIGHT_YES, + ) + } + enableEdgeToEdge( + statusBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT) { !isLight }, + navigationBarStyle = SystemBarStyle.auto(DefaultLightScrim, DefaultDarkScrim) { !isLight }, + ) + } + + private val DefaultLightScrim = Color.argb(0xe6, 0xFF, 0xFF, 0xFF) + private val DefaultDarkScrim = Color.argb(0x80, 0x1b, 0x1b, 0x1b) } diff --git a/catalog/src/main/res/values-night/themes.xml b/catalog/src/main/res/values-night/themes.xml index 05288754f..9336d31e5 100644 --- a/catalog/src/main/res/values-night/themes.xml +++ b/catalog/src/main/res/values-night/themes.xml @@ -1,5 +1,5 @@ -