Update QuizView.swift

Fixed a bug where a user could select another answer after an answer was previously selected.

Removed the Back button. There's no going back now! ;3
This commit is contained in:
Justin Oros
2025-07-03 13:14:43 -07:00
committed by GitHub
parent 5c8820fd6c
commit 4115ff91e7
@@ -23,7 +23,7 @@ struct QuizView: View {
var body: some View { var body: some View {
VStack(alignment: .leading, spacing: 20) { VStack(alignment: .leading, spacing: 20) {
if loadError { if loadError {
Text("Could not load questions for \(testName). Make sure the .json file is included in the app bundle or Documents folder.") Text("Could not load questions for \(testName).")
.foregroundColor(.red) .foregroundColor(.red)
.padding() .padding()
} else if questions.isEmpty { } else if questions.isEmpty {
@@ -36,6 +36,7 @@ struct QuizView: View {
ForEach(questions[currentIndex].answers, id: \.self) { answer in ForEach(questions[currentIndex].answers, id: \.self) { answer in
Button(action: { Button(action: {
guard selectedAnswer == nil else { return }
selectedAnswer = answer selectedAnswer = answer
checkAnswer() checkAnswer()
@@ -58,6 +59,7 @@ struct QuizView: View {
.foregroundColor(.white) .foregroundColor(.white)
} }
.buttonStyle(PlainButtonStyle()) .buttonStyle(PlainButtonStyle())
.disabled(selectedAnswer != nil)
} }
Text(feedbackText) Text(feedbackText)
@@ -92,6 +94,7 @@ struct QuizView: View {
.padding() .padding()
.onAppear(perform: syncAndLoadQuestions) .onAppear(perform: syncAndLoadQuestions)
.navigationTitle(testName + " Test") .navigationTitle(testName + " Test")
.navigationBarBackButtonHidden(true)
.alert(isPresented: $showSummaryAlert) { .alert(isPresented: $showSummaryAlert) {
let percentage = currentIndex > 0 ? Int((Double(score) / Double(currentIndex + 1)) * 100) : 0 let percentage = currentIndex > 0 ? Int((Double(score) / Double(currentIndex + 1)) * 100) : 0
return Alert( return Alert(