Skip to content

Commit

Permalink
merge pr #61 from @Ericgacoki
Browse files Browse the repository at this point in the history
Fix `Play Next` When Queue Is Empty
  • Loading branch information
cwilvx authored Dec 31, 2024
2 parents a837288 + 396b4dc commit eec4843
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -180,7 +178,10 @@ class MainActivity : ComponentActivity() {
"album/${ViewAllScreenDestination.route}",
"artist/${ViewAllScreenDestination.route}",

// Hide mini player in queue screen
"folder/${QueueScreenDestination.route}",
"album/${QueueScreenDestination.route}",
"artist/${QueueScreenDestination.route}",
))
) {
MiniPlayer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
Expand All @@ -52,7 +53,6 @@ import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -77,7 +77,6 @@ import androidx.compose.ui.tooling.preview.Wallpapers
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.android.swingmusic.album.presentation.event.AlbumWithInfoUiEvent
Expand Down Expand Up @@ -679,8 +678,6 @@ fun AlbumWithInfoScreen(
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()

val snackbarEvent by mediaControllerViewModel.snackbarEvent.collectAsStateWithLifecycle()

LaunchedEffect(
key1 = albumWithInfoState.albumHash,
key2 = albumWithInfoState.reloadRequired
Expand All @@ -692,10 +689,6 @@ fun AlbumWithInfoScreen(
}
}

SideEffect {
mediaControllerViewModel.onQueueEvent(QueueEvent.HideSnackbar)
}

SwingMusicTheme {
Scaffold(
snackbarHost = {
Expand Down Expand Up @@ -854,21 +847,50 @@ fun AlbumWithInfoScreen(

is BottomSheetAction.PlayNext -> {
mediaControllerViewModel.onQueueEvent(
QueueEvent.PlayNext(track)
QueueEvent.PlayNext(
track = track,
source = QueueSource.ALBUM(
albumHash = albumHash,
name = albumWithInfoState.infoResource.data?.albumInfo?.title
?: "Album"
)
)
)

scope.launch {
val result = snackbarHostState.showSnackbar(
message = "Track added to play next",
actionLabel = "View Queue",
duration = SnackbarDuration.Short
)
if (result == SnackbarResult.ActionPerformed) {
navigator.gotoQueueScreen()
}
}
}

is BottomSheetAction.AddToQueue -> {
mediaControllerViewModel.onQueueEvent(
QueueEvent.AddToQueue(track)
QueueEvent.AddToQueue(
track = track,
source = QueueSource.ALBUM(
albumHash = albumHash,
name = albumWithInfoState.infoResource.data?.albumInfo?.title
?: "Album"
)
)
)

mediaControllerViewModel.onQueueEvent(
QueueEvent.ShowSnackbar(
msg = "Track added to playing queue",
actionLabel = "View Queue"
scope.launch {
val result = snackbarHostState.showSnackbar(
message = "Track added to playing queue",
actionLabel = "View Queue",
duration = SnackbarDuration.Short
)
)
if (result == SnackbarResult.ActionPerformed) {
navigator.gotoQueueScreen()
}
}
}

else -> {}
Expand All @@ -883,30 +905,6 @@ fun AlbumWithInfoScreen(
)
}
}

LaunchedEffect(snackbarEvent) {
snackbarEvent?.let { event ->
scope.launch {
val result = snackbarHostState.showSnackbar(
message = event.message,
actionLabel = event.actionLabel,
duration = event.duration
)

when (result) {
SnackbarResult.ActionPerformed -> {
navigator.gotoQueueScreen()
}

SnackbarResult.Dismissed -> {}

else -> {}
}

mediaControllerViewModel.onQueueEvent(QueueEvent.HideSnackbar)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
Expand All @@ -42,7 +43,6 @@ import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -61,7 +61,6 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.android.swingmusic.artist.presentation.event.ArtistInfoUiEvent
Expand Down Expand Up @@ -200,7 +199,7 @@ private fun ArtistInfo(
onChooseArtist = { hash ->
onGotoArtist(hash)
},
onToggleTrackFavorite = {isFavorite, trackHash ->
onToggleTrackFavorite = { isFavorite, trackHash ->
onToggleTrackFavorite(isFavorite, trackHash)
}
)
Expand Down Expand Up @@ -852,8 +851,6 @@ fun ArtistInfoScreen(
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()

val snackbarEvent by mediaControllerViewModel.snackbarEvent.collectAsStateWithLifecycle()

LaunchedEffect(key1 = Unit) {
if (artistInfoState.value.requiresReload || loadNewArtist) {
if (currentArtistHash != artistHash) {
Expand All @@ -864,10 +861,6 @@ fun ArtistInfoScreen(
}
}

SideEffect {
mediaControllerViewModel.onQueueEvent(QueueEvent.HideSnackbar)
}

SwingMusicTheme {
Scaffold(
snackbarHost = {
Expand Down Expand Up @@ -1046,22 +1039,49 @@ fun ArtistInfoScreen(
is BottomSheetAction.PlayNext -> {
mediaControllerViewModel.onQueueEvent(
QueueEvent.PlayNext(
track
track = track,
source = QueueSource.ARTIST(
artistHash,
artistInfoState.value.infoResource.data?.artist?.name
?: "Artist"
)
)
)

scope.launch {
val result = snackbarHostState.showSnackbar(
message = "Track added to play next",
actionLabel = "View Queue",
duration = SnackbarDuration.Short
)
if (result == SnackbarResult.ActionPerformed) {
commonNavigator.gotoQueueScreen()
}
}
}

is BottomSheetAction.AddToQueue -> {
mediaControllerViewModel.onQueueEvent(
QueueEvent.AddToQueue(track)
QueueEvent.AddToQueue(
track = track,
source = QueueSource.ARTIST(
artistHash,
artistInfoState.value.infoResource.data?.artist?.name
?: "Artist"
)
)
)

mediaControllerViewModel.onQueueEvent(
QueueEvent.ShowSnackbar(
msg = "Track added to playing queue",
actionLabel = "View Queue"
scope.launch {
val result = snackbarHostState.showSnackbar(
message = "Track added to playing queue",
actionLabel = "View Queue",
duration = SnackbarDuration.Short
)
)
if (result == SnackbarResult.ActionPerformed) {
commonNavigator.gotoQueueScreen()
}
}
}

else -> {}
Expand All @@ -1078,30 +1098,6 @@ fun ArtistInfoScreen(
)
}
}

LaunchedEffect(snackbarEvent) {
snackbarEvent?.let { event ->
scope.launch {
val result = snackbarHostState.showSnackbar(
message = event.message,
actionLabel = event.actionLabel,
duration = event.duration
)

when (result) {
SnackbarResult.ActionPerformed -> {
commonNavigator.gotoQueueScreen()
}

SnackbarResult.Dismissed -> {}

else -> {}
}

mediaControllerViewModel.onQueueEvent(QueueEvent.HideSnackbar)
}
}
}
}
}
}
Expand Down Expand Up @@ -1503,7 +1499,7 @@ fun ArtistInfoPreview() {
onClickViewAll = { _, _, _ -> },
onGetSheetAction = { _, _ -> },
onGotoArtist = {},
onToggleTrackFavorite = {_, _ ->}
onToggleTrackFavorite = { _, _ -> }
)
}
}
Expand Down
Loading

0 comments on commit eec4843

Please sign in to comment.