Add track options menu to album rows
This commit is contained in:
1 parent
6b382003f2
commit
2ccfbff285
2 files changed
+95
-6
No files matched your search
@@ -187,7 +187,7 @@ struct AlbumDetailView: View {
|
|||||||
LazyVStack(spacing: 0) {
|
LazyVStack(spacing: 0) {
|
||||||
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
||||||
let track = viewModel.tracks[index]
|
let track = viewModel.tracks[index]
|
||||||
TrackRowView(track: track, index: index)
|
TrackRowView(track: track, index: index, hidesAlbumAction: true)
|
||||||
if index < viewModel.tracks.count - 1 {
|
if index < viewModel.tracks.count - 1 {
|
||||||
Divider()
|
Divider()
|
||||||
.padding(.leading, 42)
|
.padding(.leading, 42)
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct TrackRowView: View {
|
struct TrackRowView: View {
|
||||||
|
@EnvironmentObject private var player: PlayerManager
|
||||||
|
@State private var isSaved: Bool?
|
||||||
|
@State private var showingPlaylistNotice = false
|
||||||
|
@State private var actionError: String?
|
||||||
|
|
||||||
let track: Track
|
let track: Track
|
||||||
let index: Int
|
let index: Int
|
||||||
|
var hidesAlbumAction = false
|
||||||
var onPlay: (() -> Void)? = nil
|
var onPlay: (() -> Void)? = nil
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@@ -47,11 +53,64 @@ struct TrackRowView: View {
|
|||||||
|
|
||||||
Spacer(minLength: 8)
|
Spacer(minLength: 8)
|
||||||
|
|
||||||
Image(systemName: "ellipsis")
|
Menu {
|
||||||
.font(.body.weight(.semibold))
|
Button("Play Only This", systemImage: "play.fill") {
|
||||||
.foregroundStyle(.secondary)
|
player.play(track)
|
||||||
.frame(width: 32, height: 44)
|
}
|
||||||
.accessibilityHidden(true)
|
Button("Play Next", systemImage: "text.insert") {
|
||||||
|
player.playNext(track)
|
||||||
|
}
|
||||||
|
Button("Add to Queue", systemImage: "text.append") {
|
||||||
|
player.addToQueue(track)
|
||||||
|
}
|
||||||
|
Divider()
|
||||||
|
Button {
|
||||||
|
Task { await toggleLibrary() }
|
||||||
|
} label: {
|
||||||
|
Label(
|
||||||
|
isSaved == true ? "Remove from Library" : "Save to Library",
|
||||||
|
systemImage: isSaved == true ? "bookmark.slash" : "bookmark"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.disabled(isSaved == nil)
|
||||||
|
Button("Add to Playlist", systemImage: "text.badge.plus") {
|
||||||
|
showingPlaylistNotice = true
|
||||||
|
}
|
||||||
|
Divider()
|
||||||
|
if let artist = artists.first {
|
||||||
|
NavigationLink {
|
||||||
|
ArtistDetailView(artistID: artist.id)
|
||||||
|
} label: {
|
||||||
|
Label("Go to Artist", systemImage: "person")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hidesAlbumAction {
|
||||||
|
NavigationLink {
|
||||||
|
AlbumDetailView(albumID: albumID)
|
||||||
|
} label: {
|
||||||
|
Label("Go to Album", systemImage: "opticaldisc")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Image(systemName: "ellipsis")
|
||||||
|
.font(.body.weight(.semibold))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.frame(width: 44, height: 44)
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
}
|
||||||
|
.accessibilityLabel("Options for \(track.name)")
|
||||||
|
.task { await loadLibraryState() }
|
||||||
|
.alert("Playlists are not implemented", isPresented: $showingPlaylistNotice) {
|
||||||
|
Button("OK", role: .cancel) {}
|
||||||
|
}
|
||||||
|
.alert("Library Update Failed", isPresented: Binding(
|
||||||
|
get: { actionError != nil },
|
||||||
|
set: { if !$0 { actionError = nil } }
|
||||||
|
)) {
|
||||||
|
Button("OK", role: .cancel) { actionError = nil }
|
||||||
|
} message: {
|
||||||
|
Text(actionError ?? "")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.padding(.vertical, 8)
|
.padding(.vertical, 8)
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
@@ -64,11 +123,41 @@ struct TrackRowView: View {
|
|||||||
return artist
|
return artist
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var albumID: String {
|
||||||
|
guard case .value(let album) = track.album else { return "" }
|
||||||
|
return album.id
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadLibraryState() async {
|
||||||
|
do {
|
||||||
|
isSaved = try await MusicAPI().isTrackSaved(id: track.id)
|
||||||
|
} catch {
|
||||||
|
// Keep the menu available if Library status cannot be checked.
|
||||||
|
isSaved = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func toggleLibrary() async {
|
||||||
|
let api = MusicAPI()
|
||||||
|
do {
|
||||||
|
if isSaved == true {
|
||||||
|
try await api.removeTrack(id: track.id)
|
||||||
|
isSaved = false
|
||||||
|
} else {
|
||||||
|
try await api.saveTrack(id: track.id)
|
||||||
|
isSaved = true
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
actionError = error.localizedDescription
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TrackRowView_Previews: PreviewProvider {
|
struct TrackRowView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
TrackRowView(track: AlbumDetailViewModel.preview.tracks[0], index: 0)
|
TrackRowView(track: AlbumDetailViewModel.preview.tracks[0], index: 0)
|
||||||
|
.environmentObject(PlayerManager())
|
||||||
.padding(.horizontal)
|
.padding(.horizontal)
|
||||||
.previewLayout(.sizeThatFits)
|
.previewLayout(.sizeThatFits)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user