From 2a0643d09e34363277257474fa20d5ef185703f5 Mon Sep 17 00:00:00 2001 From: FerdyRod Date: Wed, 26 Jun 2024 15:37:04 +0200 Subject: [PATCH] decomposition of processUserAction method to smaller functions reducing it's code complexity --- .../java/org/dhis2/form/ui/FormViewModel.kt | 202 +++++++++--------- 1 file changed, 105 insertions(+), 97 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 067ef8b42b..26cb94fe15 100644 --- a/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt +++ b/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt @@ -195,117 +195,125 @@ class FormViewModel( private fun processUserAction(action: RowAction): StoreResult { return when (action.type) { - ActionType.ON_SAVE -> { - if (action.valueType == ValueType.COORDINATE) { - repository.setFieldRequestingCoordinates(action.id, false) - } + ActionType.ON_SAVE -> handleOnSaveAction(action) + ActionType.ON_FOCUS, ActionType.ON_NEXT -> handleFocusOrNextAction(action) + ActionType.ON_TEXT_CHANGE -> handleOnTextChangeAction(action) + ActionType.ON_SECTION_CHANGE -> handleOnSectionChangeAction(action) + ActionType.ON_FINISH -> handleOnFinishAction(action) + ActionType.ON_REQUEST_COORDINATES -> handleOnRequestCoordinatesAction(action) + ActionType.ON_CANCEL_REQUEST_COORDINATES -> handleOnCancelRequestCoordinatesAction(action) + ActionType.ON_ADD_IMAGE_FINISHED -> handleOnAddImageFinishedAction(action) + ActionType.ON_STORE_FILE -> handleOnStoreFileAction(action) + } + } - repository.updateErrorList(action) - if (action.error != null) { - StoreResult( - action.id, - ValueStoreResult.VALUE_HAS_NOT_CHANGED, - ) - } else { - val saveResult = repository.save(action.id, action.value, action.extraData) - if (saveResult?.valueStoreResult != ValueStoreResult.ERROR_UPDATING_VALUE) { - if (action.isEventDetailsRow) { - repository.fetchFormItems(openErrorLocation) - } else { - repository.updateValueOnList(action.id, action.value, action.valueType) - } - } else { - repository.updateErrorList( - action.copy( - error = Throwable(saveResult.valueStoreResultMessage), - ), - ) - } - saveResult ?: StoreResult( - action.id, - ValueStoreResult.VALUE_CHANGED, - ) - } - } + private fun handleOnSaveAction(action: RowAction): StoreResult { + if (action.valueType == ValueType.COORDINATE) { + repository.setFieldRequestingCoordinates(action.id, false) + } - ActionType.ON_FOCUS, ActionType.ON_NEXT -> { - val storeResult = saveLastFocusedItem(action) - repository.setFocusedItem(action) - previousActionItem = action - storeResult - } + repository.updateErrorList(action) + if (action.error != null) { + return StoreResult( + action.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + } - ActionType.ON_TEXT_CHANGE -> { + val saveResult = repository.save(action.id, action.value, action.extraData) + if (saveResult?.valueStoreResult != ValueStoreResult.ERROR_UPDATING_VALUE) { + if (action.isEventDetailsRow) { + repository.fetchFormItems(openErrorLocation) + } else { repository.updateValueOnList(action.id, action.value, action.valueType) - StoreResult( - action.id, - ValueStoreResult.TEXT_CHANGING, - ) } + } else { + repository.updateErrorList( + action.copy( + error = Throwable(saveResult.valueStoreResultMessage), + ), + ) + } + return saveResult ?: StoreResult( + action.id, + ValueStoreResult.VALUE_CHANGED, + ) + } - ActionType.ON_SECTION_CHANGE -> { - repository.updateSectionOpened(action) - StoreResult( - action.id, - ValueStoreResult.VALUE_HAS_NOT_CHANGED, - ) - } + private fun handleFocusOrNextAction(action: RowAction): StoreResult { + val storeResult = saveLastFocusedItem(action) + repository.setFocusedItem(action) + previousActionItem = action + return storeResult + } - ActionType.ON_FINISH -> { - repository.setFocusedItem(action) - StoreResult( - "", - ValueStoreResult.FINISH, - ) - } + private fun handleOnTextChangeAction(action: RowAction): StoreResult { + repository.updateValueOnList(action.id, action.value, action.valueType) + return StoreResult( + action.id, + ValueStoreResult.TEXT_CHANGING, + ) + } - ActionType.ON_REQUEST_COORDINATES -> { - repository.setFieldRequestingCoordinates(action.id, true) - StoreResult( - action.id, - ValueStoreResult.VALUE_HAS_NOT_CHANGED, - ) - } + private fun handleOnSectionChangeAction(action: RowAction): StoreResult { + repository.updateSectionOpened(action) + return StoreResult( + action.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + } - ActionType.ON_CANCEL_REQUEST_COORDINATES -> { - repository.setFieldRequestingCoordinates(action.id, false) - StoreResult( - action.id, - ValueStoreResult.VALUE_HAS_NOT_CHANGED, - ) - } + private fun handleOnFinishAction(action: RowAction): StoreResult { + repository.setFocusedItem(action) + return StoreResult( + "", + ValueStoreResult.FINISH, + ) + } - ActionType.ON_ADD_IMAGE_FINISHED -> { - repository.setFieldAddingImage(action.id, false) - StoreResult( - action.id, - ValueStoreResult.VALUE_HAS_NOT_CHANGED, - ) - } + private fun handleOnRequestCoordinatesAction(action: RowAction): StoreResult { + repository.setFieldRequestingCoordinates(action.id, true) + return StoreResult( + action.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + } - ActionType.ON_STORE_FILE -> { - val saveResult = repository.storeFile(action.id, action.value) - when (saveResult?.valueStoreResult) { - ValueStoreResult.FILE_SAVED -> { - processUserAction( - rowActionFromIntent( - FormIntent.OnSave( - uid = action.id, - value = saveResult.uid, - valueType = action.valueType, - ), - ), - ) - } + private fun handleOnCancelRequestCoordinatesAction(action: RowAction): StoreResult { + repository.setFieldRequestingCoordinates(action.id, false) + return StoreResult( + action.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + } - null -> StoreResult( - action.id, - ValueStoreResult.VALUE_HAS_NOT_CHANGED, - ) + private fun handleOnAddImageFinishedAction(action: RowAction): StoreResult { + repository.setFieldAddingImage(action.id, false) + return StoreResult( + action.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + } - else -> saveResult - } + private fun handleOnStoreFileAction(action: RowAction): StoreResult { + val saveResult = repository.storeFile(action.id, action.value) + return when (saveResult?.valueStoreResult) { + ValueStoreResult.FILE_SAVED -> { + processUserAction( + rowActionFromIntent( + FormIntent.OnSave( + uid = action.id, + value = saveResult.uid, + valueType = action.valueType, + ), + ), + ) } + null -> StoreResult( + action.id, + ValueStoreResult.VALUE_HAS_NOT_CHANGED, + ) + else -> saveResult } }