148 lines
5.9 KiB
Kotlin
148 lines
5.9 KiB
Kotlin
package com.dano.test1
|
|
|
|
import android.view.View
|
|
import android.widget.*
|
|
import android.util.TypedValue
|
|
import androidx.core.widget.TextViewCompat
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.withContext
|
|
|
|
class HandlerClientCoachCode(
|
|
private val answers: MutableMap<String, Any>,
|
|
private val languageID: String,
|
|
private val goToNextQuestion: () -> Unit,
|
|
private val goToPreviousQuestion: () -> Unit,
|
|
private val showToast: (String) -> Unit,
|
|
) : QuestionHandler {
|
|
|
|
private lateinit var question: QuestionItem.ClientCoachCodeQuestion
|
|
private lateinit var layout: View
|
|
|
|
override fun bind(layout: View, question: QuestionItem) {
|
|
if (question !is QuestionItem.ClientCoachCodeQuestion) return
|
|
|
|
this.layout = layout
|
|
this.question = question
|
|
|
|
// Bind UI components
|
|
val clientCodeField = layout.findViewById<EditText>(R.id.client_code)
|
|
val coachCodeField = layout.findViewById<EditText>(R.id.coach_code)
|
|
val questionTextView = layout.findViewById<TextView>(R.id.question)
|
|
val titleTextView = layout.findViewById<TextView>(R.id.textView)
|
|
|
|
// Fill question text using language manager
|
|
questionTextView.text = question.question?.let {
|
|
LanguageManager.getText(languageID, it)
|
|
} ?: ""
|
|
|
|
// --- Schriftgrößen prozentual zur Bildschirmhöhe setzen ---
|
|
// Passe die Prozente bei Bedarf an:
|
|
setTextSizePercentOfScreenHeight(titleTextView, 0.03f) // 5.5% der Bildschirmhöhe
|
|
setTextSizePercentOfScreenHeight(questionTextView,0.03f) // 5.0% der Bildschirmhöhe
|
|
setTextSizePercentOfScreenHeight(clientCodeField, 0.025f) // 3.5% der Bildschirmhöhe
|
|
setTextSizePercentOfScreenHeight(coachCodeField, 0.025f) // anpassen nach Geschmack
|
|
// ----------------------------------------------------------
|
|
|
|
// Load last used client code if available
|
|
val lastClientCode = GlobalValues.LAST_CLIENT_CODE
|
|
if (!lastClientCode.isNullOrBlank()) {
|
|
clientCodeField.setText(lastClientCode)
|
|
clientCodeField.isEnabled = false
|
|
} else {
|
|
clientCodeField.setText(answers["client_code"] as? String ?: "")
|
|
clientCodeField.isEnabled = true
|
|
}
|
|
|
|
// Load saved coach code
|
|
coachCodeField.setText(answers["coach_code"] as? String ?: "")
|
|
|
|
// Set click listener for Next button
|
|
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
|
|
onNextClicked(clientCodeField, coachCodeField)
|
|
}
|
|
|
|
// Set click listener for Previous button
|
|
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
|
|
onPreviousClicked(clientCodeField, coachCodeField)
|
|
}
|
|
}
|
|
|
|
// Deaktiviert AutoSize und setzt textSize in sp prozentual zur Bildschirmhöhe
|
|
private fun setTextSizePercentOfScreenHeight(view: TextView, percentOfHeight: Float) {
|
|
val dm = layout.resources.displayMetrics
|
|
val sp = (dm.heightPixels * percentOfHeight) / dm.scaledDensity
|
|
TextViewCompat.setAutoSizeTextTypeWithDefaults(view, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE)
|
|
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp)
|
|
}
|
|
|
|
// Handle Next button click
|
|
private fun onNextClicked(clientCodeField: EditText, coachCodeField: EditText) {
|
|
if (!validate()) {
|
|
val message = LanguageManager.getText(languageID, "fill_both_fields")
|
|
showToast(message)
|
|
return
|
|
}
|
|
|
|
val clientCode = clientCodeField.text.toString()
|
|
val coachCode = coachCodeField.text.toString()
|
|
|
|
// Prüfen, ob die DB-Datei vor dem Zugriff existiert
|
|
val dbPath = layout.context.getDatabasePath("questionnaire_database")
|
|
val dbExistedBefore = dbPath.exists()
|
|
|
|
// Check if client code already exists asynchronously
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
val existingClient = MyApp.database.clientDao().getClientByCode(clientCode)
|
|
|
|
withContext(Dispatchers.Main) {
|
|
if (existingClient != null && clientCodeField.isEnabled) {
|
|
// Client code already exists and field was editable
|
|
val message = LanguageManager.getText(languageID, "client_code_exists")
|
|
showToast(message)
|
|
} else {
|
|
// Either no existing client or re-using previous code
|
|
saveAnswers(clientCode, coachCode)
|
|
goToNextQuestion()
|
|
|
|
// Lösche DB-Dateien nur, wenn sie vorher nicht existierten
|
|
if (!dbExistedBefore) {
|
|
MyApp.database.close()
|
|
dbPath.delete()
|
|
val journalFile = layout.context.getDatabasePath("questionnaire_database-journal")
|
|
journalFile.delete()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Previous button click
|
|
private fun onPreviousClicked(clientCodeField: EditText, coachCodeField: EditText) {
|
|
val clientCode = clientCodeField.text.toString()
|
|
val coachCode = coachCodeField.text.toString()
|
|
saveAnswers(clientCode, coachCode)
|
|
goToPreviousQuestion()
|
|
}
|
|
|
|
// Validate that both fields are filled
|
|
override fun validate(): Boolean {
|
|
val clientCode = layout.findViewById<EditText>(R.id.client_code).text
|
|
val coachCode = layout.findViewById<EditText>(R.id.coach_code).text
|
|
return clientCode.isNotBlank() && coachCode.isNotBlank()
|
|
}
|
|
|
|
// Save answers to shared state and global value
|
|
private fun saveAnswers(clientCode: String, coachCode: String) {
|
|
GlobalValues.LAST_CLIENT_CODE = clientCode
|
|
answers["client_code"] = clientCode
|
|
answers["coach_code"] = coachCode
|
|
}
|
|
|
|
// Required override but not used here
|
|
override fun saveAnswer() {
|
|
// Not used
|
|
}
|
|
}
|