mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-11 15:29:15 +01:00
fix the now playing overlay animation and state
This commit is contained in:
@@ -56,6 +56,7 @@ import com.subtracks.ui.playback.QueueRoute
|
|||||||
import com.subtracks.ui.settings.AddSourceRoute
|
import com.subtracks.ui.settings.AddSourceRoute
|
||||||
import com.subtracks.ui.settings.SettingsRoute
|
import com.subtracks.ui.settings.SettingsRoute
|
||||||
import com.subtracks.ui.theme.rememberArtworkColors
|
import com.subtracks.ui.theme.rememberArtworkColors
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
@@ -76,6 +77,7 @@ class RootViewModel(
|
|||||||
private const val NAVIGATION_DURATION_MS = 260
|
private const val NAVIGATION_DURATION_MS = 260
|
||||||
private const val OVERLAY_DURATION_MS = 200
|
private const val OVERLAY_DURATION_MS = 200
|
||||||
private const val EXPAND_FADE = 0.1f
|
private const val EXPAND_FADE = 0.1f
|
||||||
|
private const val FLING_VELOCITY = 1000f
|
||||||
|
|
||||||
private object Routes {
|
private object Routes {
|
||||||
const val LIBRARY = "library"
|
const val LIBRARY = "library"
|
||||||
@@ -113,21 +115,34 @@ private fun MainNavigation() {
|
|||||||
val playbackController = koinInject<PlaybackController>()
|
val playbackController = koinInject<PlaybackController>()
|
||||||
val playback by playbackController.state.collectAsStateWithLifecycle()
|
val playback by playbackController.state.collectAsStateWithLifecycle()
|
||||||
var showingQueue by rememberSaveable { mutableStateOf(false) }
|
var showingQueue by rememberSaveable { mutableStateOf(false) }
|
||||||
|
var nowPlayingOpen by rememberSaveable { mutableStateOf(false) }
|
||||||
var nowPlayingProgress by remember { mutableFloatStateOf(0f) }
|
var nowPlayingProgress by remember { mutableFloatStateOf(0f) }
|
||||||
var miniPlayerTopPx by remember { mutableFloatStateOf(0f) }
|
var miniPlayerTopPx by remember { mutableFloatStateOf(0f) }
|
||||||
|
var settleJob by remember { mutableStateOf<Job?>(null) }
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
fun settleNowPlaying(open: Boolean) {
|
fun settleNowPlaying(open: Boolean) {
|
||||||
val target = if (open) 1f else 0f
|
if (open) nowPlayingOpen = true
|
||||||
|
settleJob?.cancel()
|
||||||
|
settleJob =
|
||||||
scope.launch {
|
scope.launch {
|
||||||
animate(nowPlayingProgress, target, animationSpec = tween(OVERLAY_DURATION_MS)) { value, _ ->
|
animate(nowPlayingProgress, if (open) 1f else 0f, animationSpec = tween(OVERLAY_DURATION_MS)) { value, _ ->
|
||||||
nowPlayingProgress = value
|
nowPlayingProgress = value
|
||||||
}
|
}
|
||||||
|
if (!open) nowPlayingOpen = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LaunchedEffect(Unit) { playbackController.connect() }
|
LaunchedEffect(Unit) { playbackController.connect() }
|
||||||
|
LaunchedEffect(Unit) { if (nowPlayingOpen) nowPlayingProgress = 1f }
|
||||||
|
|
||||||
val playerVisible = playback.item != null
|
val playerVisible = playback.item != null
|
||||||
|
LaunchedEffect(playerVisible) {
|
||||||
|
if (!playerVisible) {
|
||||||
|
settleJob?.cancel()
|
||||||
|
nowPlayingOpen = false
|
||||||
|
nowPlayingProgress = 0f
|
||||||
|
}
|
||||||
|
}
|
||||||
val density = LocalDensity.current
|
val density = LocalDensity.current
|
||||||
val navBarInset = with(density) { WindowInsets.navigationBars.getBottom(this).toDp() }
|
val navBarInset = with(density) { WindowInsets.navigationBars.getBottom(this).toDp() }
|
||||||
|
|
||||||
@@ -248,17 +263,19 @@ private fun MainNavigation() {
|
|||||||
onPlayPause = playbackController::togglePlayPause,
|
onPlayPause = playbackController::togglePlayPause,
|
||||||
onNext = playbackController::next,
|
onNext = playbackController::next,
|
||||||
onExpandDrag = { dragUpPx ->
|
onExpandDrag = { dragUpPx ->
|
||||||
|
nowPlayingOpen = true
|
||||||
|
settleJob?.cancel()
|
||||||
if (miniPlayerTopPx > 0f) {
|
if (miniPlayerTopPx > 0f) {
|
||||||
nowPlayingProgress = (dragUpPx / miniPlayerTopPx).coerceIn(0f, 1f)
|
nowPlayingProgress = (dragUpPx / miniPlayerTopPx).coerceIn(0f, 1f)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onExpandRelease = { settleNowPlaying(nowPlayingProgress > 0.05f) },
|
onExpandRelease = { velocity -> settleNowPlaying(nowPlayingProgress > 0.05f || velocity < -FLING_VELOCITY) },
|
||||||
modifier = Modifier.onGloballyPositioned { miniPlayerTopPx = it.positionInRoot().y },
|
modifier = Modifier.onGloballyPositioned { miniPlayerTopPx = it.positionInRoot().y },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nowPlayingProgress > 0f) {
|
if (nowPlayingOpen) {
|
||||||
Box(
|
Box(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
@@ -270,11 +287,12 @@ private fun MainNavigation() {
|
|||||||
orientation = Orientation.Vertical,
|
orientation = Orientation.Vertical,
|
||||||
state =
|
state =
|
||||||
rememberDraggableState { delta ->
|
rememberDraggableState { delta ->
|
||||||
|
settleJob?.cancel()
|
||||||
if (miniPlayerTopPx > 0f) {
|
if (miniPlayerTopPx > 0f) {
|
||||||
nowPlayingProgress = (nowPlayingProgress - delta / miniPlayerTopPx).coerceIn(0f, 1f)
|
nowPlayingProgress = (nowPlayingProgress - delta / miniPlayerTopPx).coerceIn(0f, 1f)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onDragStopped = { settleNowPlaying(nowPlayingProgress > 0.9f) },
|
onDragStopped = { velocity -> settleNowPlaying(nowPlayingProgress > 0.9f && velocity < FLING_VELOCITY) },
|
||||||
),
|
),
|
||||||
) {
|
) {
|
||||||
NowPlayingRoute(
|
NowPlayingRoute(
|
||||||
@@ -298,7 +316,7 @@ private fun MainNavigation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BackHandler(enabled = showingQueue) { showingQueue = false }
|
BackHandler(enabled = showingQueue) { showingQueue = false }
|
||||||
BackHandler(enabled = nowPlayingProgress > 0f && !showingQueue) {
|
BackHandler(enabled = nowPlayingOpen && !showingQueue) {
|
||||||
settleNowPlaying(false)
|
settleNowPlaying(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ fun MiniPlayer(
|
|||||||
onPlayPause: () -> Unit,
|
onPlayPause: () -> Unit,
|
||||||
onNext: () -> Unit,
|
onNext: () -> Unit,
|
||||||
onExpandDrag: (Float) -> Unit = {},
|
onExpandDrag: (Float) -> Unit = {},
|
||||||
onExpandRelease: () -> Unit = {},
|
onExpandRelease: (Float) -> Unit = {},
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
val item = state.item ?: return
|
val item = state.item ?: return
|
||||||
@@ -76,8 +76,8 @@ fun MiniPlayer(
|
|||||||
dragUpPx = (dragUpPx - delta).coerceAtLeast(0f)
|
dragUpPx = (dragUpPx - delta).coerceAtLeast(0f)
|
||||||
onExpandDrag(dragUpPx)
|
onExpandDrag(dragUpPx)
|
||||||
},
|
},
|
||||||
onDragStopped = {
|
onDragStopped = { velocity ->
|
||||||
onExpandRelease()
|
onExpandRelease(velocity)
|
||||||
dragUpPx = 0f
|
dragUpPx = 0f
|
||||||
},
|
},
|
||||||
).clickable(
|
).clickable(
|
||||||
|
|||||||
Reference in New Issue
Block a user