changed qfinish button design
This commit is contained in:
@ -7,6 +7,7 @@ import kotlinx.coroutines.*
|
||||
import android.util.TypedValue
|
||||
import android.widget.TextView
|
||||
import androidx.core.widget.TextViewCompat
|
||||
import com.google.android.material.button.MaterialButton
|
||||
|
||||
class HandlerLastPage(
|
||||
private val answers: Map<String, Any>,
|
||||
@ -26,6 +27,8 @@ class HandlerLastPage(
|
||||
|
||||
val titleTv = layout.findViewById<TextView>(R.id.textView)
|
||||
val questionTv = layout.findViewById<TextView>(R.id.question)
|
||||
val prevBtn = layout.findViewById<MaterialButton>(R.id.Qprev)
|
||||
val finishBtn = layout.findViewById<MaterialButton>(R.id.Qfinish)
|
||||
|
||||
// Texte setzen
|
||||
titleTv.text = LanguageManager.getText(languageID, currentQuestion.textKey)
|
||||
@ -34,43 +37,40 @@ class HandlerLastPage(
|
||||
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)
|
||||
// Finish-Button: Text + responsive Schrift
|
||||
finishBtn.text = LanguageManager.getText(languageID, "save")
|
||||
finishBtn.isAllCaps = false
|
||||
applyResponsiveTextSizing(finishBtn)
|
||||
|
||||
// Überschriften responsiv skalieren (wie zuvor)
|
||||
setTextSizePercentOfScreenHeight(titleTv, 0.03f)
|
||||
setTextSizePercentOfScreenHeight(questionTv, 0.03f)
|
||||
// =====================================================
|
||||
|
||||
// Buttons
|
||||
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
|
||||
goToPreviousQuestion()
|
||||
}
|
||||
prevBtn.setOnClickListener { goToPreviousQuestion() }
|
||||
|
||||
layout.findViewById<Button>(R.id.Qfinish).setOnClickListener {
|
||||
showLoading(true) // Show loading indicator
|
||||
finishBtn.setOnClickListener {
|
||||
showLoading(true)
|
||||
|
||||
// Save answers on a background thread
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val startTime = System.currentTimeMillis()
|
||||
|
||||
// Save answers to database (suspend function)
|
||||
// Antworten speichern
|
||||
saveAnswersToDatabase(answers)
|
||||
|
||||
// Calculate total points and update global value
|
||||
// Punkte summieren
|
||||
GlobalValues.INTEGRATION_INDEX = sumPoints()
|
||||
|
||||
// Save last client code globally if available
|
||||
// Client-Code merken
|
||||
val clientCode = answers["client_code"] as? String
|
||||
if (clientCode != null) GlobalValues.LAST_CLIENT_CODE = clientCode
|
||||
|
||||
// Ensure loading animation runs at least 2 seconds
|
||||
// min. Ladezeit einhalten
|
||||
val elapsedTime = System.currentTimeMillis() - startTime
|
||||
if (elapsedTime < minLoadingTimeMs) {
|
||||
delay(minLoadingTimeMs - elapsedTime)
|
||||
}
|
||||
if (elapsedTime < minLoadingTimeMs) delay(minLoadingTimeMs - elapsedTime)
|
||||
|
||||
// Switch back to main thread to update UI
|
||||
withContext(Dispatchers.Main) {
|
||||
showLoading(false) // Hide loading indicator
|
||||
showLoading(false)
|
||||
val activity = layout.context as? MainActivity
|
||||
activity?.finishQuestionnaire() ?: goToNextQuestion()
|
||||
}
|
||||
@ -78,25 +78,50 @@ class HandlerLastPage(
|
||||
}
|
||||
}
|
||||
|
||||
override fun validate(): Boolean = true // No validation needed on last page
|
||||
override fun saveAnswer() {} // No answers to save here
|
||||
override fun validate(): Boolean = true
|
||||
override fun saveAnswer() {}
|
||||
|
||||
// ---------- Responsive Textgröße für den Finish-Button ----------
|
||||
private fun applyResponsiveTextSizing(btn: MaterialButton) {
|
||||
// Max-/Min-Sp anhand der Bildschirmhöhe (in sp) berechnen
|
||||
val dm = btn.resources.displayMetrics
|
||||
val maxSp = (dm.heightPixels * 0.028f) / dm.scaledDensity // ~2.8% der Höhe
|
||||
val minSp = (dm.heightPixels * 0.018f) / dm.scaledDensity // ~1.8% der Höhe
|
||||
|
||||
// AutoSize aktivieren (schrumpft/expandiert den Text innerhalb des Buttons)
|
||||
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
|
||||
btn,
|
||||
minSp.toInt(),
|
||||
maxSp.toInt(),
|
||||
1,
|
||||
TypedValue.COMPLEX_UNIT_SP
|
||||
)
|
||||
btn.setSingleLine(true)
|
||||
btn.maxLines = 1
|
||||
btn.isAllCaps = false
|
||||
|
||||
// Padding nach Layout proportional zur Button-Höhe setzen (wirkt auf Lesbarkeit)
|
||||
btn.post {
|
||||
val padH = (btn.height * 0.18f).toInt()
|
||||
val padV = (btn.height * 0.12f).toInt()
|
||||
btn.setPadding(padH, padV, padH, padV)
|
||||
}
|
||||
}
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user