Skip to content

Commit dcd0abf

Browse files
committed
store: Add GlobalStore.lastVisitedAccountId
1 parent 294d842 commit dcd0abf

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

lib/model/store.dart

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ abstract class GlobalStore extends ChangeNotifier {
8282
required GlobalSettingsData globalSettings,
8383
required Map<BoolGlobalSetting, bool> boolGlobalSettings,
8484
required Iterable<Account> accounts,
85+
required int? lastVisitedAccountId,
8586
})
8687
: settings = GlobalSettingsStore(backend: backend,
8788
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;
8991

9092
/// The store for the user's account-independent settings.
9193
///
@@ -98,6 +100,9 @@ abstract class GlobalStore extends ChangeNotifier {
98100
/// A cache of the [Accounts] table in the underlying data store.
99101
final Map<int, Account> _accounts;
100102

103+
int? get lastVisitedAccountId => _lastVisitedAccountId;
104+
int? _lastVisitedAccountId;
105+
101106
// TODO push token, and other data corresponding to GlobalSessionState
102107

103108
/// Construct a new [ApiConnection], real or fake as appropriate.
@@ -323,6 +328,33 @@ abstract class GlobalStore extends ChangeNotifier {
323328
/// Remove an account from the underlying data store.
324329
Future<void> doRemoveAccount(int accountId);
325330

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+
326358
@override
327359
String toString() => '${objectRuntimeType(this, 'GlobalStore')}#${shortHash(this)}';
328360
}
@@ -878,6 +910,7 @@ class LiveGlobalStore extends GlobalStore {
878910
required super.globalSettings,
879911
required super.boolGlobalSettings,
880912
required super.accounts,
913+
required super.lastVisitedAccountId,
881914
}) : _backend = backend,
882915
super(backend: backend);
883916

@@ -909,19 +942,22 @@ class LiveGlobalStore extends GlobalStore {
909942
final t3 = stopwatch.elapsed;
910943
final accounts = await db.select(db.accounts).get();
911944
final t4 = stopwatch.elapsed;
945+
final navigation = await db.getNavigation();
946+
final t5 = stopwatch.elapsed;
912947
if (kProfileMode) {
913948
String format(Duration d) =>
914949
"${(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, "
916951
"${format(t2 - t1)} settings, ${format(t3 - t2)} bool-settings, "
917-
"${format(t4 - t3)} accounts");
952+
"${format(t4 - t3)} accounts, ${format(t5 - t4)} navigation");
918953
}
919954

920955
return LiveGlobalStore._(
921956
backend: LiveGlobalStoreBackend._(db: db),
922957
globalSettings: globalSettings,
923958
boolGlobalSettings: boolGlobalSettings,
924-
accounts: accounts);
959+
accounts: accounts,
960+
lastVisitedAccountId: navigation?.accountId);
925961
}
926962

927963
/// The file path to use for the app database.
@@ -986,6 +1022,29 @@ class LiveGlobalStore extends GlobalStore {
9861022
assert(rowsAffected == 1);
9871023
}
9881024

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+
9891048
@override
9901049
String toString() => '${objectRuntimeType(this, 'LiveGlobalStore')}#${shortHash(this)}';
9911050
}

test/example_data.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,11 +1183,13 @@ TestGlobalStore globalStore({
11831183
GlobalSettingsData? globalSettings,
11841184
Map<BoolGlobalSetting, bool>? boolGlobalSettings,
11851185
List<Account> accounts = const [],
1186+
int? lastVisitedAccountId,
11861187
}) {
11871188
return TestGlobalStore(
11881189
globalSettings: globalSettings,
11891190
boolGlobalSettings: boolGlobalSettings,
11901191
accounts: accounts,
1192+
lastVisitedAccountId: lastVisitedAccountId
11911193
);
11921194
}
11931195
const _globalStore = globalStore;

test/model/test_store.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class TestGlobalStore extends GlobalStore with _ApiConnectionsMixin, _DatabaseMi
147147
GlobalSettingsData? globalSettings,
148148
Map<BoolGlobalSetting, bool>? boolGlobalSettings,
149149
required super.accounts,
150+
super.lastVisitedAccountId,
150151
}) : super(backend: _TestGlobalStoreBackend(),
151152
globalSettings: globalSettings ?? GlobalSettingsData(),
152153
boolGlobalSettings: boolGlobalSettings ?? {},
@@ -215,6 +216,7 @@ class UpdateMachineTestGlobalStore extends GlobalStore with _ApiConnectionsMixin
215216
GlobalSettingsData? globalSettings,
216217
Map<BoolGlobalSetting, bool>? boolGlobalSettings,
217218
required super.accounts,
219+
super.lastVisitedAccountId,
218220
}) : super(backend: _TestGlobalStoreBackend(),
219221
globalSettings: globalSettings ?? GlobalSettingsData(),
220222
boolGlobalSettings: boolGlobalSettings ?? {},

0 commit comments

Comments
 (0)