Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Refactor enableEdgeToEdge out of Compose (#1704)
Browse files Browse the repository at this point in the history
There's no need to run it from Compose so we just
use a normal coroutine launch
  • Loading branch information
chrisbanes authored Jan 6, 2024
1 parent 6331c4b commit bfa5322
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions android-app/app/src/main/kotlin/app/tivi/home/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import android.content.Context
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.repeatOnLifecycle
import app.tivi.BuildConfig
import app.tivi.TiviActivity
import app.tivi.TiviApplication
Expand All @@ -32,44 +30,29 @@ import app.tivi.screens.DiscoverScreen
import app.tivi.settings.TiviPreferences
import com.slack.circuit.backstack.rememberSaveableBackStack
import com.slack.circuit.foundation.rememberCircuitNavigator
import kotlinx.coroutines.launch

class MainActivity : TiviActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
enableEdgeToEdge()
enableEdgeToEdgeForTheme(TiviPreferences.Theme.SYSTEM)

super.onCreate(savedInstanceState)
val component = AndroidActivityComponent.create(this, AndroidApplicationComponent.from(this))

setContent {
val backstack = rememberSaveableBackStack { push(DiscoverScreen) }
val navigator = rememberCircuitNavigator(backstack)
val applicationComponent = AndroidApplicationComponent.from(this)
val component = AndroidActivityComponent.create(this, applicationComponent)

val isSystemInDarkTheme = isSystemInDarkTheme()
val theme by component.applicationComponent.preferences.observeTheme()
.collectAsState(TiviPreferences.Theme.SYSTEM)
val isDarkTheme by remember {
derivedStateOf {
when (theme) {
TiviPreferences.Theme.SYSTEM -> isSystemInDarkTheme
TiviPreferences.Theme.LIGHT -> false
TiviPreferences.Theme.DARK -> true
}
lifecycle.coroutineScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
applicationComponent.preferences.observeTheme().collect { theme ->
enableEdgeToEdgeForTheme(theme)
}
}
LaunchedEffect(isDarkTheme) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
lightScrim = Color.TRANSPARENT,
darkScrim = Color.TRANSPARENT,
detectDarkMode = { isDarkTheme },
),
navigationBarStyle = SystemBarStyle.auto(
lightScrim = Color.TRANSPARENT,
darkScrim = Color.TRANSPARENT,
detectDarkMode = { isDarkTheme },
),
)
}
}

setContent {
val backstack = rememberSaveableBackStack { push(DiscoverScreen) }
val navigator = rememberCircuitNavigator(backstack)

component.tiviContent(
backstack,
Expand All @@ -92,6 +75,15 @@ class MainActivity : TiviActivity() {
}
}

private fun ComponentActivity.enableEdgeToEdgeForTheme(theme: TiviPreferences.Theme) {
val style = when (theme) {
TiviPreferences.Theme.LIGHT -> SystemBarStyle.light(Color.TRANSPARENT, Color.TRANSPARENT)
TiviPreferences.Theme.DARK -> SystemBarStyle.dark(Color.TRANSPARENT)
TiviPreferences.Theme.SYSTEM -> SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT)
}
enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style)
}

private fun AndroidApplicationComponent.Companion.from(context: Context): AndroidApplicationComponent {
return (context.applicationContext as TiviApplication).component
}

0 comments on commit bfa5322

Please sign in to comment.