1 /*
2  * Copyright 2024 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.photopicker.core.theme
18 
19 import android.content.Intent
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.material3.windowsizeclass.WindowSizeClass
27 import androidx.compose.runtime.Composable
28 import androidx.compose.runtime.CompositionLocalProvider
29 import androidx.compose.runtime.remember
30 import androidx.compose.ui.graphics.Color
31 import androidx.compose.ui.platform.LocalContext
32 import com.android.modules.utils.build.SdkLevel
33 
34 /**
35  * This composable generates all the theme related elements and creates the wrapping [MaterialTheme]
36  * composable.
37  *
38  * Additionally, the PhotopickerTheme also creates a provider for [WindowSizeClass] allowing
39  * composables downstream to react to WindowSize changes.
40  */
41 @Composable
PhotopickerThemenull42 fun PhotopickerTheme(
43     isDarkTheme: Boolean = isSystemInDarkTheme(),
44     intent: Intent?,
45     content: @Composable () -> Unit
46 ) {
47     val context = LocalContext.current
48     val accentColorHelper = AccentColorHelper(intent)
49 
50     val colorScheme =
51         remember(isDarkTheme) {
52             when {
53                 // When the accent color is available then the generic theme for the picker should
54                 // be the static baseline material theme. This will be used for any components that
55                 // are not highlighted with accent colors.
56                 (accentColorHelper.getAccentColor() != Color.Unspecified) -> {
57                     if (isDarkTheme) {
58                         darkColorScheme()
59                     } else {
60                         lightColorScheme()
61                     }
62                 }
63                 else -> {
64                     if (SdkLevel.isAtLeastS()) {
65                         if (isDarkTheme) {
66                             dynamicDarkColorScheme(context)
67                         } else {
68                             dynamicLightColorScheme(context)
69                         }
70                     } else {
71                         null
72                     }
73                 }
74             }
75         } ?: MaterialTheme.colorScheme
76 
77     // Calculate the current screen size
78     val windowSizeClass: WindowSizeClass = calculateWindowSizeClass()
79 
80     MaterialTheme(colorScheme) {
81         CompositionLocalProvider(
82             LocalWindowSizeClass provides windowSizeClass,
83             CustomAccentColorScheme provides AccentColorScheme(
84                 accentColorHelper = accentColorHelper
85             ),
86         ) {
87             content()
88         }
89     }
90 }
91