-
Couldn't load subscription status.
- Fork 348
GlobalSettingsStore, and other refactors #1416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6e4feea
17ad29c
154e059
df4e145
ea20bd4
3fc1314
6c041b1
2365bb3
bda2351
70824b4
59e78c8
0566369
f177fe0
1614322
6483514
c107fe8
793dbc7
1cf31a4
9201ae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart'; | |
| import '../generated/l10n/zulip_localizations.dart'; | ||
| import 'binding.dart'; | ||
| import 'database.dart'; | ||
| import 'store.dart'; | ||
|
|
||
| /// The user's choice of visual theme for the app. | ||
| /// | ||
|
|
@@ -44,7 +45,39 @@ enum BrowserPreference { | |
| external, | ||
| } | ||
|
|
||
| extension GlobalSettingsHelpers on GlobalSettingsData { | ||
| /// Store for the user's account-independent settings. | ||
| /// | ||
| /// From UI code, use [GlobalStoreWidget.settingsOf] to get hold of | ||
| /// an appropriate instance of this class. | ||
| class GlobalSettingsStore extends ChangeNotifier { | ||
| GlobalSettingsStore({ | ||
| required GlobalStoreBackend backend, | ||
| required GlobalSettingsData data, | ||
| }) : _backend = backend, _data = data; | ||
|
|
||
| final GlobalStoreBackend _backend; | ||
|
|
||
| /// A cache of the [GlobalSettingsData] singleton in the underlying data store. | ||
| GlobalSettingsData _data; | ||
|
|
||
| /// The user's choice of [ThemeSetting]; | ||
| /// null means the device-level choice of theme. | ||
| /// | ||
| /// See also [setThemeSetting]. | ||
| ThemeSetting? get themeSetting => _data.themeSetting; | ||
|
|
||
| /// The user's choice of [BrowserPreference]; | ||
| /// null means use our default choice. | ||
| /// | ||
| /// Consider using [effectiveBrowserPreference] or [getUrlLaunchMode]. | ||
| /// | ||
| /// See also [setBrowserPreference]. | ||
| BrowserPreference? get browserPreference => _data.browserPreference; | ||
|
|
||
| /// The value of [BrowserPreference] to use: | ||
| /// the user's choice [browserPreference] if any, else our default. | ||
| /// | ||
| /// See also [getUrlLaunchMode]. | ||
| BrowserPreference get effectiveBrowserPreference { | ||
| if (browserPreference != null) return browserPreference!; | ||
| return switch (defaultTargetPlatform) { | ||
|
|
@@ -61,6 +94,8 @@ extension GlobalSettingsHelpers on GlobalSettingsData { | |
| }; | ||
| } | ||
|
|
||
| /// The launch mode to use with `url_launcher`, | ||
| /// based on the user's choice in [browserPreference]. | ||
| UrlLaunchMode getUrlLaunchMode(Uri url) { | ||
| switch (effectiveBrowserPreference) { | ||
| case BrowserPreference.inApp: | ||
|
|
@@ -78,4 +113,20 @@ extension GlobalSettingsHelpers on GlobalSettingsData { | |
| return UrlLaunchMode.externalApplication; | ||
| } | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. commit message nit: This part confuses me a little. Is there something missing here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How's this? Same thought but perhaps clearer syntax. |
||
| Future<void> _update(GlobalSettingsCompanion data) async { | ||
| await _backend.doUpdateGlobalSettings(data); | ||
| _data = _data.copyWithCompanion(data); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| /// Set [themeSetting], persistently for future runs of the app. | ||
| Future<void> setThemeSetting(ThemeSetting? value) async { | ||
| await _update(GlobalSettingsCompanion(themeSetting: Value(value))); | ||
| } | ||
|
|
||
| /// Set [browserPreference], persistently for future runs of the app. | ||
| Future<void> setBrowserPreference(BrowserPreference? value) async { | ||
| await _update(GlobalSettingsCompanion(browserPreference: Value(value))); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A reason that
effectiveBrowserPreferenceis separate from this was that we didn't opt into implementing a customGlobalSettingsData.Now that
GlobalSettingsData _datais nicely hidden as a part of the implementation detail, we can renameeffectiveBrowserPreferencetobrowserPreference, replacing it, and skip the part of the dartdoc redirecting toeffectiveBrowserPreference.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess keeping
browserPreferencenullable is consistent withsetBrowserPreference, but I think we don't need to expose a way to set it tonull.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think hiding the raw
browserPreferencebehind (a renamed)effectiveBrowserPreferencecould be a good follow-up change. ThensetBrowserPreferencewould narrow its parameter type to match.I'll leave that out of this PR, though, because I think it's not on the path to #1409.