Add artist pages
This commit is contained in:
1 parent
7ce0827c9a
commit
20a0048749
4 files changed
+395
-18
No files matched your search
@@ -227,12 +227,7 @@ struct AlbumDetailView: View {
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
if !artistNames(for: album.artists).isEmpty {
|
||||
Text(artistNames(for: album.artists))
|
||||
.font(.title3)
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
artistLinks(for: album.artists)
|
||||
|
||||
Text(albumMetadata(for: album))
|
||||
.font(.subheadline.weight(.semibold))
|
||||
@@ -291,12 +286,30 @@ struct AlbumDetailView: View {
|
||||
return [genre, songCount].compactMap { $0 }.joined(separator: " · ")
|
||||
}
|
||||
|
||||
private func artistNames(for artists: [APIReference<Artist>]) -> String {
|
||||
artists.compactMap { reference in
|
||||
@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.name
|
||||
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)
|
||||
}
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
final class ArtistDetailViewModel: ObservableObject {
|
||||
@Published private(set) var artist: Artist?
|
||||
@Published private(set) var tracks: [Track]
|
||||
@Published private(set) var albums: [Album]
|
||||
@Published private(set) var isLoading = false
|
||||
@Published private(set) var errorMessage: String?
|
||||
|
||||
private let artistID: String
|
||||
private let api: MusicAPI
|
||||
private var hasLoaded: Bool
|
||||
|
||||
init(artistID: String, api: MusicAPI? = nil, artist: Artist? = nil,
|
||||
tracks: [Track] = [], albums: [Album] = []) {
|
||||
self.artistID = artistID
|
||||
self.api = api ?? MusicAPI()
|
||||
self.artist = artist
|
||||
self.tracks = tracks
|
||||
self.albums = albums
|
||||
hasLoaded = artist != nil
|
||||
}
|
||||
|
||||
static var preview: ArtistDetailViewModel {
|
||||
let source = AlbumDetailViewModel.preview
|
||||
let artist = Artist(id: "artist-preview", name: "Example Artist", file: nil)
|
||||
return ArtistDetailViewModel(
|
||||
artistID: artist.id,
|
||||
artist: artist,
|
||||
tracks: source.tracks,
|
||||
albums: [Album(id: "album-preview", name: "First Light", artists: [.value(artist)], file: nil, genre: nil)]
|
||||
)
|
||||
}
|
||||
|
||||
func load(forceRefresh: Bool = false) async {
|
||||
guard !isLoading, forceRefresh || !hasLoaded else { return }
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
async let artistRequest = api.artist(id: artistID)
|
||||
async let tracksRequest = loadAllTracks()
|
||||
async let albumsRequest = loadAllAlbums()
|
||||
artist = try await artistRequest
|
||||
tracks = try await tracksRequest
|
||||
albums = try await albumsRequest
|
||||
hasLoaded = true
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func artworkURL(for artist: Artist) -> URL? {
|
||||
guard let file = artist.file else { return nil }
|
||||
return api.client.configuration.mediaURL(for: file)
|
||||
}
|
||||
|
||||
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] {
|
||||
var all: [Track] = []
|
||||
var skip = 0
|
||||
while true {
|
||||
let page = try await api.artistTracks(id: artistID, limit: 100, skip: skip)
|
||||
all.append(contentsOf: page)
|
||||
guard page.count == 100 else { return all }
|
||||
skip += page.count
|
||||
}
|
||||
}
|
||||
|
||||
private func loadAllAlbums() async throws -> [Album] {
|
||||
var all: [Album] = []
|
||||
var skip = 0
|
||||
while true {
|
||||
let page = try await api.artistAlbums(id: artistID, limit: 100, skip: skip)
|
||||
all.append(contentsOf: page)
|
||||
guard page.count == 100 else { return all }
|
||||
skip += page.count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ArtistDetailView: View {
|
||||
@EnvironmentObject private var player: PlayerManager
|
||||
@StateObject private var viewModel: ArtistDetailViewModel
|
||||
|
||||
init(artistID: String) {
|
||||
_viewModel = StateObject(wrappedValue: ArtistDetailViewModel(artistID: artistID))
|
||||
}
|
||||
|
||||
init(viewModel: ArtistDetailViewModel) {
|
||||
_viewModel = StateObject(wrappedValue: viewModel)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if viewModel.isLoading && viewModel.artist == nil {
|
||||
ProgressView().tint(.red).frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else if let error = viewModel.errorMessage, viewModel.artist == nil {
|
||||
ContentUnavailableView {
|
||||
Label("Couldn't Load Artist", systemImage: "wifi.exclamationmark")
|
||||
} description: {
|
||||
Text(error)
|
||||
} actions: {
|
||||
Button("Try Again") { Task { await viewModel.load(forceRefresh: true) } }
|
||||
.tint(.red)
|
||||
}
|
||||
} else if let artist = viewModel.artist {
|
||||
content(artist)
|
||||
} else {
|
||||
ContentUnavailableView("Artist Not Found", systemImage: "music.mic",
|
||||
description: Text("This artist may no longer be available."))
|
||||
}
|
||||
}
|
||||
.navigationTitle("")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.tint(.red)
|
||||
.task { await viewModel.load() }
|
||||
}
|
||||
|
||||
private func content(_ artist: Artist) -> some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 24) {
|
||||
VStack(spacing: 14) {
|
||||
AlbumArtworkView(url: viewModel.artworkURL(for: artist), cornerRadius: 20)
|
||||
.containerRelativeFrame(.horizontal) { length, _ in min(280, length - 40) }
|
||||
.frame(maxWidth: .infinity)
|
||||
.accessibilityLabel("\(artist.name) artist artwork")
|
||||
Text(artist.name).font(.largeTitle.weight(.bold)).multilineTextAlignment(.center)
|
||||
HStack(spacing: 14) {
|
||||
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 \(artist.name)")
|
||||
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 \(artist.name)")
|
||||
}
|
||||
.padding(.top, 4)
|
||||
}
|
||||
Divider()
|
||||
HStack {
|
||||
Text("Tracks").font(.title2.weight(.bold))
|
||||
Spacer()
|
||||
NavigationLink("See All") { ArtistSongsView(artistID: artist.id, artistName: artist.name) }
|
||||
.font(.subheadline.weight(.semibold))
|
||||
}
|
||||
if let error = viewModel.errorMessage {
|
||||
retryBlock(title: "Couldn't Load Artist Content", message: error)
|
||||
} else if viewModel.isLoading && viewModel.tracks.isEmpty {
|
||||
ProgressView().frame(maxWidth: .infinity).padding(.vertical, 20)
|
||||
} else if viewModel.tracks.isEmpty {
|
||||
ContentUnavailableView("No Songs", systemImage: "music.note.list",
|
||||
description: Text("This artist doesn't have any songs yet."))
|
||||
} else {
|
||||
LazyVStack(spacing: 0) {
|
||||
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
||||
TrackRowView(track: viewModel.tracks[index], index: index) {
|
||||
player.play(viewModel.tracks, startingAt: index)
|
||||
}
|
||||
if index < viewModel.tracks.count - 1 {
|
||||
Divider().padding(.leading, 42)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("Albums").font(.title2.weight(.bold))
|
||||
if viewModel.albums.isEmpty && !viewModel.isLoading {
|
||||
Text("No albums available.").foregroundStyle(.secondary)
|
||||
} else {
|
||||
ScrollView(.horizontal) {
|
||||
LazyHStack(alignment: .top, spacing: 14) {
|
||||
ForEach(viewModel.albums) { album in
|
||||
NavigationLink { AlbumDetailView(albumID: album.id) } label: {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
AlbumArtworkView(url: viewModel.artworkURL(for: album))
|
||||
Text(album.name).font(.subheadline.weight(.medium))
|
||||
.lineLimit(2).foregroundStyle(.primary)
|
||||
}
|
||||
.containerRelativeFrame(.horizontal) { length, _ in (length - 40 - 28) / 2.2 }
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.scrollTargetLayout()
|
||||
}
|
||||
.scrollIndicators(.hidden).scrollTargetBehavior(.viewAligned)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 20).padding(.top, 16).padding(.bottom, 24)
|
||||
}
|
||||
.scrollIndicators(.hidden)
|
||||
.refreshable { await viewModel.load(forceRefresh: true) }
|
||||
.onScrollGeometryChange(for: CGFloat.self) { $0.contentOffset.y } action: { old, new in
|
||||
if new > old + 2 { player.compactMiniPlayerForDownwardScroll() }
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func retryBlock(title: String, message: String) -> some View {
|
||||
ContentUnavailableView {
|
||||
Label(title, systemImage: "wifi.exclamationmark")
|
||||
} description: {
|
||||
Text(message)
|
||||
} actions: {
|
||||
Button("Try Again") { Task { await viewModel.load(forceRefresh: true) } }.tint(.red)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ArtistDetailView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
NavigationStack { ArtistDetailView(viewModel: .preview) }
|
||||
.environmentObject(PlayerManager())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
private final class ArtistSongsViewModel: ObservableObject {
|
||||
@Published private(set) var tracks: [Track] = []
|
||||
@Published private(set) var isLoading = false
|
||||
@Published private(set) var errorMessage: String?
|
||||
|
||||
private let artistID: String
|
||||
private let api: MusicAPI
|
||||
private var hasLoaded = false
|
||||
|
||||
init(artistID: String, api: MusicAPI = MusicAPI()) {
|
||||
self.artistID = artistID
|
||||
self.api = api
|
||||
}
|
||||
|
||||
func load(forceRefresh: Bool = false) async {
|
||||
guard !isLoading, forceRefresh || !hasLoaded else { return }
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
var all: [Track] = []
|
||||
var skip = 0
|
||||
while true {
|
||||
let page = try await api.artistTracks(id: artistID, limit: 100, skip: skip)
|
||||
all.append(contentsOf: page)
|
||||
guard page.count == 100 else { break }
|
||||
skip += page.count
|
||||
}
|
||||
tracks = all
|
||||
hasLoaded = true
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ArtistSongsView: View {
|
||||
@EnvironmentObject private var player: PlayerManager
|
||||
@StateObject private var viewModel: ArtistSongsViewModel
|
||||
private let artistName: String
|
||||
|
||||
init(artistID: String, artistName: String) {
|
||||
self.artistName = artistName
|
||||
_viewModel = StateObject(wrappedValue: ArtistSongsViewModel(artistID: artistID))
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if viewModel.isLoading && viewModel.tracks.isEmpty {
|
||||
ProgressView().tint(.red).frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else if let error = viewModel.errorMessage, viewModel.tracks.isEmpty {
|
||||
ContentUnavailableView {
|
||||
Label("Couldn't Load Songs", systemImage: "wifi.exclamationmark")
|
||||
} description: {
|
||||
Text(error)
|
||||
} actions: {
|
||||
Button("Try Again") { Task { await viewModel.load(forceRefresh: true) } }
|
||||
.tint(.red)
|
||||
}
|
||||
} else if viewModel.tracks.isEmpty {
|
||||
ContentUnavailableView("No Songs", systemImage: "music.note.list",
|
||||
description: Text("\(artistName) doesn't have any songs yet."))
|
||||
} else {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: 0) {
|
||||
ForEach(viewModel.tracks.indices, id: \.self) { index in
|
||||
TrackRowView(track: viewModel.tracks[index], index: index) {
|
||||
player.play(viewModel.tracks, startingAt: index)
|
||||
}
|
||||
if index < viewModel.tracks.count - 1 {
|
||||
Divider().padding(.leading, 42)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 20).padding(.top, 12).padding(.bottom, 24)
|
||||
}
|
||||
.scrollIndicators(.hidden)
|
||||
.refreshable { await viewModel.load(forceRefresh: true) }
|
||||
}
|
||||
}
|
||||
.navigationTitle("Songs")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.tint(.red)
|
||||
.task { await viewModel.load() }
|
||||
}
|
||||
}
|
||||
|
||||
struct ArtistSongsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
NavigationStack {
|
||||
ArtistSongsView(artistID: "artist-preview", artistName: "Example Artist")
|
||||
}
|
||||
.environmentObject(PlayerManager())
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,47 @@ import SwiftUI
|
||||
struct TrackRowView: View {
|
||||
let track: Track
|
||||
let index: Int
|
||||
var onPlay: (() -> Void)? = nil
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 14) {
|
||||
Text("\(index + 1)")
|
||||
.font(.body.monospacedDigit())
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(width: 28, alignment: .trailing)
|
||||
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)
|
||||
}
|
||||
|
||||
Text(track.name)
|
||||
.font(.body)
|
||||
.foregroundStyle(.primary)
|
||||
.lineLimit(1)
|
||||
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)
|
||||
|
||||
@@ -28,6 +57,13 @@ struct TrackRowView: View {
|
||||
.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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TrackRowView_Previews: PreviewProvider {
|
||||
|
||||
Reference in new issue
Block a user