Skip to content

Commit 6ad54a0

Browse files
committed
app: Keep IntGlobalSetting.lastVisitedAccountId up-to-date
1 parent 28e1df5 commit 6ad54a0

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-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/model/settings.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ enum BoolGlobalSetting {
226226
enum IntGlobalSetting {
227227
/// A pseudo-setting recording the id of the account the user has visited most
228228
/// recently, from the list of all the available accounts on the device.
229+
///
230+
/// In some very rare cases, this may point to an account that actually
231+
/// doesn't exist on the device, for example, when the last visited account is
232+
/// logged out, but this field didn't get updated (set to null) for whatever
233+
/// reason. For cases like these, it's the responsibility of the callers to
234+
/// check for the availability of the account that relates to this id.
229235
lastVisitedAccountId,
230236

231237
// Former settings which might exist in the database,

lib/widgets/app.dart

Lines changed: 23 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,20 @@ class _PreventEmptyStack extends NavigatorObserver {
305307
}
306308
}
307309

310+
class _UpdateLastVisitedAccount extends NavigatorObserver {
311+
_UpdateLastVisitedAccount(this.globalSettings);
312+
313+
final GlobalSettingsStore globalSettings;
314+
315+
@override
316+
void didChangeTop(Route<void> topRoute, Route<void>? previousTopRoute) {
317+
final old = globalSettings.getInt(IntGlobalSetting.lastVisitedAccountId);
318+
if (topRoute case AccountPageRouteMixin(accountId: var new_) when new_ != old) {
319+
globalSettings.setInt(IntGlobalSetting.lastVisitedAccountId, new_);
320+
}
321+
}
322+
}
323+
308324
class ChooseAccountPage extends StatelessWidget {
309325
const ChooseAccountPage({super.key});
310326

@@ -337,7 +353,13 @@ class ChooseAccountPage extends StatelessWidget {
337353
if (await dialog.result == true) {
338354
if (!context.mounted) return;
339355
// TODO error handling if db write fails?
340-
unawaited(logOutAccount(GlobalStoreWidget.of(context), accountId));
356+
unawaited(Future(() async {
357+
if (!context.mounted) return;
358+
await logOutAccount(GlobalStoreWidget.of(context), accountId);
359+
if (!context.mounted) return;
360+
await removeLastVisitedAccountIfNecessary(
361+
GlobalStoreWidget.of(context), accountId);
362+
}));
341363
}
342364
},
343365
child: Text(zulipLocalizations.chooseAccountPageLogOutButton)),

0 commit comments

Comments
 (0)