320 lines
11 KiB
Swift
320 lines
11 KiB
Swift
import Combine
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
final class AlbumDetailViewModel: ObservableObject {
|
|
@Published private(set) var album: Album?
|
|
@Published private(set) var tracks: [Track]
|
|
@Published private(set) var isLoading: Bool
|
|
@Published private(set) var errorMessage: String?
|
|
|
|
private let albumID: String
|
|
private let api: MusicAPI
|
|
private var hasLoaded: Bool
|
|
|
|
init(
|
|
albumID: String,
|
|
api: MusicAPI? = nil,
|
|
album: Album? = nil,
|
|
tracks: [Track] = []
|
|
) {
|
|
self.albumID = albumID
|
|
self.api = api ?? MusicAPI()
|
|
self.album = album
|
|
self.tracks = tracks
|
|
isLoading = false
|
|
hasLoaded = album != nil
|
|
}
|
|
|
|
static var preview: AlbumDetailViewModel {
|
|
let artist = Artist(id: "artist-preview", name: "Example Artist", file: nil)
|
|
let album = Album(
|
|
id: "album-preview",
|
|
name: "First Light",
|
|
artists: [.value(artist)],
|
|
file: nil,
|
|
genre: nil
|
|
)
|
|
let trackAlbum = TrackAlbum(id: album.id, name: album.name, file: "")
|
|
let tracks = [
|
|
Track(
|
|
id: "track-preview-1",
|
|
name: "Morning Light",
|
|
album: .value(trackAlbum),
|
|
artists: [.value(artist)],
|
|
fileDir: "",
|
|
durationInSeconds: 218,
|
|
lyrics: nil
|
|
),
|
|
Track(
|
|
id: "track-preview-2",
|
|
name: "Open Roads",
|
|
album: .value(trackAlbum),
|
|
artists: [.value(artist)],
|
|
fileDir: "",
|
|
durationInSeconds: 194,
|
|
lyrics: nil
|
|
),
|
|
Track(
|
|
id: "track-preview-3",
|
|
name: "Between the Lines",
|
|
album: .value(trackAlbum),
|
|
artists: [.value(artist)],
|
|
fileDir: "",
|
|
durationInSeconds: 243,
|
|
lyrics: nil
|
|
),
|
|
]
|
|
return AlbumDetailViewModel(albumID: album.id, album: album, tracks: tracks)
|
|
}
|
|
|
|
func load(forceRefresh: Bool = false) async {
|
|
guard !isLoading, forceRefresh || !hasLoaded else { return }
|
|
isLoading = true
|
|
errorMessage = nil
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
async let albumRequest = api.album(id: albumID)
|
|
async let tracksRequest = loadAllTracks()
|
|
album = try await albumRequest
|
|
tracks = try await tracksRequest
|
|
hasLoaded = true
|
|
} catch {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func artworkURL(for album: Album) -> URL? {
|
|
guard let file = album.file else { return nil }
|
|
return api.client.configuration.mediaURL(for: file)
|
|
}
|
|
|
|
private func loadAllTracks() async throws -> [Track] {
|
|
let pageSize = 100
|
|
var allTracks: [Track] = []
|
|
var skip = 0
|
|
|
|
while true {
|
|
let page = try await api.albumTracks(id: albumID, limit: pageSize, skip: skip)
|
|
allTracks.append(contentsOf: page)
|
|
guard page.count == pageSize else { return allTracks }
|
|
skip += page.count
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AlbumDetailView: View {
|
|
@EnvironmentObject private var player: PlayerManager
|
|
@StateObject private var viewModel: AlbumDetailViewModel
|
|
|
|
init(albumID: String) {
|
|
_viewModel = StateObject(wrappedValue: AlbumDetailViewModel(albumID: albumID))
|
|
}
|
|
|
|
init(viewModel: AlbumDetailViewModel) {
|
|
_viewModel = StateObject(wrappedValue: viewModel)
|
|
}
|
|
|
|
var body: some View {
|
|
Group {
|
|
if viewModel.isLoading && viewModel.album == nil {
|
|
ProgressView()
|
|
.tint(.red)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else if let errorMessage = viewModel.errorMessage, viewModel.album == nil {
|
|
ContentUnavailableView {
|
|
Label("Couldn't Load Album", systemImage: "wifi.exclamationmark")
|
|
} description: {
|
|
Text(errorMessage)
|
|
} actions: {
|
|
Button("Try Again") {
|
|
Task { await viewModel.load(forceRefresh: true) }
|
|
}
|
|
.tint(.red)
|
|
}
|
|
} else if let album = viewModel.album {
|
|
albumContent(album)
|
|
} else {
|
|
ContentUnavailableView(
|
|
"Album Not Found",
|
|
systemImage: "square.stack",
|
|
description: Text("This album may no longer be available.")
|
|
)
|
|
}
|
|
}
|
|
.toolbar(.visible, for: .navigationBar)
|
|
.navigationTitle("")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.tint(.red)
|
|
.task {
|
|
await viewModel.load()
|
|
}
|
|
}
|
|
|
|
private func albumContent(_ album: Album) -> some View {
|
|
ScrollView {
|
|
VStack(alignment: .leading, spacing: 20) {
|
|
albumHeader(album)
|
|
Divider()
|
|
|
|
if let errorMessage = viewModel.errorMessage {
|
|
ContentUnavailableView {
|
|
Label("Couldn't Load Songs", systemImage: "wifi.exclamationmark")
|
|
} description: {
|
|
Text(errorMessage)
|
|
} actions: {
|
|
Button("Try Again") {
|
|
Task { await viewModel.load(forceRefresh: true) }
|
|
}
|
|
.tint(.red)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.padding(.top, 16)
|
|
} else if viewModel.isLoading && viewModel.tracks.isEmpty {
|
|
ProgressView()
|
|
.tint(.red)
|
|
.frame(maxWidth: .infinity)
|
|
.padding(.top, 28)
|
|
} else if viewModel.tracks.isEmpty && !viewModel.isLoading {
|
|
ContentUnavailableView(
|
|
"No Songs",
|
|
systemImage: "music.note.list",
|
|
description: Text("This album doesn't have any songs yet.")
|
|
)
|
|
.frame(maxWidth: .infinity)
|
|
.padding(.top, 16)
|
|
} else {
|
|
LazyVStack(spacing: 0) {
|
|
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
|
let track = viewModel.tracks[index]
|
|
TrackRowView(track: track, index: index, hidesAlbumAction: true)
|
|
if index < viewModel.tracks.count - 1 {
|
|
Divider()
|
|
.padding(.leading, 42)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.padding(.horizontal, 20)
|
|
.padding(.top, 16)
|
|
.padding(.bottom, 24)
|
|
}
|
|
.scrollIndicators(.hidden)
|
|
.refreshable {
|
|
await viewModel.load(forceRefresh: true)
|
|
}
|
|
}
|
|
|
|
private func albumHeader(_ album: Album) -> some View {
|
|
VStack(alignment: .center, spacing: 12) {
|
|
AlbumArtworkView(url: viewModel.artworkURL(for: album), cornerRadius: 18)
|
|
.containerRelativeFrame(.horizontal) { length, _ in
|
|
min(280, length - 40)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.accessibilityLabel("\(album.name) album artwork")
|
|
|
|
VStack(spacing: 4) {
|
|
Text(album.name)
|
|
.font(.title.weight(.bold))
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
.multilineTextAlignment(.center)
|
|
|
|
artistLinks(for: album.artists)
|
|
|
|
Text(albumMetadata(for: album))
|
|
.font(.subheadline.weight(.semibold))
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
|
|
albumActions
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
|
|
private var albumActions: some View {
|
|
HStack(spacing: 16) {
|
|
Button {
|
|
player.play(viewModel.tracks, shuffled: true)
|
|
} label: {
|
|
Image(systemName: "shuffle")
|
|
.font(.title3.weight(.semibold))
|
|
.frame(width: 54, height: 54)
|
|
.background(.thinMaterial, in: Circle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.disabled(viewModel.tracks.isEmpty)
|
|
.accessibilityLabel("Shuffle album")
|
|
|
|
Button {
|
|
player.play(viewModel.tracks)
|
|
} label: {
|
|
Label("Play", systemImage: "play.fill")
|
|
.font(.title3.weight(.semibold))
|
|
.frame(maxWidth: .infinity)
|
|
.frame(height: 54)
|
|
.foregroundStyle(.white)
|
|
.background(.red, in: Capsule())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.disabled(viewModel.tracks.isEmpty)
|
|
.accessibilityLabel("Play album")
|
|
|
|
Image(systemName: "plus")
|
|
.font(.title3.weight(.semibold))
|
|
.frame(width: 54, height: 54)
|
|
.background(.thinMaterial, in: Circle())
|
|
.accessibilityLabel("Add album")
|
|
}
|
|
.padding(.top, 8)
|
|
.accessibilityElement(children: .combine)
|
|
}
|
|
|
|
private func albumMetadata(for album: Album) -> String {
|
|
let genre = album.genre.flatMap { reference -> String? in
|
|
guard case .value(let genre) = reference else { return nil }
|
|
return genre.name
|
|
}
|
|
let songCount =
|
|
"\(viewModel.tracks.count) \(viewModel.tracks.count == 1 ? "song" : "songs")"
|
|
return [genre, songCount].compactMap { $0 }.joined(separator: " · ")
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func artistLinks(for references: [APIReference<Artist>]) -> some View {
|
|
let artists = references.compactMap { reference -> Artist? in
|
|
guard case .value(let artist) = reference else { return nil }
|
|
return artist
|
|
}
|
|
|
|
if !artists.isEmpty {
|
|
HStack(spacing: 4) {
|
|
ForEach(artists) { artist in
|
|
NavigationLink(artist.name) {
|
|
ArtistDetailView(artistID: artist.id)
|
|
}
|
|
.font(.title3)
|
|
.foregroundStyle(.secondary)
|
|
if artist.id != artists.last?.id {
|
|
Text(",")
|
|
.font(.title3)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
}
|
|
.multilineTextAlignment(.center)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AlbumDetailView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
NavigationStack {
|
|
AlbumDetailView(viewModel: .preview)
|
|
.environmentObject(PlayerManager())
|
|
}
|
|
}
|
|
}
|