Files
Questionnaire-App/app/src/main/java/com/dano/test1/HandlerRadioQuestion.kt
2025-08-04 15:21:17 +02:00

122 lines
4.2 KiB
Kotlin

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
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)
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)
} ?: ""
radioGroup.removeAllViews()
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)
}
restorePreviousAnswer(radioGroup)
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()
val nextId = question.options.find { it.key == selectedKey }?.nextQuestionId
if (!nextId.isNullOrEmpty()) {
goToQuestionById(nextId)
} else {
goToNextQuestion()
}
} else {
showToast(LanguageManager.getText(languageID, "select_one_answer"))
}
}
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
goToPreviousQuestion()
}
}
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
}
}
}
}
}
override fun validate(): Boolean {
return layout.findViewById<RadioGroup>(R.id.RadioGroup).checkedRadioButtonId != -1
}
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 ->
val oldAnswerKey = answers[questionKey] as? String
val oldPoint = oldAnswerKey?.let { question.pointsMap?.get(it) } ?: 0
points.remove(oldPoint)
answers[questionKey] = answerKey
val newPoint = question.pointsMap?.get(answerKey) ?: 0
points.add(newPoint)
}
}
}