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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

grzesiek2010
Copy link
Member

@grzesiek2010 grzesiek2010 commented Mar 7, 2024

Closes #5939

Why is this the best possible solution? Were any other approaches considered?

It was a race condition. The problem was that we always updated the index after validating answers which caused building a new widget. As a result, sometimes errors were displayed before creating a new widget and not displayed again after rebuilding the widget.
I've fixed the problem in 7ca4293 The action that triggers creating a new widget is also triggered by calling postValue on livedata so if we do the same with displaying errors the order of events should be correct.
I've also noticed that it doesn't make sense to update the index and rebuild the widget if it's currently displayed widget so I've added a small improvement in 3589b98.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

Everything around displaying errors and updating indexes (moving to the questions with errors) is at risk.
Please test validating answers in different scenarios:

  • when the question with an invalid answer in a single question on the screen
  • when the question with an invalid answer is displayed along with other questions on the same screen
  • when the question with an invalid answer is the one we currently display
  • when the question with an invalid answer is not the one we currently display (so we should be moved to that question)

Do we need any specific form for testing your changes? If so, please attach one.

Any form that can throw errors like a form with constraints or required questions.

Does this change require updates to documentation? If so, please file an issue here and include the link below.

No.

Before submitting this PR, please make sure you have:

  • added or modified tests for any new or changed behavior
  • run ./gradlew connectedAndroidTest (or ./gradlew testLab) and confirmed all checks still pass
  • added a comment above any new strings describing it for translators
  • verified that any code or assets from external sources are properly credited in comments and/or in the about file.
  • verified that any new UI elements use theme colors. UI Components Style guidelines

@grzesiek2010 grzesiek2010 marked this pull request as ready for review March 8, 2024 12:27
}, 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).

Copy link
Member

@seadowg seadowg left a comment

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The error outline disappears after clicking "Check for errors”
2 participants