|
| 1 | +package to.bitkit.ui.components |
| 2 | + |
| 3 | +import androidx.annotation.DrawableRes |
| 4 | +import androidx.compose.animation.core.RepeatMode |
| 5 | +import androidx.compose.animation.core.animateFloat |
| 6 | +import androidx.compose.animation.core.infiniteRepeatable |
| 7 | +import androidx.compose.animation.core.rememberInfiniteTransition |
| 8 | +import androidx.compose.animation.core.tween |
| 9 | +import androidx.compose.foundation.background |
| 10 | +import androidx.compose.foundation.border |
| 11 | +import androidx.compose.foundation.layout.Arrangement |
| 12 | +import androidx.compose.foundation.layout.Box |
| 13 | +import androidx.compose.foundation.layout.Row |
| 14 | +import androidx.compose.foundation.layout.fillMaxSize |
| 15 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 16 | +import androidx.compose.foundation.layout.padding |
| 17 | +import androidx.compose.foundation.layout.requiredHeight |
| 18 | +import androidx.compose.foundation.lazy.LazyColumn |
| 19 | +import androidx.compose.foundation.lazy.items |
| 20 | +import androidx.compose.material3.Icon |
| 21 | +import androidx.compose.material3.ShapeDefaults |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.runtime.getValue |
| 24 | +import androidx.compose.ui.Alignment |
| 25 | +import androidx.compose.ui.Modifier |
| 26 | +import androidx.compose.ui.draw.clip |
| 27 | +import androidx.compose.ui.geometry.Offset |
| 28 | +import androidx.compose.ui.graphics.Brush |
| 29 | +import androidx.compose.ui.graphics.Color |
| 30 | +import androidx.compose.ui.platform.LocalDensity |
| 31 | +import androidx.compose.ui.res.painterResource |
| 32 | +import androidx.compose.ui.res.stringResource |
| 33 | +import androidx.compose.ui.text.AnnotatedString |
| 34 | +import androidx.compose.ui.tooling.preview.Preview |
| 35 | +import androidx.compose.ui.unit.dp |
| 36 | +import to.bitkit.R |
| 37 | +import to.bitkit.models.ActivityBannerType |
| 38 | +import to.bitkit.ui.shared.util.clickableAlpha |
| 39 | +import to.bitkit.ui.shared.util.outerGlow |
| 40 | +import to.bitkit.ui.theme.Colors |
| 41 | + |
| 42 | +private const val GLOW_ANIMATION_MILLIS = 1200 |
| 43 | + |
| 44 | +@Composable |
| 45 | +fun ActivityBanner( |
| 46 | + gradientColor: Color, |
| 47 | + title: String, |
| 48 | + @DrawableRes icon: Int, |
| 49 | + modifier: Modifier = Modifier, |
| 50 | + onClick: (() -> Unit)? = null, |
| 51 | +) { |
| 52 | + val infiniteTransition = rememberInfiniteTransition(label = "glow") |
| 53 | + |
| 54 | + val innerShadowOpacity by infiniteTransition.animateFloat( |
| 55 | + initialValue = 0.32f, |
| 56 | + targetValue = 0.64f, |
| 57 | + animationSpec = infiniteRepeatable( |
| 58 | + animation = tween(durationMillis = GLOW_ANIMATION_MILLIS), |
| 59 | + repeatMode = RepeatMode.Reverse |
| 60 | + ), |
| 61 | + label = "inner_shadow_opacity" |
| 62 | + ) |
| 63 | + |
| 64 | + val dropShadowOpacity by infiniteTransition.animateFloat( |
| 65 | + initialValue = 1.0f, |
| 66 | + targetValue = 0.0f, |
| 67 | + animationSpec = infiniteRepeatable( |
| 68 | + animation = tween(durationMillis = GLOW_ANIMATION_MILLIS), |
| 69 | + repeatMode = RepeatMode.Reverse |
| 70 | + ), |
| 71 | + label = "drop_shadow_opacity" |
| 72 | + ) |
| 73 | + |
| 74 | + val radialGradientOpacity by infiniteTransition.animateFloat( |
| 75 | + initialValue = 0.6f, |
| 76 | + targetValue = 0.0f, |
| 77 | + animationSpec = infiniteRepeatable( |
| 78 | + animation = tween(durationMillis = GLOW_ANIMATION_MILLIS), |
| 79 | + repeatMode = RepeatMode.Reverse |
| 80 | + ), |
| 81 | + label = "radial_gradient_opacity" |
| 82 | + ) |
| 83 | + |
| 84 | + val borderOpacity by infiniteTransition.animateFloat( |
| 85 | + initialValue = 0.32f, |
| 86 | + targetValue = 1.0f, |
| 87 | + animationSpec = infiniteRepeatable( |
| 88 | + animation = tween(durationMillis = GLOW_ANIMATION_MILLIS), |
| 89 | + repeatMode = RepeatMode.Reverse |
| 90 | + ), |
| 91 | + label = "border_opacity" |
| 92 | + ) |
| 93 | + |
| 94 | + val density = LocalDensity.current.density |
| 95 | + |
| 96 | + Box( |
| 97 | + modifier = modifier |
| 98 | + .requiredHeight(72.dp) |
| 99 | + .outerGlow( |
| 100 | + glowColor = gradientColor, |
| 101 | + glowOpacity = dropShadowOpacity, |
| 102 | + glowRadius = 12.dp, |
| 103 | + cornerRadius = 16.dp |
| 104 | + ) |
| 105 | + .clickableAlpha(onClick = onClick) |
| 106 | + ) { |
| 107 | + // Main card content with clipped backgrounds |
| 108 | + Box( |
| 109 | + modifier = Modifier |
| 110 | + .fillMaxWidth() |
| 111 | + .requiredHeight(72.dp) |
| 112 | + .clip(ShapeDefaults.Large) |
| 113 | + // Layer 1: Base color (black) |
| 114 | + .background(Color.Black) |
| 115 | + // Layer 2: Inner shadow approximation (radial gradient from edges) |
| 116 | + .background( |
| 117 | + brush = Brush.radialGradient( |
| 118 | + colors = listOf( |
| 119 | + Color.Transparent, |
| 120 | + gradientColor.copy(alpha = innerShadowOpacity) |
| 121 | + ), |
| 122 | + radius = 400f |
| 123 | + ) |
| 124 | + ) |
| 125 | + // Layer 3: Linear gradient (top to bottom) |
| 126 | + .background( |
| 127 | + brush = Brush.verticalGradient( |
| 128 | + colors = listOf( |
| 129 | + gradientColor.copy(alpha = 0.32f), |
| 130 | + gradientColor.copy(alpha = 0f) |
| 131 | + ) |
| 132 | + ) |
| 133 | + ) |
| 134 | + // Layer 4: Radial gradient (top-left corner) |
| 135 | + .background( |
| 136 | + brush = Brush.radialGradient( |
| 137 | + colors = listOf( |
| 138 | + gradientColor.copy(alpha = radialGradientOpacity), |
| 139 | + gradientColor.copy(alpha = 0f) |
| 140 | + ), |
| 141 | + center = Offset(0f, 0f), |
| 142 | + radius = 160f * density |
| 143 | + ) |
| 144 | + ) |
| 145 | + // Border with animated opacity |
| 146 | + .border( |
| 147 | + width = 1.dp, |
| 148 | + color = gradientColor.copy(alpha = borderOpacity), |
| 149 | + shape = ShapeDefaults.Large |
| 150 | + ) |
| 151 | + ) |
| 152 | + |
| 153 | + Row( |
| 154 | + modifier = Modifier |
| 155 | + .padding(horizontal = 16.dp, vertical = 12.dp) |
| 156 | + .align(Alignment.Center), |
| 157 | + horizontalArrangement = Arrangement.spacedBy(8.dp), |
| 158 | + verticalAlignment = Alignment.CenterVertically |
| 159 | + ) { |
| 160 | + Icon( |
| 161 | + painter = painterResource(icon), |
| 162 | + contentDescription = null, |
| 163 | + tint = gradientColor |
| 164 | + ) |
| 165 | + |
| 166 | + Headline20( |
| 167 | + text = AnnotatedString(title), |
| 168 | + color = Colors.White, |
| 169 | + ) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +@Preview(showSystemUi = true) |
| 175 | +@Composable |
| 176 | +private fun Preview() { |
| 177 | + LazyColumn( |
| 178 | + verticalArrangement = Arrangement.spacedBy(4.dp), |
| 179 | + modifier = Modifier.fillMaxSize(), |
| 180 | + ) { |
| 181 | + items(items = ActivityBannerType.entries) { item -> |
| 182 | + ActivityBanner( |
| 183 | + gradientColor = item.color, |
| 184 | + title = stringResource(R.string.activity_banner__transfer_in_progress), |
| 185 | + icon = item.icon, |
| 186 | + onClick = {}, |
| 187 | + modifier = Modifier.fillMaxWidth() |
| 188 | + ) |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments