From 681e8258c42c04653c6f87c622cf04eef1b98dc5 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 7 Aug 2025 18:47:47 +0200 Subject: [PATCH] feat: new globalScore handling --- QuizApp/CategorySelectionView.swift | 2 +- QuizApp/ContentView.swift | 25 ++++++++++++++++++++++--- QuizApp/Topic.swift | 16 +++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/QuizApp/CategorySelectionView.swift b/QuizApp/CategorySelectionView.swift index f5e1235..d10492f 100644 --- a/QuizApp/CategorySelectionView.swift +++ b/QuizApp/CategorySelectionView.swift @@ -8,7 +8,7 @@ import SwiftUI struct CategorySelectionView: View { - @ObservedObject var viewModel = ViewModel() + @StateObject private var viewModel = ViewModel() @AppStorage("globalScore") var globalScore: Int = 0 var body: some View { diff --git a/QuizApp/ContentView.swift b/QuizApp/ContentView.swift index 5f04a36..692a6fd 100644 --- a/QuizApp/ContentView.swift +++ b/QuizApp/ContentView.swift @@ -16,6 +16,7 @@ struct ContentView: View { @State private var selectedAnswerIndex: Int? = nil @State private var isAnswered = false + @State private var showExitAlert = false var body: some View { VStack { @@ -45,6 +46,7 @@ struct ContentView: View { } .onAppear { AudioServicesPlaySystemSound(1322) + score += viewModel.score } } else if let frage = viewModel.currentQuestion { Text("Punkte: \(viewModel.score)") @@ -82,9 +84,6 @@ struct ContentView: View { viewModel.incrementScore(selectedIndex: index) viewModel.answeredCount += 1 viewModel.selectedAnswers[viewModel.currentQuestionIndex] = index - if index == frage.correctAnswer { - score += 1 - } }) { Text(frage.answers[index]) .padding(.vertical, 12) @@ -128,6 +127,26 @@ struct ContentView: View { Text("Fragen werden geladen...") } } + .navigationBarBackButtonHidden(!viewModel.isQuizFinished) // Button verstecken bei aktivem Quiz + .toolbar { + if !viewModel.isQuizFinished { // Custom Button nur bei aktivem Quiz + ToolbarItem(placement: .navigationBarLeading) { + Button("Abbrechen") { + showExitAlert = true + } + .foregroundColor(.red) + } + } + } + .alert("Quiz verlassen?", isPresented: $showExitAlert) { + Button("Quiz verlassen", role: .destructive) { + dismiss() + } + Button("Weiter spielen", role: .cancel) { + } + } message: { + Text("Deine im Quiz gesammelten Punkte werden nicht gespeichert.") + } } private func buttonColor(for index: Int, correctIndex: Int) -> Color { diff --git a/QuizApp/Topic.swift b/QuizApp/Topic.swift index 1a08f0f..b8fe44b 100644 --- a/QuizApp/Topic.swift +++ b/QuizApp/Topic.swift @@ -7,8 +7,22 @@ import Foundation -struct QuizQuestion: Decodable, Hashable { +struct QuizQuestion: Decodable, Hashable, Identifiable { + let id: UUID let question: String let answers: [String] let correctAnswer: Int + + // Manuelles Decoding, id wird generiert + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + question = try container.decode(String.self, forKey: .question) + answers = try container.decode([String].self, forKey: .answers) + correctAnswer = try container.decode(Int.self, forKey: .correctAnswer) + id = UUID() + } + + private enum CodingKeys: String, CodingKey { + case question, answers, correctAnswer + } }