Add Library Artists screen

This commit is contained in:
lucast committed 2026-10-09 00:05:42 +02:00
1 parent 2cae15ab1c
commit 85e3b3148e
2 files changed
+186 -38

No files matched your search

+32 -38
View File
@@ -3,54 +3,43 @@ import SwiftUI
@MainActor
private final class LibraryViewModel: ObservableObject {
@Published private(set) var recentlyPlayedAlbums: [RecentAlbum] = []
@Published private(set) var recentlySavedAlbums: [Album] = []
@Published private(set) var isLoading = false
@Published private(set) var errorMessage: String?
private let api = MusicAPI()
private let recentTracksKey = "music-v2-recently-played-track-ids"
func loadRecentlyPlayed(forceRefresh: Bool = false) async {
guard !isLoading else { return }
let ids = UserDefaults.standard.stringArray(forKey: recentTracksKey) ?? []
guard !ids.isEmpty else {
recentlyPlayedAlbums = []
errorMessage = nil
return
}
if !forceRefresh, !recentlyPlayedAlbums.isEmpty { return }
func loadRecentlySaved(forceRefresh: Bool = false) async {
guard !isLoading, forceRefresh || recentlySavedAlbums.isEmpty else { return }
isLoading = true
errorMessage = nil
defer { isLoading = false }
do {
let tracks = try await api.tracks(ids: ids)
let tracksByID = Dictionary(uniqueKeysWithValues: tracks.map { ($0.id, $0) })
let orderedTracks = ids.compactMap { tracksByID[$0] }
let tracks = try await api.savedTracks()
var seenAlbumIDs = Set<String>()
recentlyPlayedAlbums = orderedTracks.compactMap { track in
recentlySavedAlbums = tracks.reversed().compactMap { track in
guard case .value(let album) = track.album,
seenAlbumIDs.insert(album.id).inserted else { return nil }
return RecentAlbum(id: album.id, name: album.name, file: album.file, track: track)
return Album(
id: album.id,
name: album.name,
artists: track.artists,
file: album.file,
genre: nil
)
}
} catch {
errorMessage = error.localizedDescription
}
}
func artworkURL(for album: RecentAlbum) -> URL? {
api.client.configuration.mediaURL(for: album.file)
func artworkURL(for album: Album) -> URL? {
guard let file = album.file else { return nil }
return api.client.configuration.mediaURL(for: file)
}
}
private struct RecentAlbum: Identifiable {
let id: String
let name: String
let file: String
let track: Track
}
struct LibraryView: View {
@StateObject private var model = LibraryViewModel()
@@ -65,7 +54,7 @@ struct LibraryView: View {
categoryList
Text("Recently Played")
Text("Recently Added")
.font(.title2.weight(.bold))
.padding(.top, 26)
.padding(.bottom, 12)
@@ -79,14 +68,19 @@ struct LibraryView: View {
.background(Color.black.ignoresSafeArea())
.foregroundStyle(.white)
.toolbar(.hidden, for: .navigationBar)
.refreshable { await model.loadRecentlyPlayed(forceRefresh: true) }
.refreshable { await model.loadRecentlySaved(forceRefresh: true) }
}
.task { await model.loadRecentlyPlayed() }
.task { await model.loadRecentlySaved() }
}
private var categoryList: some View {
VStack(spacing: 0) {
categoryRow("Artists", symbol: "mic")
NavigationLink {
LibraryArtistsView()
} label: {
categoryRow("Artists", symbol: "mic")
}
.buttonStyle(.plain)
NavigationLink {
LibraryAlbumsView()
} label: {
@@ -128,31 +122,31 @@ struct LibraryView: View {
@ViewBuilder
private var recentlyPlayedSection: some View {
if model.isLoading && model.recentlyPlayedAlbums.isEmpty {
if model.isLoading && model.recentlySavedAlbums.isEmpty {
ProgressView()
.tint(.red)
.frame(maxWidth: .infinity)
.padding(.vertical, 28)
} else if let errorMessage = model.errorMessage {
ContentUnavailableView {
Label("Couldn't Load Recently Played", systemImage: "wifi.exclamationmark")
Label("Couldn't Load Recently Added", systemImage: "wifi.exclamationmark")
} description: {
Text(errorMessage)
} actions: {
Button("Try Again") {
Task { await model.loadRecentlyPlayed(forceRefresh: true) }
Task { await model.loadRecentlySaved(forceRefresh: true) }
}
.tint(.red)
}
} else if model.recentlyPlayedAlbums.isEmpty {
Text("Albums you play will appear here.")
} else if model.recentlySavedAlbums.isEmpty {
Text("Albums from songs you save will appear here.")
.font(.subheadline)
.foregroundStyle(.secondary)
.padding(.vertical, 8)
} else {
ScrollView(.horizontal) {
LazyHStack(alignment: .top, spacing: 14) {
ForEach(model.recentlyPlayedAlbums) { album in
ForEach(model.recentlySavedAlbums) { album in
NavigationLink {
AlbumDetailView(albumID: album.id)
} label: {
@@ -167,7 +161,7 @@ struct LibraryView: View {
}
}
private func recentAlbumCard(_ album: RecentAlbum) -> some View {
private func recentAlbumCard(_ album: Album) -> some View {
VStack(alignment: .leading, spacing: 5) {
AlbumArtworkView(url: model.artworkURL(for: album))
.frame(width: 170)
@@ -175,7 +169,7 @@ struct LibraryView: View {
.font(.subheadline.weight(.medium))
.lineLimit(2)
.foregroundStyle(.primary)
Text(artistNames(album.track.artists))
Text(artistNames(album.artists))
.font(.footnote)
.lineLimit(1)
.foregroundStyle(.secondary)