@@ -51,14 +51,18 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
5151/// * [LiveGlobalStore] , the implementation of this class that
5252/// we use outside of tests.
5353abstract 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;
5660
5761 /// A cache of the [Accounts] table in the underlying data store.
5862 final Map <int , Account > _accounts;
5963
6064 /// A cache of the [GlobalSettingsData] singleton in the underlying data store.
61- GlobalSettingsData ? _globalSettings;
65+ GlobalSettingsData _globalSettings;
6266
6367 // TODO settings (those that are per-device rather than per-account)
6468 // TODO push token, and other data corresponding to GlobalSessionState
@@ -226,33 +230,16 @@ abstract class GlobalStore extends ChangeNotifier {
226230 /// Remove an account from the underlying data store.
227231 Future <void > doRemoveAccount (int accountId);
228232
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;
246234
247235 /// Update the global settings in the store, return the new version.
248236 ///
249237 /// The global settings must already exist in the store.
250238 Future <GlobalSettingsData > updateGlobalSettings (GlobalSettingsCompanion data) async {
251- assert (_globalSettings != null );
252239 await doUpdateGlobalSettings (data);
253- _globalSettings = _globalSettings! .copyWithCompanion (data);
240+ _globalSettings = _globalSettings.copyWithCompanion (data);
254241 notifyListeners ();
255- return _globalSettings! ;
242+ return _globalSettings;
256243 }
257244
258245 /// Update the global settings in the underlying data store.
@@ -794,6 +781,7 @@ class LiveGlobalStore extends GlobalStore {
794781 LiveGlobalStore ._({
795782 required AppDatabase db,
796783 required super .accounts,
784+ required super .globalSettings,
797785 }) : _db = db;
798786
799787 @override
@@ -810,7 +798,10 @@ class LiveGlobalStore extends GlobalStore {
810798 static Future <GlobalStore > load () async {
811799 final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
812800 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);
814805 }
815806
816807 /// The file path to use for the app database.
@@ -872,12 +863,6 @@ class LiveGlobalStore extends GlobalStore {
872863 assert (rowsAffected == 1 );
873864 }
874865
875- @override
876- Future <GlobalSettingsData > loadGlobalSettings () async {
877- _globalSettings = await _db.ensureGlobalSettings ();
878- return _globalSettings! ;
879- }
880-
881866 @override
882867 Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data) async {
883868 final rowsAffected = await _db.update (_db.globalSettings).write (data);
0 commit comments