fixed wrong loading bug, fixed editing bug
This commit is contained in:
@ -26,50 +26,39 @@ class HandlerClientCoachCode(
|
||||
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)
|
||||
} ?: ""
|
||||
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
|
||||
// ----------------------------------------------------------
|
||||
setTextSizePercentOfScreenHeight(titleTextView, 0.03f)
|
||||
setTextSizePercentOfScreenHeight(questionTextView,0.03f)
|
||||
setTextSizePercentOfScreenHeight(clientCodeField, 0.025f)
|
||||
setTextSizePercentOfScreenHeight(coachCodeField, 0.025f)
|
||||
|
||||
// Load last used client code if available
|
||||
val lastClientCode = GlobalValues.LAST_CLIENT_CODE
|
||||
if (!lastClientCode.isNullOrBlank()) {
|
||||
clientCodeField.setText(lastClientCode)
|
||||
// *** WICHTIG: Nur den ERFOLGREICH GELADENEN Code verwenden ***
|
||||
val loadedClientCode = GlobalValues.LOADED_CLIENT_CODE
|
||||
if (!loadedClientCode.isNullOrBlank()) {
|
||||
clientCodeField.setText(loadedClientCode)
|
||||
clientCodeField.isEnabled = false
|
||||
} else {
|
||||
clientCodeField.setText(answers["client_code"] as? String ?: "")
|
||||
// Nichts ist geladen → Feld bleibt leer und editierbar
|
||||
clientCodeField.setText("")
|
||||
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
|
||||
@ -77,8 +66,10 @@ class HandlerClientCoachCode(
|
||||
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp)
|
||||
}
|
||||
|
||||
// Handle Next button click
|
||||
private fun onNextClicked(clientCodeField: EditText, coachCodeField: EditText) {
|
||||
// 1) Ohne vorher geladenen Client NICHT weiter
|
||||
val loadedClientCode = GlobalValues.LOADED_CLIENT_CODE
|
||||
|
||||
if (!validate()) {
|
||||
val message = LanguageManager.getText(languageID, "fill_both_fields")
|
||||
showToast(message)
|
||||
@ -92,21 +83,18 @@ class HandlerClientCoachCode(
|
||||
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) {
|
||||
// Wenn Feld editierbar war und Code bereits existiert → Hinweis
|
||||
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()
|
||||
@ -118,7 +106,6 @@ class HandlerClientCoachCode(
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Previous button click
|
||||
private fun onPreviousClicked(clientCodeField: EditText, coachCodeField: EditText) {
|
||||
val clientCode = clientCodeField.text.toString()
|
||||
val coachCode = coachCodeField.text.toString()
|
||||
@ -126,21 +113,19 @@ class HandlerClientCoachCode(
|
||||
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) {
|
||||
// Optional: LAST_CLIENT_CODE kann gesetzt bleiben; maßgeblich ist LOADED_CLIENT_CODE
|
||||
GlobalValues.LAST_CLIENT_CODE = clientCode
|
||||
answers["client_code"] = clientCode
|
||||
answers["coach_code"] = coachCode
|
||||
}
|
||||
|
||||
// Required override but not used here
|
||||
override fun saveAnswer() {
|
||||
// Not used
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user