EmailAlarmComponent.fromCrawledBlock constructor

EmailAlarmComponent.fromCrawledBlock(
  1. CrawledBlock block
)

Implementation

factory EmailAlarmComponent.fromCrawledBlock(CrawledBlock block) {
  assert(
    block.blockName.toUpperCase() == "VALARM",
    "Received invalid block: ${block.blockName}",
  );

  DateTimeTriggerProperty? dateTimeTrigger;
  DurationTriggerProperty? durationTrigger;
  DurationProperty? duration;
  RepeatCountProperty? repeatCount;
  List<UriAttachmentProperty>? uriAttachments;
  List<BinaryAttachmentProperty>? binaryAttachments;
  List<AttendeeProperty> attendees = [];
  DescriptionProperty? description;
  SummaryProperty? summary;

  for (var e in block.properties) {
    switch (e.name.toUpperCase()) {
      case "TRIGGER":
        {
          final isDuration = e.parameters.any(
            (element) =>
                element.name.toUpperCase() == "VALUE" &&
                ValueDataTypeParameter.fromCrawledParameter(element).type ==
                    ValueType.duration,
          );

          if (isDuration) {
            durationTrigger = DurationTriggerProperty.fromCrawledProperty(e);
          } else {
            dateTimeTrigger = DateTimeTriggerProperty.fromCrawledProperty(e);
          }
          break;
        }
      case "DURATION":
        duration = DurationProperty.fromCrawledProperty(e);
        break;
      case "REPEAT":
        repeatCount = RepeatCountProperty.fromCrawledProperty(e);
        break;
      case "ATTACH":
        {
          final isBinary = e.parameters.any(
            (element) =>
                element.name.toUpperCase() == "VALUE" &&
                ValueDataTypeParameter.fromCrawledParameter(element).type ==
                    ValueType.binary,
          );

          if (isBinary) {
            binaryAttachments ??= [];
            binaryAttachments
                .add(BinaryAttachmentProperty.fromCrawledProperty(e));
          } else {
            uriAttachments ??= [];
            uriAttachments.add(UriAttachmentProperty.fromCrawledProperty(e));
          }
          break;
        }
      case "ATTENDEE":
        attendees.add(AttendeeProperty.fromCrawledProperty(e));
        break;
      case "DESCRIPTION":
        description = DescriptionProperty.fromCrawledProperty(e);
        break;
      case "SUMMARY":
        summary = SummaryProperty.fromCrawledProperty(e);
        break;
      default:
        print(
            "Unknown property encountered in EmailAlarmComponent: ${e.name}");
    }
  }

  assert(
    description != null && summary != null,
    "[description] and [summary] cannot be null",
  );

  return EmailAlarmComponent(
    dateTimeTrigger: dateTimeTrigger,
    durationTrigger: durationTrigger,
    duration: duration,
    repeatCount: repeatCount,
    uriAttachments: uriAttachments,
    binaryAttachments: binaryAttachments,
    attendees: attendees,
    description: description!,
    summary: summary!,
  );
}