feat: new globalScore handling
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct CategorySelectionView: View {
|
||||
@ObservedObject var viewModel = ViewModel()
|
||||
@StateObject private var viewModel = ViewModel()
|
||||
@AppStorage("globalScore") var globalScore: Int = 0
|
||||
|
||||
var body: some View {
|
||||
|
||||
@@ -16,6 +16,7 @@ struct ContentView: View {
|
||||
|
||||
@State private var selectedAnswerIndex: Int? = nil
|
||||
@State private var isAnswered = false
|
||||
@State private var showExitAlert = false
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
@@ -45,6 +46,7 @@ struct ContentView: View {
|
||||
}
|
||||
.onAppear {
|
||||
AudioServicesPlaySystemSound(1322)
|
||||
score += viewModel.score
|
||||
}
|
||||
} else if let frage = viewModel.currentQuestion {
|
||||
Text("Punkte: \(viewModel.score)")
|
||||
@@ -82,9 +84,6 @@ struct ContentView: View {
|
||||
viewModel.incrementScore(selectedIndex: index)
|
||||
viewModel.answeredCount += 1
|
||||
viewModel.selectedAnswers[viewModel.currentQuestionIndex] = index
|
||||
if index == frage.correctAnswer {
|
||||
score += 1
|
||||
}
|
||||
}) {
|
||||
Text(frage.answers[index])
|
||||
.padding(.vertical, 12)
|
||||
@@ -128,6 +127,26 @@ struct ContentView: View {
|
||||
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 {
|
||||
|
||||
@@ -7,8 +7,22 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct QuizQuestion: Decodable, Hashable {
|
||||
struct QuizQuestion: Decodable, Hashable, Identifiable {
|
||||
let id: UUID
|
||||
let question: String
|
||||
let answers: [String]
|
||||
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