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
base: master
Are you sure you want to change the base?
Conversation
9442dce
to
122b53a
Compare
122b53a
to
2808966
Compare
}, result -> { | ||
validationResult.setValue(new Consumable<>(result)); | ||
} | ||
validationResult.postValue(new Consumable<>(result)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
onlivedata
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:
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:
./gradlew connectedAndroidTest
(or./gradlew testLab
) and confirmed all checks still pass