Share Library controls and use native navigation
This commit is contained in:
1 parent
89e81f0eb0
commit
de617ad1d2
4 files changed
+182
-214
No files matched your search
@@ -84,7 +84,6 @@ final class LibraryAlbumsViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
struct LibraryAlbumsView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@EnvironmentObject private var player: PlayerManager
|
||||
@StateObject private var model: LibraryAlbumsViewModel
|
||||
|
||||
@@ -97,15 +96,13 @@ struct LibraryAlbumsView: View {
|
||||
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
|
||||
LibraryPlaybackButtons(isLoading: model.isPreparingPlayback) {
|
||||
playSavedAlbums(shuffled: false)
|
||||
} onShuffle: {
|
||||
playSavedAlbums(shuffled: true)
|
||||
}
|
||||
.padding(.top, 8)
|
||||
.padding(.bottom, 20)
|
||||
}
|
||||
|
||||
@@ -117,7 +114,9 @@ struct LibraryAlbumsView: View {
|
||||
.scrollIndicators(.hidden)
|
||||
.background(Color.black.ignoresSafeArea())
|
||||
.foregroundStyle(.white)
|
||||
.toolbar(.hidden, for: .navigationBar)
|
||||
.navigationTitle("Albums")
|
||||
.navigationBarTitleDisplayMode(.large)
|
||||
.toolbar(.visible, for: .navigationBar)
|
||||
.refreshable { await model.load(forceRefresh: true) }
|
||||
.task { await model.load() }
|
||||
.alert("Couldn't Start Playback", isPresented: Binding(
|
||||
@@ -130,57 +129,6 @@ struct LibraryAlbumsView: View {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -198,31 +146,17 @@ struct LibraryAlbumsView: View {
|
||||
|
||||
@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 {
|
||||
LibraryCollectionContent(
|
||||
isLoading: model.isLoading,
|
||||
isEmpty: model.albums.isEmpty,
|
||||
errorMessage: model.errorMessage,
|
||||
loadingMessage: "Loading your albums…",
|
||||
errorTitle: "Couldn't Load Albums",
|
||||
emptyTitle: "No Saved Albums",
|
||||
emptySymbol: "square.stack",
|
||||
emptyDescription: "Albums from songs you add to your Library will appear here.",
|
||||
onRetry: { Task { await model.load(forceRefresh: true) } }
|
||||
) {
|
||||
LazyVGrid(columns: columns, alignment: .leading, spacing: 20) {
|
||||
ForEach(model.albums) { album in
|
||||
NavigationLink {
|
||||
|
||||
@@ -44,7 +44,6 @@ final class LibraryArtistsViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
struct LibraryArtistsView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@StateObject private var model: LibraryArtistsViewModel
|
||||
|
||||
private let columns = [GridItem(.flexible(), spacing: 14), GridItem(.flexible(), spacing: 14)]
|
||||
@@ -56,13 +55,6 @@ struct LibraryArtistsView: View {
|
||||
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)
|
||||
@@ -71,54 +63,26 @@ struct LibraryArtistsView: View {
|
||||
.scrollIndicators(.hidden)
|
||||
.background(Color.black.ignoresSafeArea())
|
||||
.foregroundStyle(.white)
|
||||
.toolbar(.hidden, for: .navigationBar)
|
||||
.navigationTitle("Artists")
|
||||
.navigationBarTitleDisplayMode(.large)
|
||||
.toolbar(.visible, 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 {
|
||||
LibraryCollectionContent(
|
||||
isLoading: model.isLoading,
|
||||
isEmpty: model.artists.isEmpty,
|
||||
errorMessage: model.errorMessage,
|
||||
loadingMessage: "Loading your artists…",
|
||||
errorTitle: "Couldn't Load Artists",
|
||||
emptyTitle: "No Saved Artists",
|
||||
emptySymbol: "mic",
|
||||
emptyDescription: "Artists from songs you add to your Library will appear here.",
|
||||
onRetry: { Task { await model.load(forceRefresh: true) } }
|
||||
) {
|
||||
LazyVGrid(columns: columns, alignment: .leading, spacing: 20) {
|
||||
ForEach(model.artists) { artist in
|
||||
NavigationLink {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import SwiftUI
|
||||
|
||||
struct LibraryCollectionContent<Content: View>: View {
|
||||
let isLoading: Bool
|
||||
let isEmpty: Bool
|
||||
let errorMessage: String?
|
||||
let loadingMessage: String
|
||||
let errorTitle: String
|
||||
let emptyTitle: String
|
||||
let emptySymbol: String
|
||||
let emptyDescription: String
|
||||
let onRetry: () -> Void
|
||||
private let content: Content
|
||||
|
||||
init(
|
||||
isLoading: Bool,
|
||||
isEmpty: Bool,
|
||||
errorMessage: String?,
|
||||
loadingMessage: String,
|
||||
errorTitle: String,
|
||||
emptyTitle: String,
|
||||
emptySymbol: String,
|
||||
emptyDescription: String,
|
||||
onRetry: @escaping () -> Void,
|
||||
@ViewBuilder content: () -> Content
|
||||
) {
|
||||
self.isLoading = isLoading
|
||||
self.isEmpty = isEmpty
|
||||
self.errorMessage = errorMessage
|
||||
self.loadingMessage = loadingMessage
|
||||
self.errorTitle = errorTitle
|
||||
self.emptyTitle = emptyTitle
|
||||
self.emptySymbol = emptySymbol
|
||||
self.emptyDescription = emptyDescription
|
||||
self.onRetry = onRetry
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
var body: some View {
|
||||
if isLoading && isEmpty {
|
||||
ProgressView(loadingMessage)
|
||||
.tint(.red)
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.top, 44)
|
||||
} else if let errorMessage, isEmpty {
|
||||
ContentUnavailableView {
|
||||
Label(errorTitle, systemImage: "wifi.exclamationmark")
|
||||
} description: {
|
||||
Text(errorMessage)
|
||||
} actions: {
|
||||
Button("Try Again", action: onRetry)
|
||||
.tint(.red)
|
||||
}
|
||||
.padding(.top, 28)
|
||||
} else if isEmpty {
|
||||
ContentUnavailableView(
|
||||
emptyTitle,
|
||||
systemImage: emptySymbol,
|
||||
description: Text(emptyDescription)
|
||||
)
|
||||
.padding(.top, 28)
|
||||
} else {
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LibraryCollectionContent_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
LibraryCollectionContent(
|
||||
isLoading: false,
|
||||
isEmpty: true,
|
||||
errorMessage: nil,
|
||||
loadingMessage: "Loading your songs…",
|
||||
errorTitle: "Couldn't Load Songs",
|
||||
emptyTitle: "No Saved Songs",
|
||||
emptySymbol: "music.note.list",
|
||||
emptyDescription: "Songs added to your Library will appear here.",
|
||||
onRetry: {}
|
||||
) {
|
||||
EmptyView()
|
||||
}
|
||||
.preferredColorScheme(.dark)
|
||||
}
|
||||
}
|
||||
|
||||
struct LibraryPlaybackButtons: View {
|
||||
var isLoading = false
|
||||
let onPlay: () -> Void
|
||||
let onShuffle: () -> Void
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 14) {
|
||||
button("Play", symbol: "play.fill", action: onPlay)
|
||||
button("Shuffle", symbol: "shuffle", action: onShuffle)
|
||||
}
|
||||
}
|
||||
|
||||
private func button(_ title: String, symbol: String, action: @escaping () -> Void) -> some View {
|
||||
Button(action: action) {
|
||||
Group {
|
||||
if isLoading {
|
||||
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(isLoading)
|
||||
.accessibilityLabel(title)
|
||||
}
|
||||
}
|
||||
|
||||
struct LibraryPlaybackButtons_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
LibraryPlaybackButtons(onPlay: {}, onShuffle: {})
|
||||
.padding()
|
||||
.preferredColorScheme(.dark)
|
||||
.previewLayout(.sizeThatFits)
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,6 @@ final class LibrarySongsViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
struct LibrarySongsView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@EnvironmentObject private var player: PlayerManager
|
||||
@StateObject private var model: LibrarySongsViewModel
|
||||
|
||||
@@ -75,15 +74,12 @@ struct LibrarySongsView: View {
|
||||
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
|
||||
LibraryPlaybackButtons(
|
||||
onPlay: { player.play(model.tracks) },
|
||||
onShuffle: { player.play(model.tracks, shuffled: true) }
|
||||
)
|
||||
.padding(.top, 8)
|
||||
.padding(.bottom, 20)
|
||||
}
|
||||
|
||||
@@ -95,81 +91,26 @@ struct LibrarySongsView: View {
|
||||
.scrollIndicators(.hidden)
|
||||
.background(Color.black.ignoresSafeArea())
|
||||
.foregroundStyle(.white)
|
||||
.toolbar(.hidden, for: .navigationBar)
|
||||
.navigationTitle("Songs")
|
||||
.navigationBarTitleDisplayMode(.large)
|
||||
.toolbar(.visible, 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 {
|
||||
LibraryCollectionContent(
|
||||
isLoading: model.isLoading,
|
||||
isEmpty: model.tracks.isEmpty,
|
||||
errorMessage: model.errorMessage,
|
||||
loadingMessage: "Loading your songs…",
|
||||
errorTitle: "Couldn't Load Songs",
|
||||
emptyTitle: "No Saved Songs",
|
||||
emptySymbol: "music.note.list",
|
||||
emptyDescription: "Songs you add to your Library will appear here.",
|
||||
onRetry: { Task { await model.load(forceRefresh: true) } }
|
||||
) {
|
||||
LazyVStack(spacing: 0) {
|
||||
ForEach(Array(model.tracks.enumerated()), id: \.element.id) { index, track in
|
||||
TrackRowView(
|
||||
|
||||
Reference in new issue
Block a user