113 lines
3.4 KiB
Swift
113 lines
3.4 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
|
|
// MARK: - AftermathRatingFlowView
|
|
// Sheet-basierter Bewertungs-Flow für die Nachwirkungs-Bewertung (3 Fragen).
|
|
// Wird aus einer Push-Notification heraus oder aus VisitHistorySection geöffnet.
|
|
|
|
struct AftermathRatingFlowView: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
let visit: Visit
|
|
|
|
private let questions = RatingQuestion.aftermath // 3 Fragen
|
|
@State private var currentIndex: Int = 0
|
|
@State private var values: [Int?]
|
|
@State private var showSummary: Bool = false
|
|
|
|
init(visit: Visit) {
|
|
self.visit = visit
|
|
_values = State(initialValue: Array(repeating: nil, count: RatingQuestion.aftermath.count))
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Group {
|
|
if showSummary {
|
|
VisitSummaryView(visit: visit, onDismiss: { dismiss() })
|
|
} else {
|
|
questionStep
|
|
}
|
|
}
|
|
.navigationTitle("Nachwirkung")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Abbrechen") { dismiss() }
|
|
}
|
|
if !showSummary {
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button(currentIndex == questions.count - 1 ? "Fertig" : "Weiter") {
|
|
advance()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Fragen-Screen
|
|
|
|
private var questionStep: some View {
|
|
ZStack {
|
|
RatingQuestionView(
|
|
question: questions[currentIndex],
|
|
index: currentIndex,
|
|
total: questions.count,
|
|
value: $values[currentIndex]
|
|
)
|
|
.id(currentIndex)
|
|
.transition(.asymmetric(
|
|
insertion: .move(edge: .trailing),
|
|
removal: .move(edge: .leading)
|
|
))
|
|
}
|
|
.clipped()
|
|
}
|
|
|
|
// MARK: - Navigation & Speichern
|
|
|
|
private func advance() {
|
|
if currentIndex < questions.count - 1 {
|
|
withAnimation { currentIndex += 1 }
|
|
} else {
|
|
saveAftermath()
|
|
}
|
|
}
|
|
|
|
private func saveAftermath() {
|
|
for (i, q) in questions.enumerated() {
|
|
let rating = Rating(
|
|
category: q.category,
|
|
questionIndex: i,
|
|
value: values[i],
|
|
isAftermath: true,
|
|
visit: visit
|
|
)
|
|
modelContext.insert(rating)
|
|
}
|
|
|
|
visit.status = .completed
|
|
visit.aftermathCompletedAt = Date()
|
|
|
|
// Evtl. geplante Notification abbrechen (falls Nutzer selbst geöffnet hat)
|
|
AftermathNotificationManager.shared.cancelAftermath(visitID: visit.id)
|
|
|
|
do {
|
|
try modelContext.save()
|
|
AppEventLog.shared.record(
|
|
"Nachwirkung abgeschlossen für Visit \(visit.id.uuidString)",
|
|
level: .success, category: "Visit"
|
|
)
|
|
} catch {
|
|
AppEventLog.shared.record(
|
|
"Fehler beim Speichern der Nachwirkung: \(error.localizedDescription)",
|
|
level: .error, category: "Visit"
|
|
)
|
|
}
|
|
|
|
withAnimation { showSummary = true }
|
|
}
|
|
}
|