Skip to content

Commit

Permalink
msglist: Ensure markNarrowAsRead passes before acting on it
Browse files Browse the repository at this point in the history
  • Loading branch information
Khader-1 authored and gnprice committed Aug 13, 2024
1 parent 8767297 commit 7392ccb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/widgets/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import '../model/narrow.dart';
import 'dialog.dart';
import 'store.dart';

Future<void> markNarrowAsRead(
/// Returns true if mark as read process is completed successfully.
Future<bool> markNarrowAsRead(
BuildContext context,
Narrow narrow,
bool useLegacy, // TODO(server-6)
) async {
final store = PerAccountStoreWidget.of(context);
final connection = store.connection;
if (useLegacy) {
return await _legacyMarkNarrowAsRead(context, narrow);
await _legacyMarkNarrowAsRead(context, narrow);
return true;
}

// Compare web's `mark_all_as_read` in web/src/unread_ops.js
Expand Down Expand Up @@ -66,7 +68,7 @@ Future<void> markNarrowAsRead(
flag: MessageFlag.read);
if (!context.mounted) {
scaffoldMessenger.clearSnackBars();
return;
return false;
}
responseCount++;
updatedCount += result.updatedCount;
Expand All @@ -81,7 +83,7 @@ Future<void> markNarrowAsRead(
..showSnackBar(SnackBar(behavior: SnackBarBehavior.floating,
content: Text(zulipLocalizations.markAsReadComplete(updatedCount))));
}
return;
return true;
}

if (result.lastProcessedId == null) {
Expand All @@ -91,7 +93,7 @@ Future<void> markNarrowAsRead(
await showErrorDialog(context: context,
title: zulipLocalizations.errorMarkAsReadFailedTitle,
message: zulipLocalizations.errorInvalidResponse);
return;
return false;
}
anchor = NumericAnchor(result.lastProcessedId!);

Expand Down
5 changes: 3 additions & 2 deletions lib/widgets/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ class _MarkAsReadWidgetState extends State<MarkAsReadWidget> {
final connection = store.connection;
final useLegacy = connection.zulipFeatureLevel! < 155;
setState(() => _loading = true);

bool didPass = false;
try {
await markNarrowAsRead(context, widget.narrow, useLegacy);
didPass = await markNarrowAsRead(context, widget.narrow, useLegacy);
} catch (e) {
if (!context.mounted) return;
final zulipLocalizations = ZulipLocalizations.of(context);
Expand All @@ -727,6 +727,7 @@ class _MarkAsReadWidgetState extends State<MarkAsReadWidget> {
} finally {
setState(() => _loading = false);
}
if (!didPass) return;
if (!context.mounted) return;
if (widget.narrow is CombinedFeedNarrow && !useLegacy) {
PerAccountStoreWidget.of(context).unreads.handleAllMessagesReadSuccess();
Expand Down

0 comments on commit 7392ccb

Please sign in to comment.