|
| 1 | +package dev.dimension.flare.ui.component |
| 2 | + |
| 3 | +import androidx.compose.animation.AnimatedVisibility |
| 4 | +import androidx.compose.animation.core.LinearEasing |
| 5 | +import androidx.compose.animation.core.RepeatMode |
| 6 | +import androidx.compose.animation.core.VectorConverter |
| 7 | +import androidx.compose.animation.core.animateFloat |
| 8 | +import androidx.compose.animation.core.animateValue |
| 9 | +import androidx.compose.animation.core.infiniteRepeatable |
| 10 | +import androidx.compose.animation.core.rememberInfiniteTransition |
| 11 | +import androidx.compose.animation.core.tween |
| 12 | +import androidx.compose.foundation.Canvas |
| 13 | +import androidx.compose.foundation.border |
| 14 | +import androidx.compose.foundation.layout.Arrangement |
| 15 | +import androidx.compose.foundation.layout.Box |
| 16 | +import androidx.compose.foundation.layout.Column |
| 17 | +import androidx.compose.foundation.layout.ColumnScope |
| 18 | +import androidx.compose.foundation.layout.padding |
| 19 | +import androidx.compose.foundation.layout.size |
| 20 | +import androidx.compose.foundation.shape.CircleShape |
| 21 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 22 | +import androidx.compose.material3.ExtendedFloatingActionButton |
| 23 | +import androidx.compose.material3.MaterialTheme |
| 24 | +import androidx.compose.material3.ModalBottomSheet |
| 25 | +import androidx.compose.material3.Text |
| 26 | +import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.runtime.LaunchedEffect |
| 28 | +import androidx.compose.runtime.collectAsState |
| 29 | +import androidx.compose.runtime.getValue |
| 30 | +import androidx.compose.runtime.mutableStateOf |
| 31 | +import androidx.compose.runtime.remember |
| 32 | +import androidx.compose.runtime.setValue |
| 33 | +import androidx.compose.ui.Alignment |
| 34 | +import androidx.compose.ui.Modifier |
| 35 | +import androidx.compose.ui.graphics.Color |
| 36 | +import androidx.compose.ui.graphics.drawscope.Stroke |
| 37 | +import androidx.compose.ui.graphics.graphicsLayer |
| 38 | +import androidx.compose.ui.platform.LocalDensity |
| 39 | +import androidx.compose.ui.unit.Dp |
| 40 | +import androidx.compose.ui.unit.dp |
| 41 | +import dev.dimension.flare.common.PodcastManager |
| 42 | +import dev.dimension.flare.ui.model.UiState |
| 43 | +import dev.dimension.flare.ui.screen.media.PodcastContent |
| 44 | +import dev.dimension.flare.ui.theme.screenHorizontalPadding |
| 45 | +import org.koin.compose.koinInject |
| 46 | + |
| 47 | +@OptIn(ExperimentalMaterial3Api::class) |
| 48 | +@Composable |
| 49 | +internal fun ColumnScope.PodcastFAB( |
| 50 | + onVisibilityChanged: (Boolean) -> Unit, |
| 51 | + modifier: Modifier = Modifier, |
| 52 | + podcastManager: PodcastManager = koinInject(), |
| 53 | +) { |
| 54 | + val podcast by podcastManager.currentPodcast.collectAsState(null) |
| 55 | + LaunchedEffect(podcast) { |
| 56 | + onVisibilityChanged.invoke(podcast != null) |
| 57 | + } |
| 58 | + AnimatedVisibility( |
| 59 | + podcast != null, |
| 60 | + modifier = modifier, |
| 61 | + ) { |
| 62 | + podcast?.let { data -> |
| 63 | + var showInfoSheet by remember { mutableStateOf(false) } |
| 64 | + ExtendedFloatingActionButton( |
| 65 | + shape = MaterialTheme.shapes.large, |
| 66 | + onClick = { |
| 67 | + showInfoSheet = true |
| 68 | + }, |
| 69 | + icon = { |
| 70 | + Box { |
| 71 | + AvatarComponent( |
| 72 | + data.creator.avatar, |
| 73 | + size = 36.dp, |
| 74 | + modifier = |
| 75 | + Modifier |
| 76 | + .graphicsLayer() |
| 77 | + .border( |
| 78 | + 2.dp, |
| 79 | + MaterialTheme.colorScheme.primary, |
| 80 | + shape = CircleShape, |
| 81 | + ), |
| 82 | + ) |
| 83 | + PulsingCircle( |
| 84 | + modifier = Modifier.align(Alignment.Center), |
| 85 | + ) |
| 86 | + } |
| 87 | + }, |
| 88 | + text = { |
| 89 | + Text(data.title) |
| 90 | + }, |
| 91 | + ) |
| 92 | + if (showInfoSheet) { |
| 93 | + ModalBottomSheet( |
| 94 | + onDismissRequest = { |
| 95 | + showInfoSheet = false |
| 96 | + }, |
| 97 | + ) { |
| 98 | + Column( |
| 99 | + modifier = |
| 100 | + Modifier |
| 101 | + .padding( |
| 102 | + horizontal = screenHorizontalPadding, |
| 103 | + ), |
| 104 | + verticalArrangement = Arrangement.spacedBy(8.dp), |
| 105 | + ) { |
| 106 | + PodcastContent( |
| 107 | + data = data, |
| 108 | + isPlaying = UiState.Success(true), |
| 109 | + toUser = {}, |
| 110 | + onJoinPodcast = { |
| 111 | + podcastManager.playPodcast(data) |
| 112 | + }, |
| 113 | + onLeavePodcast = { |
| 114 | + podcastManager.stopPodcast() |
| 115 | + }, |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +@Composable |
| 125 | +internal fun PulsingCircle( |
| 126 | + modifier: Modifier = Modifier, |
| 127 | + color: Color = MaterialTheme.colorScheme.primary, |
| 128 | +) { |
| 129 | + val infiniteTransition = rememberInfiniteTransition() |
| 130 | + |
| 131 | + val scale by infiniteTransition.animateFloat( |
| 132 | + initialValue = 1f, |
| 133 | + targetValue = 48f / 36f, |
| 134 | + animationSpec = |
| 135 | + infiniteRepeatable( |
| 136 | + animation = tween(durationMillis = 1000, easing = LinearEasing), |
| 137 | + repeatMode = RepeatMode.Restart, |
| 138 | + ), |
| 139 | + ) |
| 140 | + |
| 141 | + val alpha by infiniteTransition.animateFloat( |
| 142 | + initialValue = 1f, |
| 143 | + targetValue = 0f, |
| 144 | + animationSpec = |
| 145 | + infiniteRepeatable( |
| 146 | + animation = tween(durationMillis = 1000, easing = LinearEasing), |
| 147 | + repeatMode = RepeatMode.Restart, |
| 148 | + ), |
| 149 | + ) |
| 150 | + |
| 151 | + val strokeWidthDp by infiniteTransition.animateValue( |
| 152 | + initialValue = 4.dp, |
| 153 | + targetValue = 0.dp, |
| 154 | + typeConverter = Dp.VectorConverter, |
| 155 | + animationSpec = |
| 156 | + infiniteRepeatable( |
| 157 | + animation = tween(1000, easing = LinearEasing), |
| 158 | + repeatMode = RepeatMode.Restart, |
| 159 | + ), |
| 160 | + ) |
| 161 | + |
| 162 | + val baseSize = 36.dp |
| 163 | + val strokeWidthPx = with(LocalDensity.current) { strokeWidthDp.toPx() } |
| 164 | + val sizePx = with(LocalDensity.current) { baseSize.toPx() } |
| 165 | + |
| 166 | + Canvas( |
| 167 | + modifier = |
| 168 | + modifier |
| 169 | + .size(baseSize) |
| 170 | + .graphicsLayer { |
| 171 | + scaleX = scale |
| 172 | + scaleY = scale |
| 173 | + this.alpha = alpha |
| 174 | + }, |
| 175 | + ) { |
| 176 | + drawCircle( |
| 177 | + color = color, |
| 178 | + radius = sizePx / 2 - strokeWidthPx / 2, |
| 179 | + style = Stroke(width = strokeWidthPx), |
| 180 | + ) |
| 181 | + } |
| 182 | +} |
0 commit comments