Skip to content

Commit 059922a

Browse files
committed
app: Keep IntGlobalSetting.lastVisitedAccountId up-to-date
1 parent 882102d commit 059922a

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/model/actions.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
44

55
import '../notifications/display.dart';
66
import '../notifications/receive.dart';
7+
import 'settings.dart';
78
import 'store.dart';
89

910
// TODO: Make this a part of GlobalStore
@@ -21,6 +22,18 @@ Future<void> logOutAccount(GlobalStore globalStore, int accountId) async {
2122
await globalStore.removeAccount(accountId);
2223
}
2324

25+
Future<void> removeLastVisitedAccountIfNecessary(GlobalStore store, int loggedOutAccountId) async {
26+
// If account is not logged out yet, do nothing.
27+
if (store.getAccount(loggedOutAccountId) != null) return;
28+
29+
// If the logged-out account is different than the last visited one, do nothing.
30+
if (loggedOutAccountId != store.settings.getInt(IntGlobalSetting.lastVisitedAccountId)) {
31+
return;
32+
}
33+
34+
await store.settings.setInt(IntGlobalSetting.lastVisitedAccountId, null);
35+
}
36+
2437
Future<void> unregisterToken(GlobalStore globalStore, int accountId) async {
2538
final account = globalStore.getAccount(accountId);
2639
if (account == null) return; // TODO(log)

lib/widgets/app.dart

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import '../generated/l10n/zulip_localizations.dart';
88
import '../log.dart';
99
import '../model/actions.dart';
1010
import '../model/localizations.dart';
11+
import '../model/settings.dart';
1112
import '../model/store.dart';
1213
import '../notifications/open.dart';
1314
import 'about_zulip.dart';
@@ -254,6 +255,7 @@ class _ZulipAppState extends State<ZulipApp> with WidgetsBindingObserver {
254255
if (widget.navigatorObservers != null)
255256
...widget.navigatorObservers!,
256257
_PreventEmptyStack(),
258+
_UpdateLastVisitedAccount(GlobalStoreWidget.settingsOf(context)),
257259
],
258260
builder: (BuildContext context, Widget? child) {
259261
if (!ZulipApp.ready.value) {
@@ -305,6 +307,39 @@ class _PreventEmptyStack extends NavigatorObserver {
305307
}
306308
}
307309

310+
class _UpdateLastVisitedAccount extends NavigatorObserver {
311+
_UpdateLastVisitedAccount(this.globalSettings);
312+
313+
final GlobalSettingsStore globalSettings;
314+
315+
void _changeLastVisitedAccountIfNecessary(Route<dynamic>? route) {
316+
final old = globalSettings.getInt(IntGlobalSetting.lastVisitedAccountId);
317+
if (route case AccountPageRouteMixin(accountId: var new_) when new_ != old) {
318+
globalSettings.setInt(IntGlobalSetting.lastVisitedAccountId, new_);
319+
}
320+
}
321+
322+
@override
323+
void didPush(Route<void> route, Route<void>? previousRoute) {
324+
_changeLastVisitedAccountIfNecessary(route);
325+
}
326+
327+
@override
328+
void didPop(Route<void> route, Route<void>? previousRoute) {
329+
_changeLastVisitedAccountIfNecessary(previousRoute);
330+
}
331+
332+
@override
333+
void didRemove(Route<void> route, Route<void>? previousRoute) {
334+
_changeLastVisitedAccountIfNecessary(previousRoute);
335+
}
336+
337+
@override
338+
void didReplace({Route<void>? newRoute, Route<void>? oldRoute}) {
339+
_changeLastVisitedAccountIfNecessary(newRoute);
340+
}
341+
}
342+
308343
class ChooseAccountPage extends StatelessWidget {
309344
const ChooseAccountPage({super.key});
310345

@@ -337,7 +372,13 @@ class ChooseAccountPage extends StatelessWidget {
337372
if (await dialog.result == true) {
338373
if (!context.mounted) return;
339374
// TODO error handling if db write fails?
340-
unawaited(logOutAccount(GlobalStoreWidget.of(context), accountId));
375+
unawaited(Future(() async {
376+
if (!context.mounted) return;
377+
await logOutAccount(GlobalStoreWidget.of(context), accountId);
378+
if (!context.mounted) return;
379+
await removeLastVisitedAccountIfNecessary(
380+
GlobalStoreWidget.of(context), accountId);
381+
}));
341382
}
342383
},
343384
child: Text(zulipLocalizations.chooseAccountPageLogOutButton)),

0 commit comments

Comments
 (0)