From 6e775b2f592d710862c5ce63c64b923996f5d9aa Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Mon, 24 Jun 2024 11:10:02 +0200 Subject: [PATCH] fix: [ANDROAPP-6179] Do not save last focused item if value has not changed On focused event --- .../java/org/dhis2/form/ui/FormViewModel.kt | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt b/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt index e3fd22ad59..f270ad7540 100644 --- a/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt +++ b/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt @@ -65,6 +65,8 @@ class FormViewModel( private val _items = MutableLiveData>() val items: LiveData> = _items + var previousActionItem: RowAction? = null + private val _savedValue = MutableLiveData() val savedValue: LiveData = _savedValue @@ -222,6 +224,7 @@ class FormViewModel( ActionType.ON_FOCUS, ActionType.ON_NEXT -> { val storeResult = saveLastFocusedItem(action) repository.setFocusedItem(action) + previousActionItem = action storeResult } @@ -300,24 +303,32 @@ class FormViewModel( } private fun saveLastFocusedItem(rowAction: RowAction) = getLastFocusedTextItem()?.let { - val error = checkFieldError(it.valueType, it.value, it.fieldMask) - if (error != null) { - val action = rowActionFromIntent( - FormIntent.OnSave(it.uid, it.value, it.valueType, it.fieldMask), - ) - repository.updateErrorList(action) + if (previousActionItem == null) previousActionItem = rowAction + if (previousActionItem?.value != it.value && previousActionItem?.id == rowAction.id) { + val error = checkFieldError(it.valueType, it.value, it.fieldMask) + if (error != null) { + val action = rowActionFromIntent( + FormIntent.OnSave(it.uid, it.value, it.valueType, it.fieldMask), + ) + repository.updateErrorList(action) + StoreResult( + rowAction.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + } else { + checkAutoCompleteForLastFocusedItem(it) + val intent = getSaveIntent(it) + val action = rowActionFromIntent(intent) + val result = repository.save(it.uid, it.value, action.extraData) + repository.updateValueOnList(it.uid, it.value, it.valueType) + repository.updateErrorList(action) + result + } + } else { StoreResult( rowAction.id, ValueStoreResult.VALUE_HAS_NOT_CHANGED, ) - } else { - checkAutoCompleteForLastFocusedItem(it) - val intent = getSaveIntent(it) - val action = rowActionFromIntent(intent) - val result = repository.save(it.uid, it.value, action.extraData) - repository.updateValueOnList(it.uid, it.value, it.valueType) - repository.updateErrorList(action) - result } } ?: StoreResult( rowAction.id,