feat: new globalScore handling
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct CategorySelectionView: View {
|
struct CategorySelectionView: View {
|
||||||
@ObservedObject var viewModel = ViewModel()
|
@StateObject private var viewModel = ViewModel()
|
||||||
@AppStorage("globalScore") var globalScore: Int = 0
|
@AppStorage("globalScore") var globalScore: Int = 0
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ struct ContentView: View {
|
|||||||
|
|
||||||
@State private var selectedAnswerIndex: Int? = nil
|
@State private var selectedAnswerIndex: Int? = nil
|
||||||
@State private var isAnswered = false
|
@State private var isAnswered = false
|
||||||
|
@State private var showExitAlert = false
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
@@ -45,6 +46,7 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
AudioServicesPlaySystemSound(1322)
|
AudioServicesPlaySystemSound(1322)
|
||||||
|
score += viewModel.score
|
||||||
}
|
}
|
||||||
} else if let frage = viewModel.currentQuestion {
|
} else if let frage = viewModel.currentQuestion {
|
||||||
Text("Punkte: \(viewModel.score)")
|
Text("Punkte: \(viewModel.score)")
|
||||||
@@ -82,9 +84,6 @@ struct ContentView: View {
|
|||||||
viewModel.incrementScore(selectedIndex: index)
|
viewModel.incrementScore(selectedIndex: index)
|
||||||
viewModel.answeredCount += 1
|
viewModel.answeredCount += 1
|
||||||
viewModel.selectedAnswers[viewModel.currentQuestionIndex] = index
|
viewModel.selectedAnswers[viewModel.currentQuestionIndex] = index
|
||||||
if index == frage.correctAnswer {
|
|
||||||
score += 1
|
|
||||||
}
|
|
||||||
}) {
|
}) {
|
||||||
Text(frage.answers[index])
|
Text(frage.answers[index])
|
||||||
.padding(.vertical, 12)
|
.padding(.vertical, 12)
|
||||||
@@ -128,6 +127,26 @@ struct ContentView: View {
|
|||||||
Text("Fragen werden geladen...")
|
Text("Fragen werden geladen...")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.navigationBarBackButtonHidden(!viewModel.isQuizFinished) // Button verstecken bei aktivem Quiz
|
||||||
|
.toolbar {
|
||||||
|
if !viewModel.isQuizFinished { // Custom Button nur bei aktivem Quiz
|
||||||
|
ToolbarItem(placement: .navigationBarLeading) {
|
||||||
|
Button("Abbrechen") {
|
||||||
|
showExitAlert = true
|
||||||
|
}
|
||||||
|
.foregroundColor(.red)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.alert("Quiz verlassen?", isPresented: $showExitAlert) {
|
||||||
|
Button("Quiz verlassen", role: .destructive) {
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
Button("Weiter spielen", role: .cancel) {
|
||||||
|
}
|
||||||
|
} message: {
|
||||||
|
Text("Deine im Quiz gesammelten Punkte werden nicht gespeichert.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func buttonColor(for index: Int, correctIndex: Int) -> Color {
|
private func buttonColor(for index: Int, correctIndex: Int) -> Color {
|
||||||
|
|||||||
@@ -7,8 +7,22 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct QuizQuestion: Decodable, Hashable {
|
struct QuizQuestion: Decodable, Hashable, Identifiable {
|
||||||
|
let id: UUID
|
||||||
let question: String
|
let question: String
|
||||||
let answers: [String]
|
let answers: [String]
|
||||||
let correctAnswer: Int
|
let correctAnswer: Int
|
||||||
|
|
||||||
|
// Manuelles Decoding, id wird generiert
|
||||||
|
init(from decoder: Decoder) throws {
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
question = try container.decode(String.self, forKey: .question)
|
||||||
|
answers = try container.decode([String].self, forKey: .answers)
|
||||||
|
correctAnswer = try container.decode(Int.self, forKey: .correctAnswer)
|
||||||
|
id = UUID()
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum CodingKeys: String, CodingKey {
|
||||||
|
case question, answers, correctAnswer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user