176 lines
5.8 KiB
Swift
176 lines
5.8 KiB
Swift
import Combine
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
private final class LibraryViewModel: ObservableObject {
|
|
@Published private(set) var recentlySavedAlbums: [Album] = []
|
|
@Published private(set) var isLoading = false
|
|
@Published private(set) var errorMessage: String?
|
|
|
|
private let api = MusicAPI()
|
|
|
|
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.savedTracks()
|
|
var seenAlbumIDs = Set<String>()
|
|
recentlySavedAlbums = tracks.reversed().compactMap { track in
|
|
guard case .value(let album) = track.album,
|
|
seenAlbumIDs.insert(album.id).inserted else { return nil }
|
|
return Album(
|
|
id: album.id,
|
|
name: album.name,
|
|
artists: track.artists,
|
|
file: album.file,
|
|
genre: nil
|
|
)
|
|
}
|
|
} 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)
|
|
}
|
|
}
|
|
|
|
struct LibraryView: View {
|
|
@StateObject private var model = LibraryViewModel()
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ScrollView {
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
Text("Library")
|
|
.font(.system(size: 34, weight: .bold))
|
|
.padding(.top, 4)
|
|
.padding(.bottom, 16)
|
|
|
|
categoryList
|
|
|
|
Text("Recently Added")
|
|
.font(.title2.weight(.bold))
|
|
.padding(.top, 26)
|
|
.padding(.bottom, 12)
|
|
|
|
recentlyPlayedSection
|
|
}
|
|
.padding(.horizontal, 20)
|
|
.padding(.bottom, 24)
|
|
}
|
|
.scrollIndicators(.hidden)
|
|
.background(Color.black.ignoresSafeArea())
|
|
.foregroundStyle(.white)
|
|
.toolbar(.hidden, for: .navigationBar)
|
|
.refreshable { await model.loadRecentlySaved(forceRefresh: true) }
|
|
}
|
|
.task { await model.loadRecentlySaved() }
|
|
}
|
|
|
|
private var categoryList: some View {
|
|
VStack(spacing: 0) {
|
|
NavigationLink {
|
|
LibraryArtistsView()
|
|
} label: {
|
|
categoryRow("Artists", symbol: "mic")
|
|
}
|
|
.buttonStyle(.plain)
|
|
NavigationLink {
|
|
LibraryAlbumsView()
|
|
} label: {
|
|
categoryRow("Albums", symbol: "square.stack")
|
|
}
|
|
.buttonStyle(.plain)
|
|
NavigationLink {
|
|
LibrarySongsView()
|
|
} label: {
|
|
categoryRow("Songs", symbol: "music.note")
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
|
|
private func categoryRow(_ title: String, symbol: String) -> some View {
|
|
HStack(spacing: 20) {
|
|
Image(systemName: symbol)
|
|
.font(.system(size: 27, weight: .medium))
|
|
.foregroundStyle(.red)
|
|
.frame(width: 40)
|
|
Text(title)
|
|
.font(.system(size: 20))
|
|
.foregroundStyle(.primary)
|
|
Spacer()
|
|
Image(systemName: "chevron.right")
|
|
.font(.system(size: 15, weight: .semibold))
|
|
.foregroundStyle(.tertiary)
|
|
}
|
|
.frame(minHeight: 70)
|
|
.overlay(alignment: .bottom) {
|
|
Rectangle()
|
|
.fill(.white.opacity(0.16))
|
|
.frame(height: 1)
|
|
.padding(.leading, 60)
|
|
}
|
|
.accessibilityElement(children: .combine)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var recentlyPlayedSection: some View {
|
|
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 Added", systemImage: "wifi.exclamationmark")
|
|
} description: {
|
|
Text(errorMessage)
|
|
} actions: {
|
|
Button("Try Again") {
|
|
Task { await model.loadRecentlySaved(forceRefresh: true) }
|
|
}
|
|
.tint(.red)
|
|
}
|
|
} 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.recentlySavedAlbums) { album in
|
|
NavigationLink {
|
|
AlbumDetailView(albumID: album.id)
|
|
} label: {
|
|
recentAlbumCard(album)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
.padding(.vertical, 2)
|
|
}
|
|
.scrollIndicators(.hidden)
|
|
}
|
|
}
|
|
|
|
private func recentAlbumCard(_ album: Album) -> some View {
|
|
AlbumCardView(album: album, artworkURL: model.artworkURL(for: album))
|
|
.frame(width: 170, alignment: .topLeading)
|
|
}
|
|
}
|
|
|
|
struct LibraryView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
LibraryView()
|
|
.environmentObject(PlayerManager())
|
|
}
|
|
}
|