Skip to content

Commit 9897cff

Browse files
committed
store: Store global settings on global store
Signed-off-by: Zixuan James Li <[email protected]>
1 parent 3ef3507 commit 9897cff

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

lib/model/settings.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ enum ThemeSetting {
1111
/// Corresponds to [Brightness.dark].
1212
dark,
1313
}
14-

lib/model/store.dart

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
5555
abstract 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) {
799822
class 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);

test/example_data.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,16 @@ GlobalSettingsData globalSettings({
885885
themeSetting: themeSetting,
886886
);
887887
}
888+
const _globalSettings = globalSettings;
888889

889-
TestGlobalStore globalStore({List<Account> accounts = const []}) {
890-
return TestGlobalStore(accounts: accounts);
890+
TestGlobalStore globalStore({
891+
GlobalSettingsData? globalSettings,
892+
List<Account> accounts = const [],
893+
}) {
894+
return TestGlobalStore(
895+
globalSettings: globalSettings ?? _globalSettings(),
896+
accounts: accounts,
897+
);
891898
}
892899
const _globalStore = globalStore;
893900

test/model/store_checks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extension GlobalStoreChecks on Subject<GlobalStore> {
2626
Subject<Iterable<int>> get accountIds => has((x) => x.accountIds, 'accountIds');
2727
Subject<Iterable<({ int accountId, Account account })>> get accountEntries => has((x) => x.accountEntries, 'accountEntries');
2828
Subject<Account?> getAccount(int id) => has((x) => x.getAccount(id), 'getAccount($id)');
29+
Subject<GlobalSettingsData> get globalSettings => has((x) => x.globalSettings, 'globalSettings');
2930
}
3031

3132
extension PerAccountStoreChecks on Subject<PerAccountStore> {

test/model/store_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,9 @@ void main() {
11251125
}
11261126

11271127
class LoadingTestGlobalStore extends TestGlobalStore {
1128-
LoadingTestGlobalStore({required super.accounts});
1128+
LoadingTestGlobalStore({
1129+
required super.accounts,
1130+
}) : super(globalSettings: eg.globalSettings());
11291131

11301132
Map<int, List<Completer<PerAccountStore>>> completers = {};
11311133

test/model/test_store.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:zulip/api/model/events.dart';
22
import 'package:zulip/api/model/initial_snapshot.dart';
33
import 'package:zulip/api/model/model.dart';
4+
import 'package:zulip/model/database.dart';
45
import 'package:zulip/model/store.dart';
56
import 'package:zulip/widgets/store.dart';
67

@@ -22,7 +23,15 @@ import '../example_data.dart' as eg;
2223
///
2324
/// See also [TestZulipBinding.globalStore], which provides one of these.
2425
class TestGlobalStore extends GlobalStore {
25-
TestGlobalStore({required super.accounts});
26+
TestGlobalStore({required super.globalSettings, required super.accounts})
27+
: _globalSettings = globalSettings;
28+
29+
GlobalSettingsData? _globalSettings;
30+
31+
@override
32+
Future<void> doUpdateGlobalSettings(GlobalSettingsCompanion data) async {
33+
_globalSettings = _globalSettings!.copyWithCompanion(data);
34+
}
2635

2736
final Map<
2837
({Uri realmUrl, int? zulipFeatureLevel, String? email, String? apiKey}),

0 commit comments

Comments
 (0)