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

Refactor enableEdgeToEdge out of Compose #1704

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}