Skip to content

Commit 35f423c

Browse files
committed
app: Keep GlobalStore.lastVisitedAccountId updated
1 parent dcd0abf commit 35f423c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/model/actions.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ Future<void> logOutAccount(GlobalStore globalStore, int accountId) async {
2121
await globalStore.removeAccount(accountId);
2222
}
2323

24+
Future<void> removeLastVisitedAccountIfNecessary(GlobalStore store, int loggedOutAccountId) async {
25+
// If account is not logged out yet, do nothing.
26+
if (store.getAccount(loggedOutAccountId) != null) return;
27+
28+
// If the logged-out account is different than the last visited one, do nothing.
29+
if (loggedOutAccountId != store.lastVisitedAccountId) return;
30+
31+
await store.deleteLastVisitedAccount();
32+
}
33+
2434
Future<void> unregisterToken(GlobalStore globalStore, int accountId) async {
2535
final account = globalStore.getAccount(accountId);
2636
if (account == null) return; // TODO(log)

lib/widgets/app.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ class _ZulipAppState extends State<ZulipApp> with WidgetsBindingObserver {
254254
if (widget.navigatorObservers != null)
255255
...widget.navigatorObservers!,
256256
_PreventEmptyStack(),
257+
_UpdateLastVisitedAccount(GlobalStoreWidget.of(context)),
257258
],
258259
builder: (BuildContext context, Widget? child) {
259260
if (!ZulipApp.ready.value) {
@@ -305,6 +306,44 @@ class _PreventEmptyStack extends NavigatorObserver {
305306
}
306307
}
307308

309+
class _UpdateLastVisitedAccount extends NavigatorObserver {
310+
_UpdateLastVisitedAccount(this.globalStore);
311+
312+
final GlobalStore globalStore;
313+
314+
void _changeLastVisitedAccountIfNecessary(Route<dynamic>? route) {
315+
if (route case AccountPageRouteMixin(accountId: var new_)) {
316+
final old = globalStore.lastVisitedAccountId;
317+
switch ((old, new_)) {
318+
case (null, int _):
319+
globalStore.insertLastVisitedAccount(new_);
320+
case (int _, int _) when old != new_:
321+
globalStore.updateLastVisitedAccount(new_);
322+
}
323+
}
324+
}
325+
326+
@override
327+
void didPush(Route<void> route, Route<void>? previousRoute) {
328+
_changeLastVisitedAccountIfNecessary(route);
329+
}
330+
331+
@override
332+
void didPop(Route<void> route, Route<void>? previousRoute) {
333+
_changeLastVisitedAccountIfNecessary(previousRoute);
334+
}
335+
336+
@override
337+
void didRemove(Route<void> route, Route<void>? previousRoute) {
338+
_changeLastVisitedAccountIfNecessary(previousRoute);
339+
}
340+
341+
@override
342+
void didReplace({Route<void>? newRoute, Route<void>? oldRoute}) {
343+
_changeLastVisitedAccountIfNecessary(newRoute);
344+
}
345+
}
346+
308347
class ChooseAccountPage extends StatelessWidget {
309348
const ChooseAccountPage({super.key});
310349

@@ -337,7 +376,12 @@ class ChooseAccountPage extends StatelessWidget {
337376
if (await dialog.result == true) {
338377
if (!context.mounted) return;
339378
// TODO error handling if db write fails?
340-
unawaited(logOutAccount(GlobalStoreWidget.of(context), accountId));
379+
unawaited(Future(() async {
380+
if (!context.mounted) return;
381+
await logOutAccount(GlobalStoreWidget.of(context), accountId);
382+
if (!context.mounted) return;
383+
await removeLastVisitedAccountIfNecessary(GlobalStoreWidget.of(context), accountId);
384+
}));
341385
}
342386
},
343387
child: Text(zulipLocalizations.chooseAccountPageLogOutButton)),

0 commit comments

Comments
 (0)