Skip to content

Commit 37d5f2f

Browse files
committed
fixup! wip; Simplify global store by loading it only once
Signed-off-by: Zixuan James Li <[email protected]>
1 parent afa70d7 commit 37d5f2f

File tree

7 files changed

+42
-53
lines changed

7 files changed

+42
-53
lines changed

lib/model/store.dart

+15-30
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ export 'database.dart' show Account, AccountsCompanion, AccountAlreadyExistsExce
5151
/// * [LiveGlobalStore], the implementation of this class that
5252
/// we use outside of tests.
5353
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 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

6064
/// A cache of the [GlobalSettingsData] singleton in the underlying data store.
61-
GlobalSettingsData? _globalSettings;
65+
GlobalSettingsData _globalSettings;
6266

6367
// TODO settings (those that are per-device rather than per-account)
6468
// TODO push token, and other data corresponding to GlobalSessionState
@@ -226,33 +230,16 @@ abstract class GlobalStore extends ChangeNotifier {
226230
/// Remove an account from the underlying data store.
227231
Future<void> doRemoveAccount(int accountId);
228232

229-
GlobalSettingsData? get globalSettingsSync => _globalSettings;
230-
231-
/// Get global settings from the store.
232-
///
233-
/// Use the cache if already loaded.
234-
/// Otherwise, load it from the underlying store.
235-
///
236-
/// Consider checking [globalSettingsSync] before using this.
237-
Future<GlobalSettingsData> getGlobalSettings() async {
238-
if (globalSettingsSync != null) return globalSettingsSync!;
239-
return await loadGlobalSettings();
240-
}
241-
242-
/// Load global settings from the underlying data store, unconditionally.
243-
///
244-
/// This should only be called from [getGlobalSettings].
245-
Future<GlobalSettingsData> loadGlobalSettings();
233+
GlobalSettingsData get globalSettings => _globalSettings;
246234

247235
/// Update the global settings in the store, return the new version.
248236
///
249237
/// The global settings must already exist in the store.
250238
Future<GlobalSettingsData> updateGlobalSettings(GlobalSettingsCompanion data) async {
251-
assert(_globalSettings != null);
252239
await doUpdateGlobalSettings(data);
253-
_globalSettings = _globalSettings!.copyWithCompanion(data);
240+
_globalSettings = _globalSettings.copyWithCompanion(data);
254241
notifyListeners();
255-
return _globalSettings!;
242+
return _globalSettings;
256243
}
257244

258245
/// Update the global settings in the underlying data store.
@@ -794,6 +781,7 @@ class LiveGlobalStore extends GlobalStore {
794781
LiveGlobalStore._({
795782
required AppDatabase db,
796783
required super.accounts,
784+
required super.globalSettings,
797785
}) : _db = db;
798786

799787
@override
@@ -810,7 +798,10 @@ class LiveGlobalStore extends GlobalStore {
810798
static Future<GlobalStore> load() async {
811799
final db = AppDatabase(NativeDatabase.createInBackground(await _dbFile()));
812800
final accounts = await db.select(db.accounts).get();
813-
return LiveGlobalStore._(db: db, accounts: accounts);
801+
final globalSettings = await db.ensureGlobalSettings();
802+
return LiveGlobalStore._(db: db,
803+
accounts: accounts,
804+
globalSettings: globalSettings);
814805
}
815806

816807
/// The file path to use for the app database.
@@ -872,12 +863,6 @@ class LiveGlobalStore extends GlobalStore {
872863
assert(rowsAffected == 1);
873864
}
874865

875-
@override
876-
Future<GlobalSettingsData> loadGlobalSettings() async {
877-
_globalSettings = await _db.ensureGlobalSettings();
878-
return _globalSettings!;
879-
}
880-
881866
@override
882867
Future<void> doUpdateGlobalSettings(GlobalSettingsCompanion data) async {
883868
final rowsAffected = await _db.update(_db.globalSettings).write(data);

lib/widgets/content.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1343,12 +1343,12 @@ void _launchUrl(BuildContext context, String urlString) async {
13431343
return;
13441344
}
13451345

1346-
final globalSettings = GlobalStoreWidget.of(context).globalSettingsSync;
1346+
final globalSettings = GlobalStoreWidget.of(context).globalSettings;
13471347
bool launched = false;
13481348
String? errorMessage;
13491349
try {
13501350
launched = await ZulipBinding.instance.launchUrl(url,
1351-
mode: switch ((globalSettings!.browserPreference, defaultTargetPlatform)) {
1351+
mode: switch ((globalSettings.browserPreference, defaultTargetPlatform)) {
13521352
(BrowserPreference.embedded, _) => UrlLaunchMode.inAppBrowserView,
13531353
(BrowserPreference.external, _) => UrlLaunchMode.externalApplication,
13541354
// On iOS we prefer LaunchMode.externalApplication because (for

lib/widgets/theme.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import 'text.dart';
1212
ThemeData zulipThemeData(BuildContext context) {
1313
final DesignVariables designVariables;
1414
final List<ThemeExtension> themeExtensions;
15-
final globalSettings = GlobalStoreWidget.of(context).globalSettingsSync;
15+
final globalSettings = GlobalStoreWidget.of(context).globalSettings;
1616
Brightness brightness;
17-
switch (globalSettings!.themeSetting) {
17+
switch (globalSettings.themeSetting) {
1818
case ThemeSetting.none:
1919
brightness = MediaQuery.platformBrightnessOf(context);
2020
case ThemeSetting.light:

test/example_data.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:zulip/api/model/submessage.dart';
88
import 'package:zulip/api/route/messages.dart';
99
import 'package:zulip/api/route/realm.dart';
1010
import 'package:zulip/api/route/channels.dart';
11+
import 'package:zulip/model/database.dart';
1112
import 'package:zulip/model/narrow.dart';
1213
import 'package:zulip/model/store.dart';
1314

@@ -805,8 +806,12 @@ ChannelUpdateEvent channelUpdateEvent(
805806
// The entire per-account or global state.
806807
//
807808

808-
TestGlobalStore globalStore({List<Account> accounts = const []}) {
809-
return TestGlobalStore(accounts: accounts);
809+
TestGlobalStore globalStore({
810+
List<Account> accounts = const [],
811+
GlobalSettingsData globalSettings = const GlobalSettingsData(
812+
themeSetting: ThemeSetting.none, browserPreference: BrowserPreference.none)
813+
}) {
814+
return TestGlobalStore(accounts: accounts, globalSettings: globalSettings);
810815
}
811816

812817
InitialSnapshot initialSnapshot({

test/model/binding.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:test/fake.dart';
88
import 'package:url_launcher/url_launcher.dart' as url_launcher;
99
import 'package:zulip/host/android_notifications.dart';
1010
import 'package:zulip/model/binding.dart';
11+
import 'package:zulip/model/database.dart';
1112
import 'package:zulip/model/store.dart';
1213
import 'package:zulip/widgets/app.dart';
1314

@@ -85,7 +86,11 @@ class TestZulipBinding extends ZulipBinding {
8586
///
8687
/// Tests that access this getter, or that mount a [GlobalStoreWidget],
8788
/// should clean up by calling [reset].
88-
TestGlobalStore get globalStore => _globalStore ??= TestGlobalStore(accounts: []);
89+
TestGlobalStore get globalStore => _globalStore ??= TestGlobalStore(
90+
accounts: [],
91+
globalSettings: const GlobalSettingsData(
92+
themeSetting: ThemeSetting.none,
93+
browserPreference: BrowserPreference.none));
8994
TestGlobalStore? _globalStore;
9095

9196
bool _debugAlreadyLoadedStore = false;

test/model/store_test.dart

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:zulip/api/model/model.dart';
1212
import 'package:zulip/api/route/events.dart';
1313
import 'package:zulip/api/route/messages.dart';
1414
import 'package:zulip/api/route/realm.dart';
15+
import 'package:zulip/model/database.dart';
1516
import 'package:zulip/model/message_list.dart';
1617
import 'package:zulip/model/narrow.dart';
1718
import 'package:zulip/log.dart';
@@ -406,7 +407,7 @@ void main() {
406407
late FakeApiConnection connection;
407408

408409
Future<void> prepareStore({Account? account}) async {
409-
globalStore = TestGlobalStore(accounts: []);
410+
globalStore = eg.globalStore();
410411
account ??= eg.selfAccount;
411412
await globalStore.insertAccount(account.toCompanion(false));
412413
connection = (globalStore.apiConnectionFromAccount(account)
@@ -581,7 +582,7 @@ void main() {
581582
}
582583

583584
Future<void> preparePoll({int? lastEventId}) async {
584-
globalStore = TestGlobalStore(accounts: []);
585+
globalStore = eg.globalStore();
585586
await globalStore.add(eg.selfAccount, eg.initialSnapshot(
586587
lastEventId: lastEventId));
587588
await globalStore.perAccount(eg.selfAccount.id);
@@ -1086,7 +1087,12 @@ void main() {
10861087
}
10871088

10881089
class LoadingTestGlobalStore extends TestGlobalStore {
1089-
LoadingTestGlobalStore({required super.accounts});
1090+
LoadingTestGlobalStore({
1091+
super.accounts = const [],
1092+
super.globalSettings = const GlobalSettingsData(
1093+
themeSetting: ThemeSetting.none,
1094+
browserPreference: BrowserPreference.none),
1095+
});
10901096

10911097
Map<int, List<Completer<PerAccountStore>>> completers = {};
10921098

test/model/test_store.dart

+1-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import '../example_data.dart' as eg;
2323
///
2424
/// See also [TestZulipBinding.globalStore], which provides one of these.
2525
class TestGlobalStore extends GlobalStore {
26-
TestGlobalStore({required super.accounts});
26+
TestGlobalStore({required super.accounts, required super.globalSettings});
2727

2828
final Map<
2929
({Uri realmUrl, int? zulipFeatureLevel, String? email, String? apiKey}),
@@ -161,18 +161,6 @@ class TestGlobalStore extends GlobalStore {
161161

162162
GlobalSettingsData? _globalSettings;
163163

164-
@override
165-
Future<GlobalSettingsData> loadGlobalSettings() async {
166-
if (_globalSettings != null) {
167-
return _globalSettings!;
168-
}
169-
_globalSettings = const GlobalSettingsData(
170-
themeSetting: ThemeSetting.none,
171-
browserPreference: BrowserPreference.none,
172-
);
173-
return Future.value(_globalSettings);
174-
}
175-
176164
@override
177165
Future<void> doUpdateGlobalSettings(GlobalSettingsCompanion data) async {
178166
_globalSettings = _globalSettings!.copyWithCompanion(data);

0 commit comments

Comments
 (0)