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. 22
      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

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

@ -71,6 +71,7 @@ sealed class Option<T> extends ChangeNotifier implements ValueListenable<T>, Dis
/// listeners. /// listeners.
@override @override
T get value => _value; T get value => _value;
@mustCallSuper @mustCallSuper
set value(final T newValue) { set value(final T newValue) {
if (_value == 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 /// value as evaluated by the equality operator ==, this class notifies its
/// listeners. /// listeners.
bool get enabled => _enabled; bool get enabled => _enabled;
@mustCallSuper @mustCallSuper
set enabled(final bool newValue) { set enabled(final bool newValue) {
if (_enabled == newValue) { if (_enabled == newValue) {
@ -108,7 +110,9 @@ sealed class Option<T> extends ChangeNotifier implements ValueListenable<T>, Dis
final value = deserialize(data); final value = deserialize(data);
if (value != null) { 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,10 +207,14 @@ class SelectOption<T> extends Option<T> {
@override @override
set value(final T value) { set value(final T value) {
super.value = value; if (_values.keys.contains(value)) {
super.value = value;
if (value != null) {
unawaited(storage.setString(key.value, serialize()!)); if (value != null) {
unawaited(storage.setString(key.value, serialize()!));
}
} else {
debugPrint('"$value" is not in "${_values.keys.join('", "')}", ignoring');
} }
} }
@ -224,6 +232,10 @@ class SelectOption<T> extends Option<T> {
return; return;
} }
_values = newValues; _values = newValues;
if (!_values.keys.contains(_value)) {
debugPrint('"$value" is not in "${_values.keys.join('", "')}", resetting "${key.value}"');
reset();
}
notifyListeners(); 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), (final account) => MapEntry(account.id, (final context) => account.humanReadableID),
), ),
); );
if (!initialAccount.values.containsKey(initialAccount.value)) {
initialAccount.reset();
}
} }
/// Updates the values of [pushNotificationsDistributor]. /// Updates the values of [pushNotificationsDistributor].
@ -118,12 +114,7 @@ class GlobalOptions extends OptionsCollection {
), ),
); );
final enabled = pushNotificationsDistributor.values.isNotEmpty; pushNotificationsEnabled.enabled = pushNotificationsDistributor.values.isNotEmpty;
pushNotificationsEnabled.enabled = enabled;
if (!enabled) {
pushNotificationsDistributor.reset();
pushNotificationsEnabled.reset();
}
} }
/// The theme mode of the app implementing the Neon framework. /// 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.'); 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', () { test('Reset', () {
final callback = MockCallbackFunction(); final callback = MockCallbackFunction();
option option

Loading…
Cancel
Save