added edit mode for radio questions
This commit is contained in:
@ -29,7 +29,7 @@
|
||||
"questionnaire_2_rhs",
|
||||
"questionnaire_3_integration_index"
|
||||
],
|
||||
"questionnaire": "questionnaire_1_demographic_information.json",
|
||||
"questionnaire": "questionnaire_1_demographic_information",
|
||||
"questionId": "consent_instruction",
|
||||
"operator": "==",
|
||||
"value": "consent_signed"
|
||||
|
||||
@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.view.View
|
||||
import android.text.Html
|
||||
import android.widget.*
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
class HandlerRadioQuestion(
|
||||
private val context: Context,
|
||||
@ -13,7 +14,8 @@ class HandlerRadioQuestion(
|
||||
private val goToNextQuestion: () -> Unit,
|
||||
private val goToPreviousQuestion: () -> Unit,
|
||||
private val goToQuestionById: (String) -> Unit,
|
||||
private val showToast: (String) -> Unit
|
||||
private val showToast: (String) -> Unit,
|
||||
private val questionnaireMeta: String
|
||||
) : QuestionHandler {
|
||||
|
||||
private lateinit var layout: View
|
||||
@ -32,7 +34,6 @@ class HandlerRadioQuestion(
|
||||
Html.fromHtml(LanguageManager.getText(languageID, it), Html.FROM_HTML_MODE_LEGACY)
|
||||
} ?: ""
|
||||
|
||||
|
||||
radioGroup.removeAllViews()
|
||||
|
||||
question.options.forEach { option ->
|
||||
@ -53,6 +54,44 @@ class HandlerRadioQuestion(
|
||||
|
||||
restorePreviousAnswer(radioGroup)
|
||||
|
||||
val answerMapKey = question.question ?: (question.id ?: "")
|
||||
if (answerMapKey.isNotBlank() && !answers.containsKey(answerMapKey)) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val clientCode = GlobalValues.LAST_CLIENT_CODE
|
||||
if (clientCode.isNullOrBlank()) return@launch
|
||||
|
||||
val allAnswersForClient = MyApp.database.answerDao().getAnswersForClient(clientCode)
|
||||
val myQuestionId = questionnaireMeta + "-" + question.question
|
||||
val dbAnswer = allAnswersForClient.find { it.questionId == myQuestionId }?.answerValue
|
||||
|
||||
if (!dbAnswer.isNullOrBlank()) {
|
||||
withContext(Dispatchers.Main) {
|
||||
val oldAnswerKey = answers[answerMapKey] as? String
|
||||
val oldPoint = oldAnswerKey?.let { question.pointsMap?.get(it) } ?: 0
|
||||
if (oldAnswerKey != null) {
|
||||
points.remove(oldPoint)
|
||||
}
|
||||
|
||||
for (i in 0 until radioGroup.childCount) {
|
||||
val radioButton = radioGroup.getChildAt(i) as RadioButton
|
||||
if (radioButton.tag == dbAnswer) {
|
||||
radioButton.isChecked = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
answers[answerMapKey] = dbAnswer
|
||||
val newPoint = question.pointsMap?.get(dbAnswer) ?: 0
|
||||
points.add(newPoint)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
|
||||
if (validate()) {
|
||||
saveAnswer()
|
||||
@ -104,7 +143,7 @@ class HandlerRadioQuestion(
|
||||
val answerKey = selectedRadioButton.tag.toString()
|
||||
|
||||
question.question?.let { questionKey ->
|
||||
|
||||
println("questionKey " + questionKey)
|
||||
val oldAnswerKey = answers[questionKey] as? String
|
||||
val oldPoint = oldAnswerKey?.let { question.pointsMap?.get(it) } ?: 0
|
||||
|
||||
@ -116,6 +155,4 @@ class HandlerRadioQuestion(
|
||||
points.add(newPoint)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ abstract class QuestionnaireBase<T> {
|
||||
|
||||
protected open fun createHandlerForQuestion(question: QuestionItem): QuestionHandler? {
|
||||
return when (question) {
|
||||
is QuestionItem.RadioQuestion -> HandlerRadioQuestion(context, answers, points, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::goToQuestionById, ::showToast)
|
||||
is QuestionItem.RadioQuestion -> HandlerRadioQuestion(context, answers, points, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::goToQuestionById, ::showToast, questionnaireMeta.id)
|
||||
is QuestionItem.ClientCoachCodeQuestion -> HandlerClientCoachCode(answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast)
|
||||
is QuestionItem.DateSpinnerQuestion -> HandlerDateSpinner(context, answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::showToast)
|
||||
is QuestionItem.ValueSpinnerQuestion -> HandlerValueSpinner(context, answers, languageID, ::goToNextQuestion, ::goToPreviousQuestion, ::goToQuestionById, ::showToast)
|
||||
|
||||
Reference in New Issue
Block a user