Skip to content

Commit 3bb9ef1

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

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

lib/model/store.dart

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,31 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
5252
/// * [LiveGlobalStore], the implementation of this class that
5353
/// we use outside of tests.
5454
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);
5780

5881
/// A cache of the [Accounts] table in the underlying data store.
5982
final Map<int, Account> _accounts;
@@ -791,6 +814,7 @@ Uri? tryResolveUrl(Uri baseUrl, String reference) {
791814
class LiveGlobalStore extends GlobalStore {
792815
LiveGlobalStore._({
793816
required AppDatabase db,
817+
required super.globalSettings,
794818
required super.accounts,
795819
}) : _db = db;
796820

@@ -807,8 +831,11 @@ class LiveGlobalStore extends GlobalStore {
807831
// by doing this loading up front before constructing a [GlobalStore].
808832
static Future<GlobalStore> load() async {
809833
final db = AppDatabase(NativeDatabase.createInBackground(await _dbFile()));
834+
final globalSettings = await db.ensureGlobalSettings();
810835
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);
812839
}
813840

814841
/// The file path to use for the app database.
@@ -832,6 +859,12 @@ class LiveGlobalStore extends GlobalStore {
832859

833860
final AppDatabase _db;
834861

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+
835868
@override
836869
Future<PerAccountStore> doLoadPerAccount(int accountId) async {
837870
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_test.dart

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

11551155
class LoadingTestGlobalStore extends TestGlobalStore {
1156-
LoadingTestGlobalStore({required super.accounts});
1156+
LoadingTestGlobalStore({
1157+
required super.accounts,
1158+
}) : super(globalSettings: eg.globalSettings());
11571159

11581160
Map<int, List<Completer<PerAccountStore>>> completers = {};
11591161

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)