110 lines
4.4 KiB
Kotlin
110 lines
4.4 KiB
Kotlin
package com.dano.test1
|
|
|
|
import android.view.View
|
|
import android.widget.*
|
|
import android.text.Html
|
|
import kotlinx.coroutines.*
|
|
import android.util.TypedValue
|
|
import android.widget.TextView
|
|
import androidx.core.widget.TextViewCompat
|
|
|
|
class HandlerLastPage(
|
|
private val answers: Map<String, Any>,
|
|
private val languageID: String,
|
|
private val goToNextQuestion: () -> Unit,
|
|
private val goToPreviousQuestion: () -> Unit,
|
|
private val saveAnswersToDatabase: suspend (Map<String, Any>) -> Unit
|
|
) : QuestionHandler {
|
|
|
|
private lateinit var currentQuestion: QuestionItem.LastPage
|
|
private lateinit var layout: View
|
|
private val minLoadingTimeMs = 2000L // Minimum loading time in milliseconds (2 seconds)
|
|
|
|
override fun bind(layout: View, question: QuestionItem) {
|
|
this.layout = layout
|
|
currentQuestion = question as QuestionItem.LastPage
|
|
|
|
val titleTv = layout.findViewById<TextView>(R.id.textView)
|
|
val questionTv = layout.findViewById<TextView>(R.id.question)
|
|
|
|
// Texte setzen
|
|
titleTv.text = LanguageManager.getText(languageID, currentQuestion.textKey)
|
|
questionTv.text = Html.fromHtml(
|
|
LanguageManager.getText(languageID, currentQuestion.question),
|
|
Html.FROM_HTML_MODE_LEGACY
|
|
)
|
|
|
|
// ==== Schriftgrößen prozentual zur Bildschirmhöhe ====
|
|
// Passe die Faktoren bei Bedarf an (z. B. 0.032f für etwas größer)
|
|
setTextSizePercentOfScreenHeight(titleTv, 0.03f)
|
|
setTextSizePercentOfScreenHeight(questionTv, 0.03f)
|
|
// =====================================================
|
|
|
|
// Buttons
|
|
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
|
|
goToPreviousQuestion()
|
|
}
|
|
|
|
layout.findViewById<Button>(R.id.Qfinish).setOnClickListener {
|
|
showLoading(true) // Show loading indicator
|
|
|
|
// Save answers on a background thread
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
val startTime = System.currentTimeMillis()
|
|
|
|
// Save answers to database (suspend function)
|
|
saveAnswersToDatabase(answers)
|
|
|
|
// Calculate total points and update global value
|
|
GlobalValues.INTEGRATION_INDEX = sumPoints()
|
|
|
|
// Save last client code globally if available
|
|
val clientCode = answers["client_code"] as? String
|
|
if (clientCode != null) GlobalValues.LAST_CLIENT_CODE = clientCode
|
|
|
|
// Ensure loading animation runs at least 2 seconds
|
|
val elapsedTime = System.currentTimeMillis() - startTime
|
|
if (elapsedTime < minLoadingTimeMs) {
|
|
delay(minLoadingTimeMs - elapsedTime)
|
|
}
|
|
|
|
// Switch back to main thread to update UI
|
|
withContext(Dispatchers.Main) {
|
|
showLoading(false) // Hide loading indicator
|
|
val activity = layout.context as? MainActivity
|
|
activity?.finishQuestionnaire() ?: goToNextQuestion()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun validate(): Boolean = true // No validation needed on last page
|
|
override fun saveAnswer() {} // No answers to save here
|
|
|
|
// Helper: Textgröße prozentual zur Bildschirmhöhe setzen (in sp)
|
|
private fun setTextSizePercentOfScreenHeight(view: TextView, percentOfHeight: Float) {
|
|
val dm = (view.context ?: layout.context).resources.displayMetrics
|
|
val sp = (dm.heightPixels * percentOfHeight) / dm.scaledDensity
|
|
// AutoSize ausschalten, damit unsere Größe nicht überschrieben wird
|
|
TextViewCompat.setAutoSizeTextTypeWithDefaults(view, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE)
|
|
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp)
|
|
}
|
|
|
|
// Calculate the sum of all keys ending with "_points"
|
|
private fun sumPoints(): Int =
|
|
answers.filterKeys { it.endsWith("_points") }
|
|
.values.mapNotNull { it as? Int }
|
|
.sum()
|
|
|
|
// Show or hide a ProgressBar (loading spinner)
|
|
private fun showLoading(show: Boolean) {
|
|
val progressBar = layout.findViewById<ProgressBar>(R.id.progressBar)
|
|
val finishButton = layout.findViewById<Button>(R.id.Qfinish)
|
|
val prevButton = layout.findViewById<Button>(R.id.Qprev)
|
|
|
|
progressBar?.visibility = if (show) View.VISIBLE else View.GONE
|
|
finishButton?.isEnabled = !show
|
|
prevButton?.isEnabled = !show
|
|
}
|
|
}
|