@@ -82,10 +82,12 @@ abstract class GlobalStore extends ChangeNotifier {
82
82
required GlobalSettingsData globalSettings,
83
83
required Map <BoolGlobalSetting , bool > boolGlobalSettings,
84
84
required Iterable <Account > accounts,
85
+ required int ? lastVisitedAccountId,
85
86
})
86
87
: settings = GlobalSettingsStore (backend: backend,
87
88
data: globalSettings, boolData: boolGlobalSettings),
88
- _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
89
+ _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a))),
90
+ _lastVisitedAccountId = lastVisitedAccountId;
89
91
90
92
/// The store for the user's account-independent settings.
91
93
///
@@ -98,6 +100,9 @@ abstract class GlobalStore extends ChangeNotifier {
98
100
/// A cache of the [Accounts] table in the underlying data store.
99
101
final Map <int , Account > _accounts;
100
102
103
+ int ? get lastVisitedAccountId => _lastVisitedAccountId;
104
+ int ? _lastVisitedAccountId;
105
+
101
106
// TODO push token, and other data corresponding to GlobalSessionState
102
107
103
108
/// Construct a new [ApiConnection] , real or fake as appropriate.
@@ -323,6 +328,33 @@ abstract class GlobalStore extends ChangeNotifier {
323
328
/// Remove an account from the underlying data store.
324
329
Future <void > doRemoveAccount (int accountId);
325
330
331
+ Future <void > insertLastVisitedAccount (int accountId) async {
332
+ assert (_lastVisitedAccountId == null );
333
+ await doInsertLastVisitedAccount (accountId);
334
+ _lastVisitedAccountId = accountId;
335
+ notifyListeners ();
336
+ }
337
+
338
+ Future <void > doInsertLastVisitedAccount (int accountId);
339
+
340
+ Future <void > updateLastVisitedAccount (int accountId) async {
341
+ assert (_lastVisitedAccountId != accountId);
342
+ await doUpdateLastVisitedAccount (accountId);
343
+ _lastVisitedAccountId = accountId;
344
+ notifyListeners ();
345
+ }
346
+
347
+ Future <void > doUpdateLastVisitedAccount (int accountId);
348
+
349
+ Future <void > deleteLastVisitedAccount () async {
350
+ assert (_lastVisitedAccountId != null );
351
+ await doDeleteLastVisitedAccount ();
352
+ _lastVisitedAccountId = null ;
353
+ notifyListeners ();
354
+ }
355
+
356
+ Future <void > doDeleteLastVisitedAccount ();
357
+
326
358
@override
327
359
String toString () => '${objectRuntimeType (this , 'GlobalStore' )}#${shortHash (this )}' ;
328
360
}
@@ -878,6 +910,7 @@ class LiveGlobalStore extends GlobalStore {
878
910
required super .globalSettings,
879
911
required super .boolGlobalSettings,
880
912
required super .accounts,
913
+ required super .lastVisitedAccountId,
881
914
}) : _backend = backend,
882
915
super (backend: backend);
883
916
@@ -909,19 +942,22 @@ class LiveGlobalStore extends GlobalStore {
909
942
final t3 = stopwatch.elapsed;
910
943
final accounts = await db.select (db.accounts).get ();
911
944
final t4 = stopwatch.elapsed;
945
+ final navigation = await db.getNavigation ();
946
+ final t5 = stopwatch.elapsed;
912
947
if (kProfileMode) {
913
948
String format (Duration d) =>
914
949
"${(d .inMicroseconds / 1000.0 ).toStringAsFixed (1 )}ms" ;
915
- profilePrint ("db load time ${format (t4 )} total: ${format (t1 )} init, "
950
+ profilePrint ("db load time ${format (t5 )} total: ${format (t1 )} init, "
916
951
"${format (t2 - t1 )} settings, ${format (t3 - t2 )} bool-settings, "
917
- "${format (t4 - t3 )} accounts" );
952
+ "${format (t4 - t3 )} accounts, ${ format ( t5 - t4 )} navigation " );
918
953
}
919
954
920
955
return LiveGlobalStore ._(
921
956
backend: LiveGlobalStoreBackend ._(db: db),
922
957
globalSettings: globalSettings,
923
958
boolGlobalSettings: boolGlobalSettings,
924
- accounts: accounts);
959
+ accounts: accounts,
960
+ lastVisitedAccountId: navigation? .accountId);
925
961
}
926
962
927
963
/// The file path to use for the app database.
@@ -986,6 +1022,29 @@ class LiveGlobalStore extends GlobalStore {
986
1022
assert (rowsAffected == 1 );
987
1023
}
988
1024
1025
+ @override
1026
+ Future <void > doInsertLastVisitedAccount (int accountId) async {
1027
+ final rowsAffected = await _db.into (_db.navigation)
1028
+ .insert (NavigationCompanion .insert (accountId: Value (accountId)));
1029
+ assert (rowsAffected == 1 );
1030
+ }
1031
+
1032
+ @override
1033
+ Future <void > doUpdateLastVisitedAccount (int accountId) async {
1034
+ final rowsAffected = await (_db.update (_db.navigation)
1035
+ ..where ((nav) => nav.accountId.equals (_lastVisitedAccountId! ))
1036
+ ).write (NavigationCompanion (accountId: Value (accountId)));
1037
+ assert (rowsAffected == 1 );
1038
+ }
1039
+
1040
+ @override
1041
+ Future <void > doDeleteLastVisitedAccount () async {
1042
+ final rowsAffected = await (_db.delete (_db.navigation)
1043
+ ..where ((nav) => nav.accountId.equals (_lastVisitedAccountId! ))
1044
+ ).go ();
1045
+ assert (rowsAffected == 1 );
1046
+ }
1047
+
989
1048
@override
990
1049
String toString () => '${objectRuntimeType (this , 'LiveGlobalStore' )}#${shortHash (this )}' ;
991
1050
}
0 commit comments