Event.fromICalString constructor

Event.fromICalString(
  1. String icalString
)

Implementation

factory Event.fromICalString(String icalString) {
  final lines = LineSplitter.split(icalString).toList();
  final uid = lines
      .firstWhere((line) => line.startsWith('UID:'), orElse: () => '')
      .replaceAll('UID:', '');
  final summary = lines
      .firstWhere((line) => line.startsWith('SUMMARY:'), orElse: () => '')
      .replaceAll('SUMMARY:', '');
  final description = lines
      .firstWhere((line) => line.startsWith('DESCRIPTION:'), orElse: () => '')
      .replaceAll('DESCRIPTION:', '');

  List<Attachment>? attachments;
  if (lines.any((line) => line.startsWith('ATTACH:'))) {
    attachments = [];
    for (var i = 0; i < lines.length; i++) {
      final line = lines[i];
      if (line.startsWith('ATTACH:')) {
        final attachment = Attachment.fromICalString(line);
        attachments.add(attachment);
      }
    }
  }

  List<Attendee>? attendees;
  if (lines.any((line) => line.startsWith('ATTENDEE;'))) {
    attendees = [];
    for (var i = 0; i < lines.length; i++) {
      final line = lines[i];
      if (line.startsWith('ATTENDEE;')) {
        final attendee = Attendee.fromICalString(line);
        attendees.add(attendee);
      }
    }
  }

  final eventClass = lines
      .firstWhere((line) => line.startsWith('CLASS:'), orElse: () => '')
      .replaceAll('CLASS:', '');

  final organizer = Organizer.fromICalString(
    lines.firstWhere(
      (line) => line.startsWith('ORGANIZER;'),
      orElse: () => '',
    ),
  );

  final created = lines
      .firstWhere((line) => line.startsWith('CREATED:'), orElse: () => '')
      .replaceAll('CREATED:', '');
  final lastModified = lines
      .firstWhere((line) => line.startsWith('LAST-MODIFIED:'),
          orElse: () => '')
      .replaceAll('LAST-MODIFIED:', '');
  final startDate = lines
      .firstWhere((line) => line.startsWith('DTSTART:'), orElse: () => '')
      .replaceAll('DTSTART:', '');
  final endDate = lines
      .firstWhere((line) => line.startsWith('DTEND:'), orElse: () => '')
      .replaceAll('DTEND:', '');

  RecurrenceRule? recurrenceRule;
  if (lines.any((line) => line.startsWith('RRULE:'))) {
    recurrenceRule = RecurrenceRule.fromICalString(
      lines.firstWhere((line) => line.startsWith('RRULE:')),
    );
  }

  final recurrenceId = lines
      .firstWhere((line) => line.startsWith('RECURRENCE-ID:'),
          orElse: () => '')
      .replaceAll('RECURRENCE-ID:', '');

  final sequence = lines
      .firstWhere((line) => line.startsWith('SEQUENCE:'), orElse: () => '')
      .replaceAll('SEQUENCE:', '');

  final transparency = lines
      .firstWhere((line) => line.startsWith('TRANSP:'), orElse: () => '')
      .replaceAll('TRANSP:', '');

  final location = lines
      .firstWhere((line) => line.startsWith('LOCATION:'), orElse: () => '')
      .replaceAll('LOCATION:', '');
  List<Attachment>? attachFiles;
  if (lines.any((line) => line.startsWith('ATTACH;'))) {
    attachFiles = [];
    for (var i = 0; i < lines.length; i++) {
      final line = lines[i];
      if (line.startsWith('ATTACH;')) {
        final attachFile = Attachment.fromICalString(line);
        attachFiles.add(attachFile);
      }
    }
  }

  var customProperties = <String, String>{};
  for (var i = 0; i < lines.length; i++) {
    final line = lines[i];
    if (line.startsWith('X-')) {
      final key = line.split(':')[0];
      final value = line.split(':')[1];
      customProperties[key] = value;
    }
  }

  return Event(
    uid: uid,
    summary: summary.isNotEmpty ? summary : null,
    description: description.isNotEmpty ? description : null,
    attachments: attachments,
    attendees: attendees,
    organizer: organizer,
    created: created.isNotEmpty ? DateTime.parse(created) : null,
    lastModified:
        lastModified.isNotEmpty ? DateTime.parse(lastModified) : null,
    startDate: startDate.isNotEmpty ? DateTime.parse(startDate) : null,
    endDate: endDate.isNotEmpty ? DateTime.parse(endDate) : null,
    recurrenceRule: recurrenceRule,
    eventClass: eventClass.isNotEmpty ? eventClass : null,
    customProperties: customProperties,
    recurrenceId: recurrenceId.isNotEmpty ? recurrenceId : null,
    sequence: sequence.isNotEmpty ? int.parse(sequence) : null,
    transparency: transparency.isNotEmpty ? transparency : null,
    location: location.isNotEmpty ? Location.fromICalString(location) : null,
    attachFiles: attachFiles,
  );
}