Versendung: doppelseitiges Sammel PDF und diverse Verbesserungen
This commit is contained in:
1 parent
4b88aca958
commit
feab6135ff
31 files changed
+1302
-96
No files matched your search
@@ -10,7 +10,9 @@ struct DispatchAssignment: Identifiable, Hashable {
|
||||
var church: String
|
||||
var place: String
|
||||
var title: String
|
||||
var role: Role
|
||||
var roles: [Role]
|
||||
|
||||
var roleLabel: String { Role.combinedTitle(roles) }
|
||||
|
||||
var dateLabel: String {
|
||||
"\(DayMath.shortWeekdayName(date)), \(DayMath.dayNumber(date)).\(DayMath.month(date)).\(DayMath.year(date))"
|
||||
@@ -246,7 +248,7 @@ enum DispatchCoverPDFRenderer {
|
||||
assignment.dateLabel,
|
||||
assignment.timeLabel,
|
||||
assignment.church,
|
||||
"\(assignment.role.title) · \(assignment.title)"
|
||||
"\(assignment.roleLabel) · \(assignment.title)"
|
||||
]
|
||||
columnX = margin + 4
|
||||
for (index, column) in columns.enumerated() {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
import PDFKit
|
||||
|
||||
@@ -19,4 +20,31 @@ enum PDFMerger {
|
||||
static func pageCount(_ data: Data) -> Int {
|
||||
PDFDocument(data: data)?.pageCount ?? 0
|
||||
}
|
||||
|
||||
/// Appends one blank page when the document has an odd page count, so that
|
||||
/// every cover sheet and every service list starts on the front side of a
|
||||
/// sheet when printed duplex. Documents with an even page count are
|
||||
/// returned unchanged.
|
||||
static func paddedToEvenPages(_ data: Data) -> Data {
|
||||
guard let document = PDFDocument(data: data), document.pageCount % 2 == 1 else {
|
||||
return data
|
||||
}
|
||||
let size = document.page(at: document.pageCount - 1)?
|
||||
.bounds(for: .mediaBox).size ?? CGSize(width: 595, height: 842)
|
||||
guard let blank = blankPage(size: size) else { return data }
|
||||
return merge([data, blank])
|
||||
}
|
||||
|
||||
private static func blankPage(size: CGSize) -> Data? {
|
||||
guard size.width > 0, size.height > 0 else { return nil }
|
||||
let data = NSMutableData()
|
||||
var mediaBox = CGRect(origin: .zero, size: size)
|
||||
guard let consumer = CGDataConsumer(data: data as CFMutableData),
|
||||
let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil)
|
||||
else { return nil }
|
||||
context.beginPDFPage(nil)
|
||||
context.endPDFPage()
|
||||
context.closePDF()
|
||||
return data as Data
|
||||
}
|
||||
}
|
||||
@@ -227,6 +227,7 @@ final class PlanModel: ObservableObject {
|
||||
@Published var dispatchLog: [DispatchLogEntry] = []
|
||||
@Published var dispatchIntro: String = PlanModel.defaultDispatchIntro
|
||||
@Published var dispatchGreeting: String = PlanModel.defaultDispatchGreeting
|
||||
@Published var dispatchDuplex: Bool = true
|
||||
|
||||
private var toastTask: Task<Void, Never>?
|
||||
|
||||
@@ -1066,23 +1067,35 @@ final class PlanModel: ObservableObject {
|
||||
}
|
||||
|
||||
func dispatchAssignments(for personID: String) -> [DispatchAssignment] {
|
||||
var result: [DispatchAssignment] = []
|
||||
var grouped: [String: DispatchAssignment] = [:]
|
||||
var order: [String] = []
|
||||
for row in rows {
|
||||
for service in row.services {
|
||||
for assignment in service.assignments where assignment.personID == personID {
|
||||
result.append(DispatchAssignment(
|
||||
id: assignment.id,
|
||||
date: row.date,
|
||||
timeMinutes: service.service.timeMinutes,
|
||||
church: service.church.shortName,
|
||||
place: service.service.placeName(church: service.church),
|
||||
title: service.service.title,
|
||||
role: assignment.role
|
||||
))
|
||||
// One person can serve as lector and helper in the same
|
||||
// service. Combine both assignments into one overview row
|
||||
// and one calendar event.
|
||||
if var existing = grouped[service.id] {
|
||||
if !existing.roles.contains(assignment.role) {
|
||||
existing.roles.append(assignment.role)
|
||||
grouped[service.id] = existing
|
||||
}
|
||||
} else {
|
||||
grouped[service.id] = DispatchAssignment(
|
||||
id: service.id,
|
||||
date: row.date,
|
||||
timeMinutes: service.service.timeMinutes,
|
||||
church: service.church.shortName,
|
||||
place: service.service.placeName(church: service.church),
|
||||
title: service.service.title,
|
||||
roles: [assignment.role]
|
||||
)
|
||||
order.append(service.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.sorted {
|
||||
return order.compactMap { grouped[$0] }.sorted {
|
||||
if $0.date != $1.date { return $0.date < $1.date }
|
||||
return $0.timeMinutes < $1.timeMinutes
|
||||
}
|
||||
@@ -1104,7 +1117,7 @@ final class PlanModel: ObservableObject {
|
||||
durationMinutes: 60,
|
||||
summary: assignment.title,
|
||||
location: assignment.place,
|
||||
description: "Dienst: \(assignment.role.title)"
|
||||
description: "Dienst: \(assignment.roleLabel)"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1172,7 +1185,7 @@ final class PlanModel: ObservableObject {
|
||||
return "Für Sie sind in diesem Zeitraum keine Einsätze eingetragen."
|
||||
}
|
||||
return assignments
|
||||
.map { "\($0.dateLabel) · \($0.timeLabel) · \($0.church) · \($0.role.title) · \($0.title)" }
|
||||
.map { "\($0.dateLabel) · \($0.timeLabel) · \($0.church) · \($0.roleLabel) · \($0.title)" }
|
||||
.joined(separator: "\n")
|
||||
}
|
||||
|
||||
@@ -1311,11 +1324,14 @@ final class PlanModel: ObservableObject {
|
||||
assignments: recipient.assignments,
|
||||
generatedAt: Date()
|
||||
))
|
||||
if !cover.isEmpty { documents.append(cover) }
|
||||
if !cover.isEmpty {
|
||||
documents.append(dispatchDuplex ? PDFMerger.paddedToEvenPages(cover) : cover)
|
||||
}
|
||||
}
|
||||
let variant = recipient.preference.variant
|
||||
if planCache[variant] == nil {
|
||||
planCache[variant] = dispatchPlanData(variant: variant)
|
||||
let plan = dispatchPlanData(variant: variant)
|
||||
planCache[variant] = dispatchDuplex ? PDFMerger.paddedToEvenPages(plan) : plan
|
||||
}
|
||||
if let planData = planCache[variant], !planData.isEmpty {
|
||||
documents.append(planData)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
../../../doc/statistics.md
|
||||
@@ -32,6 +32,14 @@ struct DispatchView: View {
|
||||
subtitle: "Den Plan des aktuellen Zeitraums persönlich per E-Mail oder als Sammel-Ausdruck weitergeben."
|
||||
) {
|
||||
HStack(spacing: 8) {
|
||||
Toggle("Doppelseitig drucken", isOn: Binding(
|
||||
get: { model.dispatchDuplex },
|
||||
set: { model.dispatchDuplex = $0 }
|
||||
))
|
||||
.toggleStyle(.checkbox)
|
||||
.font(.system(size: 10))
|
||||
.foregroundStyle(LP.inkSecondary)
|
||||
.help("Hängt an jedes Vorblatt und jede Liste bei Bedarf eine leere Seite an, damit beide auf der Vorderseite eines Blattes beginnen.")
|
||||
SecondaryActionButton(title: "Einstellungen speichern", icon: "square.and.arrow.down") {
|
||||
model.saveDispatchSettings()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Loads help documents that are bundled with the app. The canonical Markdown
|
||||
/// lives in doc/statistics.md and is linked into the app resources.
|
||||
enum HelpDocument {
|
||||
static var statisticsMarkdown: String {
|
||||
guard let url = Bundle.module.url(forResource: "statistics", withExtension: "md"),
|
||||
let text = try? String(contentsOf: url, encoding: .utf8) else {
|
||||
return "# Statistik verstehen\n\nDie Hilfedatei konnte nicht geladen werden."
|
||||
}
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
/// Sheet that displays one bundled Markdown help document.
|
||||
struct HelpSheet: View {
|
||||
let title: String
|
||||
let markdown: String
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
HStack(alignment: .center, spacing: 12) {
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text("HILFE")
|
||||
.font(.system(size: 8.5, weight: .bold, design: .rounded))
|
||||
.tracking(1.2)
|
||||
.foregroundStyle(LP.inkTertiary)
|
||||
Text(title)
|
||||
.font(LP.serifHeadline)
|
||||
.foregroundStyle(LP.ink)
|
||||
}
|
||||
Spacer()
|
||||
Button("Schließen") { dismiss() }
|
||||
.keyboardShortcut(.cancelAction)
|
||||
}
|
||||
.padding(.horizontal, 22)
|
||||
.padding(.vertical, 15)
|
||||
|
||||
Hairline()
|
||||
|
||||
ScrollView {
|
||||
MarkdownHelpView(markdown: markdown, hidesFirstHeading: true)
|
||||
.padding(.horizontal, 26)
|
||||
.padding(.vertical, 22)
|
||||
}
|
||||
}
|
||||
.frame(minWidth: 620, idealWidth: 720, minHeight: 520, idealHeight: 680)
|
||||
.background(LP.paper)
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the small Markdown subset used by the bundled help documents:
|
||||
/// headings, paragraphs, unordered lists and inline emphasis.
|
||||
struct MarkdownHelpView: View {
|
||||
let markdown: String
|
||||
var hidesFirstHeading = false
|
||||
|
||||
private var blocks: [MarkdownBlock] {
|
||||
var parsed = MarkdownBlock.parse(markdown)
|
||||
if hidesFirstHeading, case .heading(level: 1, _)? = parsed.first {
|
||||
parsed.removeFirst()
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 9) {
|
||||
ForEach(Array(blocks.enumerated()), id: \.offset) { _, block in
|
||||
block.view
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.textSelection(.enabled)
|
||||
}
|
||||
}
|
||||
|
||||
private enum MarkdownBlock {
|
||||
case heading(level: Int, text: String)
|
||||
case paragraph(String)
|
||||
case bullet(String)
|
||||
|
||||
@ViewBuilder
|
||||
var view: some View {
|
||||
switch self {
|
||||
case .heading(let level, let text):
|
||||
Text(inline(text))
|
||||
.font(headingFont(level))
|
||||
.foregroundStyle(LP.ink)
|
||||
.padding(.top, level == 1 ? 0 : 9)
|
||||
|
||||
case .paragraph(let text):
|
||||
Text(inline(text))
|
||||
.font(.system(size: 11.5))
|
||||
.foregroundStyle(LP.inkSecondary)
|
||||
.lineSpacing(2)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
|
||||
case .bullet(let text):
|
||||
HStack(alignment: .top, spacing: 7) {
|
||||
Text("•")
|
||||
.font(.system(size: 12, weight: .bold))
|
||||
.foregroundStyle(LP.accent)
|
||||
.padding(.top, 1)
|
||||
Text(inline(text))
|
||||
.font(.system(size: 11.5))
|
||||
.foregroundStyle(LP.inkSecondary)
|
||||
.lineSpacing(2)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func headingFont(_ level: Int) -> Font {
|
||||
switch level {
|
||||
case 1: LP.serifHeadline
|
||||
case 2: LP.serifSubhead
|
||||
default: .system(size: 12, weight: .semibold)
|
||||
}
|
||||
}
|
||||
|
||||
private func inline(_ text: String) -> AttributedString {
|
||||
let options = AttributedString.MarkdownParsingOptions(
|
||||
interpretedSyntax: .inlineOnlyPreservingWhitespace
|
||||
)
|
||||
return (try? AttributedString(markdown: text, options: options)) ?? AttributedString(text)
|
||||
}
|
||||
|
||||
static func parse(_ markdown: String) -> [MarkdownBlock] {
|
||||
var blocks: [MarkdownBlock] = []
|
||||
var paragraph: [String] = []
|
||||
|
||||
func flushParagraph() {
|
||||
guard !paragraph.isEmpty else { return }
|
||||
blocks.append(.paragraph(paragraph.joined(separator: " ")))
|
||||
paragraph.removeAll(keepingCapacity: true)
|
||||
}
|
||||
|
||||
let lines = markdown
|
||||
.replacingOccurrences(of: "\r\n", with: "\n")
|
||||
.components(separatedBy: "\n")
|
||||
for raw in lines {
|
||||
let line = raw.trimmingCharacters(in: .whitespaces)
|
||||
if line.isEmpty {
|
||||
flushParagraph()
|
||||
} else if line.hasPrefix("### ") {
|
||||
flushParagraph()
|
||||
blocks.append(.heading(level: 3, text: String(line.dropFirst(4))))
|
||||
} else if line.hasPrefix("## ") {
|
||||
flushParagraph()
|
||||
blocks.append(.heading(level: 2, text: String(line.dropFirst(3))))
|
||||
} else if line.hasPrefix("# ") {
|
||||
flushParagraph()
|
||||
blocks.append(.heading(level: 1, text: String(line.dropFirst(2))))
|
||||
} else if line.hasPrefix("- ") || line.hasPrefix("* ") {
|
||||
flushParagraph()
|
||||
blocks.append(.bullet(String(line.dropFirst(2))))
|
||||
} else {
|
||||
paragraph.append(line)
|
||||
}
|
||||
}
|
||||
flushParagraph()
|
||||
return blocks
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import LektorenPlanCore
|
||||
|
||||
struct StatisticsView: View {
|
||||
@EnvironmentObject private var model: PlanModel
|
||||
@State private var showHelp = false
|
||||
|
||||
private var stats: RangeStatistics { model.stats }
|
||||
|
||||
@@ -43,6 +44,9 @@ struct StatisticsView: View {
|
||||
subtitle: "Einsatzhäufigkeit, Orte, Vorlieben und Fairness für den gewählten Zeitraum."
|
||||
) {
|
||||
HStack(spacing: 8) {
|
||||
SecondaryActionButton(title: "Hilfe", icon: "questionmark.circle") {
|
||||
showHelp = true
|
||||
}
|
||||
SecondaryActionButton(title: "PDF", icon: "doc.richtext") {
|
||||
model.createPDFPreview()
|
||||
}
|
||||
@@ -76,6 +80,9 @@ struct StatisticsView: View {
|
||||
.padding(.horizontal, 22)
|
||||
.padding(.top, 30)
|
||||
.padding(.bottom, 18)
|
||||
.sheet(isPresented: $showHelp) {
|
||||
HelpSheet(title: "Statistik verstehen", markdown: HelpDocument.statisticsMarkdown)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - KPI
|
||||
@@ -89,7 +96,7 @@ struct StatisticsView: View {
|
||||
kpiCard(icon: "star", value: "\(Int(stats.preferenceRate * 100)) %", label: "Präferenztreffer", tint: LP.burgundy)
|
||||
kpiCard(icon: "person.2", value: "\(stats.activePeople)", label: "eingesetzte Personen", tint: LP.accent)
|
||||
kpiCard(icon: "calendar.badge.minus", value: "\(stats.absenceDays)", label: "Abwesenheitstage", tint: LP.open)
|
||||
kpiCard(icon: "cross.case", value: "\(stats.sundayServiceCount)", label: "Sonntage", tint: LP.gold)
|
||||
kpiCard(icon: "cross.case", value: "\(stats.sundayServiceCount)", label: "Sonntagsdienste", tint: LP.gold)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +184,7 @@ struct StatisticsView: View {
|
||||
Text(String(format: "%.1f", average))
|
||||
.font(.system(size: 30, weight: .bold, design: .rounded))
|
||||
.foregroundStyle(LP.ink)
|
||||
Text("Einsätze pro aktive Person im Schnitt")
|
||||
Text("Einsätze pro eingesetzter Person im Schnitt")
|
||||
.font(.system(size: 9.5))
|
||||
.foregroundStyle(LP.inkTertiary)
|
||||
|
||||
@@ -186,7 +193,7 @@ struct StatisticsView: View {
|
||||
fairnessRow(label: "Höchste Last", value: "\(maximum)")
|
||||
fairnessRow(label: "Niedrigste Last", value: "\(minimum)")
|
||||
fairnessRow(label: "Spannweite", value: "\(maximum - minimum)")
|
||||
fairnessRow(label: "Aktive Personen", value: "\(totals.count)")
|
||||
fairnessRow(label: "Eingesetzte Personen", value: "\(totals.count)")
|
||||
|
||||
Hairline()
|
||||
|
||||
@@ -370,7 +377,7 @@ struct StatisticsView: View {
|
||||
.tracking(1.1)
|
||||
.foregroundStyle(LP.inkTertiary)
|
||||
Spacer()
|
||||
Text("Personen mit Einsätzen im Zeitraum")
|
||||
Text("Alle Personen im Zeitraum")
|
||||
.font(.system(size: 8.5))
|
||||
.foregroundStyle(LP.inkTertiary)
|
||||
}
|
||||
@@ -385,8 +392,8 @@ struct StatisticsView: View {
|
||||
.frame(width: 28)
|
||||
Text("K")
|
||||
.frame(width: 28)
|
||||
Text("Sonntage")
|
||||
.frame(width: 62)
|
||||
Text("Sonntags-Einsätze")
|
||||
.frame(width: 92)
|
||||
Text("Kirchen")
|
||||
.frame(width: 150, alignment: .leading)
|
||||
Text("Präferenz")
|
||||
@@ -400,7 +407,7 @@ struct StatisticsView: View {
|
||||
.padding(.vertical, 7)
|
||||
.background(LP.cardMuted.opacity(0.5))
|
||||
|
||||
ForEach(Array(activePersonStats.enumerated()), id: \.element.id) { index, stat in
|
||||
ForEach(Array(stats.personStats.enumerated()), id: \.element.id) { index, stat in
|
||||
HStack(spacing: 8) {
|
||||
HStack(spacing: 6) {
|
||||
Avatar(initials: stat.person.initials,
|
||||
@@ -418,7 +425,7 @@ struct StatisticsView: View {
|
||||
Text("\(stat.helpers)")
|
||||
.frame(width: 28)
|
||||
Text("\(stat.sundayCount)")
|
||||
.frame(width: 62)
|
||||
.frame(width: 92)
|
||||
HStack(spacing: 3) {
|
||||
ForEach(Array(stat.churchCounts.sorted { $0.value > $1.value }.prefix(3)), id: \.key) { item in
|
||||
if let church = model.churchByID[item.key] {
|
||||
|
||||
@@ -36,6 +36,22 @@ public enum Role: String, Codable, CaseIterable, Identifiable, Sendable, Hashabl
|
||||
}
|
||||
}
|
||||
|
||||
/// Readable combination of roles in canonical order, e.g.
|
||||
/// "Lektor und Kommunionhelfer" when one person fills both roles in the
|
||||
/// same service.
|
||||
public static func combinedTitle(_ roles: [Role]) -> String {
|
||||
let ordered = allCases.filter { roles.contains($0) }
|
||||
switch ordered.count {
|
||||
case 0:
|
||||
return ""
|
||||
case 1:
|
||||
return ordered[0].title
|
||||
default:
|
||||
let leading = ordered.dropLast().map(\.title).joined(separator: ", ")
|
||||
return "\(leading) und \(ordered[ordered.count - 1].title)"
|
||||
}
|
||||
}
|
||||
|
||||
/// The other role a service may need. Lector and communion helper do not
|
||||
/// exclude each other, so one person can fill both at the same service.
|
||||
public var complementary: Role {
|
||||
|
||||
Reference in new issue
Block a user