Files
LektorenPlan/Sources/LektorenPlan/Views/SpecialServiceSheet.swift
T
2026-11-07 11:32:18 +01:00

199 lines
8.7 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 LektorenPlanCore
struct SpecialServiceSheet: View {
@EnvironmentObject private var model: PlanModel
@Environment(\.dismiss) private var dismiss
let request: SpecialServiceRequest
@State private var churchID = ""
@State private var date = DayMath.today()
@State private var timeDate = Date()
@State private var title = ""
@State private var lectorsNeeded = 1
@State private var helpersNeeded = 1
@State private var notes = ""
@State private var useLiturgicalTitle = true
@State private var existing: ServiceOccurrence?
private var isValid: Bool {
!churchID.isEmpty && !title.trimmingCharacters(in: .whitespaces).isEmpty
&& (lectorsNeeded + helpersNeeded) > 0
}
private var liturgicalDay: LiturgicalDay? {
let key = DayMath.key(date)
if let day = model.liturgicalDays[key] { return day }
return LiturgicalCalendarEngine.localDay(for: date)
}
var body: some View {
VStack(spacing: 0) {
SheetHeader(
icon: "sparkles",
title: existing == nil ? "Sondergottesdienst" : "Sondergottesdienst bearbeiten",
subtitle: "Einmaliger Termin außerhalb der regelmäßigen Ordnung – z. B. Christmette, Osternacht oder Patronatsfest.",
tint: LP.burgundy
)
Hairline()
ScrollView {
VStack(alignment: .leading, spacing: 22) {
FormSection(title: "Ort & Zeit") {
HStack(spacing: 12) {
LabeledField(label: "Kirche") {
Picker("", selection: $churchID) {
ForEach(model.churches) { church in
Text(church.name).tag(church.id)
}
}
.labelsHidden()
.pickerStyle(.menu)
}
LabeledField(label: "Datum") {
DatePicker("", selection: $date, displayedComponents: .date)
.labelsHidden()
.datePickerStyle(.field)
}
LabeledField(label: "Uhrzeit") {
DatePicker("", selection: $timeDate, displayedComponents: .hourAndMinute)
.labelsHidden()
.datePickerStyle(.field)
}
}
}
if let day = liturgicalDay {
HStack(spacing: 11) {
Image(systemName: day.symbol.symbolName)
.font(.system(size: 15, weight: .medium))
.foregroundStyle(day.isHighFeast ? LP.gold : LP.burgundy)
.frame(width: 30, height: 30)
.background(Circle().fill((day.isHighFeast ? LP.gold : LP.burgundy).opacity(0.10)))
VStack(alignment: .leading, spacing: 2) {
Text(day.displayTitle)
.font(LP.serifSubhead)
.foregroundStyle(LP.ink)
Text([day.rank.label, day.isObligation ? "gebotener Feiertag" : nil]
.compactMap { $0 }.joined(separator: " · "))
.font(.system(size: 9.5))
.foregroundStyle(LP.inkTertiary)
if !day.readings.isEmpty {
Text(day.readings.shortLabel)
.font(.system(size: 9.5))
.foregroundStyle(LP.inkSecondary)
.lineLimit(2)
}
}
Spacer()
if !title.isEmpty || useLiturgicalTitle {
SecondaryActionButton(title: "Titel übernehmen", icon: "arrow.down.doc") {
title = day.displayTitle
useLiturgicalTitle = true
}
}
}
.padding(11)
.background(RoundedRectangle(cornerRadius: 10).fill(LP.cardMuted))
.overlay(RoundedRectangle(cornerRadius: 10).strokeBorder(LP.line.opacity(0.6), lineWidth: 0.8))
}
FormSection(title: "Bezeichnung & Dienste") {
LabeledField(label: "Titel") {
TextField("z. B. Christmette", text: $title)
.textFieldStyle(.roundedBorder)
}
HStack(spacing: 26) {
HStack(spacing: 8) {
RoleBadge(role: .lector)
Stepper(value: $lectorsNeeded, in: 0...4) {
Text("\(lectorsNeeded)")
.font(.system(size: 12, weight: .semibold, design: .rounded))
.monospacedDigit()
}
.fixedSize()
}
HStack(spacing: 8) {
RoleBadge(role: .helper)
Stepper(value: $helpersNeeded, in: 0...4) {
Text("\(helpersNeeded)")
.font(.system(size: 12, weight: .semibold, design: .rounded))
.monospacedDigit()
}
.fixedSize()
}
Spacer()
}
}
FormSection(title: "Notizen") {
TextEditor(text: $notes)
.font(.system(size: 11))
.frame(height: 58)
.padding(5)
.background(RoundedRectangle(cornerRadius: 7).fill(LP.card))
.overlay(RoundedRectangle(cornerRadius: 7).strokeBorder(LP.line.opacity(0.8), lineWidth: 1))
}
}
.padding(22)
}
SheetFooter {
Spacer()
SecondaryActionButton(title: "Abbrechen", icon: "xmark") { dismiss() }
PrimaryActionButton(title: "Speichern", icon: "checkmark", action: save, disabled: !isValid)
}
}
.sheetSurface(width: 600, height: 610)
.onAppear(perform: load)
}
private func load() {
existing = request.service
if let service = existing {
churchID = service.churchID
date = service.date
timeDate = calendarTime(minutes: service.timeMinutes)
title = service.title
lectorsNeeded = service.lectorsNeeded
helpersNeeded = service.helpersNeeded
notes = service.notes
return
}
churchID = request.churchID ?? model.churches.first?.id ?? ""
date = request.date
timeDate = calendarTime(minutes: 18 * 60)
if let day = liturgicalDay {
title = day.isFeast ? day.displayTitle : ""
}
}
private func calendarTime(minutes: Int) -> Date {
let start = DayMath.today()
return DayMath.calendar.date(bySettingHour: minutes / 60, minute: minutes % 60, second: 0, of: start) ?? start
}
private func save() {
let comps = DayMath.calendar.dateComponents([.hour, .minute], from: timeDate)
let minutes = (comps.hour ?? 18) * 60 + (comps.minute ?? 0)
let service = ServiceOccurrence(
id: existing?.id ?? UUID().uuidString,
churchID: churchID,
templateID: nil,
date: date,
timeMinutes: minutes,
title: title.trimmingCharacters(in: .whitespaces),
lectorsNeeded: lectorsNeeded,
helpersNeeded: helpersNeeded,
kind: .special,
notes: notes.trimmingCharacters(in: .whitespacesAndNewlines),
locked: existing?.locked ?? false,
createdAt: existing?.createdAt ?? Date()
)
model.saveSpecialService(service)
dismiss()
}
}