Initialer Upload neues Unity-Projekt

This commit is contained in:
Daniel Ocks
2025-07-25 11:17:02 +02:00
commit f7a629ccde
91 changed files with 8743 additions and 0 deletions

View File

@ -0,0 +1,136 @@
package com.dano.test1
import android.content.Context
import android.view.View
import android.text.Html
import android.widget.*
class HandlerRadioQuestion(
private val context: Context,
private val answers: MutableMap<String, Any>,
private val points: MutableList<Int>,
private val languageID: String,
private val goToNextQuestion: () -> Unit,
private val goToPreviousQuestion: () -> Unit,
private val goToQuestionById: (String) -> Unit,
private val showToast: (String) -> Unit
) : QuestionHandler {
private lateinit var layout: View
private lateinit var question: QuestionItem.RadioQuestion
// Bind the question data to the view
override fun bind(layout: View, question: QuestionItem) {
this.layout = layout
this.question = question as QuestionItem.RadioQuestion
val radioGroup = layout.findViewById<RadioGroup>(R.id.RadioGroup)
val questionTextView = layout.findViewById<TextView>(R.id.textView)
val questionTitle = layout.findViewById<TextView>(R.id.question)
// Set question text and optional text key
questionTextView.text = question.textKey?.let { LanguageManager.getText(languageID, it) } ?: ""
questionTitle.text = question.question?.let {
Html.fromHtml(LanguageManager.getText(languageID, it), Html.FROM_HTML_MODE_LEGACY)
} ?: ""
// Clear previous radio buttons if any
radioGroup.removeAllViews()
// Dynamically create radio buttons based on options
question.options.forEach { option ->
val radioButton = RadioButton(context).apply {
text = LanguageManager.getText(languageID, option.key)
tag = option.key
layoutParams = RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT
).apply {
val scale = context.resources.displayMetrics.density
val margin = (16 * scale + 0.5f).toInt()
setMargins(0, 0, 0, margin)
}
}
radioGroup.addView(radioButton)
}
// Restore previous answer if one was saved
restorePreviousAnswer(radioGroup)
// Handle Next button click
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
if (validate()) {
saveAnswer()
val selectedId = radioGroup.checkedRadioButtonId
val selectedRadioButton = layout.findViewById<RadioButton>(selectedId)
val selectedKey = selectedRadioButton.tag.toString()
// Check if there is a specific next question ID
val nextId = question.options.find { it.key == selectedKey }?.nextQuestionId
if (!nextId.isNullOrEmpty()) {
goToQuestionById(nextId)
} else {
goToNextQuestion()
}
} else {
showToast(LanguageManager.getText(languageID, "select_one_answer"))
}
}
// Handle Previous button click
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
goToPreviousQuestion()
}
}
// Restore previously saved answer (if exists)
private fun restorePreviousAnswer(radioGroup: RadioGroup) {
question.question?.let { questionKey ->
val savedAnswer = answers[questionKey] as? String
savedAnswer?.let { saved ->
for (i in 0 until radioGroup.childCount) {
val radioButton = radioGroup.getChildAt(i) as RadioButton
if (radioButton.tag == saved) {
radioButton.isChecked = true
break
}
}
}
}
}
// Validate if any radio button has been selected
override fun validate(): Boolean {
return layout.findViewById<RadioGroup>(R.id.RadioGroup).checkedRadioButtonId != -1
}
// Save selected answer and update points list
override fun saveAnswer() {
val radioGroup = layout.findViewById<RadioGroup>(R.id.RadioGroup)
val selectedId = radioGroup.checkedRadioButtonId
val selectedRadioButton = layout.findViewById<RadioButton>(selectedId)
val answerKey = selectedRadioButton.tag.toString()
question.question?.let { questionKey ->
// Alte Antwort und Punkt ermitteln
val oldAnswerKey = answers[questionKey] as? String
val oldPoint = oldAnswerKey?.let { question.pointsMap?.get(it) } ?: 0
// Alten Punkt entfernen, falls vorhanden
points.remove(oldPoint)
// Neue Antwort speichern
answers[questionKey] = answerKey
// Neuen Punkt ermitteln und hinzufügen
val newPoint = question.pointsMap?.get(answerKey) ?: 0
points.add(newPoint)
}
}
}