Skip to content

Commit 23dc9c9

Browse files
committed
notif: Ensure back navigation preserves target account context after opening notification
Fixes: zulip#1210
1 parent 9e70ae9 commit 23dc9c9

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

lib/notifications/display.dart

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import '../model/narrow.dart';
1717
import '../widgets/app.dart';
1818
import '../widgets/color.dart';
1919
import '../widgets/dialog.dart';
20+
import '../widgets/home.dart';
2021
import '../widgets/message_list.dart';
2122
import '../widgets/page.dart';
2223
import '../widgets/store.dart';
@@ -502,7 +503,19 @@ class NotificationDisplayManager {
502503
final route = routeForNotification(context: context, url: url);
503504
if (route == null) return; // TODO(log)
504505

505-
// TODO(nav): Better interact with existing nav stack on notif open
506+
int? currentAccountId;
507+
navigator.popUntil((r) {
508+
if (r is AccountRoute) {
509+
currentAccountId = r.accountId;
510+
}
511+
return true;
512+
});
513+
514+
if (currentAccountId != route.accountId) {
515+
HomePage.navigate(context, accountId: route.accountId);
516+
navigator = await ZulipApp.navigator;
517+
}
518+
506519
unawaited(navigator.push(route));
507520
}
508521

test/notifications/display_test.dart

+80
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,86 @@ void main() {
11761176
takeStartingRoutes(account: accountB);
11771177
matchesNavigation(check(pushedRoutes).single, accountB, message);
11781178
});
1179+
1180+
testWidgets('notification switches account only when from different account', (tester) async {
1181+
addTearDown(testBinding.reset);
1182+
1183+
final accountA = eg.selfAccount;
1184+
final accountB = eg.otherAccount;
1185+
final message = eg.streamMessage();
1186+
1187+
await testBinding.globalStore.add(accountA, eg.initialSnapshot());
1188+
await testBinding.globalStore.add(accountB, eg.initialSnapshot());
1189+
1190+
await prepare(tester, early: true);
1191+
await tester.pump();
1192+
takeStartingRoutes(account: accountA);
1193+
1194+
Route<dynamic>? topRoute;
1195+
final navigator = await ZulipApp.navigator;
1196+
navigator.popUntil((r) {
1197+
topRoute = r;
1198+
return true;
1199+
});
1200+
check(topRoute).isA<AccountRoute>().accountId.equals(accountA.id);
1201+
1202+
await openNotification(tester, accountA, message);
1203+
navigator.popUntil((r) {
1204+
topRoute = r;
1205+
return true;
1206+
});
1207+
check(topRoute).isA<MaterialAccountWidgetRoute>()
1208+
..accountId.equals(accountA.id)
1209+
..page.isA<MessageListPage>();
1210+
1211+
await openNotification(tester, accountB, message);
1212+
navigator.popUntil((r) {
1213+
topRoute = r;
1214+
return true;
1215+
});
1216+
check(topRoute).isA<MaterialAccountWidgetRoute>()
1217+
..accountId.equals(accountB.id)
1218+
..page.isA<MessageListPage>();
1219+
});
1220+
1221+
testWidgets('notification preserves navigation stack when in same account', (tester) async {
1222+
addTearDown(testBinding.reset);
1223+
1224+
final account = eg.selfAccount;
1225+
final message = eg.streamMessage();
1226+
1227+
await testBinding.globalStore.add(account, eg.initialSnapshot());
1228+
1229+
await prepare(tester, early: true);
1230+
await tester.pump();
1231+
takeStartingRoutes(account: account);
1232+
1233+
Route<dynamic>? topRoute;
1234+
final navigator = await ZulipApp.navigator;
1235+
navigator.popUntil((r) {
1236+
topRoute = r;
1237+
return true;
1238+
});
1239+
check(topRoute).isA<AccountRoute>().accountId.equals(account.id);
1240+
1241+
await openNotification(tester, account, message);
1242+
navigator.popUntil((r) {
1243+
topRoute = r;
1244+
return true;
1245+
});
1246+
check(topRoute).isA<MaterialAccountWidgetRoute>()
1247+
..accountId.equals(account.id)
1248+
..page.isA<MessageListPage>();
1249+
1250+
navigator.pop();
1251+
await tester.pumpAndSettle();
1252+
navigator.popUntil((r) {
1253+
topRoute = r;
1254+
return true;
1255+
});
1256+
check(topRoute).isA<AccountRoute>()
1257+
.accountId.equals(account.id);
1258+
});
11791259
});
11801260

11811261
group('NotificationOpenPayload', () {

0 commit comments

Comments
 (0)