Files
lichtkasten/Sources/Lichtkasten/Views/UploadWindow.swift
T

186 lines
9.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
import LichtkastenCore
/// ⌘U – upload dialog in the Leuchttisch look: destinations as cards, options of the chosen one, summary.
struct UploadWindow: View {
@EnvironmentObject var state: AppState
@Environment(\.dismissWindow) private var dismissWindow
@AppStorage("lastUploadTarget") private var target = ""
private var destination: FTPDestination? { state.settings.ftpDestinations.first { $0.id.uuidString == target } }
private var destIndex: Int? { state.settings.ftpDestinations.firstIndex { $0.id.uuidString == target } }
private var variant: UploadVariant {
target == "immich" ? state.settings.immich.variant : destination?.variant ?? .jpeg
}
private var files: [URL] { state.selectedItems.flatMap { state.uploadFiles(for: $0, variant: variant) } }
private var totalBytes: Int64 {
files.reduce(0) { sum, f in sum + (((try? FileManager.default.attributesOfItem(atPath: f.path)[.size]) as? NSNumber)?.int64Value ?? 0) }
}
var body: some View {
VStack(alignment: .leading, spacing: 0) {
// header
HStack(spacing: 14) {
Image(systemName: "icloud.and.arrow.up")
.font(.system(size: 20, weight: .semibold))
.foregroundStyle(LK.onAmber)
.frame(width: 42, height: 42)
.background(RoundedRectangle(cornerRadius: 11).fill(LK.amber))
VStack(alignment: .leading, spacing: 2) {
Text("Bilder hochladen").font(.system(size: 18, weight: .bold)).foregroundStyle(LK.text)
Text(summary).font(LK.mono(12)).foregroundStyle(LK.text2)
}
Spacer()
}
.padding(22)
Rectangle().fill(LK.border).frame(height: 1)
ScrollView {
VStack(alignment: .leading, spacing: 18) {
LKSectionLabel(title: "Ziel")
LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), spacing: 12)], spacing: 12) {
ForEach(state.settings.ftpDestinations) { d in
destinationCard(id: d.id.uuidString, icon: "server.rack", title: d.name,
subtitle: d.host.isEmpty ? "Server fehlt" : d.host,
detail: d.remoteDirectory, warning: credentialsMissing(d))
}
if state.settings.immich.enabled {
destinationCard(id: "immich", icon: "photo.stack", title: "Immich",
subtitle: URL(string: state.settings.immich.serverURL)?.host ?? state.settings.immich.serverURL,
detail: albumPreview, warning: false)
}
}
if let i = destIndex {
LKSectionLabel(title: "Optionen").padding(.top, 4)
VStack(spacing: 0) {
optionRow("Zielordner") {
TextField("/", text: $state.settings.ftpDestinations[i].remoteDirectory).textFieldStyle(.plain).lkField()
}
divider
optionRow("Umbenennen") {
TextField("leer = Originalname, z. B. {isodate}_{filenamebase}", text: $state.settings.ftpDestinations[i].renamePattern)
.textFieldStyle(.plain).lkField()
}
divider
optionRow("Dateien") {
LKSegmented(options: UploadVariant.allCases.map { ($0, $0.rawValue) },
selection: $state.settings.ftpDestinations[i].variant)
}
}
.background(RoundedRectangle(cornerRadius: 12).fill(LK.card))
.overlay(RoundedRectangle(cornerRadius: 12).stroke(LK.border))
} else if target == "immich" {
LKSectionLabel(title: "Optionen").padding(.top, 4)
VStack(spacing: 0) {
optionRow("Album-Muster") {
TextField("{event}", text: $state.settings.immich.albumName).textFieldStyle(.plain).lkField()
}
divider
optionRow("Dateien") {
LKSegmented(options: UploadVariant.allCases.map { ($0, $0.rawValue) }, selection: $state.settings.immich.variant)
}
}
.background(RoundedRectangle(cornerRadius: 12).fill(LK.card))
.overlay(RoundedRectangle(cornerRadius: 12).stroke(LK.border))
}
if files.count < state.selectedItems.count {
Label("\(state.selectedItems.count - files.count) Bild(er) ohne JPEG – zuerst in Lightroom exportieren (⌘⌥L).",
systemImage: "exclamationmark.triangle.fill")
.font(.system(size: 12)).foregroundStyle(LK.amber)
}
if let d = destination, credentialsMissing(d) {
Label("Benutzer oder Passwort fehlen – „Server verwalten …“", systemImage: "key.fill")
.font(.system(size: 12)).foregroundStyle(LK.red)
}
}
.padding(22)
}
Rectangle().fill(LK.border).frame(height: 1)
HStack {
SettingsLink { Text("Server verwalten …") }.buttonStyle(LKButtonStyle())
Spacer()
Button("Abbrechen") { dismissWindow(id: "upload") }
.buttonStyle(LKButtonStyle())
.keyboardShortcut(.cancelAction)
Button("\(files.count) Datei(en) hochladen") {
state.upload(to: target)
state.openWindow("tasks")
dismissWindow(id: "upload")
}
.buttonStyle(LKButtonStyle(primary: true))
.keyboardShortcut(.defaultAction)
.disabled(target.isEmpty || files.isEmpty)
}
.padding(18)
}
.frame(width: 620)
.frame(minHeight: 460)
.background(LK.ground)
.onAppear {
if target.isEmpty || (destination == nil && target != "immich"),
let first = state.settings.ftpDestinations.first { target = first.id.uuidString }
}
}
private var summary: String {
let n = state.selectedItems.count
let size = ByteCountFormatter.string(fromByteCount: totalBytes, countStyle: .file)
return "\(n) Bild\(n == 1 ? "" : "er") ausgewählt · \(files.count) Datei(en) · \(size)"
}
private var albumPreview: String {
let albums = Set(state.selectedItems.map { state.immichAlbum(for: $0) }.filter { !$0.isEmpty })
return albums.isEmpty ? "kein Album" : "Album: " + albums.sorted().joined(separator: ", ")
}
private func credentialsMissing(_ d: FTPDestination) -> Bool {
d.username.isEmpty || Keychain.get(d.keychainAccount) == nil
}
private var divider: some View { Rectangle().fill(LK.border).frame(height: 1).padding(.leading, 14) }
private func optionRow<C: View>(_ label: String, @ViewBuilder content: () -> C) -> some View {
HStack(spacing: 14) {
Text(label).font(.system(size: 13)).foregroundStyle(LK.text2).frame(width: 110, alignment: .leading)
content()
}
.padding(.horizontal, 14)
.padding(.vertical, 10)
}
private func destinationCard(id: String, icon: String, title: String, subtitle: String, detail: String, warning: Bool) -> some View {
let on = target == id
return Button { target = id } label: {
HStack(alignment: .top, spacing: 12) {
Image(systemName: icon)
.font(.system(size: 16, weight: .semibold))
.foregroundStyle(on ? LK.onAmber : LK.text2)
.frame(width: 34, height: 34)
.background(RoundedRectangle(cornerRadius: 9).fill(on ? LK.amber : LK.raised))
VStack(alignment: .leading, spacing: 3) {
HStack(spacing: 6) {
Text(title).font(.system(size: 14, weight: .bold)).foregroundStyle(LK.text).lineLimit(1)
if warning { Image(systemName: "key.fill").font(.system(size: 10)).foregroundStyle(LK.red) }
}
Text(subtitle).font(LK.mono(11)).foregroundStyle(LK.muted).lineLimit(1).truncationMode(.middle)
Text(detail).font(LK.mono(11)).foregroundStyle(on ? LK.amber : LK.text2).lineLimit(1).truncationMode(.middle)
}
Spacer(minLength: 0)
if on { Image(systemName: "checkmark.circle.fill").foregroundStyle(LK.amber) }
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
.background(RoundedRectangle(cornerRadius: 12).fill(on ? LK.selected : LK.card))
.overlay(RoundedRectangle(cornerRadius: 12).stroke(on ? LK.amber : LK.border, lineWidth: on ? 2 : 1))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
}