.ics Download und PDF Layout korrigiert

This commit is contained in:
concierge committed 2026-11-18 09:01:33 +01:00
1 parent dc20d7ec5a
commit cc2e85c111
11 files changed
+424 -73

No files matched your search

+77 -23
View File
@@ -63,6 +63,14 @@ enum PlanPDFRenderer {
let churchWidth = (pageWidth - margin * 2 - dateWidth - liturgyWidth - columnGap * 5) / 4
let contentTop = margin + headerHeight + columnHeaderHeight + 4
let contentBottom = pageHeight - margin - footerHeight
let rowHeights = sortedRows.map {
rowHeight(row: $0, churchWidth: churchWidth, people: people)
}
let totalPages = paginationCount(
rowHeights: rowHeights,
contentTop: contentTop,
contentBottom: contentBottom
)
var pageIndex = 0
@@ -78,7 +86,7 @@ enum PlanPDFRenderer {
}
func endPage() {
drawFooter(pageIndex: pageIndex, range: range)
drawFooter(pageIndex: pageIndex, totalPages: totalPages, range: range)
NSGraphicsContext.restoreGraphicsState()
context.restoreGState()
context.endPDFPage()
@@ -92,11 +100,11 @@ enum PlanPDFRenderer {
var cursorY = contentTop
for (index, row) in sortedRows.enumerated() {
let height = rowHeight(row: row, churchWidth: churchWidth, people: people)
let height = rowHeights[index]
if cursorY + height > contentBottom {
endPage()
beginPage()
drawHeader(range: range, generatedAt: generatedAt, continuation: true)
drawHeader(range: range, generatedAt: generatedAt)
drawColumnHeader(churches: churches, churchWidth: churchWidth)
cursorY = contentTop
}
@@ -137,7 +145,7 @@ enum PlanPDFRenderer {
// MARK: - Page furniture
private static func drawHeader(range: DayRange, generatedAt: Date, continuation: Bool = false) {
private static func drawHeader(range: DayRange, generatedAt: Date) {
let top = margin
drawText(
@@ -147,9 +155,9 @@ enum PlanPDFRenderer {
color: accent
)
drawText(
continuation ? "Gottesdienstplan · Fortsetzung" : "Gottesdienstplan",
in: CGRect(x: margin, y: top + 13, width: 500, height: 24),
font: serif(21, bold: true),
"Gottesdienstplan",
in: CGRect(x: margin, y: top + 13, width: 500, height: 22),
font: serif(17.5, bold: true),
color: ink
)
drawText(
@@ -219,7 +227,7 @@ enum PlanPDFRenderer {
contextLine(y: y + height)
}
private static func drawFooter(pageIndex: Int, range: DayRange) {
private static func drawFooter(pageIndex: Int, totalPages: Int, range: DayRange) {
let y = pageHeight - margin - footerHeight
contextLine(y: y + 6)
drawText(
@@ -229,14 +237,31 @@ enum PlanPDFRenderer {
color: tertiaryInk
)
drawText(
"Seite \(pageIndex + 1)",
in: CGRect(x: pageWidth - margin - 90, y: y + 10, width: 90, height: 12),
"Seite \(pageIndex + 1) von \(max(totalPages, 1))",
in: CGRect(x: pageWidth - margin - 120, y: y + 10, width: 120, height: 12),
font: sans(7),
color: tertiaryInk,
alignment: .right
)
}
private static func paginationCount(
rowHeights: [CGFloat],
contentTop: CGFloat,
contentBottom: CGFloat
) -> Int {
var pages = 1
var cursor = contentTop
for height in rowHeights {
if cursor + height > contentBottom {
pages += 1
cursor = contentTop
}
cursor += height
}
return pages
}
// MARK: - Rows
private static func rowHeight(
@@ -244,32 +269,59 @@ enum PlanPDFRenderer {
churchWidth: CGFloat,
people: [String: Person]
) -> CGFloat {
var tallest: CGFloat = 46
let isSunday = row.liturgicalDay?.isSunday == true || DayMath.isSunday(row.date)
// Base height covers the date cell including the optional SONNTAG label.
var tallest: CGFloat = isSunday ? 52 : 42
if let day = row.liturgicalDay {
var liturgyHeight: CGFloat = 14
liturgyHeight += measuredHeight(day.displayTitle, font: serif(10, bold: true), width: liturgyWidth - 8, maxLines: 3)
var liturgyHeight: CGFloat = 10
let hasBadge = day.isHighFeast || day.rank == .feast || day.rank == .obligatoryMemorial
|| (day.isObligation && !day.isSunday)
if hasBadge { liturgyHeight += 12 }
liturgyHeight += measuredHeight(
day.displayTitle,
font: serif(9.5, bold: day.isFeast),
width: liturgyWidth - 8,
maxLines: 3
)
liturgyHeight += 2
if !day.readings.isEmpty {
liturgyHeight += measuredHeight(day.readings.shortLabel, font: sans(7), width: liturgyWidth - 8, maxLines: 3) + 2
liturgyHeight += measuredHeight(
day.readings.shortLabel,
font: sans(6.5),
width: liturgyWidth - 8,
maxLines: 3
) + 2
}
tallest = max(tallest, liturgyHeight + 8)
tallest = max(tallest, liturgyHeight + 4)
}
for church in row.services.map(\.church).uniqued(by: \.id) {
let services = row.services.filter { $0.church.id == church.id }
var height: CGFloat = 5
var height: CGFloat = 8
for service in services {
height += 12
height += 11
if service.service.location != nil { height += 9 }
for role in Role.allCases where service.service.requiredCount(for: role) > 0 {
height += 9
let names = service.assignments(for: role).compactMap { people[$0.personID]?.fullName }
let prefix = role == .lector ? "L" : "K"
let value = names.isEmpty ? "offen" : names.joined(separator: ", ")
height += max(
9,
measuredHeight(
"\(prefix): \(value)",
font: sans(6.5, weight: names.isEmpty ? .semibold : .regular),
width: churchWidth - 12,
maxLines: 2
)
)
}
height += 3
}
tallest = max(tallest, height + 4)
tallest = max(tallest, height)
}
return min(tallest, 150)
return tallest
}
private static func drawRow(
@@ -389,14 +441,14 @@ enum PlanPDFRenderer {
let names = service.assignments(for: role).compactMap { people[$0.personID]?.fullName }
let prefix = role == .lector ? "L" : "K"
let value = names.isEmpty ? "offen" : names.joined(separator: ", ")
drawText(
let roleHeight = drawText(
"\(prefix): \(value)",
in: CGRect(x: x + 7, y: serviceY, width: churchWidth - 12, height: 9),
font: sans(6.5, weight: names.isEmpty ? .semibold : .regular),
color: names.isEmpty ? openColor : secondaryInk,
maxLines: 2
)
serviceY += 9
serviceY += max(9, roleHeight)
}
serviceY += 3
if serviceY > y + height - 6 { break }
@@ -472,7 +524,9 @@ enum PlanPDFRenderer {
width: CGFloat,
maxLines: Int
) -> CGFloat {
let attributes: [NSAttributedString.Key: Any] = [.font: font]
let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 0.5
let attributes: [NSAttributedString.Key: Any] = [.font: font, .paragraphStyle: paragraph]
let measured = (text as NSString).boundingRect(
with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin, .usesFontLeading],
+22
View File
@@ -1265,6 +1265,28 @@ final class PlanModel: ObservableObject {
}
}
/// Saves one recipient's personal calendar file via a save panel. The file
/// contains only that person's assignments, like the e-mail attachment.
func exportDispatchCalendar(for recipient: DispatchRecipient) {
let calendar = CalendarExport.ics(
calendarName: "Gottesdienstplan \(range.shortTitle)",
events: dispatchCalendarEvents(recipient.assignments)
)
let panel = NSSavePanel()
panel.title = "Kalenderdatei sichern"
panel.nameFieldStringValue = dispatchCalendarFilename(for: recipient)
panel.allowedContentTypes = [UTType(filenameExtension: "ics") ?? .data]
panel.canCreateDirectories = true
guard panel.runModal() == .OK, let url = panel.url else { return }
do {
try Data(calendar.utf8).write(to: url, options: .atomic)
showToast("Kalenderdatei gespeichert: \(url.lastPathComponent)", kind: .success)
} catch {
alertTitle = "Kalenderdatei konnte nicht gespeichert werden"
alertMessage = error.localizedDescription
}
}
/// Builds one printable PDF: for each print recipient an optional cover
/// sheet followed by the selected plan variant.
func createDispatchPrintPDF() {
+19 -1
View File
@@ -212,7 +212,7 @@ struct DispatchView: View {
Image(systemName: "info.circle")
.font(.system(size: 10, weight: .semibold))
.foregroundStyle(LP.accent)
Text("Ohne hinterlegte E-Mail-Adresse wird automatisch gedruckt. E-Mails werden als Entwurf im Standard-Mailprogramm geöffnet – mit persönlichem Text, dem gewählten PDF und einer Kalenderdatei (.ics) mit den eigenen Einsätzen.")
Text("Ohne hinterlegte E-Mail-Adresse wird automatisch gedruckt. E-Mails werden als Entwurf im Standard-Mailprogramm geöffnet – mit persönlichem Text, dem gewählten PDF und einer Kalenderdatei (.ics) mit den eigenen Einsätzen. Über das Kalender-Symbol in der Empfängerzeile lässt sich die persönliche .ics-Datei auch einzeln sichern.")
.font(.system(size: 9.5))
.foregroundStyle(LP.inkTertiary)
.fixedSize(horizontal: false, vertical: true)
@@ -283,6 +283,8 @@ struct DispatchView: View {
.frame(width: 136, alignment: .leading)
Text("VORBLATT")
.frame(width: 66, alignment: .center)
Text("ICS")
.frame(width: 30, alignment: .center)
}
.font(.system(size: 8.5, weight: .bold, design: .rounded))
.tracking(0.7)
@@ -351,6 +353,22 @@ struct DispatchView: View {
.frame(width: 66)
.disabled(recipient.effectiveChannel == .none)
.help("Anschreiben als Vorblatt voranstellen")
Button {
model.exportDispatchCalendar(for: recipient)
} label: {
Image(systemName: "calendar.badge.plus")
.font(.system(size: 11.5, weight: .semibold))
.foregroundStyle(recipient.assignments.isEmpty ? LP.inkTertiary : LP.accent)
}
.buttonStyle(.plain)
.frame(width: 30)
.disabled(recipient.assignments.isEmpty)
.help(
recipient.assignments.isEmpty
? "Keine Einsätze im Zeitraum"
: "Kalenderdatei (.ics) für \(recipient.person.fullName) sichern"
)
}
.padding(.horizontal, 4)
.padding(.vertical, 5)