Cotton code
194 lines · 7.9 KB · KotlinRaw
package com.cottondb.android.ui.meetingsimport androidx.compose.foundation.Canvasimport androidx.compose.foundation.clickableimport androidx.compose.foundation.gestures.detectTapGesturesimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.widthimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.runtime.rememberUpdatedStateimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Pathimport androidx.compose.ui.input.pointer.pointerInputimport androidx.compose.ui.platform.LocalDensityimport androidx.compose.ui.semantics.Roleimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.unit.dpimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.work.meetings.MeetingPresentationFactsimport com.cottondb.android.work.meetings.MeetingWaveformFacts/** * Controlled meeting scrubber. Waveform touches move the playhead silently; a transcript action * is the only interaction that calls [AndroidMeetingPlayerController.playFromTranscript]. */@Composablefun MeetingPlayerStrip(    meetingId: String,    state: MeetingPlaybackState,    durationHintSeconds: Double?,    unavailableNote: String?,    onToggle: () -> Unit,    onSeekFraction: (Float) -> Unit,    onRetry: (() -> Unit)? = null,    modifier: Modifier = Modifier,) {    val latestToggle by rememberUpdatedState(onToggle)    val latestSeek by rememberUpdatedState(onSeekFraction)    val latestRetry by rememberUpdatedState(onRetry)    val duration = durationMs(state, durationHintSeconds)    val position = positionMs(state)    val note = unavailableNote ?: (state as? MeetingPlaybackState.Failed)?.message    val playable = note == null && (        state is MeetingPlaybackState.Paused ||            state is MeetingPlaybackState.Playing ||            state is MeetingPlaybackState.Completed        )    val playing = state is MeetingPlaybackState.Playing    val label = if (playing) "Pause" else "Play"    Row(        modifier = modifier,        verticalAlignment = Alignment.Bottom,    ) {        Canvas(            modifier = Modifier                .width(50.dp)                .height(50.dp)                .semantics { contentDescription = label }                .clickable(                    enabled = playable,                    onClickLabel = label,                    role = Role.Button,                ) {                    latestToggle()                },        ) {            val alpha = if (playable) 1f else .4f            drawCircle(                color = CottonPalette.inkSecondary.copy(alpha = .16f * alpha),                radius = size.minDimension / 2f,            )            if (playing) {                val barWidth = size.width * .12f                drawRect(                    color = CottonPalette.ink.copy(alpha = alpha),                    topLeft = androidx.compose.ui.geometry.Offset(size.width * .34f, size.height * .32f),                    size = androidx.compose.ui.geometry.Size(barWidth, size.height * .36f),                )                drawRect(                    color = CottonPalette.ink.copy(alpha = alpha),                    topLeft = androidx.compose.ui.geometry.Offset(size.width * .54f, size.height * .32f),                    size = androidx.compose.ui.geometry.Size(barWidth, size.height * .36f),                )            } else {                val triangle = Path().apply {                    moveTo(size.width * .43f, size.height * .33f)                    lineTo(size.width * .43f, size.height * .67f)                    lineTo(size.width * .69f, size.height * .5f)                    close()                }                drawPath(triangle, CottonPalette.ink, alpha = alpha)            }        }        Box(            modifier = Modifier                .weight(1f)                .height(66.dp)                .semantics { contentDescription = "Recording" },        ) {            val density = LocalDensity.current            Canvas(                modifier = Modifier                    .fillMaxWidth()                    .height(46.dp)                    .align(Alignment.BottomStart)                    .pointerInput(playable) {                        detectTapGestures { offset ->                            if (playable && size.width > 0) {                                latestSeek((offset.x / size.width).coerceIn(0f, 1f))                            }                        }                    },            ) {                // Player source uses a four-point pitch and .42 bar fraction.                val pitch = with(density) { 4.dp.toPx().toDouble() }                val bars = MeetingWaveformFacts.bars(                    MeetingWaveformFacts.samples(meetingId),                    MeetingWaveformFacts.barCount(size.width.toDouble(), pitch),                )                val fraction = if (duration > 0) position.toFloat() / duration else 0f                val barWidth = if (bars.isEmpty()) 0f else size.width / bars.size                bars.forEachIndexed { index, sample ->                    val height = size.height * sample.toFloat()                    val color = if (playable && index * barWidth <= size.width * fraction) {                        CottonPalette.ink                    } else {                        CottonPalette.inkSecondary.copy(alpha = if (note == null) .7f else .35f)                    }                    drawRect(                        color = color,                        topLeft = androidx.compose.ui.geometry.Offset(                            x = index * barWidth + barWidth * .29f,                            y = (size.height - height) / 2f,                        ),                        size = androidx.compose.ui.geometry.Size(barWidth * .42f, height),                    )                }                if (playable) {                    val x = size.width * fraction                    drawLine(                        color = CottonPalette.ink,                        start = androidx.compose.ui.geometry.Offset(x, 0f),                        end = androidx.compose.ui.geometry.Offset(x, size.height),                        strokeWidth = 1.2f,                    )                    drawCircle(                        color = CottonPalette.ink,                        radius = 3f,                        center = androidx.compose.ui.geometry.Offset(x, 0f),                    )                }            }            Text(                text = note ?: MeetingPresentationFacts.clock(position.toDouble()),                color = CottonPalette.inkSecondary,                modifier = Modifier.align(Alignment.TopStart),            )            if (note != null && latestRetry != null) {                TextButton(                    onClick = { latestRetry?.invoke() },                    modifier = Modifier.align(Alignment.TopEnd),                ) {                    Text("Try again")                }            }        }    }}private fun durationMs(state: MeetingPlaybackState, hintSeconds: Double?): Int = when (state) {    is MeetingPlaybackState.Paused -> state.durationMs    is MeetingPlaybackState.Playing -> state.durationMs    is MeetingPlaybackState.Completed -> state.durationMs    else -> ((hintSeconds ?: 0.0) * 1_000.0)        .coerceIn(0.0, Int.MAX_VALUE.toDouble())        .toInt()}private fun positionMs(state: MeetingPlaybackState): Int = when (state) {    is MeetingPlaybackState.Paused -> state.positionMs    is MeetingPlaybackState.Playing -> state.positionMs    is MeetingPlaybackState.Completed -> state.durationMs    else -> 0}