parse static method

VFreeBusy parse(
  1. String vfreebusyString
)

Implementation

static VFreeBusy parse(String vfreebusyString) {
  final lines = vfreebusyString.split('\n');
  String? uid;
  DateTime? dtstamp;
  DateTime? dtstart;
  DateTime? dtend;
  List<DateRangeUtil>? freeTimes;
  List<DateRangeUtil>? busyTimes;
  MailTo? organizer;
  String? contact;

  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(':');

    if (key.contains(';')) {
      final keyParts = key.split(';');
      if (keyParts.length == 2 && keyParts[0] == 'FREEBUSY') {
        final fbTypeParts = keyParts[1].split('=');
        if (fbTypeParts.length == 2) {
          final fbType = fbTypeParts[1];
          if (fbType == 'FREE') {
            freeTimes ??= [];
            freeTimes.add(DateRangeUtil.parse(value) );
          } else if (fbType == 'BUSY') {
            busyTimes ??= [];
            busyTimes.add(DateRangeUtil.parse(value));
          }
        }
      }
      continue;
    }

    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 'ORGANIZER':
        organizer = MailTo(value);
        break;
      case 'CONTACT':
        contact = value;
        break;
    }
  }

  return VFreeBusy(
    uid: uid ?? '',
    dtstamp: dtstamp ?? DateTime.now(),
    dtstart: dtstart ?? DateTime.now(),
    dtend: dtend ?? DateTime.now().add(const Duration(hours: 1)),
    freeTimes: freeTimes,
    busyTimes: busyTimes,
    organizer: organizer,
    contact: contact,
  );
}