feat: added score counter and end of quiz
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user