diff static method
- NiceViewDefinition from,
- NiceViewDefinition to
Compute the diff between two view definitions.
Implementation
static NiceViewDiff diff(NiceViewDefinition from, NiceViewDefinition to) {
final changes = <NiceViewChange>[];
final fromMap = {for (final c in from.components) c.id: c};
final toMap = {for (final c in to.components) c.id: c};
// Check for removed components
for (final id in fromMap.keys) {
if (!toMap.containsKey(id)) {
changes.add(NiceViewChange(
type: NiceViewChangeType.removed,
componentId: id,
));
}
}
// Check for added components
for (final id in toMap.keys) {
if (!fromMap.containsKey(id)) {
changes.add(NiceViewChange(
type: NiceViewChangeType.added,
componentId: id,
));
}
}
// Check for modified components
for (final id in fromMap.keys) {
final fromComp = fromMap[id]!;
final toComp = toMap[id];
if (toComp == null) continue;
if (fromComp.type != toComp.type) {
changes.add(NiceViewChange(
type: NiceViewChangeType.modified,
componentId: id,
field: 'type',
oldValue: fromComp.type.name,
newValue: toComp.type.name,
));
}
if (fromComp.title != toComp.title) {
changes.add(NiceViewChange(
type: NiceViewChangeType.modified,
componentId: id,
field: 'title',
oldValue: fromComp.title,
newValue: toComp.title,
));
}
if (fromComp.flex != toComp.flex) {
changes.add(NiceViewChange(
type: NiceViewChangeType.modified,
componentId: id,
field: 'flex',
oldValue: fromComp.flex,
newValue: toComp.flex,
));
}
if (jsonEncode(fromComp.config) != jsonEncode(toComp.config)) {
changes.add(NiceViewChange(
type: NiceViewChangeType.modified,
componentId: id,
field: 'config',
oldValue: fromComp.config,
newValue: toComp.config,
));
}
}
// Check for reordering
final fromIds = from.components.map((c) => c.id).toList();
final toIds = to.components.map((c) => c.id).toList();
final commonIds = fromIds.where(toIds.contains).toList();
final commonInTo = toIds.where(commonIds.contains).toList();
if (commonIds.join(',') != commonInTo.join(',')) {
changes.add(NiceViewChange(
type: NiceViewChangeType.reordered,
componentId: '*',
oldValue: fromIds,
newValue: toIds,
));
}
// Layout change
if (from.layout != to.layout) {
changes.add(NiceViewChange(
type: NiceViewChangeType.modified,
componentId: '*',
field: 'layout',
oldValue: from.layout.name,
newValue: to.layout.name,
));
}
return NiceViewDiff(
fromVersion: from.version,
toVersion: to.version,
changes: changes,
);
}