Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.siloserver.silo.common.player

import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.delay
import org.siloserver.silo.common.R
import org.siloserver.silo.common.i18n.currentFormatLocale
import org.siloserver.silo.common.i18n.localizedResources
import java.text.DateFormat
import java.util.Date

/** "11:42 PM": when [remainingSec] of media finishes at [speed], or null if nothing is left. */
private fun playerFinishTime(
nowMillis: Long,
remainingSec: Double,
speed: Double,
timeFormat: DateFormat,
): String? {
if (!remainingSec.isFinite() || remainingSec <= 0.0) return null
val rate = if (speed.isFinite() && speed > 0.0) speed else 1.0
return timeFormat.format(Date(nowMillis + (remainingSec / rate * 1000).toLong()))
}

/**
* "Ends 11:42 PM" in the formatting locale, with the device's 12/24-hour
* setting. The clock ticks on its own because the position stands still while
* paused, and a paused end time still has to move with the clock. The label
* is derived from the clock and the latest position, so a scrub updates it in
* the same frame, and the caller recomposes only when the text changes.
*/
@Composable
fun rememberPlayerEndTimeLabel(remainingSec: Double, speed: Double): String? {
val context = LocalContext.current
// Keyed on the configuration: the activity keeps its context across an
// in-place language change.
val configuration = LocalConfiguration.current
val formatLocale = currentFormatLocale()
val timeFormat = remember(context, configuration, formatLocale) {
val localized = context.createConfigurationContext(
android.content.res.Configuration(configuration).apply { setLocale(formatLocale) },
)
android.text.format.DateFormat.getTimeFormat(localized)
}
val resources = localizedResources()
val remaining = rememberUpdatedState(remainingSec)
val rate = rememberUpdatedState(speed)
val now = produceState(System.currentTimeMillis()) {
while (true) {
delay(1_000)
value = System.currentTimeMillis()
}
}
val endTimeLabel by remember(timeFormat, resources) {
derivedStateOf {
playerFinishTime(now.value, remaining.value, rate.value, timeFormat)
?.let { resources.getString(R.string.player_scrubber_endsAt, it) }
}
}
return endTimeLabel
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ fun PlayerControls(
},
thumbnail = scrubThumbnail,
onScrubTarget = onScrubTarget,
playbackSpeed = playbackSpeed,
)
PlayerActionPillRow(
hasTracks = hasTracks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ fun PlayerOverlay(
isOrientationLocked = isOrientationLocked,
orientationLockSupported = orientationLockSupported,
tabletopMode = tabletopMode,
playbackSpeed = playbackSpeed,
// A party plays at 1x; the saved speed is never changed from it.
playbackSpeed = if (inRoom) 1.0 else playbackSpeed,
playbackSpeedEnabled = !inRoom,
// A shuffle replaces the series order: no sequential next episode.
nextEpisode = state.nextEpisode.takeUnless { inRoom || state.shuffle != null },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import kotlin.math.roundToInt
import org.siloserver.silo.android.ui.components.LeftToRight
import org.siloserver.silo.android.ui.util.formatClockTime
import org.siloserver.silo.common.i18n.LocaleFormat
import org.siloserver.silo.common.i18n.isRightToLeftText
import org.siloserver.silo.common.player.rememberPlayerEndTimeLabel
import org.siloserver.silo.model.catalog.TimeRange
import org.siloserver.silo.model.catalog.VersionChapter
import org.siloserver.silo.common.R as CommonR
Expand Down Expand Up @@ -102,13 +104,14 @@ fun PlayerProgressBar(
onScrubbingChange: (Boolean) -> Unit = {},
thumbnail: ImageBitmap? = null,
onScrubTarget: (Double?) -> Unit = {},
playbackSpeed: Double = 1.0,
) {
// A timeline runs left to right in every language: the bar is drawn and
// hit-tested from the left edge, so its times sit the same way round.
LeftToRight {
ProgressBarContent(
position, duration, onSeek, modifier, bufferedPosition, enabled, chapters, intro, credits, recap,
preview, showTimes, onScrubbingChange, thumbnail, onScrubTarget,
preview, showTimes, onScrubbingChange, thumbnail, onScrubTarget, playbackSpeed,
)
}
}
Expand All @@ -130,6 +133,7 @@ private fun ProgressBarContent(
onScrubbingChange: (Boolean) -> Unit,
thumbnail: ImageBitmap?,
onScrubTarget: (Double?) -> Unit,
playbackSpeed: Double,
) {
var isSeeking by remember { mutableStateOf(false) }
var seekPosition by remember { mutableDoubleStateOf(0.0) }
Expand Down Expand Up @@ -171,6 +175,10 @@ private fun ProgressBarContent(

fun chapterIndexAt(seconds: Double): Int = latestChapters.indexOfLast { it.startSeconds <= seconds }

val endTimeLabel = rememberPlayerEndTimeLabel(
remainingSec = if (hasKnownDuration) duration - displayPosition else 0.0,
speed = playbackSpeed,
)
val positionDescription = stringResource(CommonR.string.player_progress_position)
// Formatted inside semantics, as before, rather than on every position tick.
val resources = LocalResources.current
Expand All @@ -189,12 +197,20 @@ private fun ProgressBarContent(
modifier = Modifier.weight(1f),
)
// Tap to switch between time left and total length (iOS).
val time = if (showTotal && hasKnownDuration) {
formatClockTime(duration)
} else {
remainingTimeLabel(displayPosition, duration)
}
// Beside a right-to-left "Ends" label the "−" would move to
// the far end of the time, so the time keeps its own run.
val isolatedTime = if (endTimeLabel != null && isRightToLeftText()) {
"\u2066$time\u2069" // i18n-ignore: isolate marks
} else {
time
}
Text(
text = if (showTotal && hasKnownDuration) {
formatClockTime(duration)
} else {
remainingTimeLabel(displayPosition, duration)
},
text = listOfNotNull(isolatedTime, endTimeLabel).joinToString(" · "),
style = PlayerType.Time.copy(color = PlayerChrome.Graphite),
modifier = Modifier.clickable(
interactionSource = remember { MutableInteractionSource() },
Expand Down Expand Up @@ -316,7 +332,9 @@ private fun ProgressBarContent(
val delta = seekPosition - scrubStart
val chapterTitle = chapterTitleAt(chapters, seekPosition)
// A preview grows the bubble upward: its bottom edge stays put.
val lift = if (thumbnail != null) 52.dp + ScrubThumbnailBlockHeight / 2 else 52.dp
// The end time line does the same.
val lift = (if (thumbnail != null) 52.dp + ScrubThumbnailBlockHeight / 2 else 52.dp) +
(if (endTimeLabel != null) 9.dp else 0.dp)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
Expand Down Expand Up @@ -362,6 +380,7 @@ private fun ProgressBarContent(
.padding(top = 2.dp)
.widthIn(max = 220.dp),
)
endTimeLabel?.let { Text(text = it, style = PlayerType.BubbleDetail, maxLines = 1) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import androidx.compose.foundation.border
import androidx.compose.foundation.Image
import androidx.compose.foundation.Canvas
import androidx.compose.ui.graphics.drawscope.clipRect
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import org.siloserver.silo.common.R as CommonR
import android.os.SystemClock
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -67,14 +63,14 @@ import androidx.compose.ui.unit.dp
import androidx.tv.material3.Icon
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.Text
import org.siloserver.silo.common.player.rememberPlayerEndTimeLabel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlin.math.abs
import kotlin.math.roundToInt
import org.siloserver.silo.common.i18n.LocaleFormat
import org.siloserver.silo.common.i18n.currentFormatLocale
import org.siloserver.silo.common.i18n.isRightToLeftText

/**
Expand Down Expand Up @@ -279,18 +275,6 @@ fun TvPlayerScrubber(

// The bar sits above its clock row, as on tvOS: elapsed on the left, time
// left and the wall-clock finish on the right.
val context = LocalContext.current
// In the formatting locale, with the device's 12/24-hour setting. Keyed on
// the configuration: the activity keeps its context across an in-place
// language change.
val configuration = LocalConfiguration.current
val formatLocale = currentFormatLocale()
val timeFormat = remember(context, configuration, formatLocale) {
val localized = context.createConfigurationContext(
android.content.res.Configuration(configuration).apply { setLocale(formatLocale) },
)
android.text.format.DateFormat.getTimeFormat(localized)
}
Column(modifier = modifier) {
Box(modifier = Modifier.fillMaxWidth().height(28.dp)) {

Expand Down Expand Up @@ -543,17 +527,15 @@ fun TvPlayerScrubber(
modifier = Modifier.weight(1f),
)
val remainingSec = (durationSec - labelPositionSec).coerceAtLeast(0.0)
val wallClockRemainingSec = if (playbackSpeed > 0.0) remainingSec / playbackSpeed else remainingSec
val endTimeLabel = rememberPlayerEndTimeLabel(remainingSec, playbackSpeed)
// In an Arabic sentence the "−" would move to the far end of the
// time, so right-to-left text keeps the time in its own run.
val remaining = formatRemainingTime(remainingSec)
.let { if (isRightToLeftText()) "\u2066$it\u2069" else it } // i18n-ignore: isolate marks
Text(
text = listOfNotNull(
remaining,
finishTimeLabel(timeFormat, wallClockRemainingSec)?.let {
stringResource(CommonR.string.player_scrubber_endsAt, it)
},
endTimeLabel,
).joinToString(" · "),
style = TvPlayerType.Time.copy(color = TvPlayerChrome.Graphite),
)
Expand Down Expand Up @@ -600,13 +582,6 @@ private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawTvSegmentedTrac
}
}

/** "11:42 PM" in the device's 12/24-hour [timeFormat], [remainingSec] of wall-clock time from now. */
private fun finishTimeLabel(timeFormat: java.text.DateFormat, remainingSec: Double): String? {
if (remainingSec <= 0.0 || !remainingSec.isFinite()) return null
val finish = java.util.Date(System.currentTimeMillis() + (remainingSec * 1000).toLong())
return timeFormat.format(finish)
}

@Composable
private fun TvScrubberAutoSeekChip(rate: Int, modifier: Modifier = Modifier) {
Row(
Expand Down
Loading