Skip to content

lightbox: Replace onDismiss with await showErrorDialog #937

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

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

PIG208
Copy link
Member

@PIG208 PIG208 commented Sep 12, 2024

showDialog returns a Future that resolves when the dialog is dismissed.

This refactors showErrorDialog to stop overriding onPressed and barrierIsDismissable, and make the caller call Navigator.pop one more time once the dialog is closed.

Note that we check for mounted again because there has been an asynchronous gap.

See also:
#587 (comment)

@PIG208 PIG208 requested a review from chrisbobbe September 12, 2024 04:18
@PIG208 PIG208 added the maintainer review PR ready for review by Zulip maintainers label Sep 12, 2024
@PIG208 PIG208 mentioned this pull request Sep 12, 2024
@chrisbobbe
Copy link
Collaborator

LGTM assuming you've tested this manually; marking for Greg's review.

@chrisbobbe chrisbobbe added integration review Added by maintainers when PR may be ready for integration and removed maintainer review PR ready for review by Zulip maintainers labels Sep 19, 2024
@chrisbobbe chrisbobbe requested review from gnprice and removed request for chrisbobbe September 19, 2024 01:49
@chrisbobbe chrisbobbe assigned gnprice and unassigned chrisbobbe Sep 19, 2024
@PIG208
Copy link
Member Author

PIG208 commented Sep 19, 2024

I forgot to mention that this shouldn't be an nfc change, because the user can close the dialog by tapping the barrier now. Pushed an update to the commit message to reflect that.

@chrisbobbe
Copy link
Collaborator

Makes sense. Ah and I think barrierIsDismissable in the commit message is a typo for barrierDismissable.

@PIG208 PIG208 force-pushed the pr-lightbox-dialog branch 2 times, most recently from f27b62c to 1df1035 Compare September 19, 2024 02:02
The `showDialog` function returns a future that resolves when the
dialog is dismissed, and showErrorDialog returns that future.

The one caller of showErrorDialog that was passing an onDismiss
callback can therefore await that future instead.  Do that, and
eliminate the parameter.

This is not an NFC change, because now the user can tap the barrier
(the darkened area around the dialog) to close it.

Discussion from when this parameter was introduced:
  zulip#587 (comment)

Signed-off-by: Zixuan James Li <[email protected]>
Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks! This is a nice cleanup.

Merging, with a revision to the commit message:

     lightbox: Replace onDismiss with `await showErrorDialog`
 
-    `showDialog` returns a `Future` that resolves when the dialog is
-    dismissed.
+    The `showDialog` function returns a future that resolves when the
+    dialog is dismissed, and showErrorDialog returns that future.
 
-    This refactors `showErrorDialog` to stop overriding `onPressed` and
-    `barrierDismissible`, and make the caller call `Navigator.pop` one
-    more time once the dialog is closed.
+    The one caller of showErrorDialog that was passing an onDismiss
+    callback can therefore await that future instead.  Do that, and
+    eliminate the parameter.
 
-    This is not an nfc because now the user can tap the barrier (darkened
-    area around the dialog) to close it.
+    This is not an NFC change, because now the user can tap the barrier
+    (the darkened area around the dialog) to close it.
 
-    Note that we check for `mounted` again because there has been an
-    asynchronous gap.
-
-    See also:
+    Discussion from when this parameter was introduced:
       https://github.com/zulip/zulip-flutter/pull/587#discussion_r1585845428

The main change is to explain more of the "why", and delete several details of "what" which the reader can more easily learn by looking at the diff.

Comment on lines +20 to 21
/// Returns a [Future] that resolves when the dialog is closed.
Future<void> showErrorDialog({
Copy link
Member

Choose a reason for hiding this comment

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

This behavior (which showDialog has and we faithfully wrap) is kind of a bad pattern for an API, as we've discussed in the context of #731 / the unawaited_futures lint. If a function is named "show thing" and its return type is some Future<T>, the future should complete when the thing has been shown, not when some possibly-much-later event happens.

I think a good model here is ScaffoldMessengerState.showSnackBar: there's a Future it wants to provide for callers to potentially await on, but it's for something that happens after the function's own work is done, so instead of returning a value that is a Future it returns a value that has a Future. And the property name, closed, makes it clear what event is being awaited.

Anyway, that's a problem for outside this PR.

@gnprice gnprice merged commit 12f39ea into zulip:main Sep 25, 2024
1 check passed
@PIG208 PIG208 deleted the pr-lightbox-dialog branch September 25, 2024 16:07
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Sep 25, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Sep 27, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Sep 28, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Oct 5, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Oct 5, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Oct 7, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
PIG208 added a commit to PIG208/zulip-flutter that referenced this pull request Oct 7, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
gnprice pushed a commit to PIG208/zulip-flutter that referenced this pull request Oct 7, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
oriohac pushed a commit to oriohac/zulip-flutter that referenced this pull request Oct 26, 2024
As a result, showErrorDialog no longer returns a Future that can be
mistakenly awaited.  The wrapped return value offers clarity on how the
Future is supposed to be used.

There is no user visible change because existing callers (other than
lightbox's) that wait for its return value are all asynchronous
handlers for UI elements like buttons, and waits unnecessarily.

See also discussion
  zulip#937 (comment).

Signed-off-by: Zixuan James Li <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration review Added by maintainers when PR may be ready for integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants