Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [ANDROAPP-6179] Do not save value if it has not changed in form on focus event #3695

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions form/src/main/java/org/dhis2/form/ui/FormViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class FormViewModel(
private val _items = MutableLiveData<List<FieldUiModel>>()
val items: LiveData<List<FieldUiModel>> = _items

var previousActionItem: RowAction? = null

private val _savedValue = MutableLiveData<RowAction>()
val savedValue: LiveData<RowAction> = _savedValue

Expand Down Expand Up @@ -222,6 +224,7 @@ class FormViewModel(
ActionType.ON_FOCUS, ActionType.ON_NEXT -> {
val storeResult = saveLastFocusedItem(action)
repository.setFocusedItem(action)
previousActionItem = action
storeResult
}

Expand Down Expand Up @@ -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,
Expand Down