130 lines
3.8 KiB
Swift
130 lines
3.8 KiB
Swift
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)
|
|
}
|
|
}
|