Files
MobileMusicAssistant/Mobile Music Assistant/ViewsLibraryAlbumDetailView.swift
T
2026-04-05 19:44:30 +02:00

454 lines
16 KiB
Swift

//
// AlbumDetailView.swift
// Mobile Music Assistant
//
// Created by Sven Hanold on 26.03.26.
//
import SwiftUI
struct AlbumDetailView: View {
@Environment(MAService.self) private var service
let album: MAAlbum
@State private var tracks: [MAMediaItem] = []
@State private var isLoading = true
@State private var errorMessage: String?
@State private var showError = false
@State private var showPlayerPicker = false
@State private var selectedPlayer: MAPlayer?
@State private var kenBurnsScale: CGFloat = 1.0
@State private var completeAlbum: MAAlbum?
@State private var albumDescription: String?
@State private var isDescriptionExpanded = false
private var players: [MAPlayer] {
Array(service.playerManager.players.values)
.filter { $0.available }
.sorted { $0.name < $1.name }
}
var body: some View {
ZStack {
// Blurred Background with Ken Burns Effect
backgroundArtwork
// Content
ScrollView {
VStack(spacing: 24) {
// Album Header
albumHeader
// Play Button
playButton
Divider()
.background(Color.white.opacity(0.3))
// Album description
if let albumDescription {
descriptionSection(albumDescription)
}
// Tracklist
if isLoading {
ProgressView()
.padding()
.tint(.white)
} else if tracks.isEmpty {
Text("No tracks found")
.foregroundStyle(.white.opacity(0.7))
.padding()
} else {
trackList
}
// Show when the album came from a provider-specific URI and the
// full library version is available
if let completeAlbum {
NavigationLink(value: completeAlbum) {
Label("Show complete album", systemImage: "music.note.list")
.font(.subheadline.bold())
.foregroundStyle(.white.opacity(0.85))
.frame(maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color.white.opacity(0.1))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.white.opacity(0.2), lineWidth: 1)
)
)
}
.padding(.horizontal)
.padding(.bottom, 8)
}
}
}
}
.navigationTitle(album.name)
.navigationBarTitleDisplayMode(.inline)
.toolbarColorScheme(.dark, for: .navigationBar)
.task {
async let tracksLoad: () = loadTracks()
async let detailLoad: () = loadAlbumDetail()
_ = await (tracksLoad, detailLoad)
withAnimation(.linear(duration: 20).repeatForever(autoreverses: true)) {
kenBurnsScale = 1.15
}
}
.alert("Error", isPresented: $showError) {
Button("OK", role: .cancel) { }
} message: {
if let errorMessage {
Text(errorMessage)
}
}
.sheet(isPresented: $showPlayerPicker) {
EnhancedPlayerPickerView(
players: players,
onSelect: { player in
Task { await playAlbum(on: player) }
}
)
}
}
// MARK: - Background Artwork
@ViewBuilder
private var backgroundArtwork: some View {
GeometryReader { geometry in
CachedAsyncImage(url: service.imageProxyURL(path: album.imageUrl, provider: album.imageProvider, size: 512)) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: geometry.size.width, height: geometry.size.height)
.scaleEffect(kenBurnsScale)
.blur(radius: 50)
.overlay {
// Dark gradient overlay for better text contrast
LinearGradient(
colors: [
Color.black.opacity(0.7),
Color.black.opacity(0.5),
Color.black.opacity(0.7)
],
startPoint: .top,
endPoint: .bottom
)
}
.clipped()
} placeholder: {
Rectangle()
.fill(
LinearGradient(
colors: [Color(.systemGray6), Color(.systemGray5)],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.overlay {
Color.black.opacity(0.6)
}
}
}
.ignoresSafeArea()
}
// MARK: - Album Header
@ViewBuilder
private var albumHeader: some View {
VStack(spacing: 16) {
// Cover Art
CachedAsyncImage(url: service.imageProxyURL(path: album.imageUrl, provider: album.imageProvider, size: 512)) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
RoundedRectangle(cornerRadius: 12)
.fill(Color.white.opacity(0.1))
.overlay {
Image(systemName: "opticaldisc")
.font(.system(size: 60))
.foregroundStyle(.white.opacity(0.5))
}
}
.frame(width: 250, height: 250)
.clipShape(RoundedRectangle(cornerRadius: 12))
.shadow(color: .black.opacity(0.5), radius: 20, y: 10)
// Album Info
VStack(spacing: 8) {
if let artists = album.artists, !artists.isEmpty {
Text(artists.map { $0.name }.joined(separator: ", "))
.font(.title3)
.fontWeight(.semibold)
.foregroundStyle(.white)
.multilineTextAlignment(.center)
.shadow(color: .black.opacity(0.3), radius: 2, x: 0, y: 1)
}
HStack {
if let year = album.year {
Text(String(year))
.font(.subheadline)
.foregroundStyle(.white.opacity(0.8))
}
if !tracks.isEmpty {
Text("")
.foregroundStyle(.white.opacity(0.8))
Text("\(tracks.count) tracks")
.font(.subheadline)
.foregroundStyle(.white.opacity(0.8))
}
}
.shadow(color: .black.opacity(0.3), radius: 2, x: 0, y: 1)
}
.padding(.horizontal)
}
.padding(.top)
}
// MARK: - Play Button
@ViewBuilder
private var playButton: some View {
Button {
if players.count == 1 {
selectedPlayer = players.first
Task {
await playAlbum(on: players.first!)
}
} else {
showPlayerPicker = true
}
} label: {
Label("Play Album", systemImage: "play.fill")
.font(.headline)
.frame(maxWidth: .infinity)
.padding()
.background(
LinearGradient(
colors: [Color.white.opacity(0.3), Color.white.opacity(0.2)],
startPoint: .top,
endPoint: .bottom
)
)
.foregroundStyle(.white)
.clipShape(RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.white.opacity(0.3), lineWidth: 1)
)
.shadow(color: .black.opacity(0.3), radius: 10, y: 5)
}
.padding(.horizontal)
.disabled(tracks.isEmpty || players.isEmpty)
.opacity((tracks.isEmpty || players.isEmpty) ? 0.5 : 1.0)
}
// MARK: - Track List
@ViewBuilder
private var trackList: some View {
LazyVStack(spacing: 0) {
ForEach(Array(tracks.enumerated()), id: \.element.id) { index, track in
TrackRow(track: track, trackNumber: index + 1, useLightTheme: true)
.contentShape(Rectangle())
.onTapGesture {
if players.count == 1 {
Task {
await playTrack(track, on: players.first!)
}
} else {
showPlayerPicker = true
}
}
if index < tracks.count - 1 {
Divider()
.background(Color.white.opacity(0.2))
.padding(.leading, 60)
}
}
}
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color.black.opacity(0.3))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.white.opacity(0.1), lineWidth: 1)
)
)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(.horizontal)
.padding(.bottom, 24)
}
// MARK: - Description Section
@ViewBuilder
private func descriptionSection(_ text: String) -> some View {
VStack(alignment: .leading, spacing: 10) {
Text("About")
.font(.title2.bold())
.foregroundStyle(.white)
.shadow(color: .black.opacity(0.3), radius: 2, x: 0, y: 1)
Text(verbatim: text)
.font(.body)
.foregroundStyle(.white.opacity(0.85))
.lineLimit(isDescriptionExpanded ? nil : 4)
.lineSpacing(3)
Button {
withAnimation(.easeInOut(duration: 0.25)) {
isDescriptionExpanded.toggle()
}
} label: {
Text(isDescriptionExpanded ? "Show less" : "Show more")
.font(.subheadline.bold())
.foregroundStyle(.white.opacity(0.6))
}
}
.padding(.horizontal)
.frame(maxWidth: .infinity, alignment: .leading)
}
// MARK: - Actions
private func loadTracks() async {
print("🔵 AlbumDetailView: Loading tracks for album: \(album.name)")
print("🔵 AlbumDetailView: Album URI: \(album.uri)")
isLoading = true
errorMessage = nil
do {
tracks = try await service.libraryManager.getAlbumTracks(albumUri: album.uri)
print("✅ AlbumDetailView: Loaded \(tracks.count) tracks")
isLoading = false
} catch {
print("❌ AlbumDetailView: Failed to load tracks: \(error)")
errorMessage = error.localizedDescription
showError = true
isLoading = false
}
// If this album came from a provider-specific URI (not the full library version),
// try to find the matching library album so we can offer a "Show complete album" link.
if let scheme = URL(string: album.uri)?.scheme, scheme != "library" {
completeAlbum = service.libraryManager.albums.first {
$0.name.caseInsensitiveCompare(album.name) == .orderedSame
&& $0.uri.hasPrefix("library://")
&& ($0.year == album.year || album.year == nil)
}
}
}
private func loadAlbumDetail() async {
do {
let detail = try await service.getAlbumDetail(albumUri: album.uri)
if let desc = detail.metadata?.description, !desc.isEmpty {
albumDescription = desc
}
} catch {
// Description is optional silently ignore if unavailable
}
}
private func playAlbum(on player: MAPlayer) async {
do {
try await service.playerManager.playMedia(
playerId: player.playerId,
uri: album.uri
)
} catch {
errorMessage = error.localizedDescription
showError = true
}
}
private func playTrack(_ track: MAMediaItem, on player: MAPlayer) async {
do {
try await service.playerManager.playMedia(
playerId: player.playerId,
uri: track.uri
)
} catch {
errorMessage = error.localizedDescription
showError = true
}
}
}
// MARK: - Track Row
struct TrackRow: View {
let track: MAMediaItem
let trackNumber: Int
var useLightTheme: Bool = false
var body: some View {
HStack(spacing: 12) {
// Track Number
Text("\(trackNumber)")
.font(.subheadline)
.foregroundStyle(useLightTheme ? .white.opacity(0.7) : .secondary)
.frame(width: 30, alignment: .trailing)
// Track Info
VStack(alignment: .leading, spacing: 4) {
Text(track.name)
.font(.body)
.foregroundStyle(useLightTheme ? .white : .primary)
.lineLimit(1)
if let artists = track.artists, !artists.isEmpty {
Text(artists.map { $0.name }.joined(separator: ", "))
.font(.caption)
.foregroundStyle(useLightTheme ? .white.opacity(0.7) : .secondary)
.lineLimit(1)
}
}
Spacer()
// Duration
if let duration = track.duration {
Text(formatDuration(duration))
.font(.caption)
.foregroundStyle(useLightTheme ? .white.opacity(0.7) : .secondary)
}
}
.padding(.vertical, 8)
.padding(.horizontal)
}
private func formatDuration(_ seconds: Int) -> String {
let minutes = seconds / 60
let remainingSeconds = seconds % 60
return String(format: "%d:%02d", minutes, remainingSeconds)
}
}
#Preview {
NavigationStack {
AlbumDetailView(
album: MAAlbum(
uri: "library://album/1",
name: "Test Album",
artists: [
MAArtist(uri: "library://artist/1", name: "Test Artist", imageUrl: nil, sortName: nil, musicbrainzId: nil)
],
imageUrl: nil,
year: 2024
)
)
.environment(MAService())
}
}