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

Fixed displaying errors after validating answers #6005

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ public void exit() {
public void validate() {
worker.immediate(
() -> {
FormIndex originalIndex = formController.getFormIndex();
ValidationResult result = null;
try {
result = formController.validateAnswers(true, true);
Expand All @@ -369,14 +370,12 @@ public void validate() {
}

// JavaRosa moves to the index where the contraint failed
if (result instanceof FailedValidationResult) {
if (result instanceof FailedValidationResult && !((FailedValidationResult) result).getIndex().equals(originalIndex)) {
updateIndex(true);
}

return result;
}, result -> {
validationResult.setValue(new Consumable<>(result));
}
validationResult.postValue(new Consumable<>(result));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

I'm not confident this change will always fix the problem if we have a potentially race condition here: setValue in the foreground and postValue in the background are semantically very similar as far as I can understand (as postValue will just do everything setValue does but on a future UI thread loop). It feels to me like we're at risk of recreating this bug in the future.

I'm wondering if instead we should rework how we propagate validation errors to the UI from the ViewModel so the UI is not reacting to multiple events and creating a risk of them overlapping badly. I'm imagining that getCurrentIndex could be of type LiveData<Pair<FormIndex, FailedValidationResult?>> so that the view that the error can simply be set as part of us building the view (this would mean removing the nice optimisation you've added around not re-rendering for the same index sadly). Alternatively, FormEntryViewModel could expose a getValidationErrorForIndex(index: FormIndex) method that returns an error for the index if there is one and could be called for each question while creating the view. We'd still need a getValidationResult() method for showing the success toast, but it's fine to have overlapping (and consumable) events there as the UI it's interacting with is different.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like this more: 3325413
I'm not sure so you can decide what's better.

Copy link
Member

@seadowg seadowg Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like this more: 3325413

Yeah I like that approach a lot more. Now we get both pieces of information (the index and the optional error) at the same time and have more control how we react to that in the UI. I think there might also be some simplifications we can introduce given we're now able to determine that we're moving screens and then showing an error (we might not need to interact with the swipeHandler when showing the error for instance).

return null;
}, ignored -> {}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,35 @@ public void validate_whenThereIsAnErrorValidating_setsError() {
assertThat(viewModel.getError().getValue(), equalTo(new FormError.NonFatal("OH NO")));
}

@Test
public void doNotUpdateCurrentIndex_whenValidationFails_butTheIndexOfAQuestionThatWeShouldBeMovedToIsTheOneWeCurrentlyDisplay() {
Consumable<FailedValidationResult> failedValidationResult =
new Consumable<>(new FailedValidationResult(formController.getFormIndex(), 0, null, org.odk.collect.strings.R.string.invalid_answer_error));
formController.setFailedConstraint(failedValidationResult.getValue());

FormIndex oldIndex = viewModel.getCurrentIndex().getValue();
viewModel.validate();
scheduler.flush();

FormIndex newIndex = viewModel.getCurrentIndex().getValue();
assertThat(oldIndex == newIndex, is(true));
}

@Test
public void updateCurrentIndex_whenValidationFails_butTheIndexOfAQuestionThatWeShouldBeMovedToIsNotTheOneWeCurrentlyDisplay() {
FormIndex index = new FormIndex(null, formController.getFormIndex().getLocalIndex() + 2, 0, new TreeReference());
Consumable<FailedValidationResult> failedValidationResult =
new Consumable<>(new FailedValidationResult(index, 0, null, org.odk.collect.strings.R.string.invalid_answer_error));
formController.setFailedConstraint(failedValidationResult.getValue());

FormIndex oldIndex = viewModel.getCurrentIndex().getValue();
viewModel.validate();
scheduler.flush();

FormIndex newIndex = viewModel.getCurrentIndex().getValue();
assertThat(oldIndex == newIndex, is(false));
}

@Test
public void refresh_whenThereIsASelectOnePrompt_preloadsSelectChoices() {
formController.setCurrentEvent(FormEntryController.EVENT_QUESTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public FormEntryPrompt getQuestionPrompt() {
public ValidationResult validateAnswers(boolean markCompleted, boolean moveToInvalidIndex) throws JavaRosaException {
if (validationError != null) {
throw validationError;
} else if (failedConstraint != null) {
return failedConstraint;
} else {
return SuccessValidationResult.INSTANCE;
}
Expand Down