Skip to content

Commit 2abe893

Browse files
sm-sayedichrisbobbe
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 ce734ff commit 2abe893

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
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 lastVisitedAccount = globalStore.lastVisitedAccount;
215+
216216
return [
217-
if (initialAccountId == null)
217+
if (lastVisitedAccount == 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: lastVisitedAccount.id),
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 lastVisitedAccount = globalStore.lastVisitedAccount;
65+
final accountId = lastVisitedAccount?.id ?? globalStore.accountIds.firstOrNull;
6666

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

test/notifications/open_test.dart

Lines changed: 7 additions & 7 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) {
119-
takeChooseAccountPageRoute();
116+
final lastVisitedAccount = testBinding.globalStore.lastVisitedAccount;
117+
if (lastVisitedAccount != null) {
118+
takeHomePageRouteForAccount(lastVisitedAccount.id);
120119
} else {
121-
takeHomePageRouteForAccount(initialAccountId);
120+
takeChooseAccountPageRoute();
122121
}
123122
check(pushedRoutes).isEmpty();
124123
}
@@ -274,7 +273,8 @@ void main() {
274273

275274
testWidgets('wait for app to become ready', (tester) async {
276275
addTearDown(testBinding.reset);
277-
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
276+
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot(),
277+
markLastVisited: false);
278278
await prepare(tester, early: true);
279279
final message = eg.streamMessage();
280280
await openNotification(tester, eg.selfAccount, message);
@@ -287,7 +287,7 @@ void main() {
287287
// Now let the GlobalStore get loaded and the app's main UI get mounted.
288288
await tester.pump();
289289
// The navigator first pushes the starting routes…
290-
takeHomePageRouteForAccount(eg.selfAccount.id); // because first in list
290+
takeChooseAccountPageRoute(); // (no lastVisitedAccountId in this test)
291291
// … and then the one the notification leads to.
292292
matchesNavigation(check(pushedRoutes).single, eg.selfAccount, message);
293293
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));

test/widgets/app_test.dart

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,53 @@ 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.add(eg.selfAccount, eg.initialSnapshot());
51+
await testBinding.globalStore.add(eg.otherAccount, eg.initialSnapshot(
52+
realmUsers: [eg.otherUser]));
53+
check(testBinding.globalStore).lastVisitedAccount.equals(eg.otherAccount);
54+
await prepare(tester);
55+
56+
check(pushedRoutes).deepEquals(<Condition<Object?>>[
57+
(it) => it.isA<MaterialAccountWidgetRoute>()
58+
..accountId.equals(eg.otherAccount.id)
59+
..page.isA<HomePage>(),
60+
]);
61+
});
5262

53-
check(pushedRoutes).deepEquals(<Condition<Object?>>[
54-
(it) => it.isA<MaterialAccountWidgetRoute>()
55-
..accountId.equals(eg.selfAccount.id)
56-
..page.isA<HomePage>(),
57-
]);
63+
// testWidgets('when just upgraded to get the last-visited-account feature, '
64+
// 'if there is at least one account, open that one, not the choose-account page', (tester) async {
65+
// // The database migration step from9To10 ensures that
66+
// // [IntGlobalSetting.lastVisitedAccountId], thus [GlobalStore.lastVisitedAccount],
67+
// // points to the first account in the list. That migration step has
68+
// // its own tests in test/model/database_test.dart.
69+
// });
70+
71+
testWidgets('with last visited account logged out, go to choose account', (tester) async {
72+
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
73+
await testBinding.globalStore.add(
74+
eg.otherAccount, eg.initialSnapshot(realmUsers: [eg.otherUser]),
75+
markLastVisited: false);
76+
check(testBinding.globalStore).lastVisitedAccount.equals(eg.selfAccount);
77+
final future = logOutAccount(testBinding.globalStore, eg.selfAccount.id);
78+
await tester.pump(TestGlobalStore.removeAccountDuration);
79+
await future;
80+
check(testBinding.globalStore).lastVisitedAccount.isNull();
81+
await prepare(tester);
82+
83+
check(pushedRoutes).deepEquals(<Condition<Object?>>[
84+
(it) => it.isA<WidgetRoute>().page.isA<ChooseAccountPage>(),
85+
]);
86+
});
5887
});
5988
});
6089

@@ -99,6 +128,7 @@ void main() {
99128
testWidgets('push route when popping last route on stack', (tester) async {
100129
// Set up the loading of per-account data to fail.
101130
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
131+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
102132
testBinding.globalStore.loadPerAccountDuration = Duration.zero;
103133
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized();
104134
await prepare(tester);
@@ -133,6 +163,7 @@ void main() {
133163
const loadPerAccountDuration = Duration(seconds: 30);
134164
assert(loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135165
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
166+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
136167
testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137168
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized();
138169
await prepare(tester);
@@ -281,8 +312,9 @@ void main() {
281312
testWidgets('choosing an account clears the navigator stack', (tester) async {
282313
addTearDown(testBinding.reset);
283314
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
284-
await testBinding.globalStore.add(eg.otherAccount, eg.initialSnapshot(
285-
realmUsers: [eg.otherUser]));
315+
await testBinding.globalStore.add(
316+
eg.otherAccount, eg.initialSnapshot(realmUsers: [eg.otherUser]),
317+
markLastVisited: false);
286318

287319
final pushedRoutes = <Route<void>>[];
288320
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)