migrate static method
- NiceViewDefinition current,
- NiceViewDiff diff
Migrate a definition from an older version, applying changes. Returns a new definition with the target version number.
Implementation
static NiceViewDefinition migrate(
NiceViewDefinition current,
NiceViewDiff diff,
) {
var components = current.components.toList();
for (final change in diff.changes) {
switch (change.type) {
case NiceViewChangeType.added:
// Add a placeholder component
components.add(NiceViewComponent(
id: change.componentId,
type: NiceViewComponentType.text,
title: 'New component',
));
break;
case NiceViewChangeType.removed:
components.removeWhere((c) => c.id == change.componentId);
break;
case NiceViewChangeType.modified:
// For config-level changes, replace the component
if (change.componentId != '*') {
final idx = components.indexWhere((c) => c.id == change.componentId);
if (idx >= 0) {
final old = components[idx];
components[idx] = NiceViewComponent(
id: old.id,
type: change.field == 'type' && change.newValue is String
? NiceViewComponentType.values.firstWhere(
(t) => t.name == change.newValue,
orElse: () => old.type,
)
: old.type,
title: change.field == 'title' ? change.newValue as String? : old.title,
config: change.field == 'config'
? (change.newValue as Map<String, dynamic>?) ?? old.config
: old.config,
flex: change.field == 'flex' ? change.newValue as int : old.flex,
children: old.children,
);
}
}
break;
case NiceViewChangeType.reordered:
if (change.newValue is List) {
final order = (change.newValue as List).cast<String>();
final ordered = <NiceViewComponent>[];
for (final id in order) {
final comp = components.where((c) => c.id == id).firstOrNull;
if (comp != null) ordered.add(comp);
}
// Add any components not in order list
for (final c in components) {
if (!order.contains(c.id)) ordered.add(c);
}
components = ordered;
}
break;
}
}
return NiceViewDefinition(
id: current.id,
name: current.name,
description: current.description,
layout: current.layout,
components: components,
version: diff.toVersion,
);
}