Skip to content

Commit a9137f2

Browse files
sm-sayedichrisbobbe
authored andcommitted
app: On launch, go to the last visited account
Fixes: #524 [chris: rebased atop some notification-test refactors in the last few commits] Co-authored-by: Chris Bobbe <[email protected]>
1 parent a26a469 commit a9137f2

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

lib/widgets/app.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,14 @@ class _ZulipAppState extends State<ZulipApp> with WidgetsBindingObserver {
211211
}
212212

213213
final globalStore = GlobalStoreWidget.of(context);
214-
// TODO(#524) choose initial account as last one used
215-
final initialAccountId = globalStore.accounts.firstOrNull?.id;
214+
final lastVisitedAccountId = globalStore.lastVisitedAccount?.id;
215+
216216
return [
217-
if (initialAccountId == null)
217+
if (lastVisitedAccountId == null)
218+
// There are no accounts, or the last-visited account was logged out.
218219
MaterialWidgetRoute(page: const ChooseAccountPage())
219220
else
220-
HomePage.buildRoute(accountId: initialAccountId),
221+
HomePage.buildRoute(accountId: lastVisitedAccountId),
221222
];
222223
}
223224

lib/widgets/share.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class ShareService {
6060

6161
final globalStore = GlobalStoreWidget.of(context);
6262

63-
// TODO(#524) use last account used, not the first in the list
6463
// TODO(#1779) allow selecting account, if there are multiple
65-
final accountId = globalStore.accounts.firstOrNull?.id;
64+
final accountId = globalStore.lastVisitedAccount?.id
65+
?? globalStore.accountIds.firstOrNull;
6666

6767
if (accountId == null) {
6868
final zulipLocalizations = ZulipLocalizations.of(context);

test/notifications/open_test.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ void main() {
113113
return;
114114
}
115115
await tester.pump();
116-
final accountIds = testBinding.globalStore.accountIds;
117-
final initialAccountId = accountIds.firstOrNull;
118-
if (initialAccountId == null) {
116+
final lastVisitedAccountId = testBinding.globalStore.lastVisitedAccount?.id;
117+
if (lastVisitedAccountId == null) {
119118
takeChooseAccountPageRoute();
120119
} else {
121-
takeHomePageRouteForAccount(initialAccountId);
120+
takeHomePageRouteForAccount(lastVisitedAccountId);
122121
}
123122
check(pushedRoutes).isEmpty();
124123
}
@@ -287,7 +286,7 @@ void main() {
287286
// Now let the GlobalStore get loaded and the app's main UI get mounted.
288287
await tester.pump();
289288
// The navigator first pushes the starting routes…
290-
takeHomePageRouteForAccount(eg.selfAccount.id); // because first in list
289+
takeHomePageRouteForAccount(eg.selfAccount.id); // because last-visited
291290
// … and then the one the notification leads to.
292291
matchesNavigation(check(pushedRoutes).single, eg.selfAccount, message);
293292
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));

test/widgets/app_test.dart

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,47 @@ void main() {
3737
}
3838

3939
testWidgets('when no accounts, go to choose account', (tester) async {
40+
check(testBinding.globalStore).accounts.isEmpty();
41+
check(testBinding.globalStore).lastVisitedAccount.isNull();
4042
await prepare(tester);
4143
check(pushedRoutes).deepEquals(<Condition<Object?>>[
4244
(it) => it.isA<WidgetRoute>().page.isA<ChooseAccountPage>(),
4345
]);
4446
});
4547

46-
testWidgets('when have accounts, go to home page for first account', (tester) async {
47-
// We'll need per-account data for the account that a page will be opened
48-
// for, but not for the other account.
49-
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
50-
await testBinding.globalStore.insertAccount(eg.otherAccount.toCompanion(false));
51-
await prepare(tester);
48+
group('when have accounts', () {
49+
testWidgets('with account(s) visited, go to home page for the last visited account', (tester) async {
50+
await testBinding.globalStore.insertAccount(eg.otherAccount.toCompanion(false));
51+
// We'll need per-account data for the account that a page will be opened
52+
// for, but not for the other accounts.
53+
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
54+
await testBinding.globalStore.insertAccount(eg.thirdAccount.toCompanion(false));
55+
check(testBinding.globalStore).lastVisitedAccount.equals(eg.selfAccount);
56+
await prepare(tester);
57+
58+
check(pushedRoutes).deepEquals(<Condition<Object?>>[
59+
(it) => it.isA<MaterialAccountWidgetRoute>()
60+
..accountId.equals(eg.selfAccount.id)
61+
..page.isA<HomePage>(),
62+
]);
63+
});
5264

53-
check(pushedRoutes).deepEquals(<Condition<Object?>>[
54-
(it) => it.isA<MaterialAccountWidgetRoute>()
55-
..accountId.equals(eg.selfAccount.id)
56-
..page.isA<HomePage>(),
57-
]);
65+
testWidgets('with last visited account logged out, go to choose account', (tester) async {
66+
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
67+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
68+
await testBinding.globalStore.insertAccount(eg.otherAccount.toCompanion(false));
69+
check(testBinding.globalStore).lastVisitedAccount.equals(eg.selfAccount);
70+
final future = logOutAccount(testBinding.globalStore, eg.selfAccount.id);
71+
await tester.pump(TestGlobalStore.removeAccountDuration);
72+
await future;
73+
check(testBinding.globalStore).lastVisitedAccount.isNull();
74+
check(testBinding.globalStore).accounts.isNotEmpty();
75+
await prepare(tester);
76+
77+
check(pushedRoutes).deepEquals(<Condition<Object?>>[
78+
(it) => it.isA<WidgetRoute>().page.isA<ChooseAccountPage>(),
79+
]);
80+
});
5881
});
5982
});
6083

@@ -99,6 +122,7 @@ void main() {
99122
testWidgets('push route when popping last route on stack', (tester) async {
100123
// Set up the loading of per-account data to fail.
101124
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
125+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
102126
testBinding.globalStore.loadPerAccountDuration = Duration.zero;
103127
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized();
104128
await prepare(tester);
@@ -133,6 +157,7 @@ void main() {
133157
const loadPerAccountDuration = Duration(seconds: 30);
134158
assert(loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135159
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
160+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
136161
testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137162
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized();
138163
await prepare(tester);
@@ -281,8 +306,9 @@ void main() {
281306
testWidgets('choosing an account clears the navigator stack', (tester) async {
282307
addTearDown(testBinding.reset);
283308
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
284-
await testBinding.globalStore.add(eg.otherAccount, eg.initialSnapshot(
285-
realmUsers: [eg.otherUser]));
309+
await testBinding.globalStore.add(
310+
eg.otherAccount, eg.initialSnapshot(realmUsers: [eg.otherUser]),
311+
markLastVisited: false);
286312

287313
final pushedRoutes = <Route<void>>[];
288314
final poppedRoutes = <Route<void>>[];

test/widgets/home_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@ void main () {
333333
pushedRoutes = [];
334334
lastPoppedRoute = null;
335335
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
336-
await testBinding.globalStore.add(eg.otherAccount, eg.initialSnapshot(
337-
realmUsers: [eg.otherUser]));
336+
await testBinding.globalStore.add(
337+
eg.otherAccount, eg.initialSnapshot(realmUsers: [eg.otherUser]),
338+
markLastVisited: false);
338339
await tester.pumpWidget(ZulipApp(navigatorObservers: [testNavObserver]));
339340
await tester.pump(Duration.zero); // wait for the loading page
340341
checkOnLoadingPage();

0 commit comments

Comments
 (0)