-
Notifications
You must be signed in to change notification settings - Fork 306
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
Merged
+1,097
−164
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6e4feea
test [nfc]: Inline eg.globalSettings
gnprice 17ad29c
db test: Test updating GlobalSettings
gnprice 154e059
settings [nfc]: Drop return value from updateGlobalSettings
gnprice df4e145
settings [nfc]: Write docs on extension members
gnprice ea20bd4
settings [nfc]: Cut "todo" comment calling for settings in store
gnprice 3fc1314
db [nfc]: Extract _migrationSteps as a static
gnprice 6c041b1
db [nfc]: Make _dropAndCreateAll static too
gnprice 2365bb3
db: Create GlobalSettings row as part of schema setup
gnprice bda2351
db [nfc]: Simplify name and tests of getGlobalSettings
gnprice 70824b4
store [nfc]: Drop no-op should-notify override in _GlobalStoreInherit…
gnprice 59e78c8
store test: Add test for GlobalStoreWidget.of
gnprice 0566369
settings [nfc]: Split out a GlobalSettingsStore, and GlobalStoreWidge…
gnprice f177fe0
settings [nfc]: Move the settings data out to GlobalSettingsStore
gnprice 1614322
settings [nfc]: Have inherited widget use GlobalSettingsStore directly
gnprice 6483514
settings [nfc]: Have settingsOf return whole store, not just data
gnprice c107fe8
store [nfc]: Simplify name GlobalStore.settings, from globalSettings
gnprice 793dbc7
store [nfc]: Separate "backend" class from store itself
gnprice 1cf31a4
settings [nfc]: Use `update` method directly on GlobalSettingsStore
gnprice 9201ae4
settings [nfc]: Give settings specific setter methods
gnprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
effectiveBrowserPreference
is separate from this was that we didn't opt into implementing a customGlobalSettingsData
.Now that
GlobalSettingsData _data
is nicely hidden as a part of the implementation detail, we can renameeffectiveBrowserPreference
tobrowserPreference
, 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
browserPreference
nullable 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
browserPreference
behind (a renamed)effectiveBrowserPreference
could be a good follow-up change. ThensetBrowserPreference
would 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.