Add Library Albums screen and playback
This commit is contained in:
1 parent
e397c93fd5
commit
2cae15ab1c
2 files changed
+276
-1
No files matched your search
@@ -0,0 +1,270 @@
|
|||||||
|
import Combine
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
final class LibraryAlbumsViewModel: ObservableObject {
|
||||||
|
@Published private(set) var albums: [Album]
|
||||||
|
@Published private(set) var isLoading = false
|
||||||
|
@Published private(set) var isPreparingPlayback = false
|
||||||
|
@Published var playbackErrorMessage: String?
|
||||||
|
@Published private(set) var errorMessage: String?
|
||||||
|
|
||||||
|
private let api: MusicAPI
|
||||||
|
private var hasLoaded: Bool
|
||||||
|
private var cachedTracks: [Track]?
|
||||||
|
|
||||||
|
init(api: MusicAPI? = nil, albums: [Album]? = nil) {
|
||||||
|
self.api = api ?? MusicAPI()
|
||||||
|
self.albums = albums ?? []
|
||||||
|
hasLoaded = albums != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
static var preview: LibraryAlbumsViewModel {
|
||||||
|
let artist = Artist(id: "library-artist", name: "Example Artist", file: nil)
|
||||||
|
let albums = [
|
||||||
|
Album(
|
||||||
|
id: "library-album-1",
|
||||||
|
name: "First Light",
|
||||||
|
artists: [.value(artist)],
|
||||||
|
file: nil,
|
||||||
|
genre: nil
|
||||||
|
),
|
||||||
|
Album(
|
||||||
|
id: "library-album-2",
|
||||||
|
name: "Open Roads",
|
||||||
|
artists: [.value(artist)],
|
||||||
|
file: nil,
|
||||||
|
genre: nil
|
||||||
|
),
|
||||||
|
]
|
||||||
|
return LibraryAlbumsViewModel(albums: albums)
|
||||||
|
}
|
||||||
|
|
||||||
|
func load(forceRefresh: Bool = false) async {
|
||||||
|
guard !isLoading, forceRefresh || !hasLoaded else { return }
|
||||||
|
isLoading = true
|
||||||
|
errorMessage = nil
|
||||||
|
defer { isLoading = false }
|
||||||
|
|
||||||
|
do {
|
||||||
|
albums = try await api.savedAlbums()
|
||||||
|
cachedTracks = nil
|
||||||
|
hasLoaded = true
|
||||||
|
} catch {
|
||||||
|
errorMessage = error.localizedDescription
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tracksForPlayback() async throws -> [Track] {
|
||||||
|
if let cachedTracks { return cachedTracks }
|
||||||
|
|
||||||
|
isPreparingPlayback = true
|
||||||
|
playbackErrorMessage = nil
|
||||||
|
defer { isPreparingPlayback = false }
|
||||||
|
|
||||||
|
var tracks: [Track] = []
|
||||||
|
let pageSize = 100
|
||||||
|
for album in albums {
|
||||||
|
var skip = 0
|
||||||
|
while true {
|
||||||
|
let page = try await api.albumTracks(id: album.id, limit: pageSize, skip: skip)
|
||||||
|
tracks.append(contentsOf: page)
|
||||||
|
guard page.count == pageSize else { break }
|
||||||
|
skip += page.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cachedTracks = tracks
|
||||||
|
return tracks
|
||||||
|
}
|
||||||
|
|
||||||
|
func artworkURL(for album: Album) -> URL? {
|
||||||
|
guard let file = album.file else { return nil }
|
||||||
|
return api.client.configuration.mediaURL(for: file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibraryAlbumsView: View {
|
||||||
|
@Environment(\.dismiss) private var dismiss
|
||||||
|
@EnvironmentObject private var player: PlayerManager
|
||||||
|
@StateObject private var model: LibraryAlbumsViewModel
|
||||||
|
|
||||||
|
private let columns = [GridItem(.flexible(), spacing: 14), GridItem(.flexible(), spacing: 14)]
|
||||||
|
|
||||||
|
init(viewModel: LibraryAlbumsViewModel? = nil) {
|
||||||
|
_model = StateObject(wrappedValue: viewModel ?? LibraryAlbumsViewModel())
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
ScrollView {
|
||||||
|
VStack(alignment: .leading, spacing: 0) {
|
||||||
|
topBar
|
||||||
|
|
||||||
|
Text("Albums")
|
||||||
|
.font(.system(size: 34, weight: .bold))
|
||||||
|
.padding(.top, 22)
|
||||||
|
.padding(.bottom, 18)
|
||||||
|
|
||||||
|
if !model.albums.isEmpty {
|
||||||
|
playbackActions
|
||||||
|
.padding(.bottom, 20)
|
||||||
|
}
|
||||||
|
|
||||||
|
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() }
|
||||||
|
.alert("Couldn't Start Playback", isPresented: Binding(
|
||||||
|
get: { model.playbackErrorMessage != nil },
|
||||||
|
set: { if !$0 { model.playbackErrorMessage = nil } }
|
||||||
|
)) {
|
||||||
|
Button("OK", role: .cancel) { model.playbackErrorMessage = nil }
|
||||||
|
} message: {
|
||||||
|
Text(model.playbackErrorMessage ?? "Please try again.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var playbackActions: some View {
|
||||||
|
HStack(spacing: 14) {
|
||||||
|
playbackButton("Play", symbol: "play.fill") {
|
||||||
|
playSavedAlbums(shuffled: false)
|
||||||
|
}
|
||||||
|
playbackButton("Shuffle", symbol: "shuffle") {
|
||||||
|
playSavedAlbums(shuffled: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func playbackButton(
|
||||||
|
_ title: String,
|
||||||
|
symbol: String,
|
||||||
|
action: @escaping () -> Void
|
||||||
|
) -> some View {
|
||||||
|
Button(action: action) {
|
||||||
|
Group {
|
||||||
|
if model.isPreparingPlayback {
|
||||||
|
ProgressView()
|
||||||
|
.tint(.red)
|
||||||
|
} else {
|
||||||
|
Label(title, systemImage: symbol)
|
||||||
|
.font(.system(size: 18, weight: .semibold))
|
||||||
|
.foregroundStyle(.red)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.frame(height: 56)
|
||||||
|
.background(.white.opacity(0.10), in: Capsule())
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.disabled(model.isPreparingPlayback)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func playSavedAlbums(shuffled: Bool) {
|
||||||
|
Task {
|
||||||
|
do {
|
||||||
|
let tracks = try await model.tracksForPlayback()
|
||||||
|
guard !tracks.isEmpty else {
|
||||||
|
model.playbackErrorMessage = "These albums don't contain any playable songs."
|
||||||
|
return
|
||||||
|
}
|
||||||
|
player.play(tracks, shuffled: shuffled)
|
||||||
|
} catch {
|
||||||
|
model.playbackErrorMessage = error.localizedDescription
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private var content: some View {
|
||||||
|
if model.isLoading && model.albums.isEmpty {
|
||||||
|
ProgressView("Loading your albums…")
|
||||||
|
.tint(.red)
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.padding(.top, 44)
|
||||||
|
} else if let errorMessage = model.errorMessage, model.albums.isEmpty {
|
||||||
|
ContentUnavailableView {
|
||||||
|
Label("Couldn't Load Albums", systemImage: "wifi.exclamationmark")
|
||||||
|
} description: {
|
||||||
|
Text(errorMessage)
|
||||||
|
} actions: {
|
||||||
|
Button("Try Again") {
|
||||||
|
Task { await model.load(forceRefresh: true) }
|
||||||
|
}
|
||||||
|
.tint(.red)
|
||||||
|
}
|
||||||
|
.padding(.top, 28)
|
||||||
|
} else if model.albums.isEmpty {
|
||||||
|
ContentUnavailableView(
|
||||||
|
"No Saved Albums",
|
||||||
|
systemImage: "square.stack",
|
||||||
|
description: Text("Albums from songs you add to your Library will appear here.")
|
||||||
|
)
|
||||||
|
.padding(.top, 28)
|
||||||
|
} else {
|
||||||
|
LazyVGrid(columns: columns, alignment: .leading, spacing: 20) {
|
||||||
|
ForEach(model.albums) { album in
|
||||||
|
NavigationLink {
|
||||||
|
AlbumDetailView(albumID: album.id)
|
||||||
|
} label: {
|
||||||
|
albumCard(album)
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func albumCard(_ album: Album) -> some View {
|
||||||
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
|
AlbumArtworkView(url: model.artworkURL(for: album))
|
||||||
|
Text(album.name)
|
||||||
|
.font(.subheadline.weight(.medium))
|
||||||
|
.lineLimit(2)
|
||||||
|
.foregroundStyle(.primary)
|
||||||
|
Text(artistNames(album.artists))
|
||||||
|
.font(.footnote)
|
||||||
|
.lineLimit(1)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
.accessibilityElement(children: .combine)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func artistNames(_ references: [APIReference<Artist>]) -> String {
|
||||||
|
let names = references.compactMap { reference -> String? in
|
||||||
|
guard case .value(let artist) = reference else { return nil }
|
||||||
|
return artist.name
|
||||||
|
}
|
||||||
|
return names.isEmpty ? "Unknown Artist" : names.joined(separator: ", ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibraryAlbumsView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
NavigationStack {
|
||||||
|
LibraryAlbumsView(viewModel: .preview)
|
||||||
|
}
|
||||||
|
.environmentObject(PlayerManager())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -87,7 +87,12 @@ struct LibraryView: View {
|
|||||||
private var categoryList: some View {
|
private var categoryList: some View {
|
||||||
VStack(spacing: 0) {
|
VStack(spacing: 0) {
|
||||||
categoryRow("Artists", symbol: "mic")
|
categoryRow("Artists", symbol: "mic")
|
||||||
categoryRow("Albums", symbol: "square.stack")
|
NavigationLink {
|
||||||
|
LibraryAlbumsView()
|
||||||
|
} label: {
|
||||||
|
categoryRow("Albums", symbol: "square.stack")
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
NavigationLink {
|
NavigationLink {
|
||||||
LibrarySongsView()
|
LibrarySongsView()
|
||||||
} label: {
|
} label: {
|
||||||
|
|||||||
Reference in new issue
Block a user