From 79f03fd606ed4efd040cd5d91e9f16a9c43f81aa Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Mon, 4 Dec 2023 19:33:41 +0000 Subject: [PATCH] star in action sheet --- assets/l10n/app_en.arb | 16 ++++++++++++ lib/widgets/action_sheet.dart | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/assets/l10n/app_en.arb b/assets/l10n/app_en.arb index 8a005225d84..4a1fe23977d 100644 --- a/assets/l10n/app_en.arb +++ b/assets/l10n/app_en.arb @@ -55,6 +55,14 @@ "@actionSheetOptionQuoteAndReply": { "description": "Label for Quote and reply button on action sheet." }, + "actionSheetOptionStarMessage": "Star message", + "@actionSheetOptionStarMessage": { + "description": "Label for star button on action sheet." + }, + "actionSheetOptionUnstarMessage": "Unstar message", + "@actionSheetOptionUnstarMessage": { + "description": "Label for unstar button on action sheet." + }, "errorCouldNotFetchMessageSource": "Could not fetch message source", "@errorCouldNotFetchMessageSource": { "description": "Error message when the source of a message could not be fetched." @@ -128,6 +136,14 @@ "@errorSharingFailed": { "description": "Error message when sharing a message failed." }, + "errorStarMessageFailedTitle": "Failed to star message", + "@errorStarMessageFailedTitle": { + "description": "Error title when starring a message failed." + }, + "errorUnstarMessageFailedTitle": "Failed to unstar message", + "@errorUnstarMessageFailedTitle": { + "description": "Error title when unstarring a message failed." + }, "successLinkCopied": "Link copied", "@successLinkCopied": { "description": "Success message after copy link action completed." diff --git a/lib/widgets/action_sheet.dart b/lib/widgets/action_sheet.dart index a35cb08f3bc..01047a94c3a 100644 --- a/lib/widgets/action_sheet.dart +++ b/lib/widgets/action_sheet.dart @@ -44,6 +44,7 @@ void showMessageActionSheet({required BuildContext context, required Message mes messageListContext: context, ), CopyButton(message: message, messageListContext: context), + StarButton(message: message, messageListContext: context), ]); }); } @@ -317,3 +318,50 @@ class CopyButton extends MessageActionSheetMenuItemButton { data: ClipboardData(text: rawContent)); }; } + +class StarButton extends MessageActionSheetMenuItemButton { + StarButton({ + super.key, + required super.message, + required super.messageListContext, + }); + + @override get icon => Icons.star; + + @override + String label(ZulipLocalizations zulipLocalizations) { + return message.flags.contains(MessageFlag.starred) + ? zulipLocalizations.actionSheetOptionUnstarMessage + : zulipLocalizations.actionSheetOptionStarMessage; + } + + @override get onPressed => (BuildContext context) async { + Navigator.of(context).pop(); + String? errorMessage; + final op = (message.flags.contains(MessageFlag.starred)) + ? UpdateMessageFlagsOp.remove + : UpdateMessageFlagsOp.add; + try { + final connection = PerAccountStoreWidget.of(messageListContext).connection; + await updateMessageFlags(connection, messages: [message.id], + op: op, flag: MessageFlag.starred); + } catch (e) { + if (!messageListContext.mounted) return; + final zulipLocalizations = ZulipLocalizations.of(messageListContext); + + switch (e) { + case ZulipApiException(): + errorMessage = e.message; + // TODO specific messages for common errors, like network errors + // (support with reusable code) + default: + } + + await showErrorDialog(context: messageListContext, + title: switch(op) { + UpdateMessageFlagsOp.add => zulipLocalizations.errorStarMessageFailedTitle, + UpdateMessageFlagsOp.remove => zulipLocalizations.errorUnstarMessageFailedTitle, + }, message: errorMessage); + } + }; +}