feat: added new question type

This commit is contained in:
2025-08-08 21:29:06 +02:00
parent eeb72ca189
commit 27fda7b3fa
2 changed files with 43 additions and 2 deletions

View File

@@ -13,16 +13,39 @@ struct QuizQuestion: Decodable, Hashable, Identifiable {
let answers: [String] let answers: [String]
let correctAnswer: Int let correctAnswer: Int
let type: String?
let minValue: Double?
let maxValue: Double?
let correctValue: Double?
let unit: String?
// Manuelles Decoding, id wird generiert // Manuelles Decoding, id wird generiert
init(from decoder: Decoder) throws { init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
question = try container.decode(String.self, forKey: .question) question = try container.decode(String.self, forKey: .question)
answers = try container.decode([String].self, forKey: .answers)
correctAnswer = try container.decode(Int.self, forKey: .correctAnswer) // Falls im JSON nicht vorhanden, Standard setzen
answers = (try? container.decode([String].self, forKey: .answers)) ?? []
correctAnswer = (try? container.decode(Int.self, forKey: .correctAnswer)) ?? 0
// Optional
type = try container.decodeIfPresent(String.self, forKey: .type)
minValue = try container.decodeIfPresent(Double.self, forKey: .minValue)
maxValue = try container.decodeIfPresent(Double.self, forKey: .maxValue)
correctValue = try container.decodeIfPresent(Double.self, forKey: .correctValue)
unit = try container.decodeIfPresent(String.self, forKey: .unit)
id = UUID() id = UUID()
} }
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case question, answers, correctAnswer case question, answers, correctAnswer
case type, minValue, maxValue, correctValue, unit
}
var isEstimation: Bool {
if let t = type?.lowercased() { return t == "estimation" }
return !answers.isEmpty ? false : (minValue != nil || maxValue != nil || correctValue != nil)
} }
} }

View File

@@ -182,5 +182,23 @@
], ],
"correctAnswer": 1 "correctAnswer": 1
} }
],
"Schätzen": [
{
"type": "estimation",
"question": "Wie hoch ist der Eiffelturm?",
"minValue": 0,
"maxValue": 1000,
"correctValue": 330,
"unit": "m"
},
{
"type": "estimation",
"question": "In welchem Jahr begann der Bau des Kölner Doms?",
"minValue": 1000,
"maxValue": 2025,
"correctValue": 1248,
"unit": "Jahr"
}
] ]
} }