parse static method

VEvent parse(
  1. String veventString
)

Implementation

static VEvent parse(String veventString) {

  final lines = veventString.split('\n');

  String? uid;
  DateTime? dtstamp;
  DateTime? dtstart;
  DateTime? dtend;
  String? summary;
  String? description;
  String? location;
  EVENTStatus? status;
  RecurrenceRule? rrule;
  List<DateTime>? exDates;
  List<Attendee>? attendees;
  MailTo? organizer;
  String? contact;
  String? attachment;

  for (var line in lines) {
    final parts = line.split(':');
    if (parts.length < 2) continue;
    final key = parts[0];
    final value = parts.getRange(1, parts.length).join(':');

    switch (key) {
      case 'UID':
        uid = value;
        break;
      case 'DTSTAMP':
        dtstamp = DateTime.parse(value.replaceAll('Z', ''));
        break;
      case 'DTSTART':
        dtstart = DateTime.parse(value.replaceAll('Z', ''));
        break;
      case 'DTEND':
        dtend = DateTime.parse(value.replaceAll('Z', ''));
        break;
      case 'SUMMARY':
        summary = value;
        break;
      case 'DESCRIPTION':
        description = value;
        break;
      case 'LOCATION':
        location = value;
        break;
      case 'STATUS':
        status = EVENTStatus.values.firstWhere((e) => Heplers.camelToSnake(e.name).toUpperCase() == value);
        break;
      case 'RRULE':
        rrule = RecurrenceRule.parse(value);
        break;
      case 'EXDATE':
        exDates ??= [];
        exDates.add(DateTime.parse(value.replaceAll('Z', '')));
        break;
      case 'ATTENDEE':
        attendees ??= [];
        attendees.add(Attendee.parse(value) );
        break;
      case 'ORGANIZER':
        organizer = MailTo.parse(value);
        break;
      case 'CONTACT':
        contact = value;
        break;
      case 'ATTACH':
        attachment = value;
        break;
    }
  }

  return VEvent(
    uid: uid ?? '',
    dtstamp: dtstamp ?? DateTime.now(),
    dtstart: dtstart ?? DateTime.now(),
    dtend: dtend ?? DateTime.now().add(const Duration(hours: 1)),
    summary: summary ?? '',
    description: description,
    location: location,
    status: status,
    rrule: rrule,
    exDates: exDates,
    attendees: attendees,
    organizer: organizer,
    contact: contact,
    attachment: attachment,
  );
}