From 1ce4ca62417331c297200a76fcd9ed3cdf118140 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 5 Aug 2025 21:00:13 +0200 Subject: [PATCH] feat: added score counter and end of quiz --- QuizApp/ContentView.swift | 44 ++++++++++++++++++++++++++++++++++----- QuizApp/ViewModel.swift | 10 ++++++++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/QuizApp/ContentView.swift b/QuizApp/ContentView.swift index cf971bc..b7e9ae5 100644 --- a/QuizApp/ContentView.swift +++ b/QuizApp/ContentView.swift @@ -13,10 +13,30 @@ struct ContentView: View { @State private var selectedAnswerIndex: Int? = nil @State private var isAnswered = false - + var body: some View { VStack { - if let frage = viewModel.currentQuestion { + if viewModel.isQuizFinished { + VStack(spacing: 20) { + Text("🎉 Quiz beendet!") + .font(.title) + + Text("Dein Ergebnis: \(viewModel.score) von \(viewModel.questions.count) Punkten") + .font(.headline) + + Button("Nochmal spielen") { + restartQuiz() + } + .padding() + .background(Color.blue) + .foregroundColor(.white) + .cornerRadius(10) + } + } else if let frage = viewModel.currentQuestion { + Text("Punkte: \(viewModel.score)") + .font(.headline) + .padding(.bottom, 10) + Text(frage.question) .font(.title) .multilineTextAlignment(.center) @@ -28,15 +48,21 @@ struct ContentView: View { isAnswered = true print("Selected:", index) print("Correct:", frage.correctAnswer) + viewModel.incrementScore(selectedIndex: index) }) { Text(frage.answers[index]) - .padding() + .padding(.vertical, 12) + .padding(.horizontal, 20) .frame(maxWidth: 300) .background(buttonColor(for: index, correctIndex: frage.correctAnswer)) .foregroundColor(.white) .cornerRadius(10) + .scaleEffect(isAnswered && selectedAnswerIndex == index ? 1.05 : 1.0) + .shadow(radius: isAnswered && selectedAnswerIndex == index ? 5 : 2) } + .animation(.easeInOut(duration: 0.3), value: isAnswered) .disabled(isAnswered) + } VStack { if isAnswered { @@ -61,7 +87,7 @@ struct ContentView: View { } } } - .frame(height: 80) + .frame(height: 100) } else { Text("Fragen werden geladen...") } @@ -69,7 +95,7 @@ struct ContentView: View { } private func buttonColor(for index: Int, correctIndex: Int) -> Color { guard isAnswered else { return .blue } - + if index == correctIndex { return .green } else if index == selectedAnswerIndex { @@ -78,6 +104,14 @@ struct ContentView: View { return .gray } } + private func restartQuiz() { + viewModel.currentQuestionIndex = 0 + viewModel.score = 0 + viewModel.isQuizFinished = false + selectedAnswerIndex = nil + isAnswered = false + } + } #Preview { diff --git a/QuizApp/ViewModel.swift b/QuizApp/ViewModel.swift index abbb918..6718d98 100644 --- a/QuizApp/ViewModel.swift +++ b/QuizApp/ViewModel.swift @@ -11,6 +11,8 @@ class ViewModel: ObservableObject { @Published var questions: [QuizQuestion] = [] @Published var currentQuestionIndex: Int = 0 + @Published var score: Int = 0 + @Published var isQuizFinished: Bool = false var currentQuestion: QuizQuestion? { guard currentQuestionIndex < questions.count else { return nil } @@ -36,7 +38,13 @@ class ViewModel: ObservableObject { if currentQuestionIndex + 1 < questions.count { currentQuestionIndex += 1 } else { - currentQuestionIndex = 0 // oder Quiz-Ende behandeln + isQuizFinished = true + } + } + + func incrementScore(selectedIndex: Int) { + if let correct = currentQuestion?.correctAnswer, selectedIndex == correct { + score += 1 } } }