Skip to content

Queue enhancement #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: concerto
Choose a base branch
from
Open
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
Expand Up @@ -55,6 +55,7 @@ val AlbumViewTypeKey = stringPreferencesKey("albumViewType")
val PlaylistViewTypeKey = stringPreferencesKey("playlistViewType")

val PlaylistEditLockKey = booleanPreferencesKey("playlistEditLock")
val QueueEditLockKey = booleanPreferencesKey("queueEditLock")

enum class LibraryViewType {
LIST, GRID;
Expand Down
195 changes: 195 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/menu/QueueListPlayerMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.zionhuang.music.ui.menu

import android.content.Intent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import androidx.media3.exoplayer.offline.DownloadRequest
import androidx.media3.exoplayer.offline.DownloadService
import androidx.navigation.NavController
import com.zionhuang.music.LocalDatabase
import com.zionhuang.music.LocalDownloadUtil
import com.zionhuang.music.R
import com.zionhuang.music.constants.ListItemHeight
import com.zionhuang.music.db.entities.PlaylistSongMap
import com.zionhuang.music.models.MediaMetadata
import com.zionhuang.music.playback.ExoDownloadService
import com.zionhuang.music.ui.component.BottomSheetState
import com.zionhuang.music.ui.component.DownloadGridMenu
import com.zionhuang.music.ui.component.GridMenu
import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import kotlin.math.log2
import kotlin.math.pow
import kotlin.math.round

@Composable
fun QueueListPlayerMenu(
mediaMetadata: MediaMetadata?,
navController: NavController,
playerBottomSheetState: BottomSheetState,
onDismiss: () -> Unit,
) {
mediaMetadata ?: return
val context = LocalContext.current
val database = LocalDatabase.current

val download by LocalDownloadUtil.current.getDownload(mediaMetadata.id).collectAsState(initial = null)

var showChoosePlaylistDialog by rememberSaveable {
mutableStateOf(false)
}

AddToPlaylistDialog(
isVisible = showChoosePlaylistDialog,
onAdd = { playlist ->
database.transaction {
insert(mediaMetadata)
insert(
PlaylistSongMap(
songId = mediaMetadata.id,
playlistId = playlist.id,
position = playlist.songCount
)
)
}
},
onDismiss = {
showChoosePlaylistDialog = false
}
)

var showSelectArtistDialog by rememberSaveable {
mutableStateOf(false)
}

if (showSelectArtistDialog) {
ListDialog(
onDismiss = { showSelectArtistDialog = false }
) {
items(mediaMetadata.artists) { artist ->
Box(
contentAlignment = Alignment.CenterStart,
modifier = Modifier
.fillParentMaxWidth()
.height(ListItemHeight)
.clickable {
navController.navigate("artist/${artist.id}")
showSelectArtistDialog = false
playerBottomSheetState.collapseSoft()
onDismiss()
}
.padding(horizontal = 24.dp),
) {
Text(
text = artist.name,
fontSize = 18.sp,
fontWeight = FontWeight.Bold,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
}
}

Divider()

GridMenu(
contentPadding = PaddingValues(
start = 8.dp,
top = 8.dp,
end = 8.dp,
bottom = 8.dp + WindowInsets.systemBars.asPaddingValues().calculateBottomPadding()
)
) {
GridMenuItem(
icon = R.drawable.playlist_add,
title = R.string.add_to_playlist
) {
showChoosePlaylistDialog = true
}
DownloadGridMenu(
state = download?.state,
onDownload = {
database.transaction {
insert(mediaMetadata)
}
val downloadRequest = DownloadRequest.Builder(mediaMetadata.id, mediaMetadata.id.toUri())
.setCustomCacheKey(mediaMetadata.id)
.setData(mediaMetadata.title.toByteArray())
.build()
DownloadService.sendAddDownload(
context,
ExoDownloadService::class.java,
downloadRequest,
false
)
},
onRemoveDownload = {
DownloadService.sendRemoveDownload(
context,
ExoDownloadService::class.java,
mediaMetadata.id,
false
)
}
)
GridMenuItem(
icon = R.drawable.artist,
title = R.string.view_artist
) {
if (mediaMetadata.artists.size == 1) {
navController.navigate("artist/${mediaMetadata.artists[0].id}")
playerBottomSheetState.collapseSoft()
onDismiss()
} else {
showSelectArtistDialog = true
}
}
if (mediaMetadata.album != null) {
GridMenuItem(
icon = R.drawable.album,
title = R.string.view_album
) {
navController.navigate("album/${mediaMetadata.album.id}")
playerBottomSheetState.collapseSoft()
onDismiss()
}
}
GridMenuItem(
icon = R.drawable.share,
title = R.string.share
) {
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "https://music.youtube.com/watch?v=${mediaMetadata.id}")
}
context.startActivity(Intent.createChooser(intent, null))
onDismiss()
}
}
}
Loading