Skip to content

Commit 2739149

Browse files
committed
action_sheet [nfc]: Move fetchRawContentWithFeedback to ZulipAction
1 parent a850103 commit 2739149

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

lib/widgets/action_sheet.dart

+6-53
Original file line numberDiff line numberDiff line change
@@ -765,54 +765,6 @@ class StarButton extends MessageActionSheetMenuItemButton {
765765
}
766766
}
767767

768-
/// Fetch and return the raw Markdown content for [messageId],
769-
/// showing an error dialog on failure.
770-
Future<String?> fetchRawContentWithFeedback({
771-
required BuildContext context,
772-
required int messageId,
773-
required String errorDialogTitle,
774-
}) async {
775-
Message? fetchedMessage;
776-
String? errorMessage;
777-
// TODO, supported by reusable code:
778-
// - (?) Retry with backoff on plausibly transient errors.
779-
// - If request(s) take(s) a long time, show snackbar with cancel
780-
// button, like "Still working on quote-and-reply…".
781-
// On final failure or success, auto-dismiss the snackbar.
782-
final zulipLocalizations = ZulipLocalizations.of(context);
783-
try {
784-
fetchedMessage = await getMessageCompat(PerAccountStoreWidget.of(context).connection,
785-
messageId: messageId,
786-
applyMarkdown: false,
787-
);
788-
if (fetchedMessage == null) {
789-
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
790-
}
791-
} catch (e) {
792-
switch (e) {
793-
case ZulipApiException():
794-
errorMessage = e.message;
795-
// TODO specific messages for common errors, like network errors
796-
// (support with reusable code)
797-
default:
798-
errorMessage = zulipLocalizations.errorCouldNotFetchMessageSource;
799-
}
800-
}
801-
802-
if (!context.mounted) return null;
803-
804-
if (fetchedMessage == null) {
805-
assert(errorMessage != null);
806-
// TODO(?) give no feedback on error conditions we expect to
807-
// flag centrally in event polling, like invalid auth,
808-
// user/realm deactivated. (Support with reusable code.)
809-
showErrorDialog(context: context,
810-
title: errorDialogTitle, message: errorMessage);
811-
}
812-
813-
return fetchedMessage?.content;
814-
}
815-
816768
class QuoteAndReplyButton extends MessageActionSheetMenuItemButton {
817769
QuoteAndReplyButton({super.key, required super.message, required super.pageContext});
818770

@@ -849,7 +801,7 @@ class QuoteAndReplyButton extends MessageActionSheetMenuItemButton {
849801
message: message,
850802
);
851803

852-
final rawContent = await fetchRawContentWithFeedback(
804+
final rawContent = await ZulipAction.fetchRawContentWithFeedback(
853805
context: pageContext,
854806
messageId: message.id,
855807
errorDialogTitle: zulipLocalizations.errorQuotationFailed,
@@ -901,12 +853,13 @@ class CopyMessageTextButton extends MessageActionSheetMenuItemButton {
901853

902854
@override void onPressed() async {
903855
// This action doesn't show request progress.
904-
// But hopefully it won't take long at all; and
905-
// fetchRawContentWithFeedback has a TODO for giving feedback if it does.
856+
// But hopefully it won't take long at all,
857+
// and [ZulipAction.fetchRawContentWithFeedback] has a TODO
858+
// for giving feedback if it does.
906859

907860
final zulipLocalizations = ZulipLocalizations.of(pageContext);
908861

909-
final rawContent = await fetchRawContentWithFeedback(
862+
final rawContent = await ZulipAction.fetchRawContentWithFeedback(
910863
context: pageContext,
911864
messageId: message.id,
912865
errorDialogTitle: zulipLocalizations.errorCopyingFailed,
@@ -973,7 +926,7 @@ class ShareButton extends MessageActionSheetMenuItemButton {
973926

974927
final zulipLocalizations = ZulipLocalizations.of(pageContext);
975928

976-
final rawContent = await fetchRawContentWithFeedback(
929+
final rawContent = await ZulipAction.fetchRawContentWithFeedback(
977930
context: pageContext,
978931
messageId: message.id,
979932
errorDialogTitle: zulipLocalizations.errorSharingFailed,

lib/widgets/actions.dart

+48
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,54 @@ abstract final class ZulipAction {
240240
return;
241241
}
242242
}
243+
244+
/// Fetch and return the raw Markdown content for [messageId],
245+
/// showing an error dialog on failure.
246+
static Future<String?> fetchRawContentWithFeedback({
247+
required BuildContext context,
248+
required int messageId,
249+
required String errorDialogTitle,
250+
}) async {
251+
Message? fetchedMessage;
252+
String? errorMessage;
253+
// TODO, supported by reusable code:
254+
// - (?) Retry with backoff on plausibly transient errors.
255+
// - If request(s) take(s) a long time, show snackbar with cancel
256+
// button, like "Still working on quote-and-reply…".
257+
// On final failure or success, auto-dismiss the snackbar.
258+
final zulipLocalizations = ZulipLocalizations.of(context);
259+
try {
260+
fetchedMessage = await getMessageCompat(PerAccountStoreWidget.of(context).connection,
261+
messageId: messageId,
262+
applyMarkdown: false,
263+
);
264+
if (fetchedMessage == null) {
265+
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
266+
}
267+
} catch (e) {
268+
switch (e) {
269+
case ZulipApiException():
270+
errorMessage = e.message;
271+
// TODO specific messages for common errors, like network errors
272+
// (support with reusable code)
273+
default:
274+
errorMessage = zulipLocalizations.errorCouldNotFetchMessageSource;
275+
}
276+
}
277+
278+
if (!context.mounted) return null;
279+
280+
if (fetchedMessage == null) {
281+
assert(errorMessage != null);
282+
// TODO(?) give no feedback on error conditions we expect to
283+
// flag centrally in event polling, like invalid auth,
284+
// user/realm deactivated. (Support with reusable code.)
285+
showErrorDialog(context: context,
286+
title: errorDialogTitle, message: errorMessage);
287+
}
288+
289+
return fetchedMessage?.content;
290+
}
243291
}
244292

245293
/// Methods that act through platform APIs and show feedback in the UI.

0 commit comments

Comments
 (0)