Skip to content

Commit

Permalink
notif: Ensure back navigation preserves target account context after …
Browse files Browse the repository at this point in the history
…opening notification

Fixes: #1210
  • Loading branch information
lakshya1goel committed Feb 22, 2025
1 parent a198f72 commit 509ba69
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/notifications/display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import '../model/narrow.dart';
import '../widgets/app.dart';
import '../widgets/color.dart';
import '../widgets/dialog.dart';
import '../widgets/home.dart';
import '../widgets/message_list.dart';
import '../widgets/page.dart';
import '../widgets/store.dart';
Expand Down Expand Up @@ -502,6 +503,18 @@ class NotificationDisplayManager {
final route = routeForNotification(context: context, url: url);
if (route == null) return; // TODO(log)

Route<dynamic>? topRoute;
navigator.popUntil((r) {
topRoute = r;
return true;
});

if (topRoute == null || topRoute is! AccountRoute
|| (topRoute as AccountRoute).accountId != route.accountId) {
HomePage.navigate(context, accountId: route.accountId);
navigator = await ZulipApp.navigator;
}

// TODO(nav): Better interact with existing nav stack on notif open
unawaited(navigator.push(route));
}
Expand Down
43 changes: 43 additions & 0 deletions test/notifications/display_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,49 @@ void main() {
takeStartingRoutes(account: accountB);
matchesNavigation(check(pushedRoutes).single, accountB, message);
});

testWidgets('back navigation after opening a notification will preserve target account context', (tester) async {
addTearDown(testBinding.reset);

final accountA = eg.selfAccount;
final accountB = eg.otherAccount;
final message = eg.streamMessage();

await testBinding.globalStore.add(accountA, eg.initialSnapshot());
await testBinding.globalStore.add(accountB, eg.initialSnapshot());

await prepare(tester, early: true);
await tester.pump();
takeStartingRoutes(account: accountA);

Route<dynamic>? topRoute;
final navigator = await ZulipApp.navigator;
navigator.popUntil((r) {
topRoute = r;
return true;
});
check(topRoute).isA<AccountRoute>().accountId.equals(accountA.id);
await openNotification(tester, accountB, message);

navigator.popUntil((r) {
topRoute = r;
return true;
});
check(topRoute).isA<MaterialAccountWidgetRoute>()
..accountId.equals(accountB.id)
..page.isA<MessageListPage>();

navigator.pop();
await tester.pumpAndSettle();

navigator.popUntil((r) {
topRoute = r;
return true;
});
check(topRoute).isA<MaterialAccountWidgetRoute>()
..accountId.equals(accountB.id)
..page.isA<HomePage>();
});
});

group('NotificationOpenPayload', () {
Expand Down

0 comments on commit 509ba69

Please sign in to comment.