165 lines
5.7 KiB
Swift
165 lines
5.7 KiB
Swift
import SwiftUI
|
|
|
|
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 index: Int
|
|
var hidesAlbumAction = false
|
|
var onPlay: (() -> Void)? = nil
|
|
|
|
var body: some View {
|
|
HStack(spacing: 14) {
|
|
if let onPlay {
|
|
Button(action: onPlay) {
|
|
Image(systemName: "play.fill")
|
|
.font(.caption.weight(.semibold))
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: 28, height: 44)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.accessibilityLabel("Play \(track.name)")
|
|
} else {
|
|
Text("\(index + 1)")
|
|
.font(.body.monospacedDigit())
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: 28, alignment: .trailing)
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 3) {
|
|
Text(track.name)
|
|
.font(.body)
|
|
.foregroundStyle(.primary)
|
|
.lineLimit(1)
|
|
if !artists.isEmpty {
|
|
HStack(spacing: 4) {
|
|
ForEach(artists) { artist in
|
|
NavigationLink(artist.name) {
|
|
ArtistDetailView(artistID: artist.id)
|
|
}
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
if artist.id != artists.last?.id {
|
|
Text(",").font(.caption).foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
}
|
|
.lineLimit(1)
|
|
}
|
|
}
|
|
|
|
Spacer(minLength: 8)
|
|
|
|
Menu {
|
|
Button("Play Only This", systemImage: "play.fill") {
|
|
player.play(track)
|
|
}
|
|
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)
|
|
.contentShape(Rectangle())
|
|
.accessibilityElement(children: .combine)
|
|
}
|
|
|
|
private var artists: [Artist] {
|
|
track.artists.compactMap { reference in
|
|
guard case .value(let artist) = reference else { return nil }
|
|
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 {
|
|
static var previews: some View {
|
|
TrackRowView(track: AlbumDetailViewModel.preview.tracks[0], index: 0)
|
|
.environmentObject(PlayerManager())
|
|
.padding(.horizontal)
|
|
.previewLayout(.sizeThatFits)
|
|
}
|
|
}
|