collect only the visible tab and split the position ticker from playback state

This commit is contained in:
austinried
2026-10-04 00:29:58 +00:00
parent 97ca4230da
commit 8429a4529f
11 changed files with 143 additions and 57 deletions
@@ -47,7 +47,6 @@ data class PlaybackState(
val error: String? = null,
val isBuffering: Boolean = false,
val isPlaying: Boolean = false,
val positionMs: Long = 0,
val durationMs: Long = 0,
val hasNext: Boolean = false,
val hasPrevious: Boolean = false,
@@ -104,6 +103,9 @@ class PlaybackController(
private val _state = MutableStateFlow(PlaybackState())
val state: StateFlow<PlaybackState> = _state
private val _positionMs = MutableStateFlow(0L)
val positionMs: StateFlow<Long> = _positionMs
private var player: PlayerHandle? = null
private var connecting = false
private var pendingPlay: PendingPlay? = null
@@ -690,6 +692,7 @@ class PlaybackController(
clear()
}
_state.value = PlaybackState()
_positionMs.value = 0L
stopPositionTicker()
}
@@ -920,6 +923,7 @@ class PlaybackController(
player.durationMs.takeIf { it > 0 }
?: player.currentItem?.durationMs?.takeIf { it > 0 }
?: 0L
_positionMs.value = player.currentPositionMs
_state.value =
PlaybackState(
item = player.currentItem,
@@ -928,7 +932,6 @@ class PlaybackController(
error = lastError,
isBuffering = showBuffering,
isPlaying = player.playWhenReady && !player.isIdle && !player.isEnded,
positionMs = player.currentPositionMs,
durationMs = durationMs,
hasNext = true,
hasPrevious = true,
@@ -946,7 +949,7 @@ class PlaybackController(
delay(POSITION_TICK_MS)
val player = player ?: break
val positionMs = player.currentPositionMs
_state.value = _state.value.copy(positionMs = positionMs)
_positionMs.value = positionMs
if (positionMs - lastSavedPositionMs >= POSITION_SAVE_INTERVAL_MS || positionMs < lastSavedPositionMs) {
lastSavedPositionMs = positionMs
queueRepository.setPosition(positionMs)
@@ -290,9 +290,11 @@ private fun MainNavigation() {
enter = expandVertically(tween(MINI_PLAYER_ANIM_MS), expandFrom = Alignment.Bottom),
exit = shrinkVertically(tween(MINI_PLAYER_ANIM_MS), shrinkTowards = Alignment.Bottom),
) {
val positionMs by playbackController.positionMs.collectAsStateWithLifecycle()
val miniArt = playbackController.coverArt(playback.item, thumbnail = true)
MiniPlayer(
state = playback,
positionMs = positionMs,
coverArt = miniArt,
artwork = artwork,
onExpand = { settleNowPlaying(true) },
@@ -99,7 +99,7 @@ import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.paging.compose.LazyPagingItems
import androidx.paging.PagingData
import androidx.paging.compose.collectAsLazyPagingItems
import com.subtracks.data.model.Album
import com.subtracks.data.model.Artist
@@ -119,6 +119,7 @@ import com.subtracks.ui.theme.ArtworkColors
import com.subtracks.ui.theme.librarySurfaceColor
import com.subtracks.ui.theme.playerSurfaceColor
import com.subtracks.ui.theme.rememberArtworkColors
import kotlinx.coroutines.flow.Flow
import org.koin.compose.koinInject
import org.koin.compose.viewmodel.koinViewModel
import kotlin.math.abs
@@ -219,10 +220,10 @@ fun LibraryRoute(
syncing = syncing,
bottomInset = bottomInset,
artwork = artwork,
albums = viewModel.albums.collectAsLazyPagingItems(),
artists = viewModel.artists.collectAsLazyPagingItems(),
songs = viewModel.songs.collectAsLazyPagingItems(),
playlists = viewModel.playlists.collectAsLazyPagingItems(),
albums = viewModel.albums,
artists = viewModel.artists,
songs = viewModel.songs,
playlists = viewModel.playlists,
coverArt = viewModel::coverArt,
onAlbumClick = onAlbumClick,
onArtistClick = onArtistClick,
@@ -260,10 +261,10 @@ fun LibraryRoute(
fun LibraryScreen(
selectedTab: LibraryTab,
onTabSelected: (LibraryTab) -> Unit,
albums: LazyPagingItems<Album>,
artists: LazyPagingItems<Artist>,
songs: LazyPagingItems<SongListItem>,
playlists: LazyPagingItems<Playlist>,
albums: Flow<PagingData<Album>>,
artists: Flow<PagingData<Artist>>,
songs: Flow<PagingData<SongListItem>>,
playlists: Flow<PagingData<Playlist>>,
coverArt: (String?, Boolean) -> CoverArtRef?,
onAlbumClick: (Album) -> Unit,
onArtistClick: (Artist) -> Unit,
@@ -356,7 +357,7 @@ fun LibraryScreen(
when (pageTab) {
LibraryTab.Albums -> {
AlbumsContent(
albums,
albums.collectAsLazyPagingItems(),
coverArt,
listBottomInset,
{
@@ -373,7 +374,7 @@ fun LibraryScreen(
LibraryTab.Artists -> {
ArtistsContent(
artists,
artists.collectAsLazyPagingItems(),
coverArt,
listBottomInset,
{
@@ -390,7 +391,7 @@ fun LibraryScreen(
LibraryTab.Songs -> {
SongsContent(
songs,
songs.collectAsLazyPagingItems(),
coverArt,
listBottomInset,
{
@@ -408,7 +409,7 @@ fun LibraryScreen(
LibraryTab.Playlists -> {
PlaylistsContent(
playlists,
playlists.collectAsLazyPagingItems(),
coverArt,
listBottomInset,
{
@@ -52,6 +52,7 @@ private const val PROGRESS_INSET_MS = 260
@Composable
fun MiniPlayer(
state: PlaybackState,
positionMs: Long,
coverArt: CoverArtRef?,
artwork: ArtworkColors?,
onExpand: () -> Unit,
@@ -64,7 +65,7 @@ fun MiniPlayer(
) {
val item = state.item ?: return
var dragUpPx by remember { mutableFloatStateOf(0f) }
val progress = if (state.durationMs > 0) (state.positionMs.toFloat() / state.durationMs).coerceIn(0f, 1f) else 0f
val progress = if (state.durationMs > 0) (positionMs.toFloat() / state.durationMs).coerceIn(0f, 1f) else 0f
val progressInsetDp by animateDpAsState(
targetValue = if (progressInset) PROGRESS_INSET else 0.dp,
animationSpec = tween(PROGRESS_INSET_MS),
@@ -80,6 +80,7 @@ fun NowPlayingRoute(
controller: PlaybackController = koinInject(),
) {
val state by controller.state.collectAsStateWithLifecycle()
val positionMs by controller.positionMs.collectAsStateWithLifecycle()
val context = LocalPlatformContext.current
val art = controller.coverArt(state.item)
val thumbnail = controller.coverArt(state.item, thumbnail = true)
@@ -102,6 +103,7 @@ fun NowPlayingRoute(
}
NowPlayingScreen(
state = state,
positionMs = positionMs,
title = sourceTitle.value,
coverArt = art,
thumbnailRef = thumbnail,
@@ -136,6 +138,7 @@ private fun prefetchImage(
@Composable
fun NowPlayingScreen(
state: PlaybackState,
positionMs: Long,
title: String,
coverArt: CoverArtRef?,
artwork: ArtworkColors?,
@@ -263,7 +266,7 @@ fun NowPlayingScreen(
var dragging by remember { mutableStateOf(false) }
var dragPosition by remember { mutableFloatStateOf(0f) }
Slider(
value = if (dragging) dragPosition else state.positionMs.toFloat(),
value = if (dragging) dragPosition else positionMs.toFloat(),
onValueChange = {
dragging = true
dragPosition = it
@@ -280,7 +283,7 @@ fun NowPlayingScreen(
modifier = Modifier.fillMaxWidth().padding(top = 10.dp),
horizontalArrangement = Arrangement.SpaceBetween,
) {
Text(formatTime(state.positionMs), style = MaterialTheme.typography.bodySmall)
Text(formatTime(positionMs), style = MaterialTheme.typography.bodySmall)
Text(formatTime(state.durationMs), style = MaterialTheme.typography.bodySmall)
}
if (state.error != null) {