|
1 | 1 | package to.bitkit.ui.components |
2 | 2 |
|
| 3 | +import androidx.activity.compose.BackHandler |
| 4 | +import androidx.compose.animation.AnimatedVisibility |
| 5 | +import androidx.compose.animation.core.Animatable |
| 6 | +import androidx.compose.animation.core.animateFloatAsState |
| 7 | +import androidx.compose.animation.core.tween |
| 8 | +import androidx.compose.animation.fadeIn |
| 9 | +import androidx.compose.animation.fadeOut |
| 10 | +import androidx.compose.animation.slideInVertically |
| 11 | +import androidx.compose.animation.slideOutVertically |
| 12 | +import androidx.compose.foundation.background |
| 13 | +import androidx.compose.foundation.clickable |
| 14 | +import androidx.compose.foundation.gestures.detectVerticalDragGestures |
| 15 | +import androidx.compose.foundation.layout.Box |
| 16 | +import androidx.compose.foundation.layout.Column |
| 17 | +import androidx.compose.foundation.layout.fillMaxSize |
| 18 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 19 | +import androidx.compose.foundation.layout.navigationBarsPadding |
| 20 | +import androidx.compose.foundation.layout.offset |
| 21 | +import androidx.compose.runtime.Composable |
| 22 | +import androidx.compose.runtime.LaunchedEffect |
| 23 | +import androidx.compose.runtime.getValue |
| 24 | +import androidx.compose.runtime.mutableFloatStateOf |
| 25 | +import androidx.compose.runtime.mutableStateOf |
| 26 | +import androidx.compose.runtime.remember |
| 27 | +import androidx.compose.runtime.rememberCoroutineScope |
| 28 | +import androidx.compose.runtime.setValue |
| 29 | +import androidx.compose.ui.Alignment |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.draw.clip |
| 32 | +import androidx.compose.ui.graphics.Color |
| 33 | +import androidx.compose.ui.input.pointer.pointerInput |
| 34 | +import androidx.compose.ui.layout.onSizeChanged |
| 35 | +import androidx.compose.ui.unit.IntOffset |
| 36 | +import kotlinx.coroutines.launch |
| 37 | +import to.bitkit.ui.shared.modifiers.sheetHeight |
| 38 | +import to.bitkit.ui.theme.AppShapes |
| 39 | +import to.bitkit.ui.theme.Colors |
| 40 | +import kotlin.math.roundToInt |
| 41 | + |
| 42 | +private const val MS_DURATION_ANIM = 300 |
| 43 | +private const val THRESHOLD_DISMISS = 0.45f |
| 44 | +private const val OFFSET_MIN_DRAG = -0.1f |
| 45 | +private val sheetContainerColor = Color.Black |
| 46 | + |
3 | 47 | enum class SheetSize { LARGE, MEDIUM, SMALL, CALENDAR; } |
4 | 48 |
|
5 | | -/**@param priority Priority levels for timed sheets (higher number = higher priority)*/ |
| 49 | +/** @param priority Priority levels for timed sheets (higher number = higher priority)*/ |
6 | 50 | enum class TimedSheetType(val priority: Int) { |
7 | 51 | APP_UPDATE(priority = 5), |
8 | 52 | BACKUP(priority = 4), |
9 | 53 | NOTIFICATIONS(priority = 3), |
10 | 54 | QUICK_PAY(priority = 2), |
11 | 55 | HIGH_BALANCE(priority = 1) |
12 | 56 | } |
| 57 | + |
| 58 | +@Composable |
| 59 | +fun SheetHost( |
| 60 | + sheetSize: SheetSize, |
| 61 | + onDismiss: () -> Unit, |
| 62 | + content: @Composable () -> Unit, |
| 63 | +) { |
| 64 | + val scope = rememberCoroutineScope() |
| 65 | + var sheetHeightPx by remember { mutableFloatStateOf(0f) } |
| 66 | + val offsetY = remember { Animatable(1f) } |
| 67 | + var sheetVisible by remember { mutableStateOf(false) } |
| 68 | + |
| 69 | + LaunchedEffect(Unit) { |
| 70 | + sheetVisible = true |
| 71 | + offsetY.animateTo(0f, animationSpec = tween(MS_DURATION_ANIM)) |
| 72 | + } |
| 73 | + |
| 74 | + fun dismiss() { |
| 75 | + scope.launch { |
| 76 | + offsetY.animateTo(1f, animationSpec = tween(MS_DURATION_ANIM)) |
| 77 | + sheetVisible = false |
| 78 | + onDismiss() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + BackHandler(enabled = offsetY.value < 1f) { |
| 83 | + dismiss() |
| 84 | + } |
| 85 | + |
| 86 | + Box(modifier = Modifier.fillMaxSize()) { |
| 87 | + Scrim( |
| 88 | + isVisible = offsetY.value < 1f, |
| 89 | + progress = 1f - offsetY.value, |
| 90 | + onClick = { dismiss() }, |
| 91 | + ) |
| 92 | + |
| 93 | + AnimatedVisibility( |
| 94 | + visible = sheetVisible, |
| 95 | + enter = slideInVertically { it } + fadeIn(), |
| 96 | + exit = slideOutVertically { it } + fadeOut(), |
| 97 | + modifier = Modifier.align(Alignment.BottomCenter), |
| 98 | + ) { |
| 99 | + Column( |
| 100 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 101 | + modifier = Modifier |
| 102 | + .offset { IntOffset(0, (offsetY.value * sheetHeightPx).roundToInt()) } |
| 103 | + .onSizeChanged { sheetHeightPx = it.height.toFloat() } |
| 104 | + .clip(AppShapes.sheet) |
| 105 | + .background(sheetContainerColor) |
| 106 | + .fillMaxWidth() |
| 107 | + .pointerInput(Unit) { |
| 108 | + detectVerticalDragGestures( |
| 109 | + onDragEnd = { |
| 110 | + scope.launch { |
| 111 | + if (offsetY.value > THRESHOLD_DISMISS) { |
| 112 | + offsetY.animateTo( |
| 113 | + 1f, |
| 114 | + animationSpec = tween(MS_DURATION_ANIM) |
| 115 | + ) |
| 116 | + sheetVisible = false |
| 117 | + onDismiss() |
| 118 | + } else { |
| 119 | + offsetY.animateTo( |
| 120 | + 0f, |
| 121 | + animationSpec = tween(MS_DURATION_ANIM) |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + }, |
| 126 | + onVerticalDrag = { _, dragAmount -> |
| 127 | + val newOffset = offsetY.value + (dragAmount / sheetHeightPx) |
| 128 | + scope.launch { |
| 129 | + offsetY.snapTo(newOffset.coerceIn(OFFSET_MIN_DRAG, 1f)) |
| 130 | + } |
| 131 | + } |
| 132 | + ) |
| 133 | + } |
| 134 | + ) { |
| 135 | + Box( |
| 136 | + contentAlignment = Alignment.TopCenter, |
| 137 | + modifier = Modifier |
| 138 | + .fillMaxWidth() |
| 139 | + .background(Colors.White08) |
| 140 | + ) { |
| 141 | + SheetDragHandle() |
| 142 | + } |
| 143 | + Box( |
| 144 | + modifier = Modifier |
| 145 | + .sheetHeight(sheetSize) |
| 146 | + .navigationBarsPadding() |
| 147 | + ) { |
| 148 | + content() |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +@Composable |
| 156 | +private fun Scrim( |
| 157 | + isVisible: Boolean, |
| 158 | + progress: Float, |
| 159 | + onClick: () -> Unit, |
| 160 | +) { |
| 161 | + val scrimAlpha by animateFloatAsState( |
| 162 | + targetValue = if (isVisible) progress * 0.5f else 0f, |
| 163 | + animationSpec = tween(durationMillis = MS_DURATION_ANIM), |
| 164 | + label = "sheetScrimAlpha" |
| 165 | + ) |
| 166 | + if (scrimAlpha > 0f || isVisible) { |
| 167 | + Box( |
| 168 | + modifier = Modifier |
| 169 | + .fillMaxSize() |
| 170 | + .background(Colors.Black.copy(alpha = scrimAlpha)) |
| 171 | + .clickable( |
| 172 | + interactionSource = null, |
| 173 | + indication = null, |
| 174 | + onClick = onClick, |
| 175 | + ) |
| 176 | + ) |
| 177 | + } |
| 178 | +} |
0 commit comments