Skip to content

Commit d71db6a

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 25be20e commit d71db6a

File tree

5 files changed

+61
-28
lines changed

5 files changed

+61
-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: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,52 @@ 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('with no account visited, go to home page for the first account', (tester) async {
64+
// // The database migration step from10To11 ensures that
65+
// // [IntGlobalSetting.lastVisitedAccountId], thus [GlobalStore.lastVisitedAccount],
66+
// // points to the first account in the list. That migration step has its
67+
// // own tests in test/model/database_test.dart.
68+
// });
69+
70+
testWidgets('with last visited account logged out, go to choose account', (tester) async {
71+
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
72+
await testBinding.globalStore.add(
73+
eg.otherAccount, eg.initialSnapshot(realmUsers: [eg.otherUser]),
74+
markLastVisited: false);
75+
check(testBinding.globalStore).lastVisitedAccount.equals(eg.selfAccount);
76+
final future = logOutAccount(testBinding.globalStore, eg.selfAccount.id);
77+
await tester.pump(TestGlobalStore.removeAccountDuration);
78+
await future;
79+
check(testBinding.globalStore).lastVisitedAccount.isNull();
80+
await prepare(tester);
81+
82+
check(pushedRoutes).deepEquals(<Condition<Object?>>[
83+
(it) => it.isA<WidgetRoute>().page.isA<ChooseAccountPage>(),
84+
]);
85+
});
5886
});
5987
});
6088

@@ -99,6 +127,7 @@ void main() {
99127
testWidgets('push route when popping last route on stack', (tester) async {
100128
// Set up the loading of per-account data to fail.
101129
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
130+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
102131
testBinding.globalStore.loadPerAccountDuration = Duration.zero;
103132
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized();
104133
await prepare(tester);
@@ -133,6 +162,7 @@ void main() {
133162
const loadPerAccountDuration = Duration(seconds: 30);
134163
assert(loadPerAccountDuration > kTryAnotherAccountWaitPeriod);
135164
await testBinding.globalStore.insertAccount(eg.selfAccount.toCompanion(false));
165+
await testBinding.globalStore.setLastVisitedAccount(eg.selfAccount.id);
136166
testBinding.globalStore.loadPerAccountDuration = loadPerAccountDuration;
137167
testBinding.globalStore.loadPerAccountException = eg.apiExceptionUnauthorized();
138168
await prepare(tester);
@@ -281,8 +311,9 @@ void main() {
281311
testWidgets('choosing an account clears the navigator stack', (tester) async {
282312
addTearDown(testBinding.reset);
283313
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
284-
await testBinding.globalStore.add(eg.otherAccount, eg.initialSnapshot(
285-
realmUsers: [eg.otherUser]));
314+
await testBinding.globalStore.add(
315+
eg.otherAccount, eg.initialSnapshot(realmUsers: [eg.otherUser]),
316+
markLastVisited: false);
286317

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