@@ -53,8 +53,31 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
5353/// * [LiveGlobalStore] , the implementation of this class that
5454/// we use outside of tests.
5555abstract class GlobalStore extends ChangeNotifier {
56- GlobalStore ({required Iterable <Account > accounts})
57- : _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
56+ GlobalStore ({
57+ required GlobalSettingsData globalSettings,
58+ required Iterable <Account > accounts,
59+ })
60+ : _globalSettings = globalSettings,
61+ _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
62+
63+ /// A cache of the [GlobalSettingsData] singleton in the underlying data store.
64+ GlobalSettingsData get globalSettings => _globalSettings;
65+ GlobalSettingsData _globalSettings;
66+
67+ /// Update the global settings in the store, return the new version.
68+ ///
69+ /// The global settings must already exist in the store.
70+ Future <GlobalSettingsData > updateGlobalSettings (GlobalSettingsCompanion data) async {
71+ await doUpdateGlobalSettings (data);
72+ _globalSettings = _globalSettings.copyWithCompanion (data);
73+ notifyListeners ();
74+ return _globalSettings;
75+ }
76+
77+ /// Update the global settings in the underlying data store.
78+ ///
79+ /// This should only be called from [updateGlobalSettings] .
80+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data);
5881
5982 /// A cache of the [Accounts] table in the underlying data store.
6083 final Map <int , Account > _accounts;
@@ -799,6 +822,7 @@ Uri? tryResolveUrl(Uri baseUrl, String reference) {
799822class LiveGlobalStore extends GlobalStore {
800823 LiveGlobalStore ._({
801824 required AppDatabase db,
825+ required super .globalSettings,
802826 required super .accounts,
803827 }) : _db = db;
804828
@@ -815,8 +839,11 @@ class LiveGlobalStore extends GlobalStore {
815839 // by doing this loading up front before constructing a [GlobalStore].
816840 static Future <GlobalStore > load () async {
817841 final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
842+ final globalSettings = await db.ensureGlobalSettings ();
818843 final accounts = await db.select (db.accounts).get ();
819- return LiveGlobalStore ._(db: db, accounts: accounts);
844+ return LiveGlobalStore ._(db: db,
845+ globalSettings: globalSettings,
846+ accounts: accounts);
820847 }
821848
822849 /// The file path to use for the app database.
@@ -840,6 +867,12 @@ class LiveGlobalStore extends GlobalStore {
840867
841868 final AppDatabase _db;
842869
870+ @override
871+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data) async {
872+ final rowsAffected = await _db.update (_db.globalSettings).write (data);
873+ assert (rowsAffected == 1 );
874+ }
875+
843876 @override
844877 Future <PerAccountStore > doLoadPerAccount (int accountId) async {
845878 final updateMachine = await UpdateMachine .load (this , accountId);
0 commit comments