@@ -51,8 +51,31 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
51
51
/// * [LiveGlobalStore] , the implementation of this class that
52
52
/// we use outside of tests.
53
53
abstract 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);
56
79
57
80
/// A cache of the [Accounts] table in the underlying data store.
58
81
final Map <int , Account > _accounts;
@@ -756,6 +779,7 @@ Uri? tryResolveUrl(Uri baseUrl, String reference) {
756
779
class LiveGlobalStore extends GlobalStore {
757
780
LiveGlobalStore ._({
758
781
required AppDatabase db,
782
+ required super .globalSettings,
759
783
required super .accounts,
760
784
}) : _db = db;
761
785
@@ -772,8 +796,11 @@ class LiveGlobalStore extends GlobalStore {
772
796
// by doing this loading up front before constructing a [GlobalStore].
773
797
static Future <GlobalStore > load () async {
774
798
final db = AppDatabase (NativeDatabase .createInBackground (await _dbFile ()));
799
+ final globalSettings = await db.ensureGlobalSettings ();
775
800
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);
777
804
}
778
805
779
806
/// The file path to use for the app database.
@@ -800,6 +827,12 @@ class LiveGlobalStore extends GlobalStore {
800
827
801
828
final AppDatabase _db;
802
829
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
+
803
836
@override
804
837
Future <PerAccountStore > doLoadPerAccount (int accountId) async {
805
838
final updateMachine = await UpdateMachine .load (this , accountId);
0 commit comments