1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.avatarpicker.ui.theme
18 
19 import android.app.Activity
20 import androidx.compose.foundation.isSystemInDarkTheme
21 import androidx.compose.material3.MaterialTheme
22 import androidx.compose.material3.darkColorScheme
23 import androidx.compose.material3.dynamicDarkColorScheme
24 import androidx.compose.material3.dynamicLightColorScheme
25 import androidx.compose.material3.lightColorScheme
26 import androidx.compose.runtime.Composable
27 import androidx.compose.runtime.SideEffect
28 import androidx.compose.ui.graphics.toArgb
29 import androidx.compose.ui.platform.LocalContext
30 import androidx.compose.ui.platform.LocalView
31 import androidx.core.view.WindowCompat
32 
33 private val DarkColorScheme = darkColorScheme()
34 
35 private val LightColorScheme = lightColorScheme()
36 
37 @Composable
AvatarPickerThemenull38 fun AvatarPickerTheme(
39     darkTheme: Boolean = isSystemInDarkTheme(),
40     dynamicColor: Boolean = true, content: @Composable () -> Unit
41 ) {
42     val colorScheme = when {
43         dynamicColor -> {
44             val context = LocalContext.current
45             if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
46         }
47 
48         darkTheme -> DarkColorScheme
49         else -> LightColorScheme
50     }
51     val view = LocalView.current
52     if (!view.isInEditMode) {
53         SideEffect {
54             val window = (view.context as Activity).window
55             window.statusBarColor = colorScheme.background.copy(alpha = 0f).toArgb()
56             window.navigationBarColor = colorScheme.background.copy(alpha = 0f).toArgb()
57             WindowCompat.setDecorFitsSystemWindows(window, false)
58             WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme
59             WindowCompat.getInsetsController(window, view).isAppearanceLightNavigationBars =
60                 !darkTheme
61         }
62     }
63 
64     MaterialTheme(
65         colorScheme = colorScheme, typography = getTypography(), content = content
66     )
67 }