@@ -51,8 +51,31 @@ 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 GlobalSettingsData globalSettings,
56+ required Iterable <Account > accounts,
57+ })
58+ : _globalSettings = globalSettings,
59+ _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
60+
61+ /// A cache of the [GlobalSettingsData] singleton in the underlying data store.
62+ GlobalSettingsData get globalSettings => _globalSettings;
63+ GlobalSettingsData _globalSettings;
64+
65+ /// Update the global settings in the store, return the new version.
66+ ///
67+ /// The global settings must already exist in the store.
68+ Future <GlobalSettingsData > updateGlobalSettings (GlobalSettingsCompanion data) async {
69+ await doUpdateGlobalSettings (data);
70+ _globalSettings = _globalSettings.copyWithCompanion (data);
71+ notifyListeners ();
72+ return _globalSettings;
73+ }
74+
75+ /// Update the global settings in the underlying data store.
76+ ///
77+ /// This should only be called from [updateGlobalSettings] .
78+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data);
5679
5780 /// A cache of the [Accounts] table in the underlying data store.
5881 final Map <int , Account > _accounts;
@@ -756,6 +779,7 @@ Uri? tryResolveUrl(Uri baseUrl, String reference) {
756779class LiveGlobalStore extends GlobalStore {
757780 LiveGlobalStore ._({
758781 required AppDatabase db,
782+ required super .globalSettings,
759783 required super .accounts,
760784 }) : _db = db;
761785
@@ -772,8 +796,11 @@ class LiveGlobalStore extends GlobalStore {
772796 // by doing this loading up front before constructing a [GlobalStore].
773797 static Future <GlobalStore > load () async {
774798 final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
799+ final globalSettings = await db.ensureGlobalSettings ();
775800 final accounts = await db.select (db.accounts).get ();
776- return LiveGlobalStore ._(db: db, accounts: accounts);
801+ return LiveGlobalStore ._(db: db,
802+ globalSettings: globalSettings,
803+ accounts: accounts);
777804 }
778805
779806 /// The file path to use for the app database.
@@ -800,6 +827,12 @@ class LiveGlobalStore extends GlobalStore {
800827
801828 final AppDatabase _db;
802829
830+ @override
831+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data) async {
832+ final rowsAffected = await _db.update (_db.globalSettings).write (data);
833+ assert (rowsAffected == 1 );
834+ }
835+
803836 @override
804837 Future <PerAccountStore > doLoadPerAccount (int accountId) async {
805838 final updateMachine = await UpdateMachine .load (this , accountId);
0 commit comments