feat: added "Nächste Frage" button

This commit is contained in:
2025-08-05 17:27:58 +02:00
parent 20c74361c9
commit 7cacfc984b
4 changed files with 70 additions and 9 deletions

View File

@@ -16,7 +16,7 @@ struct ContentView: View {
var body: some View {
VStack {
if let frage = viewModel.topics.first {
if let frage = viewModel.currentQuestion {
Text(frage.question)
.font(.title)
.multilineTextAlignment(.center)
@@ -46,9 +46,18 @@ struct ContentView: View {
.padding()
}
if isAnswered {
Button("Nächste Frage") {
Button(action: {
viewModel.loadNextQuestion()
selectedAnswerIndex = nil
isAnswered = false
}) {
Text("Nächste Frage")
.font(.headline)
.padding(.vertical, 10)
.padding(.horizontal, 30)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}
}

View File

@@ -7,7 +7,7 @@
import Foundation
struct Topic: Decodable, Hashable {
struct QuizQuestion: Decodable, Hashable {
let question: String
let answers: [String]

View File

@@ -9,22 +9,34 @@ import Foundation
class ViewModel: ObservableObject {
let topics: [Topic]
@Published var questions: [QuizQuestion] = []
@Published var currentQuestionIndex: Int = 0
var currentQuestion: QuizQuestion? {
guard currentQuestionIndex < questions.count else { return nil }
return questions[currentQuestionIndex]
}
init() {
guard let url = Bundle.main.url(forResource: "data", withExtension: "json") else {
topics = []
questions = []
return
}
do {
let data = try Data(contentsOf: url)
topics = try JSONDecoder().decode([Topic].self, from: data)
questions = try JSONDecoder().decode([QuizQuestion].self, from: data)
} catch {
print("Fehler beim Laden des Inhalts: \(error)")
topics = []
questions = []
}
}
func loadNextQuestion() {
if currentQuestionIndex + 1 < questions.count {
currentQuestionIndex += 1
} else {
currentQuestionIndex = 0 // oder Quiz-Ende behandeln
}
}
}

View File

@@ -6,7 +6,47 @@
},
{
"question": "Was ist die Hauptstadt von Deutschland?",
"answers": ["Rom", "Berlin", "Paris"],
"answers": ["Berlin", "Wien", "Hamburg"],
"correctAnswer": 0
},
{
"question": "Was ist die Hauptstadt von Italien?",
"answers": ["Rom", "Mailand", "Neapel"],
"correctAnswer": 0
},
{
"question": "Was ist die Hauptstadt von Spanien?",
"answers": ["Barcelona", "Madrid", "Valencia"],
"correctAnswer": 1
},
{
"question": "Was ist die Hauptstadt von Österreich?",
"answers": ["Graz", "Salzburg", "Wien"],
"correctAnswer": 2
},
{
"question": "Was ist die Hauptstadt der Schweiz?",
"answers": ["Zürich", "Bern", "Genf"],
"correctAnswer": 1
},
{
"question": "Was ist die Hauptstadt von Portugal?",
"answers": ["Lissabon", "Porto", "Faro"],
"correctAnswer": 0
},
{
"question": "Was ist die Hauptstadt von Belgien?",
"answers": ["Brüssel", "Antwerpen", "Brügge"],
"correctAnswer": 0
},
{
"question": "Was ist die Hauptstadt von Niederlande?",
"answers": ["Rotterdam", "Amsterdam", "Den Haag"],
"correctAnswer": 1
},
{
"question": "Was ist die Hauptstadt von Polen?",
"answers": ["Warschau", "Krakau", "Danzig"],
"correctAnswer": 0
}
]