From 7cacfc984bac3591f3ffc9161a4646e6b3df5f58 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 5 Aug 2025 17:27:58 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20added=20"N=C3=A4chste=20Frage"=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QuizApp/ContentView.swift | 13 ++++++++++-- QuizApp/Topic.swift | 2 +- QuizApp/ViewModel.swift | 22 +++++++++++++++----- QuizApp/data.json | 42 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/QuizApp/ContentView.swift b/QuizApp/ContentView.swift index 460cd4a..cf971bc 100644 --- a/QuizApp/ContentView.swift +++ b/QuizApp/ContentView.swift @@ -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) } } } diff --git a/QuizApp/Topic.swift b/QuizApp/Topic.swift index f1ee44b..93f27f7 100644 --- a/QuizApp/Topic.swift +++ b/QuizApp/Topic.swift @@ -7,7 +7,7 @@ import Foundation -struct Topic: Decodable, Hashable { +struct QuizQuestion: Decodable, Hashable { let question: String let answers: [String] diff --git a/QuizApp/ViewModel.swift b/QuizApp/ViewModel.swift index 38c076b..abbb918 100644 --- a/QuizApp/ViewModel.swift +++ b/QuizApp/ViewModel.swift @@ -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 } } } diff --git a/QuizApp/data.json b/QuizApp/data.json index b24718f..a44f64c 100644 --- a/QuizApp/data.json +++ b/QuizApp/data.json @@ -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 } ]