Skip to content

Commit

Permalink
fix(neon_framework): Fix settings import
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Sep 8, 2024
1 parent ed3b361 commit 51da2eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
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

0 comments on commit 51da2eb

Please sign in to comment.