parse method
Implementation
CampaignCanvas parse(
Map<String, dynamic> json, {
Set<String> allowedWidgetTypes = supportedCampaignCanvasWidgetTypes,
}) {
final version = optInt(json, 'version', 2);
if (version < 2) throw FormatException('Old canvas version $version');
if (version > 2) {
_log.e('Dropped — unsupported canvas version $version',
stage: TimelineStage.parse,
reason: TimelineReason.schemaVersionTooNew,
extras: {'required': '$version', 'supported': '2'});
throw const ReportedParseFailure();
}
final width = _propertyDouble(json, 'canvasWidth', 360);
final height = _propertyDouble(json, 'canvasHeight', 420);
final canvasWidth = width > 0 ? width : 360.0;
final canvasHeight = height > 0 ? height : 420.0;
final children = <CampaignCanvasChild>[];
for (final raw in optList(json, 'children') ?? const []) {
if (raw is! Map) continue;
final child = raw.cast<String, dynamic>();
final rectJson = optMap(child, 'rect');
if (rectJson == null) continue;
final rawX = _propertyDouble(rectJson, 'x', 0);
final rawY = _propertyDouble(rectJson, 'y', 0);
final rawWidth = _propertyDouble(rectJson, 'width', 0);
final rawHeight = _propertyDouble(rectJson, 'height', 0);
final rect = CampaignCanvasRect(
rawX * canvasWidth,
rawY * canvasHeight,
rawWidth * canvasWidth,
rawHeight * canvasHeight,
);
final id = optString(child, 'id');
switch (optString(child, 'kind')) {
case 'tapRegion':
final actions =
const EngageActionParser().parse(optMap(child, 'onClick'));
final isPrimary = optBool(child, 'isPrimary', false);
if (isPrimary || actions.isNotEmpty) {
children.add(
CampaignCanvasTapRegionChild(
id,
rect,
actions,
isPrimary: isPrimary,
),
);
}
case 'widget':
final widgetJson = optMap(child, 'widget');
if (widgetJson == null) continue;
final type = optString(widgetJson, 'type');
if (type.isNotEmpty &&
!supportedCampaignCanvasWidgetTypes.contains(type)) {
_log.w('Skipped — unsupported canvas widget (type=$type)',
stage: TimelineStage.parse,
reason: TimelineReason.unsupportedWidgetType,
extras: {'widget_type': type});
}
if (!allowedWidgetTypes.contains(type)) continue;
final widget = _widget(type, widgetJson);
if (widget != null) {
children.add(CampaignCanvasWidgetChild(id, rect, widget));
}
}
}
return CampaignCanvas(
version: version,
width: canvasWidth,
height: canvasHeight,
background: _background(optMap(json, 'background')),
children: children,
);
}