Compare commits

...

2 Commits

Author SHA1 Message Date
jld3103 4f94102352
refactor(neon): Remove unnecessary options resets for invalid values 1 year ago
jld3103 195d11eeda
feat(neon): Reset SelectOption if value is not in values 1 year ago
  1. 14
      packages/neon/neon/lib/src/settings/models/option.dart
  2. 11
      packages/neon/neon/lib/src/utils/global_options.dart
  3. 15
      packages/neon/neon/test/option_test.dart

14
packages/neon/neon/lib/src/settings/models/option.dart

@ -71,6 +71,7 @@ sealed class Option<T> extends ChangeNotifier implements ValueListenable<T>, Dis
/// listeners.
@override
T get value => _value;
@mustCallSuper
set value(final T newValue) {
if (_value == newValue) {
@ -88,6 +89,7 @@ sealed class Option<T> extends ChangeNotifier implements ValueListenable<T>, Dis
/// value as evaluated by the equality operator ==, this class notifies its
/// listeners.
bool get enabled => _enabled;
@mustCallSuper
set enabled(final bool newValue) {
if (_enabled == newValue) {
@ -108,7 +110,9 @@ sealed class Option<T> extends ChangeNotifier implements ValueListenable<T>, Dis
final value = deserialize(data);
if (value != null) {
this.value = value;
// Do not trigger the validation to avoid resetting when the values haven't been loaded yet.
_value = value;
notifyListeners();
}
}
@ -203,11 +207,15 @@ class SelectOption<T> extends Option<T> {
@override
set value(final T value) {
if (_values.keys.contains(value)) {
super.value = value;
if (value != null) {
unawaited(storage.setString(key.value, serialize()!));
}
} else {
debugPrint('"$value" is not in "${_values.keys.join('", "')}", ignoring');
}
}
/// A collection of different values this can have.
@ -224,6 +232,10 @@ class SelectOption<T> extends Option<T> {
return;
}
_values = newValues;
if (!_values.keys.contains(_value)) {
debugPrint('"$value" is not in "${_values.keys.join('", "')}", resetting "${key.value}"');
reset();
}
notifyListeners();
}

11
packages/neon/neon/lib/src/utils/global_options.dart

@ -101,10 +101,6 @@ class GlobalOptions extends OptionsCollection {
(final account) => MapEntry(account.id, (final context) => account.humanReadableID),
),
);
if (!initialAccount.values.containsKey(initialAccount.value)) {
initialAccount.reset();
}
}
/// Updates the values of [pushNotificationsDistributor].
@ -118,12 +114,7 @@ class GlobalOptions extends OptionsCollection {
),
);
final enabled = pushNotificationsDistributor.values.isNotEmpty;
pushNotificationsEnabled.enabled = enabled;
if (!enabled) {
pushNotificationsDistributor.reset();
pushNotificationsEnabled.reset();
}
pushNotificationsEnabled.enabled = pushNotificationsDistributor.values.isNotEmpty;
}
/// The theme mode of the app implementing the Neon framework.

15
packages/neon/neon/test/option_test.dart

@ -147,6 +147,21 @@ void main() {
expect(option.values, newValues, reason: 'Should keep the values.');
});
test('Invalid values', () {
expect(option.values, equals(valuesLabel));
option
..value = SelectValues.second
..values = {
SelectValues.first: (final _) => 'first',
SelectValues.third: (final _) => 'third',
};
expect(option.value, SelectValues.first, reason: 'Invalid value.');
option.value = SelectValues.second;
expect(option.value, SelectValues.first, reason: 'Invalid value.');
});
test('Reset', () {
final callback = MockCallbackFunction();
option

Loading…
Cancel
Save