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

722 lines
30 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 PlanningView: View {
@EnvironmentObject private var model: PlanModel
@State private var showReplanConfirm = false
@State private var showSyncHelp = false
var body: some View {
VStack(spacing: 14) {
header
kpiRow
TimelineRangePicker()
CalendarTable()
.frame(maxHeight: .infinity)
}
.padding(.horizontal, 22)
.padding(.top, 30)
.padding(.bottom, 18)
.confirmationDialog(
"Gesamten Zeitraum neu planen?",
isPresented: $showReplanConfirm,
titleVisibility: .visible
) {
Button("Neu planen", role: .destructive) {
_ = model.autoPlan(replaceExisting: true)
}
Button("Abbrechen", role: .cancel) {}
} message: {
Text("Nicht gesperrte Zuweisungen im Zeitraum werden verworfen und fair neu verteilt. Gesperrte Einsätze bleiben erhalten.")
}
}
private var header: some View {
PageHeader(
eyebrow: "Pfarreiengemeinschaft Bad Godesberg",
title: "Gottesdienste im Blick",
subtitle: "Lektoren, Kommunionhelfer und liturgischer Kalender · Erzbistum Köln"
) {
HStack(spacing: 8) {
if model.isSyncing {
HStack(spacing: 6) {
ProgressView().controlSize(.small)
Text(model.syncMessage.isEmpty ? "Kalender…" : model.syncMessage)
.font(.system(size: 10.5))
.foregroundStyle(LP.inkSecondary)
}
.padding(.horizontal, 10)
}
SecondaryActionButton(title: "Kalender", icon: "arrow.triangle.2.circlepath") {
Task { await model.syncLiturgy() }
}
.help("Liturgischen Kalender für \(rangeYearsLabel) von www.eucharistiefeier.de aktualisieren")
.disabled(model.isSyncing)
SecondaryActionButton(title: "PDF", icon: "doc.richtext") {
model.createPDFPreview()
}
.help("Planung als PDF ansehen, sichern oder drucken")
Menu {
Button {
_ = model.autoPlan(replaceExisting: false)
} label: {
Label("Offene Dienste besetzen", systemImage: "wand.and.stars")
}
Button {
showReplanConfirm = true
} label: {
Label("Zeitraum neu planen…", systemImage: "arrow.triangle.2.circlepath")
}
} label: {
HStack(spacing: 7) {
Image(systemName: "wand.and.stars")
.font(.system(size: 11, weight: .bold))
Text("Automatisch planen")
.font(.system(size: 12, weight: .semibold))
Image(systemName: "chevron.down")
.font(.system(size: 8, weight: .bold))
.opacity(0.7)
}
.foregroundStyle(.white)
.padding(.horizontal, 14)
.padding(.vertical, 8)
.background(
RoundedRectangle(cornerRadius: 9, style: .continuous)
.fill(LinearGradient(colors: [LP.accent, LP.accent.opacity(0.82)],
startPoint: .top, endPoint: .bottom))
.shadow(color: LP.accent.opacity(0.20), radius: 5, y: 2)
)
}
.menuStyle(.borderlessButton)
.fixedSize()
.help("Alle offenen Dienste im Zeitraum automatisch besetzen")
}
}
}
private var rangeYearsLabel: String {
let years = Array(Set(model.range.days.map { DayMath.year($0) })).sorted()
return years.map(String.init).joined(separator: ", ")
}
private var kpiRow: some View {
SurfaceCard(padding: 14, radius: 14, background: LP.card.opacity(0.88)) {
HStack(spacing: 0) {
StatPill(icon: "building.columns", value: "\(model.stats.serviceCount)", label: "Gottesdienste")
divider
StatPill(icon: "checkmark.seal", value: "\(model.stats.assignmentCount)", label: "Einsätze", tint: LP.success)
divider
StatPill(icon: "person.crop.circle.badge.exclamationmark", value: "\(model.stats.openSlots)", label: "Offene Dienste", tint: LP.open)
divider
StatPill(icon: "person.2", value: "\(model.people.filter(\.active).count)", label: "Aktive Personen", tint: LP.burgundy)
divider
StatPill(icon: "cross.case", value: "\(model.stats.sundayServiceCount)", label: "Sonntagsgottesdienste", tint: LP.gold)
Spacer(minLength: 12)
VStack(alignment: .trailing, spacing: 4) {
Text("BESETZUNGSQUOTE")
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.tracking(1)
.foregroundStyle(LP.inkTertiary)
HStack(alignment: .firstTextBaseline, spacing: 4) {
Text("\(Int(model.stats.coverageRate * 100))")
.font(.system(size: 24, weight: .bold, design: .rounded))
.foregroundStyle(model.stats.openSlots == 0 ? LP.success : LP.ink)
Text("%")
.font(.system(size: 12, weight: .semibold, design: .rounded))
.foregroundStyle(LP.inkTertiary)
}
ProgressView(value: model.stats.coverageRate)
.progressViewStyle(.linear)
.tint(model.stats.openSlots == 0 ? LP.success : LP.accent)
.frame(width: 100)
}
}
}
}
private var divider: some View {
Rectangle()
.fill(LP.line.opacity(0.55))
.frame(width: 1, height: 32)
.padding(.horizontal, 14)
}
}
// MARK: - Calendar table
private struct CalendarTable: View {
@EnvironmentObject private var model: PlanModel
private let gap: CGFloat = 8
private let horizontalPadding: CGFloat = 12
private let dateWidth: CGFloat = 86
private let liturgyWidth: CGFloat = 240
private let churchMinWidth: CGFloat = 186
var body: some View {
SurfaceCard(padding: 0, radius: 16, background: LP.card) {
VStack(spacing: 0) {
toolbar
Hairline()
GeometryReader { geo in
let minimum = horizontalPadding * 2 + dateWidth + liturgyWidth + churchMinWidth * 4 + gap * 5
let width = max(geo.size.width, minimum)
let churchWidth = (width - horizontalPadding * 2 - dateWidth - liturgyWidth - gap * 5) / 4
ScrollView(.horizontal, showsIndicators: false) {
VStack(spacing: 0) {
columnHeader(churchWidth: churchWidth)
Hairline()
if model.filteredRows.isEmpty {
EmptyStateView(
icon: "calendar",
title: "Keine Tage im Filter",
message: "Für diesen Zeitraum und Filter sind keine Einträge vorhanden."
)
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
ScrollView(.vertical) {
LazyVStack(spacing: 0, pinnedViews: []) {
ForEach(monthGroups) { group in
monthHeader(group, churchWidth: churchWidth)
ForEach(group.rows) { row in
DayRow(
row: row,
churchWidth: churchWidth,
dateWidth: dateWidth,
liturgyWidth: liturgyWidth,
gap: gap,
horizontalPadding: horizontalPadding
)
}
}
}
.padding(.bottom, 10)
}
}
}
.frame(width: width, height: geo.size.height, alignment: .top)
}
}
}
}
}
private var toolbar: some View {
HStack(spacing: 12) {
HStack(spacing: 2) {
ForEach(PlanFilter.allCases) { filter in
Button {
withAnimation(.easeInOut(duration: 0.18)) {
model.filter = filter
}
} label: {
HStack(spacing: 4) {
Text(filter.title)
.font(.system(size: 10.5, weight: .semibold))
if filter == .open, model.stats.openSlots > 0 {
Text("\(model.stats.openSlots)")
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.foregroundStyle(.white)
.padding(.horizontal, 4)
.padding(.vertical, 1)
.background(Capsule().fill(LP.open))
}
}
.foregroundStyle(model.filter == filter ? Color.white : LP.inkSecondary)
.padding(.horizontal, 10)
.padding(.vertical, 5)
.background(
Capsule().fill(model.filter == filter ? LP.accent : Color.clear)
)
}
.buttonStyle(.plain)
}
}
.padding(3)
.background(Capsule().fill(LP.cardMuted))
.overlay(Capsule().strokeBorder(LP.line.opacity(0.55), lineWidth: 0.7))
Toggle(isOn: Binding(
get: { model.showOnlySundaysAndFeasts },
set: { model.setSundayFilter($0) }
)) {
Text("Sonn- & Feiertage")
.font(.system(size: 10.5, weight: .medium))
.foregroundStyle(LP.inkSecondary)
}
.toggleStyle(.checkbox)
searchField
Spacer(minLength: 8)
legend
Text("\(model.filteredRows.count) Tage")
.font(.system(size: 9.5, weight: .medium, design: .rounded))
.foregroundStyle(LP.inkTertiary)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Capsule().fill(LP.cardMuted))
}
.padding(.horizontal, 14)
.padding(.vertical, 10)
}
private var searchField: some View {
HStack(spacing: 5) {
Image(systemName: "magnifyingglass")
.font(.system(size: 10, weight: .medium))
.foregroundStyle(LP.inkTertiary)
TextField("Person, Ort, Lesung…", text: $model.searchText)
.textFieldStyle(.plain)
.font(.system(size: 10.5))
.frame(width: 150)
if !model.searchText.isEmpty {
Button {
model.searchText = ""
} label: {
Image(systemName: "xmark.circle.fill")
.font(.system(size: 9))
.foregroundStyle(LP.inkTertiary)
}
.buttonStyle(.plain)
}
}
.padding(.horizontal, 8)
.padding(.vertical, 5)
.background(RoundedRectangle(cornerRadius: 7).fill(LP.cardMuted))
.overlay(RoundedRectangle(cornerRadius: 7).strokeBorder(LP.line.opacity(0.6), lineWidth: 0.7))
}
private var legend: some View {
HStack(spacing: 9) {
legendItem(color: LP.sundayWash, text: "Sonntag")
legendItem(color: LP.feastWash, text: "Hochfest")
HStack(spacing: 3) {
Image(systemName: "circle.dashed")
.font(.system(size: 8))
.foregroundStyle(LP.open)
Text("offen")
}
.font(.system(size: 8.5, weight: .medium))
.foregroundStyle(LP.inkTertiary)
}
}
private func legendItem(color: Color, text: String) -> some View {
HStack(spacing: 3) {
RoundedRectangle(cornerRadius: 2).fill(color).frame(width: 8, height: 8)
.overlay(RoundedRectangle(cornerRadius: 2).strokeBorder(LP.line.opacity(0.5), lineWidth: 0.5))
Text(text)
}
.font(.system(size: 8.5, weight: .medium))
.foregroundStyle(LP.inkTertiary)
}
private func columnHeader(churchWidth: CGFloat) -> some View {
HStack(spacing: gap) {
Text("TAG")
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.tracking(1.1)
.foregroundStyle(LP.inkTertiary)
.frame(width: dateWidth, alignment: .leading)
Text("LITURGIE & LESUNGEN")
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.tracking(1.1)
.foregroundStyle(LP.inkTertiary)
.frame(width: liturgyWidth, alignment: .leading)
ForEach(model.churches) { church in
ChurchColumnHeader(church: church, width: churchWidth)
}
}
.padding(.horizontal, horizontalPadding)
.padding(.vertical, 10)
.background(LP.cardMuted.opacity(0.55))
}
private func monthHeader(_ group: MonthGroup, churchWidth: CGFloat) -> some View {
HStack(spacing: 8) {
Text(group.title.uppercased())
.font(.system(size: 9.5, weight: .bold, design: .rounded))
.tracking(1.1)
.foregroundStyle(LP.inkSecondary)
Hairline()
Text("\(group.serviceCount) Gottesdienste")
.font(.system(size: 8.5, weight: .medium, design: .rounded))
.foregroundStyle(LP.inkTertiary)
if group.openSlots > 0 {
Text("\(group.openSlots) offen")
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.foregroundStyle(LP.open)
}
}
.padding(.horizontal, horizontalPadding)
.padding(.vertical, 7)
.frame(width: horizontalPadding * 2 + dateWidth + liturgyWidth + churchWidth * 4 + gap * 5, alignment: .leading)
.background(LP.paperDeep.opacity(0.55))
.overlay(alignment: .top) { Hairline() }
.overlay(alignment: .bottom) { Hairline().opacity(0.55) }
}
private struct MonthGroup: Identifiable {
var id: String
var title: String
var rows: [DayPlanRow]
var serviceCount: Int
var openSlots: Int
}
private var monthGroups: [MonthGroup] {
var groups: [MonthGroup] = []
for row in model.filteredRows {
let key = DayMath.monthKey(row.date)
if groups.last?.id == key {
groups[groups.count - 1].rows.append(row)
groups[groups.count - 1].serviceCount += row.services.count
groups[groups.count - 1].openSlots += row.openSlots
} else {
let title = "\(DayMath.monthName(row.date)) \(DayMath.year(row.date))"
groups.append(MonthGroup(
id: key,
title: title,
rows: [row],
serviceCount: row.services.count,
openSlots: row.openSlots
))
}
}
return groups
}
}
private struct ChurchColumnHeader: View {
var church: Church
var width: CGFloat
var body: some View {
HStack(spacing: 8) {
ChurchGlyph(symbol: church.symbol, tint: LP.churchColor(church.colorHex), lineWidth: 1.5)
.frame(width: 34, height: 25)
VStack(alignment: .leading, spacing: 2) {
Text(church.name)
.font(LP.serifSubhead)
.foregroundStyle(LP.ink)
.lineLimit(1)
Text(church.address)
.font(.system(size: 8, weight: .medium))
.foregroundStyle(LP.inkTertiary)
.lineLimit(1)
}
Spacer(minLength: 0)
Image(systemName: "info.circle")
.font(.system(size: 9))
.foregroundStyle(LP.inkTertiary.opacity(0.65))
}
.frame(width: width, alignment: .leading)
.help("\(church.name)\n\(church.address)\n\(church.notes)\n\n\(church.symbol.caption)")
}
}
private struct DayRow: View {
@EnvironmentObject private var model: PlanModel
var row: DayPlanRow
var churchWidth: CGFloat
var dateWidth: CGFloat
var liturgyWidth: CGFloat
var gap: CGFloat
var horizontalPadding: CGFloat
private var day: LiturgicalDay? { row.liturgicalDay }
private var isHighFeast: Bool { day?.isHighFeast == true }
private var isFeast: Bool { day?.isFeast == true }
private var isSunday: Bool { day?.isSunday == true || DayMath.isSunday(row.date) }
private var isSpecialDay: Bool { isSunday || isFeast || day?.isObligation == true }
var body: some View {
HStack(alignment: .top, spacing: gap) {
dateCell
.frame(width: dateWidth, alignment: .leading)
liturgyCell
.frame(width: liturgyWidth, alignment: .leading)
ForEach(model.churches) { church in
churchCell(church: church)
.frame(width: churchWidth, alignment: .topLeading)
}
}
.padding(.horizontal, horizontalPadding)
.padding(.vertical, 9)
.background(background)
.overlay(alignment: .bottom) {
Rectangle()
.fill(LP.line.opacity(isSpecialDay ? 0.55 : 0.32))
.frame(height: 0.7)
}
}
private var background: some View {
Group {
if isHighFeast {
LinearGradient(colors: [LP.feastWash, LP.sundayWash.opacity(0.6)],
startPoint: .leading, endPoint: .trailing)
} else if isSunday || day?.isObligation == true {
LP.sundayWash.opacity(0.72)
} else if isFeast {
LP.feastWash.opacity(0.42)
} else {
Color.clear
}
}
}
private var accentColor: Color {
guard let day else { return LP.line }
return LP.liturgicalColor(day.color)
}
private var dateCell: some View {
HStack(spacing: 8) {
RoundedRectangle(cornerRadius: 2)
.fill(accentColor)
.frame(width: 3, height: 46)
.opacity(isSpecialDay ? 1 : 0.35)
VStack(alignment: .leading, spacing: 1) {
Text(DayMath.shortWeekdayName(row.date).uppercased())
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.tracking(0.8)
.foregroundStyle(isSunday ? LP.burgundy : LP.inkTertiary)
HStack(alignment: .firstTextBaseline, spacing: 3) {
Text("\(DayMath.dayNumber(row.date))")
.font(.system(size: 24, weight: .semibold, design: .serif))
.foregroundStyle(isHighFeast ? LP.gold : (isSunday ? LP.burgundy : LP.ink))
Text(DayMath.shortMonthName(row.date))
.font(.system(size: 9, weight: .medium, design: .rounded))
.foregroundStyle(LP.inkTertiary)
}
if isSunday {
Text("SONNTAG")
.font(.system(size: 7, weight: .bold, design: .rounded))
.tracking(0.8)
.foregroundStyle(LP.burgundy.opacity(0.8))
} else if day?.isObligation == true {
Text("GEBOTEN")
.font(.system(size: 7, weight: .bold, design: .rounded))
.tracking(0.8)
.foregroundStyle(LP.burgundy.opacity(0.8))
} else {
Text(DayMath.yearString(row.date))
.font(.system(size: 7.5, weight: .medium, design: .rounded))
.foregroundStyle(LP.inkTertiary.opacity(0.8))
}
}
}
}
private var liturgyCell: some View {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 5) {
if let day {
RankBadge(day: day)
if day.rank == .weekday, !day.isSunday, !day.isObligation {
Text(day.season.shortTitle.uppercased())
.font(.system(size: 7.5, weight: .bold, design: .rounded))
.tracking(0.5)
.foregroundStyle(LP.inkTertiary)
}
}
Spacer(minLength: 0)
if let day, day.isFeast {
Image(systemName: day.symbol.symbolName)
.font(.system(size: 10, weight: .medium))
.foregroundStyle(isHighFeast ? LP.gold : LP.burgundy.opacity(0.7))
.help("\(day.symbol.label) · \(day.rank.label)")
}
}
Text(day?.displayTitle ?? "—")
.font(.system(size: 13.5, weight: isSpecialDay ? .semibold : .regular, design: .serif))
.foregroundStyle(isHighFeast ? LP.ink : (isSunday ? LP.burgundy : LP.ink))
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
if let day, !day.readings.isEmpty {
HStack(alignment: .top, spacing: 4) {
Image(systemName: "book.closed")
.font(.system(size: 7.5, weight: .medium))
.foregroundStyle(LP.inkTertiary)
.padding(.top, 1)
Text(day.readings.shortLabel)
.font(.system(size: 8.8, weight: .regular))
.foregroundStyle(LP.inkSecondary)
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
}
} else if day?.title == "Karsamstag" {
Text("Keine Eucharistiefeier am Tag")
.font(.system(size: 8.5, weight: .medium))
.foregroundStyle(LP.inkTertiary)
}
}
.padding(.top, 1)
.help(liturgyHelp)
}
private var liturgyHelp: String {
guard let day else { return DayMath.longString(row.date) }
var parts = ["\(DayMath.longString(row.date))", day.title]
if !day.remark.isEmpty { parts.append(day.remark) }
if !day.badgeText.isEmpty { parts.append(day.badgeText) }
parts.append("Liturgische Farbe: \(day.color.label)")
if !day.readings.isEmpty { parts.append(day.readings.fullLabel) }
if day.isObligation { parts.append("Gebotener Feiertag") }
return parts.joined(separator: "\n")
}
private func churchCell(church: Church) -> some View {
let services = row.services.filter { $0.church.id == church.id }
return Group {
if services.isEmpty {
HStack {
Spacer()
Image(systemName: "circle.dotted")
.font(.system(size: 8))
.foregroundStyle(LP.inkTertiary.opacity(0.18))
Spacer()
}
.frame(minHeight: 58)
} else {
VStack(spacing: 5) {
ForEach(services) { service in
ServiceCard(service: service, liturgy: day)
}
}
}
}
}
}
private struct ServiceCard: View {
@EnvironmentObject private var model: PlanModel
var service: ServiceWithAssignments
var liturgy: LiturgicalDay?
private var churchColor: Color { LP.churchColor(service.church.colorHex) }
var body: some View {
Button {
model.serviceDetail = ServiceDetailRequest(serviceID: service.service.id)
} label: {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 5) {
Text(service.service.timeLabel)
.font(.system(size: 10.5, weight: .bold, design: .rounded))
.foregroundStyle(churchColor)
Text(service.service.title)
.font(.system(size: 9.5, weight: .semibold))
.foregroundStyle(LP.ink)
.lineLimit(1)
if service.service.kind == .special {
Text("S")
.font(.system(size: 7, weight: .black, design: .rounded))
.foregroundStyle(.white)
.frame(width: 12, height: 12)
.background(Circle().fill(LP.burgundy))
}
Spacer(minLength: 0)
if service.isFullyStaffed {
Image(systemName: "checkmark.circle.fill")
.font(.system(size: 9))
.foregroundStyle(LP.success.opacity(0.8))
} else {
Text("\(service.openSlots)")
.font(.system(size: 7.5, weight: .bold, design: .rounded))
.foregroundStyle(LP.open)
.padding(.horizontal, 4)
.padding(.vertical, 1)
.background(Capsule().fill(LP.openSoft))
}
}
ForEach(Role.allCases) { role in
if service.service.requiredCount(for: role) > 0 {
roleLine(role: role)
}
}
}
.padding(.horizontal, 7)
.padding(.vertical, 6)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 8, style: .continuous)
.fill(service.service.kind == .special ? LP.burgundySoft.opacity(0.55) : LP.card)
)
.overlay(
RoundedRectangle(cornerRadius: 8, style: .continuous)
.strokeBorder(service.service.kind == .special ? LP.burgundy.opacity(0.28) : LP.line.opacity(0.55), lineWidth: 0.8)
)
.overlay(alignment: .leading) {
RoundedRectangle(cornerRadius: 1)
.fill(churchColor.opacity(0.55))
.frame(width: 2.2)
.padding(.vertical, 6)
}
}
.buttonStyle(.plain)
.help(helpText)
}
private func roleLine(role: Role) -> some View {
let assigned = service.assignments(for: role)
let open = service.openSlots(for: role)
return HStack(spacing: 3) {
Image(systemName: role.symbolName)
.font(.system(size: 7))
.foregroundStyle(role == .lector ? LP.accent : LP.burgundy)
.frame(width: 10)
if assigned.isEmpty {
Text(open > 1 ? "\(open)× \(role.title) offen" : "\(role.title) offen")
.font(.system(size: 8.5, weight: .medium))
.foregroundStyle(LP.open)
.padding(.horizontal, 4)
.padding(.vertical, 1)
.background(Capsule().fill(LP.openSoft.opacity(0.7)))
} else {
ForEach(assigned) { assignment in
if let person = model.peopleByID[assignment.personID] {
PersonMiniChip(person: person, role: role, color: churchColor)
}
}
if open > 0 {
Text("+\(open)")
.font(.system(size: 8, weight: .bold, design: .rounded))
.foregroundStyle(LP.open)
}
}
Spacer(minLength: 0)
}
}
private var helpText: String {
var lines = [
"\(service.church.name) · \(DayMath.longString(service.service.date)) · \(service.service.timeLabel)",
service.service.title
]
for role in Role.allCases {
let names = service.assignments(for: role).compactMap { model.peopleByID[$0.personID]?.fullName }
if service.service.requiredCount(for: role) > 0 {
lines.append("\(role.title): \(names.isEmpty ? "offen" : names.joined(separator: ", "))")
}
}
if let liturgy {
if !liturgy.readings.isEmpty { lines.append(liturgy.readings.shortLabel) }
if !liturgy.remark.isEmpty { lines.append(liturgy.remark) }
}
if !service.service.notes.isEmpty { lines.append(service.service.notes) }
lines.append("Klicken zum Bearbeiten")
return lines.joined(separator: "\n")
}
}
private extension DayMath {
static func yearString(_ date: Date) -> String {
String(year(date))
}
}