diff --git a/QuizApp/CategorySelectionView.swift b/QuizApp/CategorySelectionView.swift new file mode 100644 index 0000000..0950639 --- /dev/null +++ b/QuizApp/CategorySelectionView.swift @@ -0,0 +1,39 @@ +// +// CategorySelectionView.swift +// QuizApp +// +// Created by Paul on 06.08.25. +// + +import SwiftUI + +struct CategorySelectionView: View { + @ObservedObject var viewModel = ViewModel() + @State private var selectedCategory: String? = nil + + var body: some View { + if viewModel.selectedCategory != nil && !viewModel.questions.isEmpty { + ContentView(viewModel: viewModel) + } else { + VStack(spacing: 20) { + Text("Wähle eine Kategorie") + .font(.largeTitle) + + ForEach(viewModel.availableCategories, id: \.self) { category in + Button(action: { + viewModel.loadQuestions(for: category) + selectedCategory = category + }) { + Text(category) + .font(.title2) + .padding() + .frame(maxWidth: 300) + .background(Color.blue) + .foregroundColor(.white) + .cornerRadius(12) + } + } + } + } + } +} diff --git a/QuizApp/ContentView.swift b/QuizApp/ContentView.swift index b0490ab..79f31bf 100644 --- a/QuizApp/ContentView.swift +++ b/QuizApp/ContentView.swift @@ -2,7 +2,7 @@ // ContentView.swift // QuizApp // -// Created by Paul Nowotka on 02.08.25. +// Created by Paul on 02.08.25. // import SwiftUI @@ -31,6 +31,14 @@ struct ContentView: View { .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) + + Button("Zurück zur Kategorieauswahl") { + viewModel.selectedCategory = nil + } + .padding() + .background(Color.gray) + .foregroundColor(.white) + .cornerRadius(10) } } else if let frage = viewModel.currentQuestion { Text("Punkte: \(viewModel.score)") @@ -132,6 +140,7 @@ struct ContentView: View { selectedAnswerIndex = nil isAnswered = false viewModel.answeredCount = 0 + viewModel.selectedAnswers = Array(repeating: nil, count: viewModel.questions.count) } func colorForAnswer(at index: Int) -> Color { @@ -155,5 +164,5 @@ struct ContentView: View { } #Preview { - ContentView() + CategorySelectionView() } diff --git a/QuizApp/QuizAppApp.swift b/QuizApp/QuizAppApp.swift index c61739a..3dab26c 100644 --- a/QuizApp/QuizAppApp.swift +++ b/QuizApp/QuizAppApp.swift @@ -2,7 +2,7 @@ // QuizAppApp.swift // QuizApp // -// Created by Paul Nowotka on 02.08.25. +// Created by Paul on 02.08.25. // import SwiftUI @@ -11,7 +11,7 @@ import SwiftUI struct QuizAppApp: App { var body: some Scene { WindowGroup { - ContentView() + CategorySelectionView() } } } diff --git a/QuizApp/Topic.swift b/QuizApp/Topic.swift index 93f27f7..1a08f0f 100644 --- a/QuizApp/Topic.swift +++ b/QuizApp/Topic.swift @@ -2,15 +2,13 @@ // Topic.swift // QuizApp // -// Created by Paul Nowotka on 03.08.25. +// Created by Paul on 03.08.25. // import Foundation struct QuizQuestion: Decodable, Hashable { - let question: String let answers: [String] let correctAnswer: Int - } diff --git a/QuizApp/ViewModel.swift b/QuizApp/ViewModel.swift index b02485c..3b32bc4 100644 --- a/QuizApp/ViewModel.swift +++ b/QuizApp/ViewModel.swift @@ -2,14 +2,16 @@ // ViewModel.swift // QuizApp // -// Created by Paul Nowotka on 03.08.25. +// Created by Paul on 03.08.25. // import Foundation class ViewModel: ObservableObject { + private var allQuestionsByCategory: [String: [QuizQuestion]] = [:] @Published var questions: [QuizQuestion] = [] + @Published var selectedCategory: String? = nil @Published var currentQuestionIndex: Int = 0 @Published var score: Int = 0 @Published var isQuizFinished: Bool = false @@ -27,6 +29,11 @@ class ViewModel: ObservableObject { return questions[currentQuestionIndex] } + var availableCategories: [String] { + return Array(allQuestionsByCategory.keys).sorted() + } + + init() { guard let url = Bundle.main.url(forResource: "data", withExtension: "json") else { questions = [] @@ -35,11 +42,10 @@ class ViewModel: ObservableObject { do { let data = try Data(contentsOf: url) - questions = try JSONDecoder().decode([QuizQuestion].self, from: data) - self.selectedAnswers = Array(repeating: nil, count: questions.count) + allQuestionsByCategory = try JSONDecoder().decode([String: [QuizQuestion]].self, from: data) } catch { print("Fehler beim Laden des Inhalts: \(error)") - questions = [] + allQuestionsByCategory = [:] } } @@ -51,6 +57,17 @@ class ViewModel: ObservableObject { } } + func loadQuestions(for category: String) { + selectedCategory = category + questions = allQuestionsByCategory[category] ?? [] + currentQuestionIndex = 0 + score = 0 + isQuizFinished = false + answeredCount = 0 + selectedAnswers = Array(repeating: nil, count: questions.count) + } + + func incrementScore(selectedIndex: Int) { if let correct = currentQuestion?.correctAnswer, selectedIndex == correct { score += 1 diff --git a/QuizApp/data.json b/QuizApp/data.json index a44f64c..a1ab7ac 100644 --- a/QuizApp/data.json +++ b/QuizApp/data.json @@ -1,52 +1,186 @@ -[ - { - "question": "Was ist die Hauptstadt von Frankreich?", - "answers": ["Rom", "Berlin", "Paris"], - "correctAnswer": 2 - }, - { - "question": "Was ist die Hauptstadt von Deutschland?", - "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 - } -] +{ + "Hauptstädte": [ + { + "question": "Was ist die Hauptstadt von Frankreich?", + "answers": [ + "Rom", + "Berlin", + "Paris" + ], + "correctAnswer": 2 + }, + { + "question": "Was ist die Hauptstadt von Deutschland?", + "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 + } + ], + "Mathematik": [ + { + "question": "Was ist 5 + 3?", + "answers": [ + "7", + "8", + "9" + ], + "correctAnswer": 1 + }, + { + "question": "Was ist 12 - 4?", + "answers": [ + "6", + "8", + "9" + ], + "correctAnswer": 1 + }, + { + "question": "Was ist 6 × 7?", + "answers": [ + "42", + "36", + "48" + ], + "correctAnswer": 0 + }, + { + "question": "Was ist 81 ÷ 9?", + "answers": [ + "8", + "9", + "10" + ], + "correctAnswer": 1 + }, + { + "question": "Was ist das Quadrat von 5?", + "answers": [ + "20", + "25", + "30" + ], + "correctAnswer": 1 + }, + { + "question": "Was ist 10 % von 200?", + "answers": [ + "20", + "25", + "10" + ], + "correctAnswer": 0 + }, + { + "question": "Was ergibt 2³?", + "answers": [ + "6", + "8", + "9" + ], + "correctAnswer": 1 + }, + { + "question": "Was ist die Wurzel aus 64?", + "answers": [ + "6", + "7", + "8" + ], + "correctAnswer": 2 + }, + { + "question": "Was ist 3 × (4 + 2)?", + "answers": [ + "18", + "20", + "12" + ], + "correctAnswer": 0 + }, + { + "question": "Wie viel ist 100 ÷ 4?", + "answers": [ + "20", + "25", + "30" + ], + "correctAnswer": 1 + } + ] +}