Skip to content

Commit

Permalink
fix(neon): fix late initialization of SelectOption.values
Browse files Browse the repository at this point in the history
  • Loading branch information
Leptopoda committed Jul 19, 2023
1 parent e481e8e commit b344a4f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/neon/neon/lib/src/settings/models/select_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ class SelectOption<T> extends Option<T> {
required super.label,
required super.defaultValue,
required final Map<T, LabelBuilder> values,

/// Force loading the stored value.
///
/// This is needed when [values] is empty but the stored value should still
/// be loaded.
/// This only works when [T] is of type String.
final bool forceLoadValue = false,
super.category,
super.enabled,
}) : _values = values,
super(initialValue: _fromString(values, storage.getString(key)));
super(initialValue: loadValue(values, storage.getString(key), forceLoad: forceLoadValue));

/// Creates a SelectOption depending on the State of another [Option].
SelectOption.depend({
Expand All @@ -25,9 +32,27 @@ class SelectOption<T> extends Option<T> {
required super.defaultValue,
required final Map<T, LabelBuilder> values,
required super.enabled,

/// Force loading the stored value.
///
/// This is needed when [values] is empty but the stored value should still
/// be loaded.
/// This only works when [T] is of type String.
final bool forceLoadValue = false,
super.category,
}) : _values = values,
super.depend(initialValue: _fromString(values, storage.getString(key)));
super.depend(initialValue: loadValue(values, storage.getString(key), forceLoad: forceLoadValue));

static T? loadValue<T>(final Map<T, LabelBuilder> vs, final String? stored, {final bool forceLoad = false}) {
if (forceLoad) {
assert(vs.isEmpty, 'Force loading should only be used when no initial valus are specified.');
assert(stored is T?, 'Force loading is only supported on String values.');

return stored as T?;
}

return _fromString(vs, stored);
}

static T? _fromString<T>(final Map<T, LabelBuilder> vs, final String? valueStr) {
if (valueStr == null) {
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon/lib/src/utils/account_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AccountSpecificOptions {
key: 'initial-app',
label: (final context) => AppLocalizations.of(context).accountOptionsInitialApp,
defaultValue: null,
forceLoadValue: true,
values: {},
);
}
2 changes: 2 additions & 0 deletions packages/neon/neon/lib/src/utils/global_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class GlobalOptions {
key: 'push-notifications-distributor',
label: (final context) => AppLocalizations.of(context).globalOptionsPushNotificationsDistributor,
defaultValue: null,
forceLoadValue: true,
values: {},
enabled: pushNotificationsEnabled,
);
Expand Down Expand Up @@ -210,6 +211,7 @@ class GlobalOptions {
key: 'initial-account',
label: (final context) => AppLocalizations.of(context).globalOptionsAccountsInitialAccount,
defaultValue: null,
forceLoadValue: true,
values: {},
);

Expand Down

0 comments on commit b344a4f

Please sign in to comment.