155 lines
5.3 KiB
Swift
155 lines
5.3 KiB
Swift
import SwiftUI
|
|
|
|
enum LibraryLayout: String, CaseIterable, Identifiable {
|
|
case grid
|
|
case list
|
|
|
|
var id: String { rawValue }
|
|
var label: String { self == .grid ? "Kachelansicht" : "Listenansicht" }
|
|
var systemImage: String { self == .grid ? "square.grid.2x2" : "list.bullet" }
|
|
}
|
|
|
|
struct LibraryListView: View {
|
|
let items: [LibraryItem]
|
|
let onSelect: (LibraryItem) -> Void
|
|
|
|
var body: some View {
|
|
ScrollView {
|
|
LazyVStack(spacing: 0) {
|
|
ForEach(Array(items.enumerated()), id: \.element.id) { idx, item in
|
|
LibraryListRow(item: item)
|
|
.contentShape(Rectangle())
|
|
.onTapGesture { onSelect(item) }
|
|
if idx < items.count - 1 {
|
|
Divider().padding(.leading, 76)
|
|
}
|
|
}
|
|
}
|
|
.padding(.vertical, 4)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct LibraryListRow: View {
|
|
@Environment(AppState.self) private var app
|
|
let item: LibraryItem
|
|
|
|
var body: some View {
|
|
HStack(spacing: 12) {
|
|
cover
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(item.title)
|
|
.font(.headline)
|
|
.lineLimit(1)
|
|
Text(item.author)
|
|
.font(.subheadline)
|
|
.foregroundStyle(.secondary)
|
|
.lineLimit(1)
|
|
let fraction = app.progressFraction(itemId: item.id, episodeId: item.episodeId)
|
|
if fraction > 0 {
|
|
GeometryReader { geo in
|
|
ZStack(alignment: .leading) {
|
|
RoundedRectangle(cornerRadius: 1.5).fill(Color.gray.opacity(0.3))
|
|
RoundedRectangle(cornerRadius: 1.5).fill(Color.green)
|
|
.frame(width: max(2, geo.size.width * fraction))
|
|
}
|
|
}
|
|
.frame(height: 3)
|
|
.padding(.top, 2)
|
|
.padding(.trailing, 40)
|
|
}
|
|
}
|
|
Spacer(minLength: 8)
|
|
if item.durationSeconds > 0 {
|
|
Text(formatDuration(item.durationSeconds))
|
|
.font(.caption.monospacedDigit())
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
downloadStatus
|
|
.frame(width: 28)
|
|
}
|
|
.padding(.horizontal, 16)
|
|
.padding(.vertical, 8)
|
|
.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)
|
|
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: 48, height: 48)
|
|
.clipShape(RoundedRectangle(cornerRadius: 4))
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var downloadStatus: some View {
|
|
let state = app.downloads.state(for: item.downloadKey)
|
|
switch state {
|
|
case .downloaded:
|
|
Image(systemName: "checkmark.circle.fill")
|
|
.foregroundStyle(.white, .green)
|
|
.font(.title3)
|
|
case .downloading(let p):
|
|
DownloadProgressRing(progress: p)
|
|
.frame(width: 24, height: 24)
|
|
case .failed:
|
|
Image(systemName: "exclamationmark.circle.fill")
|
|
.foregroundStyle(.white, .red)
|
|
.font(.title3)
|
|
case .notDownloaded:
|
|
EmptyView()
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var downloadMenuItems: some View {
|
|
let key = item.downloadKey
|
|
let state = app.downloads.state(for: key)
|
|
if item.isPodcastContainer {
|
|
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")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func formatDuration(_ seconds: Double) -> String {
|
|
guard seconds.isFinite, seconds > 0 else { return "" }
|
|
let total = Int(seconds)
|
|
let h = total / 3600
|
|
let m = (total % 3600) / 60
|
|
if h > 0 { return "\(h) h \(m) min" }
|
|
return "\(m) min"
|
|
}
|
|
}
|