fixed wrong loading bug, fixed editing bug
This commit is contained in:
@ -14,7 +14,9 @@ class EditButtonHandler(
|
||||
private val questionnaireFiles: Map<Button, String>,
|
||||
private val buttonPoints: MutableMap<String, Int>,
|
||||
private val updateButtonTexts: () -> Unit,
|
||||
private val setButtonsEnabled: (List<Button>) -> Unit
|
||||
private val setButtonsEnabled: (List<Button>) -> Unit,
|
||||
// vor "Bearbeiten" ggf. Laden anstoßen
|
||||
private val triggerLoad: () -> Unit
|
||||
) {
|
||||
|
||||
fun setup() {
|
||||
@ -23,18 +25,38 @@ class EditButtonHandler(
|
||||
}
|
||||
|
||||
private fun handleEditButtonClick() {
|
||||
val clientCode = editText.text.toString().trim()
|
||||
if (clientCode.isBlank()) {
|
||||
val typed = editText.text.toString().trim()
|
||||
val desiredCode = when {
|
||||
typed.isNotBlank() -> typed
|
||||
!GlobalValues.LOADED_CLIENT_CODE.isNullOrBlank() -> GlobalValues.LOADED_CLIENT_CODE!!
|
||||
else -> ""
|
||||
}
|
||||
|
||||
if (desiredCode.isBlank()) {
|
||||
val message = LanguageManager.getText(languageIDProvider(), "please_client_code")
|
||||
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
GlobalValues.LAST_CLIENT_CODE = clientCode
|
||||
// Nutzerwunsch merken (info)
|
||||
GlobalValues.LAST_CLIENT_CODE = desiredCode
|
||||
|
||||
// Nur laden, wenn noch nicht/anders geladen
|
||||
val needLoad = GlobalValues.LOADED_CLIENT_CODE?.equals(desiredCode) != true
|
||||
if (needLoad) triggerLoad()
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val loadedOk = waitUntilClientLoaded(desiredCode, timeoutMs = 2500, stepMs = 50)
|
||||
if (!loadedOk) {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(activity, "Bitte den Klienten über \"Laden\" öffnen.", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
return@launch
|
||||
}
|
||||
|
||||
// Ab hier: geladen → Bearbeiten-Logik
|
||||
val completedEntries: List<CompletedQuestionnaire> =
|
||||
MyApp.database.completedQuestionnaireDao().getAllForClient(clientCode)
|
||||
MyApp.database.completedQuestionnaireDao().getAllForClient(desiredCode)
|
||||
|
||||
val completedFiles = completedEntries
|
||||
.filter { it.isDone }
|
||||
@ -58,4 +80,17 @@ class EditButtonHandler(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun waitUntilClientLoaded(expectedCode: String, timeoutMs: Long, stepMs: Long): Boolean {
|
||||
// sofort ok, wenn bereits korrekt geladen
|
||||
if (GlobalValues.LOADED_CLIENT_CODE?.equals(expectedCode) == true) return true
|
||||
|
||||
var waited = 0L
|
||||
while (waited < timeoutMs) {
|
||||
delay(stepMs)
|
||||
waited += stepMs
|
||||
if (GlobalValues.LOADED_CLIENT_CODE?.equals(expectedCode) == true) return true
|
||||
}
|
||||
return GlobalValues.LOADED_CLIENT_CODE?.equals(expectedCode) == true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user