feat: added global quiz score
This commit is contained in:
@@ -9,16 +9,61 @@ import SwiftUI
|
|||||||
|
|
||||||
struct CategorySelectionView: View {
|
struct CategorySelectionView: View {
|
||||||
@ObservedObject var viewModel = ViewModel()
|
@ObservedObject var viewModel = ViewModel()
|
||||||
|
@AppStorage("globalScore") var globalScore: Int = 0
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
VStack(spacing: 24) {
|
VStack(spacing: 24) {
|
||||||
// Minimaler Header
|
// Willkommensnachricht
|
||||||
Text("Quiz Kategorien")
|
VStack(spacing: 8) {
|
||||||
.font(.title)
|
Text("Willkommen!")
|
||||||
|
.font(.largeTitle)
|
||||||
.fontWeight(.bold)
|
.fontWeight(.bold)
|
||||||
|
.foregroundColor(.primary)
|
||||||
|
|
||||||
|
Text("Wähle eine Kategorie und teste dein Wissen")
|
||||||
|
.font(.subheadline)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
.multilineTextAlignment(.center)
|
||||||
|
}
|
||||||
.padding(.top)
|
.padding(.top)
|
||||||
|
|
||||||
|
// Punktestand
|
||||||
|
HStack {
|
||||||
|
VStack(alignment: .leading, spacing: 4) {
|
||||||
|
Text("Deine Punkte")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
Text("\(globalScore)")
|
||||||
|
.font(.title2)
|
||||||
|
.fontWeight(.bold)
|
||||||
|
.foregroundColor(.blue)
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer()
|
||||||
|
|
||||||
|
Image(systemName: "star.fill")
|
||||||
|
.foregroundColor(.orange)
|
||||||
|
.font(.title2)
|
||||||
|
}
|
||||||
|
.padding()
|
||||||
|
.background(
|
||||||
|
RoundedRectangle(cornerRadius: 12)
|
||||||
|
.fill(Color(.systemGray6))
|
||||||
|
.overlay(
|
||||||
|
RoundedRectangle(cornerRadius: 12)
|
||||||
|
.stroke(Color(.systemGray4), lineWidth: 1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.padding(.horizontal)
|
||||||
|
|
||||||
|
// Kategorien Header
|
||||||
|
Text("Verfügbare Quiz")
|
||||||
|
.font(.title2)
|
||||||
|
.fontWeight(.semibold)
|
||||||
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
|
.padding(.horizontal)
|
||||||
|
|
||||||
// Liste der Kategorien
|
// Liste der Kategorien
|
||||||
VStack(spacing: 12) {
|
VStack(spacing: 12) {
|
||||||
ForEach(viewModel.availableCategories, id: \.self) { category in
|
ForEach(viewModel.availableCategories, id: \.self) { category in
|
||||||
@@ -60,7 +105,7 @@ struct CategorySelectionView: View {
|
|||||||
}
|
}
|
||||||
.background(Color.white)
|
.background(Color.white)
|
||||||
.navigationBarHidden(true)
|
.navigationBarHidden(true)
|
||||||
|
.navigationTitle("Quiz-Auswahl")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,13 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import AudioToolbox
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
|
||||||
@ObservedObject var viewModel = ViewModel()
|
@ObservedObject var viewModel = ViewModel()
|
||||||
|
@AppStorage("globalScore") var score: Int = 0
|
||||||
|
@Environment(\.dismiss) var dismiss
|
||||||
|
|
||||||
@State private var selectedAnswerIndex: Int? = nil
|
@State private var selectedAnswerIndex: Int? = nil
|
||||||
@State private var isAnswered = false
|
@State private var isAnswered = false
|
||||||
@@ -33,13 +36,16 @@ struct ContentView: View {
|
|||||||
.cornerRadius(10)
|
.cornerRadius(10)
|
||||||
|
|
||||||
Button("Zurück zur Kategorieauswahl") {
|
Button("Zurück zur Kategorieauswahl") {
|
||||||
viewModel.selectedCategory = nil
|
dismiss()
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
.background(Color.gray)
|
.background(Color.gray)
|
||||||
.foregroundColor(.white)
|
.foregroundColor(.white)
|
||||||
.cornerRadius(10)
|
.cornerRadius(10)
|
||||||
}
|
}
|
||||||
|
.onAppear {
|
||||||
|
AudioServicesPlaySystemSound(1322)
|
||||||
|
}
|
||||||
} else if let frage = viewModel.currentQuestion {
|
} else if let frage = viewModel.currentQuestion {
|
||||||
Text("Punkte: \(viewModel.score)")
|
Text("Punkte: \(viewModel.score)")
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
@@ -76,6 +82,9 @@ 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)
|
||||||
@@ -158,9 +167,6 @@ struct ContentView: View {
|
|||||||
let correct = viewModel.questions[index].correctAnswer
|
let correct = viewModel.questions[index].correctAnswer
|
||||||
return selected == correct ? .green : .red
|
return selected == correct ? .green : .red
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
|
|||||||
Reference in New Issue
Block a user