|
1 | 1 | package com.wei.picquest
|
2 | 2 |
|
| 3 | +import android.graphics.Color |
3 | 4 | import android.os.Bundle
|
4 | 5 | import androidx.activity.ComponentActivity
|
| 6 | +import androidx.activity.SystemBarStyle |
5 | 7 | import androidx.activity.compose.setContent
|
6 |
| -import androidx.compose.foundation.layout.fillMaxSize |
7 |
| -import androidx.compose.material3.MaterialTheme |
8 |
| -import androidx.compose.material3.Surface |
9 |
| -import androidx.compose.material3.Text |
| 8 | +import androidx.activity.enableEdgeToEdge |
| 9 | +import androidx.compose.foundation.isSystemInDarkTheme |
| 10 | +import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi |
| 11 | +import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass |
10 | 12 | import androidx.compose.runtime.Composable
|
11 |
| -import androidx.compose.ui.Modifier |
12 |
| -import androidx.compose.ui.tooling.preview.Preview |
| 13 | +import androidx.compose.runtime.CompositionLocalProvider |
| 14 | +import androidx.compose.runtime.DisposableEffect |
| 15 | +import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen |
| 16 | +import androidx.lifecycle.lifecycleScope |
| 17 | +import com.google.accompanist.adaptive.calculateDisplayFeatures |
| 18 | +import com.wei.picquest.core.data.utils.NetworkMonitor |
13 | 19 | import com.wei.picquest.core.designsystem.theme.PqTheme
|
| 20 | +import com.wei.picquest.core.manager.SnackbarManager |
| 21 | +import com.wei.picquest.ui.PqApp |
| 22 | +import dagger.hilt.android.AndroidEntryPoint |
| 23 | +import kotlinx.coroutines.delay |
| 24 | +import kotlinx.coroutines.launch |
| 25 | +import javax.inject.Inject |
14 | 26 |
|
| 27 | +@OptIn(ExperimentalMaterial3WindowSizeClassApi::class) |
| 28 | +@AndroidEntryPoint |
15 | 29 | class MainActivity : ComponentActivity() {
|
| 30 | + |
| 31 | + @Inject |
| 32 | + lateinit var snackbarManager: SnackbarManager |
| 33 | + |
| 34 | + @Inject |
| 35 | + lateinit var networkMonitor: NetworkMonitor |
| 36 | + |
16 | 37 | override fun onCreate(savedInstanceState: Bundle?) {
|
17 | 38 | super.onCreate(savedInstanceState)
|
| 39 | + val splashScreen = installSplashScreen() |
| 40 | + |
| 41 | + splashScreen.setKeepOnScreenCondition { true } |
| 42 | + |
| 43 | + // Turn off the decor fitting system windows, which allows us to handle insets, |
| 44 | + // including IME animations, and go edge-to-edge |
| 45 | + // This also sets up the initial system bar style based on the platform theme |
| 46 | + enableEdgeToEdge() |
| 47 | + |
18 | 48 | setContent {
|
19 |
| - PqTheme { |
20 |
| - // A surface container using the 'background' color from the theme |
21 |
| - Surface( |
22 |
| - modifier = Modifier.fillMaxSize(), |
23 |
| - color = MaterialTheme.colorScheme.background, |
24 |
| - ) { |
25 |
| - Greeting("PicQuest") |
| 49 | + val darkTheme = shouldUseDarkTheme() |
| 50 | + |
| 51 | + // Update the edge to edge configuration to match the theme |
| 52 | + // This is the same parameters as the default enableEdgeToEdge call, but we manually |
| 53 | + // resolve whether or not to show dark theme using uiState, since it can be different |
| 54 | + // than the configuration's dark theme value based on the user preference. |
| 55 | + DisposableEffect(darkTheme) { |
| 56 | + enableEdgeToEdge( |
| 57 | + statusBarStyle = SystemBarStyle.auto( |
| 58 | + Color.TRANSPARENT, |
| 59 | + Color.TRANSPARENT, |
| 60 | + ) { darkTheme }, |
| 61 | + navigationBarStyle = SystemBarStyle.auto( |
| 62 | + lightScrim, |
| 63 | + darkScrim, |
| 64 | + ) { darkTheme }, |
| 65 | + ) |
| 66 | + onDispose {} |
| 67 | + } |
| 68 | + |
| 69 | + CompositionLocalProvider() { |
| 70 | + PqTheme(darkTheme = darkTheme) { |
| 71 | + PqApp( |
| 72 | + networkMonitor = networkMonitor, |
| 73 | + windowSizeClass = calculateWindowSizeClass(this@MainActivity), |
| 74 | + displayFeatures = calculateDisplayFeatures(this@MainActivity), |
| 75 | + snackbarManager = snackbarManager, |
| 76 | + ) |
26 | 77 | }
|
27 | 78 | }
|
28 | 79 | }
|
| 80 | + |
| 81 | + lifecycleScope.launch { |
| 82 | + // TODO Wei Loading user data |
| 83 | + delay(2_000) |
| 84 | + splashScreen.setKeepOnScreenCondition { false } |
| 85 | + } |
29 | 86 | }
|
30 | 87 | }
|
31 | 88 |
|
| 89 | +/** |
| 90 | + * Returns `true` if dark theme should be used, as a function of the [uiState] and the |
| 91 | + * current system context. |
| 92 | + */ |
32 | 93 | @Composable
|
33 |
| -fun Greeting(name: String, modifier: Modifier = Modifier) { |
34 |
| - Text( |
35 |
| - text = "Hello $name!", |
36 |
| - modifier = modifier, |
37 |
| - ) |
38 |
| -} |
| 94 | +private fun shouldUseDarkTheme(): Boolean = isSystemInDarkTheme() |
39 | 95 |
|
40 |
| -@Preview(showBackground = true) |
41 |
| -@Composable |
42 |
| -fun GreetingPreview() { |
43 |
| - PqTheme { |
44 |
| - Greeting("PicQuest") |
45 |
| - } |
46 |
| -} |
| 96 | +/** |
| 97 | + * The default light scrim, as defined by androidx and the platform: |
| 98 | + * https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt;l=35-38;drc=27e7d52e8604a080133e8b842db10c89b4482598 |
| 99 | + */ |
| 100 | +private val lightScrim = android.graphics.Color.argb(0xe6, 0xFF, 0xFF, 0xFF) |
| 101 | + |
| 102 | +/** |
| 103 | + * The default dark scrim, as defined by androidx and the platform: |
| 104 | + * https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:activity/activity/src/main/java/androidx/activity/EdgeToEdge.kt;l=40-44;drc=27e7d52e8604a080133e8b842db10c89b4482598 |
| 105 | + */ |
| 106 | +private val darkScrim = android.graphics.Color.argb(0x80, 0x1b, 0x1b, 0x1b) |
0 commit comments