Skip to content
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

fix(neon_framework): Fix settings import #2474

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/neon_framework/lib/src/settings/models/exportable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ extension SerializeOptions on Iterable<Option<dynamic>> {
void deserialize(Map<String, Object?> data) {
for (final entry in data.entries) {
final option = firstWhereOrNull((option) => option.key.value == entry.key);
if (option == null) {
continue;
}

if (entry.value != null) {
option?.load(entry.value);
final value = option.deserialize(data);
if (value != null) {
option.value = value;
}
} else {
option?.reset();
option.reset();
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions packages/neon_framework/lib/src/settings/models/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,6 @@ sealed class Option<T> extends ChangeNotifier implements ValueListenable<T>, Dis
value = defaultValue;
}

/// Loads [data] into [value] by calling [deserialize] on it.
void load(Object? data) {
final value = deserialize(data);

if (value != null) {
// Do not trigger the validation to avoid resetting when the values haven't been loaded yet.
_value = value;
notifyListeners();
}
}

/// Deserializes the data.
T? deserialize(Object? data);

Expand Down
12 changes: 3 additions & 9 deletions packages/neon_framework/test/option_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ void main() {
values: valuesLabel,
);

// ignore: cascade_invocations
option.load('SelectValues.second');

expect(option.value, SelectValues.second);
expect(option.deserialize('SelectValues.second'), SelectValues.second);
});

test('Stream', () async {
Expand Down Expand Up @@ -320,11 +317,8 @@ void main() {
});

test('Deserialize', () {
expect(option.value, true);

option.load(false);

expect(option.value, false);
expect(option.deserialize(true), true);
expect(option.deserialize(false), false);
});
});
}
2 changes: 1 addition & 1 deletion packages/neon_framework/test/options_collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void main() {

collection.import(json);

verify(() => option1.load(false)).called(1);
verify(() => option1.deserialize({'key1': false, 'key2': null})).called(1);
verify(option2.reset).called(1);
});
});
Expand Down