Skip to content

Commit

Permalink
Migrate from systemuicontroller to enableEdgeToEdge (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3y-the-programmer authored Nov 13, 2023
1 parent 13077aa commit 623c1cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
1 change: 0 additions & 1 deletion androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ dependencies {
implementation(compose.preview)
implementation(compose.runtime)
implementation(compose.foundation)
implementation(libs.accompanist.system.ui.controller)

// Tooling
debugImplementation(compose.uiTooling)
Expand Down
46 changes: 31 additions & 15 deletions androidApp/src/main/kotlin/com/mr3y/ludi/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.mr3y.ludi

import android.app.Activity
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.mr3y.ludi.shared.App
import com.mr3y.ludi.shared.di.AndroidApplicationComponent
import com.mr3y.ludi.shared.di.HostActivityComponent
Expand All @@ -35,42 +36,38 @@ class MainActivity : ComponentActivity(), HostActivityComponentOwner {
// Initialize the component.
hostActivityComponent

WindowCompat.setDecorFitsSystemWindows(window, false)
val appState = hostActivityComponent.parent.appState
lifecycleScope.launch {
appState.preferences.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED).collect { userPreferences ->
splashScreen.setKeepOnScreenCondition { userPreferences == null }
}
}

goEdgeToEdge()

setContent {
val userPreferences by appState.preferences.collectAsStateWithLifecycle(minActiveState = Lifecycle.State.CREATED)
val systemUiController = rememberSystemUiController()
val useDarkIcons = !isSystemInDarkTheme()

DisposableEffect(systemUiController, useDarkIcons) {
// Update all of the system bar colors to be transparent, and use
// dark icons if we're in light theme
systemUiController.setSystemBarsColor(
color = Color.Transparent,
darkIcons = useDarkIcons
)
DisposableEffect(useDarkIcons) {
goEdgeToEdge(isDarkTheme = { !useDarkIcons })

onDispose {}
}
if (userPreferences != null) {
App(
isDarkTheme = when (userPreferences!!.theme) {
Theme.Light -> {
systemUiController.setSystemBarsColor(Color.Transparent, true)
goEdgeToEdge(isDarkTheme = { false })
false
}
Theme.Dark -> {
systemUiController.setSystemBarsColor(Color.Transparent, false)
goEdgeToEdge(isDarkTheme = { true })
true
}
Theme.SystemDefault -> {
val isDarkTheme = isSystemInDarkTheme()
systemUiController.setSystemBarsColor(Color.Transparent, !isDarkTheme)
goEdgeToEdge(isDarkTheme = { isDarkTheme })
isDarkTheme
}
},
Expand All @@ -80,6 +77,25 @@ class MainActivity : ComponentActivity(), HostActivityComponentOwner {
}
}
}

private fun goEdgeToEdge(
isDarkTheme: (Resources) -> Boolean = { resources ->
(resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
}
) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
lightScrim = android.graphics.Color.TRANSPARENT,
darkScrim = android.graphics.Color.TRANSPARENT,
detectDarkMode = isDarkTheme
),
navigationBarStyle = SystemBarStyle.auto(
lightScrim = android.graphics.Color.argb(0xe6, 0xFF, 0xFF, 0xFF),
darkScrim = android.graphics.Color.argb(0x80, 0x1b, 0x1b, 0x1b),
detectDarkMode = isDarkTheme
)
)
}
}

private fun AndroidApplicationComponent.Companion.from(activity: Activity): AndroidApplicationComponent {
Expand Down
3 changes: 0 additions & 3 deletions androidApp/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Ludi" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar">false</item>
<item name="colorPrimary">@color/md_theme_dark_primary</item>
<item name="colorOnPrimary">@color/md_theme_dark_onPrimary</item>
<item name="colorSecondary">@color/md_theme_dark_secondary</item>
Expand Down
3 changes: 0 additions & 3 deletions androidApp/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="Theme.Ludi" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar">true</item>
<item name="colorPrimary">@color/md_theme_light_primary</item>
<item name="colorOnPrimary">@color/md_theme_light_onPrimary</item>
<item name="colorSecondary">@color/md_theme_light_secondary</item>
Expand Down
4 changes: 0 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ views-material = "com.google.android.material:material:1.9.0"
splash-screen = "androidx.core:core-splashscreen:1.0.1"
## ⬆ :1.1.0-alpha01"
## ⬆ :1.1.0-alpha02"
accompanist-system-ui-controller = "com.google.accompanist:accompanist-systemuicontroller:0.32.0"
## ⬆ :0.33.0-alpha"
## ⬆ :0.33.1-alpha"
## ⬆ :0.33.2-alpha"
androidx-annotations = "androidx.annotation:annotation:1.7.0"
leakcanary = "com.squareup.leakcanary:leakcanary-android:2.12"
coil = "io.coil-kt:coil-compose:2.5.0"
Expand Down

0 comments on commit 623c1cb

Please sign in to comment.