159 lines
5.5 KiB
Swift
159 lines
5.5 KiB
Swift
import SwiftUI
|
|
|
|
struct LibraryItemCell: View {
|
|
@Environment(AppState.self) private var app
|
|
let item: LibraryItem
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
ZStack(alignment: .bottom) {
|
|
ZStack(alignment: .topTrailing) {
|
|
cover
|
|
downloadBadge
|
|
.padding(8)
|
|
}
|
|
CoverProgressBar(fraction: app.progressFraction(itemId: item.id, episodeId: item.episodeId))
|
|
.padding(.horizontal, 6)
|
|
.padding(.bottom, 6)
|
|
}
|
|
Text(item.title)
|
|
.font(.headline)
|
|
.lineLimit(2, reservesSpace: true)
|
|
.multilineTextAlignment(.leading)
|
|
Text(item.author)
|
|
.font(.subheadline)
|
|
.foregroundStyle(.secondary)
|
|
.lineLimit(1, reservesSpace: true)
|
|
}
|
|
.contextMenu {
|
|
downloadMenuItems
|
|
}
|
|
}
|
|
|
|
private var cover: some View {
|
|
Group {
|
|
if let url = app.client.coverURL(itemId: item.id) {
|
|
AsyncImage(url: url) { phase in
|
|
switch phase {
|
|
case .empty:
|
|
Rectangle().fill(.quaternary)
|
|
.overlay(ProgressView().controlSize(.small))
|
|
case .success(let img):
|
|
img.resizable().aspectRatio(contentMode: .fill)
|
|
case .failure:
|
|
Rectangle().fill(.quaternary)
|
|
.overlay(Image(systemName: "book.closed").foregroundStyle(.secondary))
|
|
@unknown default:
|
|
Rectangle().fill(.quaternary)
|
|
}
|
|
}
|
|
} else {
|
|
Rectangle().fill(.quaternary)
|
|
}
|
|
}
|
|
.frame(width: 180, height: 180)
|
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var downloadBadge: some View {
|
|
let state = app.downloads.state(for: item.syncKey)
|
|
switch state {
|
|
case .downloaded:
|
|
Image(systemName: "checkmark.circle.fill")
|
|
.foregroundStyle(.white, .green)
|
|
.font(.title3)
|
|
.shadow(radius: 2)
|
|
case .downloading(let p):
|
|
DownloadProgressRing(progress: p)
|
|
case .failed:
|
|
Image(systemName: "exclamationmark.circle.fill")
|
|
.foregroundStyle(.white, .red)
|
|
.font(.title3)
|
|
.shadow(radius: 2)
|
|
case .notDownloaded:
|
|
EmptyView()
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var downloadMenuItems: some View {
|
|
let key = item.syncKey
|
|
let state = app.downloads.state(for: key)
|
|
if item.isPodcastContainer {
|
|
// Whole-podcast downloads aren't supported; instructions only.
|
|
Text("Episoden zum Download in der Podcast-Ansicht auswählen")
|
|
} else {
|
|
switch state {
|
|
case .notDownloaded, .failed:
|
|
Button {
|
|
app.downloads.startDownload(item: item)
|
|
} label: {
|
|
Label("Für Offline herunterladen", systemImage: "arrow.down.circle")
|
|
}
|
|
case .downloading:
|
|
Button {
|
|
app.downloads.cancel(downloadKey: key)
|
|
} label: {
|
|
Label("Download abbrechen", systemImage: "xmark.circle")
|
|
}
|
|
case .downloaded:
|
|
Button(role: .destructive) {
|
|
app.downloads.delete(downloadKey: key)
|
|
} label: {
|
|
Label("Heruntergeladene Dateien löschen", systemImage: "trash")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Green progress bar drawn at the bottom of a cover (grid view).
|
|
/// Hidden completely when there's no known progress.
|
|
struct CoverProgressBar: View {
|
|
let fraction: Double
|
|
|
|
var body: some View {
|
|
if fraction > 0 {
|
|
GeometryReader { geo in
|
|
ZStack(alignment: .leading) {
|
|
RoundedRectangle(cornerRadius: 2, style: .continuous)
|
|
.fill(Color.black.opacity(0.55))
|
|
.frame(height: 4)
|
|
RoundedRectangle(cornerRadius: 2, style: .continuous)
|
|
.fill(Color.green)
|
|
.frame(width: max(2, geo.size.width * fraction), height: 4)
|
|
}
|
|
}
|
|
.frame(height: 4)
|
|
.shadow(color: .black.opacity(0.35), radius: 1, y: 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct DownloadProgressRing: View {
|
|
let progress: Double
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
Circle()
|
|
.fill(Color.black.opacity(0.75))
|
|
Circle()
|
|
.stroke(Color.white.opacity(0.25), lineWidth: 3)
|
|
.padding(4)
|
|
Circle()
|
|
.trim(from: 0, to: max(0.03, min(progress, 1)))
|
|
.stroke(Color.white, style: StrokeStyle(lineWidth: 3, lineCap: .round))
|
|
.rotationEffect(.degrees(-90))
|
|
.padding(4)
|
|
.animation(.easeInOut(duration: 0.25), value: progress)
|
|
Image(systemName: "arrow.down")
|
|
.font(.system(size: 12, weight: .bold))
|
|
.foregroundStyle(.white)
|
|
}
|
|
.frame(width: 32, height: 32)
|
|
.shadow(color: .black.opacity(0.4), radius: 3, x: 0, y: 1)
|
|
.help("Wird heruntergeladen … \(Int(progress * 100)) %")
|
|
}
|
|
}
|