@@ -51,14 +51,18 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
51
51
/// * [LiveGlobalStore] , the implementation of this class that
52
52
/// we use outside of tests.
53
53
abstract class GlobalStore extends ChangeNotifier {
54
- GlobalStore ({required Iterable <Account > accounts})
55
- : _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
54
+ GlobalStore ({
55
+ required Iterable <Account > accounts,
56
+ required GlobalSettingsData globalSettings,
57
+ })
58
+ : _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a))),
59
+ _globalSettings = globalSettings;
56
60
57
61
/// A cache of the [Accounts] table in the underlying data store.
58
62
final Map <int , Account > _accounts;
59
63
60
64
/// A cache of the [GlobalSettingsData] singleton in the underlying data store.
61
- GlobalSettingsData ? _globalSettings;
65
+ GlobalSettingsData _globalSettings;
62
66
63
67
// TODO settings (those that are per-device rather than per-account)
64
68
// TODO push token, and other data corresponding to GlobalSessionState
@@ -226,33 +230,16 @@ abstract class GlobalStore extends ChangeNotifier {
226
230
/// Remove an account from the underlying data store.
227
231
Future <void > doRemoveAccount (int accountId);
228
232
229
- GlobalSettingsData ? get globalSettingsSync => _globalSettings;
230
-
231
- /// Get global settings from the store.
232
- ///
233
- /// Use the cache if already loaded.
234
- /// Otherwise, load it from the underlying store.
235
- ///
236
- /// Consider checking [globalSettingsSync] before using this.
237
- Future <GlobalSettingsData > getGlobalSettings () async {
238
- if (globalSettingsSync != null ) return globalSettingsSync! ;
239
- return await loadGlobalSettings ();
240
- }
241
-
242
- /// Load global settings from the underlying data store, unconditionally.
243
- ///
244
- /// This should only be called from [getGlobalSettings] .
245
- Future <GlobalSettingsData > loadGlobalSettings ();
233
+ GlobalSettingsData get globalSettings => _globalSettings;
246
234
247
235
/// Update the global settings in the store, return the new version.
248
236
///
249
237
/// The global settings must already exist in the store.
250
238
Future <GlobalSettingsData > updateGlobalSettings (GlobalSettingsCompanion data) async {
251
- assert (_globalSettings != null );
252
239
await doUpdateGlobalSettings (data);
253
- _globalSettings = _globalSettings! .copyWithCompanion (data);
240
+ _globalSettings = _globalSettings.copyWithCompanion (data);
254
241
notifyListeners ();
255
- return _globalSettings! ;
242
+ return _globalSettings;
256
243
}
257
244
258
245
/// Update the global settings in the underlying data store.
@@ -794,6 +781,7 @@ class LiveGlobalStore extends GlobalStore {
794
781
LiveGlobalStore ._({
795
782
required AppDatabase db,
796
783
required super .accounts,
784
+ required super .globalSettings,
797
785
}) : _db = db;
798
786
799
787
@override
@@ -810,7 +798,10 @@ class LiveGlobalStore extends GlobalStore {
810
798
static Future <GlobalStore > load () async {
811
799
final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
812
800
final accounts = await db.select (db.accounts).get ();
813
- return LiveGlobalStore ._(db: db, accounts: accounts);
801
+ final globalSettings = await db.ensureGlobalSettings ();
802
+ return LiveGlobalStore ._(db: db,
803
+ accounts: accounts,
804
+ globalSettings: globalSettings);
814
805
}
815
806
816
807
/// The file path to use for the app database.
@@ -872,12 +863,6 @@ class LiveGlobalStore extends GlobalStore {
872
863
assert (rowsAffected == 1 );
873
864
}
874
865
875
- @override
876
- Future <GlobalSettingsData > loadGlobalSettings () async {
877
- _globalSettings = await _db.ensureGlobalSettings ();
878
- return _globalSettings! ;
879
- }
880
-
881
866
@override
882
867
Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data) async {
883
868
final rowsAffected = await _db.update (_db.globalSettings).write (data);
0 commit comments