Skip to content

Commit 42391e0

Browse files
committed
store: Add insertAccount and more account getters
For accountEntries, we make use of Dart 3's fancy new "record" types to keep the API simple to use.
1 parent 1dfd957 commit 42391e0

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/model/store.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,32 @@ abstract class GlobalStore extends ChangeNotifier {
9595
/// and/or [perAccountSync].
9696
Future<PerAccountStore> loadPerAccount(Account account);
9797

98-
// Just an Iterable, not the actual Map, to avoid clients mutating the map.
98+
// Just the Iterables, not the actual Map, to avoid clients mutating the map.
9999
// Mutations should go through the setters/mutators below.
100100
Iterable<Account> get accounts => _accounts.values;
101+
Iterable<int> get accountIds => _accounts.keys;
102+
Iterable<({ int accountId, Account account })> get accountEntries {
103+
return _accounts.entries.map((entry) {
104+
return (accountId: entry.key, account: entry.value);
105+
});
106+
}
101107

102108
Account? getAccount(int id) => _accounts[id];
103109

104-
// TODO add setters/mutators; will want to write to database
105-
// Future<void> insertAccount...
110+
// TODO(#13): rewrite these setters/mutators with a database
111+
112+
int _nextAccountId = 1;
113+
114+
/// Add an account to the store, returning its assigned account ID.
115+
Future<int> insertAccount(Account account) async {
116+
final accountId = _nextAccountId;
117+
_nextAccountId++;
118+
assert(!_accounts.containsKey(accountId));
119+
_accounts[accountId] = account;
120+
return accountId;
121+
}
122+
123+
// More mutators as needed:
106124
// Future<void> updateAccount...
107125
}
108126

@@ -205,7 +223,7 @@ class LiveGlobalStore extends GlobalStore {
205223
// We keep the API simple and synchronous for the bulk of the app's code
206224
// by doing this loading up front before constructing a [GlobalStore].
207225
static Future<GlobalStore> load() async {
208-
const accounts = {fixtureAccountId: _fixtureAccount};
226+
final accounts = {fixtureAccountId: _fixtureAccount};
209227
return LiveGlobalStore._(accounts: accounts);
210228
}
211229

0 commit comments

Comments
 (0)