Skip to content

dialog [nfc]: Wrap showErrorDialog's return value #970

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 2 commits into from
Oct 7, 2024
Merged
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
8 changes: 4 additions & 4 deletions lib/widgets/action_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class AddThumbsUpButton extends MessageActionSheetMenuItemButton {
default:
}

await showErrorDialog(context: context,
showErrorDialog(context: context,
title: 'Adding reaction failed', message: errorMessage);
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ class StarButton extends MessageActionSheetMenuItemButton {
default:
}

await showErrorDialog(context: messageListContext,
showErrorDialog(context: messageListContext,
title: switch(op) {
UpdateMessageFlagsOp.remove => zulipLocalizations.errorUnstarMessageFailedTitle,
UpdateMessageFlagsOp.add => zulipLocalizations.errorStarMessageFailedTitle,
Expand Down Expand Up @@ -215,7 +215,7 @@ Future<String?> fetchRawContentWithFeedback({
// TODO(?) give no feedback on error conditions we expect to
// flag centrally in event polling, like invalid auth,
// user/realm deactivated. (Support with reusable code.)
await showErrorDialog(context: context,
showErrorDialog(context: context,
title: errorDialogTitle, message: errorMessage);
}

Expand Down Expand Up @@ -423,7 +423,7 @@ class ShareButton extends MessageActionSheetMenuItemButton {
// Until we learn otherwise, assume something wrong happened.
case ShareResultStatus.unavailable:
if (!messageListContext.mounted) return;
await showErrorDialog(context: messageListContext,
showErrorDialog(context: messageListContext,
title: zulipLocalizations.errorSharingFailed);
case ShareResultStatus.success:
case ShareResultStatus.dismissed:
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Future<void> markNarrowAsRead(BuildContext context, Narrow narrow) async {
return;
} catch (e) {
if (!context.mounted) return;
await showErrorDialog(context: context,
showErrorDialog(context: context,
title: zulipLocalizations.errorMarkAsReadFailedTitle,
message: e.toString()); // TODO(#741): extract user-facing message better
return;
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ class GlobalTime extends StatelessWidget {
}

void _launchUrl(BuildContext context, String urlString) async {
Future<void> showError(BuildContext context, String? message) {
DialogStatus showError(BuildContext context, String? message) {
return showErrorDialog(context: context,
title: 'Unable to open link',
message: [
Expand All @@ -1207,7 +1207,7 @@ void _launchUrl(BuildContext context, String urlString) async {
final store = PerAccountStoreWidget.of(context);
final url = store.tryResolveUrl(urlString);
if (url == null) { // TODO(log)
await showError(context, null);
showError(context, null);
return;
}

Expand Down Expand Up @@ -1236,7 +1236,7 @@ void _launchUrl(BuildContext context, String urlString) async {
}
if (!launched) { // TODO(log)
if (!context.mounted) return;
await showError(context, errorMessage);
showError(context, errorMessage);
}
}

Expand Down
23 changes: 20 additions & 3 deletions lib/widgets/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,32 @@ Widget _dialogActionText(String text) {
);
}

/// Tracks the status of a dialog, in being still open or already closed.
///
/// See also:
/// * [showDialog], whose return value this class is intended to wrap.
class DialogStatus {
const DialogStatus(this.closed);

/// Resolves when the dialog is closed.
final Future<void> closed;
}

/// Displays an [AlertDialog] with a dismiss button.
///
/// Returns a [Future] that resolves when the dialog is closed.
Future<void> showErrorDialog({
/// The [DialogStatus.closed] field of the return value can be used
/// for waiting for the dialog to be closed.
// This API is inspired by [ScaffoldManager.showSnackBar]. We wrap
// [showDialog]'s return value, a [Future], inside [DialogStatus]
// whose documentation can be accessed. This helps avoid confusion when
// intepreting the meaning of the [Future].
DialogStatus showErrorDialog({
required BuildContext context,
required String title,
String? message,
}) {
final zulipLocalizations = ZulipLocalizations.of(context);
return showDialog(
final future = showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(title),
Expand All @@ -34,6 +50,7 @@ Future<void> showErrorDialog({
onPressed: () => Navigator.pop(context),
child: _dialogActionText(zulipLocalizations.errorDialogContinue)),
]));
return DialogStatus(future);
}

void showSuggestedActionDialog({
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/lightbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,11 @@ class _VideoLightboxPageState extends State<VideoLightboxPage> with PerAccountSt
assert(debugLog("VideoPlayerController.initialize failed: $error"));
if (!mounted) return;
final zulipLocalizations = ZulipLocalizations.of(context);
// Wait until the dialog is closed
await showErrorDialog(
final dialog = showErrorDialog(
context: context,
title: zulipLocalizations.errorDialogTitle,
message: zulipLocalizations.errorVideoPlayerFailed);
await dialog.closed;
if (!mounted) return;
Navigator.pop(context); // Pops the lightbox
}
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class _LoginPageState extends State<LoginPage> {
if (e is PlatformException && e.message != null) {
message = e.message!;
}
await showErrorDialog(context: context,
showErrorDialog(context: context,
title: zulipLocalizations.errorWebAuthOperationalErrorTitle,
message: message);
} finally {
Expand Down Expand Up @@ -351,7 +351,7 @@ class _LoginPageState extends State<LoginPage> {
if (e is PlatformException && e.message != null) {
message = e.message!;
}
await showErrorDialog(context: context,
showErrorDialog(context: context,
title: zulipLocalizations.errorWebAuthOperationalErrorTitle,
message: message);
}
Expand Down