@@ -52,8 +52,31 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
52
52
/// * [LiveGlobalStore] , the implementation of this class that
53
53
/// we use outside of tests.
54
54
abstract class GlobalStore extends ChangeNotifier {
55
- GlobalStore ({required Iterable <Account > accounts})
56
- : _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
55
+ GlobalStore ({
56
+ required GlobalSettingsData globalSettings,
57
+ required Iterable <Account > accounts,
58
+ })
59
+ : _globalSettings = globalSettings,
60
+ _accounts = Map .fromEntries (accounts.map ((a) => MapEntry (a.id, a)));
61
+
62
+ /// A cache of the [GlobalSettingsData] singleton in the underlying data store.
63
+ GlobalSettingsData get globalSettings => _globalSettings;
64
+ GlobalSettingsData _globalSettings;
65
+
66
+ /// Update the global settings in the store, return the new version.
67
+ ///
68
+ /// The global settings must already exist in the store.
69
+ Future <GlobalSettingsData > updateGlobalSettings (GlobalSettingsCompanion data) async {
70
+ await doUpdateGlobalSettings (data);
71
+ _globalSettings = _globalSettings.copyWithCompanion (data);
72
+ notifyListeners ();
73
+ return _globalSettings;
74
+ }
75
+
76
+ /// Update the global settings in the underlying data store.
77
+ ///
78
+ /// This should only be called from [updateGlobalSettings] .
79
+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data);
57
80
58
81
/// A cache of the [Accounts] table in the underlying data store.
59
82
final Map <int , Account > _accounts;
@@ -791,6 +814,7 @@ Uri? tryResolveUrl(Uri baseUrl, String reference) {
791
814
class LiveGlobalStore extends GlobalStore {
792
815
LiveGlobalStore ._({
793
816
required AppDatabase db,
817
+ required super .globalSettings,
794
818
required super .accounts,
795
819
}) : _db = db;
796
820
@@ -807,8 +831,11 @@ class LiveGlobalStore extends GlobalStore {
807
831
// by doing this loading up front before constructing a [GlobalStore].
808
832
static Future <GlobalStore > load () async {
809
833
final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
834
+ final globalSettings = await db.ensureGlobalSettings ();
810
835
final accounts = await db.select (db.accounts).get ();
811
- return LiveGlobalStore ._(db: db, accounts: accounts);
836
+ return LiveGlobalStore ._(db: db,
837
+ globalSettings: globalSettings,
838
+ accounts: accounts);
812
839
}
813
840
814
841
/// The file path to use for the app database.
@@ -832,6 +859,12 @@ class LiveGlobalStore extends GlobalStore {
832
859
833
860
final AppDatabase _db;
834
861
862
+ @override
863
+ Future <void > doUpdateGlobalSettings (GlobalSettingsCompanion data) async {
864
+ final rowsAffected = await _db.update (_db.globalSettings).write (data);
865
+ assert (rowsAffected == 1 );
866
+ }
867
+
835
868
@override
836
869
Future <PerAccountStore > doLoadPerAccount (int accountId) async {
837
870
final updateMachine = await UpdateMachine .load (this , accountId);
0 commit comments