• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.credentials.flags.Flags
20 import androidx.compose.animation.animateContentSize
21 import androidx.compose.foundation.background
22 import androidx.compose.foundation.layout.Box
23 import androidx.compose.foundation.layout.WindowInsets
24 import androidx.compose.foundation.layout.fillMaxWidth
25 import androidx.compose.foundation.layout.navigationBars
26 import androidx.compose.foundation.layout.padding
27 import androidx.compose.foundation.layout.wrapContentHeight
28 import androidx.compose.material3.ExperimentalMaterial3Api
29 import androidx.compose.material3.MaterialTheme
30 import androidx.compose.runtime.Composable
31 import androidx.compose.runtime.LaunchedEffect
32 import androidx.compose.runtime.rememberCoroutineScope
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.graphics.Color
35 import com.android.compose.rememberSystemUiController
36 import androidx.compose.ui.unit.dp
37 import com.android.credentialmanager.common.material.ModalBottomSheetLayout
38 import com.android.credentialmanager.common.material.ModalBottomSheetValue
39 import com.android.credentialmanager.common.material.rememberModalBottomSheetState
40 import com.android.credentialmanager.ui.theme.EntryShape
41 import kotlinx.coroutines.launch
42 
43 /** Draws a modal bottom sheet with the same styles and effects shared by various flows. */
44 @Composable
45 @OptIn(ExperimentalMaterial3Api::class)
ModalBottomSheetnull46 fun ModalBottomSheet(
47         sheetContent: @Composable () -> Unit,
48         onDismiss: () -> Unit,
49         isInitialRender: Boolean,
50         onInitialRenderComplete: () -> Unit,
51         isAutoSelectFlow: Boolean,
52 ) {
53     if (Flags.selectorUiImprovementsEnabled()) {
54         val state = androidx.compose.material3.rememberModalBottomSheetState(
55                 skipPartiallyExpanded = true
56         )
57         androidx.compose.material3.ModalBottomSheet(
58                 onDismissRequest = onDismiss,
59                 containerColor = MaterialTheme.colorScheme.surfaceBright,
60                 sheetState = state,
61                 content = {
62                     Box(
63                             modifier = Modifier
64                                     .animateContentSize()
65                                     .wrapContentHeight()
66                                     .fillMaxWidth()
67                     ) {
68                         sheetContent()
69                     }
70                 },
71                 scrimColor = MaterialTheme.colorScheme.scrim.copy(alpha = .32f),
72                 shape = EntryShape.TopRoundedCorner,
73                 contentWindowInsets = { WindowInsets.navigationBars },
74                 dragHandle = null,
75                 // Never take over the full screen. We always want to leave some top scrim space
76                 // for exiting and viewing the underlying app to help a user gain context.
77                 modifier = Modifier.padding(top = 72.dp),
78         )
79     } else {
80         val scope = rememberCoroutineScope()
81         val state = rememberModalBottomSheetState(
82                 initialValue = if (isAutoSelectFlow) ModalBottomSheetValue.Expanded
83                 else ModalBottomSheetValue.Hidden,
84                 skipHalfExpanded = true
85         )
86         val sysUiController = rememberSystemUiController()
87         if (state.targetValue == ModalBottomSheetValue.Hidden || isAutoSelectFlow) {
88             setTransparentSystemBarsColor(sysUiController)
89         } else {
90             setBottomSheetSystemBarsColor(sysUiController)
91         }
92         ModalBottomSheetLayout(
93                 sheetBackgroundColor = MaterialTheme.colorScheme.surfaceBright,
94                 modifier = Modifier.background(Color.Transparent),
95                 sheetState = state,
96                 sheetContent = { sheetContent() },
97                 sheetShape = EntryShape.TopRoundedCorner,
98         ) {}
99         LaunchedEffect(state.currentValue, state.targetValue) {
100             if (state.currentValue == ModalBottomSheetValue.Hidden) {
101                 if (isInitialRender) {
102                     onInitialRenderComplete()
103                     scope.launch { state.show() }
104                 } else if (state.targetValue == ModalBottomSheetValue.Hidden) {
105                     // Only dismiss ui when the motion is downwards
106                     onDismiss()
107                 }
108             }
109         }
110     }
111 }
112