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.credentialmanager.common.ui
18 
19 import androidx.compose.foundation.background
20 import androidx.compose.foundation.layout.ColumnScope
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.LaunchedEffect
23 import androidx.compose.runtime.rememberCoroutineScope
24 import androidx.compose.ui.Modifier
25 import androidx.compose.ui.graphics.Color
26 import com.android.compose.rememberSystemUiController
27 import com.android.credentialmanager.common.material.ModalBottomSheetLayout
28 import com.android.credentialmanager.common.material.ModalBottomSheetValue
29 import com.android.credentialmanager.common.material.rememberModalBottomSheetState
30 import com.android.credentialmanager.ui.theme.EntryShape
31 import com.android.credentialmanager.ui.theme.LocalAndroidColorScheme
32 import kotlinx.coroutines.launch
33 
34 
35 /** Draws a modal bottom sheet with the same styles and effects shared by various flows. */
36 @Composable
37 fun ModalBottomSheet(
38     sheetContent: @Composable ColumnScope.() -> Unit,
39     onDismiss: () -> Unit,
40     isInitialRender: Boolean,
41     onInitialRenderComplete: () -> Unit,
42     isAutoSelectFlow: Boolean,
43 ) {
44     val scope = rememberCoroutineScope()
45     val state = rememberModalBottomSheetState(
46         initialValue = if (isAutoSelectFlow) ModalBottomSheetValue.Expanded
47         else ModalBottomSheetValue.Hidden,
48         skipHalfExpanded = true
49     )
50     val sysUiController = rememberSystemUiController()
51     if (state.targetValue == ModalBottomSheetValue.Hidden || isAutoSelectFlow) {
52         setTransparentSystemBarsColor(sysUiController)
53     } else {
54         setBottomSheetSystemBarsColor(sysUiController)
55     }
56     ModalBottomSheetLayout(
57         sheetBackgroundColor = LocalAndroidColorScheme.current.colorSurfaceBright,
58         modifier = Modifier.background(Color.Transparent),
59         sheetState = state,
60         sheetContent = sheetContent,
61         sheetShape = EntryShape.TopRoundedCorner,
62     ) {}
63     LaunchedEffect(state.currentValue) {
64         if (state.currentValue == ModalBottomSheetValue.Hidden) {
65             if (isInitialRender) {
66                 onInitialRenderComplete()
67                 scope.launch { state.show() }
68             } else {
69                 onDismiss()
70             }
71         }
72     }
73 }