Add Library Songs screen
This commit is contained in:
1 parent
9188b2084a
commit
8f330e86c1
3 files changed
+215
-1
No files matched your search
@@ -0,0 +1,203 @@
|
|||||||
|
import Combine
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
final class LibrarySongsViewModel: ObservableObject {
|
||||||
|
@Published private(set) var tracks: [Track]
|
||||||
|
@Published private(set) var isLoading = false
|
||||||
|
@Published private(set) var errorMessage: String?
|
||||||
|
|
||||||
|
private let api: MusicAPI
|
||||||
|
private var hasLoaded: Bool
|
||||||
|
|
||||||
|
init(api: MusicAPI? = nil, tracks: [Track]? = nil) {
|
||||||
|
self.api = api ?? MusicAPI()
|
||||||
|
self.tracks = tracks ?? []
|
||||||
|
hasLoaded = tracks != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
static var preview: LibrarySongsViewModel {
|
||||||
|
let artist = Artist(id: "library-artist", name: "Example Artist", file: nil)
|
||||||
|
let album = TrackAlbum(id: "library-album", name: "Example Album", file: "")
|
||||||
|
let tracks = [
|
||||||
|
Track(
|
||||||
|
id: "library-track-1",
|
||||||
|
name: "First Song",
|
||||||
|
album: .value(album),
|
||||||
|
artists: [.value(artist)],
|
||||||
|
fileDir: "",
|
||||||
|
durationInSeconds: 200,
|
||||||
|
lyrics: nil
|
||||||
|
),
|
||||||
|
Track(
|
||||||
|
id: "library-track-2",
|
||||||
|
name: "Second Song",
|
||||||
|
album: .value(album),
|
||||||
|
artists: [.value(artist)],
|
||||||
|
fileDir: "",
|
||||||
|
durationInSeconds: 215,
|
||||||
|
lyrics: nil
|
||||||
|
),
|
||||||
|
]
|
||||||
|
return LibrarySongsViewModel(tracks: tracks)
|
||||||
|
}
|
||||||
|
|
||||||
|
func load(forceRefresh: Bool = false) async {
|
||||||
|
guard !isLoading, forceRefresh || !hasLoaded else { return }
|
||||||
|
isLoading = true
|
||||||
|
errorMessage = nil
|
||||||
|
defer { isLoading = false }
|
||||||
|
|
||||||
|
do {
|
||||||
|
tracks = try await api.savedTracks()
|
||||||
|
hasLoaded = true
|
||||||
|
} catch {
|
||||||
|
errorMessage = error.localizedDescription
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func remove(_ track: Track) {
|
||||||
|
withAnimation(.easeOut(duration: 0.2)) {
|
||||||
|
tracks.removeAll { $0.id == track.id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibrarySongsView: View {
|
||||||
|
@Environment(\.dismiss) private var dismiss
|
||||||
|
@EnvironmentObject private var player: PlayerManager
|
||||||
|
@StateObject private var model: LibrarySongsViewModel
|
||||||
|
|
||||||
|
init(viewModel: LibrarySongsViewModel? = nil) {
|
||||||
|
_model = StateObject(wrappedValue: viewModel ?? LibrarySongsViewModel())
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
ScrollView {
|
||||||
|
VStack(alignment: .leading, spacing: 0) {
|
||||||
|
topBar
|
||||||
|
|
||||||
|
Text("Songs")
|
||||||
|
.font(.system(size: 34, weight: .bold))
|
||||||
|
.padding(.top, 22)
|
||||||
|
.padding(.bottom, 18)
|
||||||
|
|
||||||
|
if !model.tracks.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() }
|
||||||
|
}
|
||||||
|
|
||||||
|
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") {
|
||||||
|
player.play(model.tracks)
|
||||||
|
}
|
||||||
|
playbackButton("Shuffle", symbol: "shuffle") {
|
||||||
|
player.play(model.tracks, shuffled: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func playbackButton(
|
||||||
|
_ title: String,
|
||||||
|
symbol: String,
|
||||||
|
action: @escaping () -> Void
|
||||||
|
) -> some View {
|
||||||
|
Button(action: action) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private var content: some View {
|
||||||
|
if model.isLoading && model.tracks.isEmpty {
|
||||||
|
ProgressView("Loading your songs…")
|
||||||
|
.tint(.red)
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
.padding(.top, 44)
|
||||||
|
} else if let errorMessage = model.errorMessage, model.tracks.isEmpty {
|
||||||
|
ContentUnavailableView {
|
||||||
|
Label("Couldn't Load Songs", systemImage: "wifi.exclamationmark")
|
||||||
|
} description: {
|
||||||
|
Text(errorMessage)
|
||||||
|
} actions: {
|
||||||
|
Button("Try Again") {
|
||||||
|
Task { await model.load(forceRefresh: true) }
|
||||||
|
}
|
||||||
|
.tint(.red)
|
||||||
|
}
|
||||||
|
.padding(.top, 28)
|
||||||
|
} else if model.tracks.isEmpty {
|
||||||
|
ContentUnavailableView(
|
||||||
|
"No Saved Songs",
|
||||||
|
systemImage: "music.note.list",
|
||||||
|
description: Text("Songs you add to your Library will appear here.")
|
||||||
|
)
|
||||||
|
.padding(.top, 28)
|
||||||
|
} else {
|
||||||
|
LazyVStack(spacing: 0) {
|
||||||
|
ForEach(Array(model.tracks.enumerated()), id: \.element.id) { index, track in
|
||||||
|
TrackRowView(
|
||||||
|
track: track,
|
||||||
|
index: index,
|
||||||
|
showsArtwork: true,
|
||||||
|
onPlayOnlyThis: { player.play(track) },
|
||||||
|
onPlay: { player.play(model.tracks, startingAt: index) },
|
||||||
|
isInLibrary: true,
|
||||||
|
onRemoveFromLibrary: model.remove
|
||||||
|
)
|
||||||
|
|
||||||
|
if index < model.tracks.count - 1 {
|
||||||
|
Divider()
|
||||||
|
.overlay(.white.opacity(0.16))
|
||||||
|
.padding(.leading, 70)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LibrarySongsView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
NavigationStack {
|
||||||
|
LibrarySongsView(viewModel: .preview)
|
||||||
|
}
|
||||||
|
.environmentObject(PlayerManager())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -88,8 +88,13 @@ struct LibraryView: View {
|
|||||||
VStack(spacing: 0) {
|
VStack(spacing: 0) {
|
||||||
categoryRow("Artists", symbol: "mic")
|
categoryRow("Artists", symbol: "mic")
|
||||||
categoryRow("Albums", symbol: "square.stack")
|
categoryRow("Albums", symbol: "square.stack")
|
||||||
|
NavigationLink {
|
||||||
|
LibrarySongsView()
|
||||||
|
} label: {
|
||||||
categoryRow("Songs", symbol: "music.note")
|
categoryRow("Songs", symbol: "music.note")
|
||||||
}
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func categoryRow(_ title: String, symbol: String) -> some View {
|
private func categoryRow(_ title: String, symbol: String) -> some View {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ struct TrackRowView: View {
|
|||||||
var subtitlePrefix: String? = nil
|
var subtitlePrefix: String? = nil
|
||||||
var onPlayOnlyThis: (() -> Void)? = nil
|
var onPlayOnlyThis: (() -> Void)? = nil
|
||||||
var onPlay: (() -> Void)? = nil
|
var onPlay: (() -> Void)? = nil
|
||||||
|
var isInLibrary = false
|
||||||
|
var onRemoveFromLibrary: ((Track) -> Void)? = nil
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
HStack(spacing: 14) {
|
HStack(spacing: 14) {
|
||||||
@@ -124,7 +126,10 @@ struct TrackRowView: View {
|
|||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
}
|
}
|
||||||
.accessibilityLabel("Options for \(track.name)")
|
.accessibilityLabel("Options for \(track.name)")
|
||||||
.task { await loadLibraryState() }
|
.task {
|
||||||
|
if isInLibrary { isSaved = true }
|
||||||
|
else { await loadLibraryState() }
|
||||||
|
}
|
||||||
.alert("Playlists are not implemented", isPresented: $showingPlaylistNotice) {
|
.alert("Playlists are not implemented", isPresented: $showingPlaylistNotice) {
|
||||||
Button("OK", role: .cancel) {}
|
Button("OK", role: .cancel) {}
|
||||||
}
|
}
|
||||||
@@ -169,6 +174,7 @@ struct TrackRowView: View {
|
|||||||
if isSaved == true {
|
if isSaved == true {
|
||||||
try await api.removeTrack(id: track.id)
|
try await api.removeTrack(id: track.id)
|
||||||
isSaved = false
|
isSaved = false
|
||||||
|
onRemoveFromLibrary?(track)
|
||||||
} else {
|
} else {
|
||||||
try await api.saveTrack(id: track.id)
|
try await api.saveTrack(id: track.id)
|
||||||
isSaved = true
|
isSaved = true
|
||||||
|
|||||||
Reference in new issue
Block a user