-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathAppTheme.kt
69 lines (64 loc) · 2.53 KB
/
AppTheme.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/
package at.bitfire.davdroid.ui
import androidx.activity.SystemBarStyle
import androidx.activity.compose.LocalActivity
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.LocalView
import androidx.lifecycle.compose.LifecycleResumeEffect
import at.bitfire.davdroid.ui.composable.SafeAndroidUriHandler
@Composable
fun AppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
windowInsets: WindowInsets = WindowInsets.safeDrawing,
content: @Composable () -> Unit
) {
val activity = LocalActivity.current
SideEffect {
// If applicable, call Activity.enableEdgeToEdge to enable edge-to-edge layout on Android <15, too.
// When we have moved everything into one Activity with Compose navigation, we can call it there instead.
(activity as? AppCompatActivity)?.enableEdgeToEdge(
navigationBarStyle = SystemBarStyle.auto(
lightScrim = M3ColorScheme.lightScheme.scrim.toArgb(),
darkScrim = M3ColorScheme.darkScheme.scrim.toArgb()
) { darkTheme }
)
}
// Apply SafeAndroidUriHandler to the composition
val uriHandler = SafeAndroidUriHandler(LocalContext.current)
CompositionLocalProvider(LocalUriHandler provides uriHandler) {
MaterialTheme(
colorScheme = if (!darkTheme)
M3ColorScheme.lightScheme
else
M3ColorScheme.darkScheme,
) {
Box(Modifier.windowInsetsPadding(windowInsets)) {
content()
}
}
}
// Track if the app is in the foreground
val view = LocalView.current
LifecycleResumeEffect(view) {
ForegroundTracker.onResume()
onPauseOrDispose {
ForegroundTracker.onPaused()
}
}
}