@@ -51,12 +51,19 @@ 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
64+ /// A cache of the [GlobalSettingsData] singleton in the underlying data store.
65+ GlobalSettingsData _globalSettings;
66+
6067 // TODO settings (those that are per-device rather than per-account)
6168 // TODO push token, and other data corresponding to GlobalSessionState
6269
@@ -223,6 +230,23 @@ abstract class GlobalStore extends ChangeNotifier {
223230 /// Remove an account from the underlying data store.
224231 Future <void > doRemoveAccount (int accountId);
225232
233+ GlobalSettingsData get globalSettings => _globalSettings;
234+
235+ /// Update the global settings in the store, return the new version.
236+ ///
237+ /// The global settings must already exist in the store.
238+ Future <GlobalSettingsData > updateGlobalSettings (GlobalSettingsCompanion data) async {
239+ await doUpdateGlobalSettings (data);
240+ _globalSettings = _globalSettings.copyWithCompanion (data);
241+ notifyListeners ();
242+ return _globalSettings;
243+ }
244+
245+ /// Update the global settings in the underlying data store.
246+ ///
247+ /// This should only be called from [updateGlobalSettings] .
248+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data);
249+
226250 @override
227251 String toString () => '${objectRuntimeType (this , 'GlobalStore' )}#${shortHash (this )}' ;
228252}
@@ -757,6 +781,7 @@ class LiveGlobalStore extends GlobalStore {
757781 LiveGlobalStore ._({
758782 required AppDatabase db,
759783 required super .accounts,
784+ required super .globalSettings,
760785 }) : _db = db;
761786
762787 @override
@@ -773,7 +798,10 @@ class LiveGlobalStore extends GlobalStore {
773798 static Future <GlobalStore > load () async {
774799 final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
775800 final accounts = await db.select (db.accounts).get ();
776- return LiveGlobalStore ._(db: db, accounts: accounts);
801+ final globalSettings = await db.ensureGlobalSettings ();
802+ return LiveGlobalStore ._(db: db,
803+ accounts: accounts,
804+ globalSettings: globalSettings);
777805 }
778806
779807 /// The file path to use for the app database.
@@ -835,6 +863,12 @@ class LiveGlobalStore extends GlobalStore {
835863 assert (rowsAffected == 1 );
836864 }
837865
866+ @override
867+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data) async {
868+ final rowsAffected = await _db.update (_db.globalSettings).write (data);
869+ assert (rowsAffected == 1 );
870+ }
871+
838872 @override
839873 String toString () => '${objectRuntimeType (this , 'LiveGlobalStore' )}#${shortHash (this )}' ;
840874}
0 commit comments