package com.cottondb.android.ui.meetings import androidx.compose.foundation.Canvas import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.width import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Path import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.unit.dp import com.cottondb.android.generated.CottonPalette import com.cottondb.android.work.meetings.MeetingPresentationFacts import 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]. */ @Composable fun 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 }