Add Library Artists screen
This commit is contained in:
1 parent
2cae15ab1c
commit
85e3b3148e
2 files changed
+186
-38
No files matched your search
@@ -0,0 +1,154 @@
|
|||||||
|
import Combine
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
final class LibraryArtistsViewModel: ObservableObject {
|
||||||
|
@Published private(set) var artists: [Artist]
|
||||||
|
@Published private(set) var isLoading = false
|
||||||
|
@Published private(set) var errorMessage: String?
|
||||||
|
|
||||||
|
private let api: MusicAPI
|
||||||
|
private var hasLoaded: Bool
|
||||||
|
|
||||||
|
init(api: MusicAPI? = nil, artists: [Artist]? = nil) {
|
||||||
|
self.api = api ?? MusicAPI()
|
||||||
|
self.artists = artists ?? []
|
||||||
|
hasLoaded = artists != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
static var preview: LibraryArtistsViewModel {
|
||||||
|
LibraryArtistsViewModel(artists: [
|
||||||
|
Artist(id: "library-artist-1", name: "Example Artist", file: nil),
|
||||||
|
Artist(id: "library-artist-2", name: "Another Artist", file: nil),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
func load(forceRefresh: Bool = false) async {
|
||||||
|
guard !isLoading, forceRefresh || !hasLoaded else { return }
|
||||||
|
isLoading = true
|
||||||
|
errorMessage = nil
|
||||||
|
defer { isLoading = false }
|
||||||
|
|
||||||
|
do {
|
||||||
|
artists = try await api.savedArtists()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibraryArtistsView: View {
|
||||||
|
@Environment(\.dismiss) private var dismiss
|
||||||
|
@StateObject private var model: LibraryArtistsViewModel
|
||||||
|
|
||||||
|
private let columns = [GridItem(.flexible(), spacing: 14), GridItem(.flexible(), spacing: 14)]
|
||||||
|
|
||||||
|
init(viewModel: LibraryArtistsViewModel? = nil) {
|
||||||
|
_model = StateObject(wrappedValue: viewModel ?? LibraryArtistsViewModel())
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
ScrollView {
|
||||||
|
VStack(alignment: .leading, spacing: 0) {
|
||||||
|
topBar
|
||||||
|
|
||||||
|
Text("Artists")
|
||||||
|
.font(.system(size: 34, weight: .bold))
|
||||||
|
.padding(.top, 22)
|
||||||
|
.padding(.bottom, 18)
|
||||||
|
|
||||||
|
content
|
||||||
|
}
|
||||||
|
.padding(.horizontal, 20)
|
||||||
|
.padding(.bottom, 24)
|
||||||
|
}
|
||||||
|
.scrollIndicators(.hidden)
|
||||||
|
.background(Color.black.ignoresSafeArea())
|
||||||
|
.foregroundStyle(.white)
|
||||||
|
.toolbar(.hidden, for: .navigationBar)
|
||||||
|
.refreshable { await model.load(forceRefresh: true) }
|
||||||
|
.task { await model.load() }
|
||||||
|
}
|
||||||
|
|
||||||
|
private var topBar: some View {
|
||||||
|
HStack {
|
||||||
|
Button(action: { dismiss() }) {
|
||||||
|
Image(systemName: "chevron.left")
|
||||||
|
.font(.system(size: 20, weight: .medium))
|
||||||
|
.frame(width: 48, height: 48)
|
||||||
|
.background(.ultraThinMaterial, in: Circle())
|
||||||
|
.overlay(Circle().stroke(.white.opacity(0.12), lineWidth: 1))
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.accessibilityLabel("Back to Library")
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
.padding(.top, 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private var content: some View {
|
||||||
|
if model.isLoading && model.artists.isEmpty {
|
||||||
|
ProgressView("Loading your artists…")
|
||||||
|
.tint(.red)
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.padding(.top, 44)
|
||||||
|
} else if let errorMessage = model.errorMessage, model.artists.isEmpty {
|
||||||
|
ContentUnavailableView {
|
||||||
|
Label("Couldn't Load Artists", systemImage: "wifi.exclamationmark")
|
||||||
|
} description: {
|
||||||
|
Text(errorMessage)
|
||||||
|
} actions: {
|
||||||
|
Button("Try Again") {
|
||||||
|
Task { await model.load(forceRefresh: true) }
|
||||||
|
}
|
||||||
|
.tint(.red)
|
||||||
|
}
|
||||||
|
.padding(.top, 28)
|
||||||
|
} else if model.artists.isEmpty {
|
||||||
|
ContentUnavailableView(
|
||||||
|
"No Saved Artists",
|
||||||
|
systemImage: "mic",
|
||||||
|
description: Text("Artists from songs you add to your Library will appear here.")
|
||||||
|
)
|
||||||
|
.padding(.top, 28)
|
||||||
|
} else {
|
||||||
|
LazyVGrid(columns: columns, alignment: .leading, spacing: 20) {
|
||||||
|
ForEach(model.artists) { artist in
|
||||||
|
NavigationLink {
|
||||||
|
ArtistDetailView(artistID: artist.id)
|
||||||
|
} label: {
|
||||||
|
artistCard(artist)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func artistCard(_ artist: Artist) -> some View {
|
||||||
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
|
AlbumArtworkView(url: model.artworkURL(for: artist))
|
||||||
|
Text(artist.name)
|
||||||
|
.font(.subheadline.weight(.medium))
|
||||||
|
.lineLimit(2)
|
||||||
|
.foregroundStyle(.primary)
|
||||||
|
}
|
||||||
|
.accessibilityElement(children: .combine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibraryArtistsView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
NavigationStack {
|
||||||
|
LibraryArtistsView(viewModel: .preview)
|
||||||
|
}
|
||||||
|
.environmentObject(PlayerManager())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,54 +3,43 @@ import SwiftUI
|
|||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
private final class LibraryViewModel: ObservableObject {
|
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 isLoading = false
|
||||||
@Published private(set) var errorMessage: String?
|
@Published private(set) var errorMessage: String?
|
||||||
|
|
||||||
private let api = MusicAPI()
|
private let api = MusicAPI()
|
||||||
private let recentTracksKey = "music-v2-recently-played-track-ids"
|
|
||||||
|
|
||||||
func loadRecentlyPlayed(forceRefresh: Bool = false) async {
|
func loadRecentlySaved(forceRefresh: Bool = false) async {
|
||||||
guard !isLoading else { return }
|
guard !isLoading, forceRefresh || recentlySavedAlbums.isEmpty else { return }
|
||||||
let ids = UserDefaults.standard.stringArray(forKey: recentTracksKey) ?? []
|
|
||||||
guard !ids.isEmpty else {
|
|
||||||
recentlyPlayedAlbums = []
|
|
||||||
errorMessage = nil
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !forceRefresh, !recentlyPlayedAlbums.isEmpty { return }
|
|
||||||
isLoading = true
|
isLoading = true
|
||||||
errorMessage = nil
|
errorMessage = nil
|
||||||
defer { isLoading = false }
|
defer { isLoading = false }
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let tracks = try await api.tracks(ids: ids)
|
let tracks = try await api.savedTracks()
|
||||||
let tracksByID = Dictionary(uniqueKeysWithValues: tracks.map { ($0.id, $0) })
|
|
||||||
let orderedTracks = ids.compactMap { tracksByID[$0] }
|
|
||||||
var seenAlbumIDs = Set<String>()
|
var seenAlbumIDs = Set<String>()
|
||||||
recentlyPlayedAlbums = orderedTracks.compactMap { track in
|
recentlySavedAlbums = tracks.reversed().compactMap { track in
|
||||||
guard case .value(let album) = track.album,
|
guard case .value(let album) = track.album,
|
||||||
seenAlbumIDs.insert(album.id).inserted else { return nil }
|
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 {
|
} catch {
|
||||||
errorMessage = error.localizedDescription
|
errorMessage = error.localizedDescription
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func artworkURL(for album: RecentAlbum) -> URL? {
|
func artworkURL(for album: Album) -> URL? {
|
||||||
api.client.configuration.mediaURL(for: album.file)
|
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 {
|
struct LibraryView: View {
|
||||||
@StateObject private var model = LibraryViewModel()
|
@StateObject private var model = LibraryViewModel()
|
||||||
|
|
||||||
@@ -65,7 +54,7 @@ struct LibraryView: View {
|
|||||||
|
|
||||||
categoryList
|
categoryList
|
||||||
|
|
||||||
Text("Recently Played")
|
Text("Recently Added")
|
||||||
.font(.title2.weight(.bold))
|
.font(.title2.weight(.bold))
|
||||||
.padding(.top, 26)
|
.padding(.top, 26)
|
||||||
.padding(.bottom, 12)
|
.padding(.bottom, 12)
|
||||||
@@ -79,14 +68,19 @@ struct LibraryView: View {
|
|||||||
.background(Color.black.ignoresSafeArea())
|
.background(Color.black.ignoresSafeArea())
|
||||||
.foregroundStyle(.white)
|
.foregroundStyle(.white)
|
||||||
.toolbar(.hidden, for: .navigationBar)
|
.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 {
|
private var categoryList: some View {
|
||||||
VStack(spacing: 0) {
|
VStack(spacing: 0) {
|
||||||
categoryRow("Artists", symbol: "mic")
|
NavigationLink {
|
||||||
|
LibraryArtistsView()
|
||||||
|
} label: {
|
||||||
|
categoryRow("Artists", symbol: "mic")
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
NavigationLink {
|
NavigationLink {
|
||||||
LibraryAlbumsView()
|
LibraryAlbumsView()
|
||||||
} label: {
|
} label: {
|
||||||
@@ -128,31 +122,31 @@ struct LibraryView: View {
|
|||||||
|
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
private var recentlyPlayedSection: some View {
|
private var recentlyPlayedSection: some View {
|
||||||
if model.isLoading && model.recentlyPlayedAlbums.isEmpty {
|
if model.isLoading && model.recentlySavedAlbums.isEmpty {
|
||||||
ProgressView()
|
ProgressView()
|
||||||
.tint(.red)
|
.tint(.red)
|
||||||
.frame(maxWidth: .infinity)
|
.frame(maxWidth: .infinity)
|
||||||
.padding(.vertical, 28)
|
.padding(.vertical, 28)
|
||||||
} else if let errorMessage = model.errorMessage {
|
} else if let errorMessage = model.errorMessage {
|
||||||
ContentUnavailableView {
|
ContentUnavailableView {
|
||||||
Label("Couldn't Load Recently Played", systemImage: "wifi.exclamationmark")
|
Label("Couldn't Load Recently Added", systemImage: "wifi.exclamationmark")
|
||||||
} description: {
|
} description: {
|
||||||
Text(errorMessage)
|
Text(errorMessage)
|
||||||
} actions: {
|
} actions: {
|
||||||
Button("Try Again") {
|
Button("Try Again") {
|
||||||
Task { await model.loadRecentlyPlayed(forceRefresh: true) }
|
Task { await model.loadRecentlySaved(forceRefresh: true) }
|
||||||
}
|
}
|
||||||
.tint(.red)
|
.tint(.red)
|
||||||
}
|
}
|
||||||
} else if model.recentlyPlayedAlbums.isEmpty {
|
} else if model.recentlySavedAlbums.isEmpty {
|
||||||
Text("Albums you play will appear here.")
|
Text("Albums from songs you save will appear here.")
|
||||||
.font(.subheadline)
|
.font(.subheadline)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
.padding(.vertical, 8)
|
.padding(.vertical, 8)
|
||||||
} else {
|
} else {
|
||||||
ScrollView(.horizontal) {
|
ScrollView(.horizontal) {
|
||||||
LazyHStack(alignment: .top, spacing: 14) {
|
LazyHStack(alignment: .top, spacing: 14) {
|
||||||
ForEach(model.recentlyPlayedAlbums) { album in
|
ForEach(model.recentlySavedAlbums) { album in
|
||||||
NavigationLink {
|
NavigationLink {
|
||||||
AlbumDetailView(albumID: album.id)
|
AlbumDetailView(albumID: album.id)
|
||||||
} label: {
|
} 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) {
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
AlbumArtworkView(url: model.artworkURL(for: album))
|
AlbumArtworkView(url: model.artworkURL(for: album))
|
||||||
.frame(width: 170)
|
.frame(width: 170)
|
||||||
@@ -175,7 +169,7 @@ struct LibraryView: View {
|
|||||||
.font(.subheadline.weight(.medium))
|
.font(.subheadline.weight(.medium))
|
||||||
.lineLimit(2)
|
.lineLimit(2)
|
||||||
.foregroundStyle(.primary)
|
.foregroundStyle(.primary)
|
||||||
Text(artistNames(album.track.artists))
|
Text(artistNames(album.artists))
|
||||||
.font(.footnote)
|
.font(.footnote)
|
||||||
.lineLimit(1)
|
.lineLimit(1)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
|
|||||||
Reference in new issue
Block a user