@@ -53,49 +53,59 @@ Future<void> unregisterToken(GlobalStore globalStore, int accountId) async {
5353 }
5454}
5555
56- Future <void > markNarrowAsRead (BuildContext context, Narrow narrow) async {
57- final store = PerAccountStoreWidget .of (context);
58- final connection = store.connection;
59- final zulipLocalizations = ZulipLocalizations .of (context);
60- final useLegacy = connection.zulipFeatureLevel! < 155 ; // TODO(server-6)
61- if (useLegacy) {
62- try {
63- await _legacyMarkNarrowAsRead (context, narrow);
64- return ;
65- } catch (e) {
66- if (! context.mounted) return ;
67- showErrorDialog (context: context,
68- title: zulipLocalizations.errorMarkAsReadFailedTitle,
69- message: e.toString ()); // TODO(#741): extract user-facing message better
70- return ;
56+ /// High-level operations that interact with the Zulip API and handle UI feedback.
57+ ///
58+ /// Methods in this class show progress and error feedback in the UI,
59+ /// and handle error cases internally rather than expecting the caller to handle them.
60+ abstract final class ZulipAction {
61+ /// Marks all messages in the given [narrow] as read, showing feedback in the UI.
62+ ///
63+ /// Shows progress feedback for long-running operations, and error feedback
64+ /// if the operation fails. Updates unread counts in the UI on success.
65+ static Future <void > markNarrowAsRead (BuildContext context, Narrow narrow) async {
66+ final store = PerAccountStoreWidget .of (context);
67+ final connection = store.connection;
68+ final zulipLocalizations = ZulipLocalizations .of (context);
69+ final useLegacy = connection.zulipFeatureLevel! < 155 ; // TODO(server-6)
70+ if (useLegacy) {
71+ try {
72+ await _legacyMarkNarrowAsRead (context, narrow);
73+ return ;
74+ } catch (e) {
75+ if (! context.mounted) return ;
76+ showErrorDialog (context: context,
77+ title: zulipLocalizations.errorMarkAsReadFailedTitle,
78+ message: e.toString ()); // TODO(#741): extract user-facing message better
79+ return ;
80+ }
7181 }
72- }
7382
74- final didPass = await updateMessageFlagsStartingFromAnchor (
75- context: context,
76- // Include `is:unread` in the narrow. That has a database index, so
77- // this can be an important optimization in narrows with a lot of history.
78- // The server applies the same optimization within the (deprecated)
79- // specialized endpoints for marking messages as read; see
80- // `do_mark_stream_messages_as_read` in `zulip:zerver/actions/message_flags.py`.
81- apiNarrow: narrow.apiEncode ()..add (ApiNarrowIs (IsOperand .unread)),
82- // Use [AnchorCode.oldest], because [AnchorCode.firstUnread]
83- // will be the oldest non-muted unread message, which would
84- // result in muted unreads older than the first unread not
85- // being processed.
86- anchor: AnchorCode .oldest,
87- // [AnchorCode.oldest] is an anchor ID lower than any valid
88- // message ID.
89- includeAnchor: false ,
90- op: UpdateMessageFlagsOp .add,
91- flag: MessageFlag .read,
92- onCompletedMessage: zulipLocalizations.markAsReadComplete,
93- progressMessage: zulipLocalizations.markAsReadInProgress,
94- onFailedTitle: zulipLocalizations.errorMarkAsReadFailedTitle);
83+ final didPass = await updateMessageFlagsStartingFromAnchor (
84+ context: context,
85+ // Include `is:unread` in the narrow. That has a database index, so
86+ // this can be an important optimization in narrows with a lot of history.
87+ // The server applies the same optimization within the (deprecated)
88+ // specialized endpoints for marking messages as read; see
89+ // `do_mark_stream_messages_as_read` in `zulip:zerver/actions/message_flags.py`.
90+ apiNarrow: narrow.apiEncode ()..add (ApiNarrowIs (IsOperand .unread)),
91+ // Use [AnchorCode.oldest], because [AnchorCode.firstUnread]
92+ // will be the oldest non-muted unread message, which would
93+ // result in muted unreads older than the first unread not
94+ // being processed.
95+ anchor: AnchorCode .oldest,
96+ // [AnchorCode.oldest] is an anchor ID lower than any valid
97+ // message ID.
98+ includeAnchor: false ,
99+ op: UpdateMessageFlagsOp .add,
100+ flag: MessageFlag .read,
101+ onCompletedMessage: zulipLocalizations.markAsReadComplete,
102+ progressMessage: zulipLocalizations.markAsReadInProgress,
103+ onFailedTitle: zulipLocalizations.errorMarkAsReadFailedTitle);
95104
96- if (! didPass || ! context.mounted) return ;
97- if (narrow is CombinedFeedNarrow ) {
98- PerAccountStoreWidget .of (context).unreads.handleAllMessagesReadSuccess ();
105+ if (! didPass || ! context.mounted) return ;
106+ if (narrow is CombinedFeedNarrow ) {
107+ PerAccountStoreWidget .of (context).unreads.handleAllMessagesReadSuccess ();
108+ }
99109 }
100110}
101111
0 commit comments