toICalString method

String toICalString({
  1. String? dtstamp,
})

Implementation

String toICalString({String? dtstamp}) {
  dtstamp ??=
      DateFormat("yyyyMMdd'T'HHmmss'Z'").format(DateTime.now().toUtc());
  final buffer = StringBuffer();
  buffer.writeln(_foldLine('BEGIN:VEVENT'));
  buffer.writeln(_foldLine('DTSTAMP:$dtstamp'));

  if (startDate != null) {
    if ((timeZone != null && timeZone!.tzId == 'DATE') || allDay) {
      buffer.writeln(_foldLine(
          'DTSTART;VALUE=DATE:${DateFormat('yyyyMMdd').format(startDate!)}'));
      if (endDate != null) {
        buffer.writeln(_foldLine(
            'DTEND;VALUE=DATE:${DateFormat('yyyyMMdd').format(endDate!)}'));
      }
    } else if (timeZone != null) {
      buffer.writeln(_foldLine(
          'DTSTART;TZID=${timeZone!.tzId}:${DateFormat('yyyyMMdd\'T\'HHmmss').format(startDate!)}'));
      if (endDate != null) {
        buffer.writeln(_foldLine(
            'DTEND;TZID=${timeZone!.tzId}:${DateFormat('yyyyMMdd\'T\'HHmmss').format(endDate!)}'));
      }
    } else {
      buffer.writeln(_foldLine(
          'DTSTART:${DateFormat('yyyyMMdd\'T\'HHmmss\'Z\'').format(startDate!)}'));
      if (endDate != null) {
        buffer.writeln(_foldLine(
            'DTEND:${DateFormat('yyyyMMdd\'T\'HHmmss\'Z\'').format(endDate!)}'));
      }
    }
  }

  if (recurrenceRule != null) {
    buffer.writeln(_foldLine(recurrenceRule!.toICalString()));
  }

  if (xDates != null) {
    for (final xDate in xDates!) {
      buffer.writeln(_foldLine(xDate.toICalString()));
    }
  }

  if (stamp != null) {
    buffer.writeln(_foldLine(
        'DTSTAMP:${DateFormat('yyyyMMdd\'T\'HHmmss\'Z\'').format(stamp!.toUtc())}'));
  }

  if (organizer != null) {
    buffer.writeln(_foldLine(organizer!.toICalString()));
  }

  buffer.writeln(_foldLine('UID:$uid'));

  if ((timeZone != null && timeZone!.tzId == 'DATE') || allDay) {
    buffer.writeln(_foldLine('X-MICROSOFT-CDO-ALLDAYEVENT:TRUE'));
  }

  if (attendees != null && attendees!.isNotEmpty) {
    for (final attendee in attendees!) {
      buffer.writeln(_foldLine(attendee.toICalString()));
    }
  }

  if (customProperties != null &&
      customProperties!.isNotEmpty &&
      customProperties!.containsKey('X-GOOGLE-CONFERENCE')) {
    for (final key in customProperties!.keys) {
      if (!key.contains('X-LIC')) {
        buffer.writeln(_foldLine('$key:${customProperties![key]}'));
      }
    }
  }

  if (eventClass != null) {
    buffer.writeln(_foldLine('CLASS:$eventClass'));
  }

  if (recurrenceId != null) {
    buffer.writeln(_foldLine('RECURRENCE-ID;$recurrenceId'));
  }

  if (created != null) {
    buffer.writeln(_foldLine(
        'CREATED:${DateFormat('yyyyMMdd\'T\'HHmmss\'Z\'').format(created!.toUtc())}'));
  }

  if (description != null) {
    buffer.writeln(_foldLine('DESCRIPTION:$description'));
  }

  if (lastModified != null) {
    buffer.writeln(_foldLine(
        'LAST-MODIFIED:${DateFormat('yyyyMMdd\'T\'HHmmss\'Z\'').format(lastModified!.toUtc())}'));
  }

  if (location != null) {
    buffer.writeln(_foldLine(location!.toICalString()));
  }

  if (sequence != null) {
    buffer.writeln(_foldLine('SEQUENCE:$sequence'));
  }
  if (status != null) {
    buffer.writeln(_foldLine(status!.toICalString()));
  }
  if (summary != null) {
    buffer.writeln(_foldLine('SUMMARY:$summary'));
  }
  if (transparency != null) {
    buffer.writeln(_foldLine('TRANSP:$transparency'));
  }

  if (attachments != null && attachments!.isNotEmpty) {
    for (final attachment in attachments!) {
      buffer.writeln(_foldLine(attachment.toICalString()));
    }
  }

  if (customProperties != null &&
      customProperties!.isNotEmpty &&
      !customProperties!.containsKey('X-GOOGLE-CONFERENCE')) {
    for (final key in customProperties!.keys) {
      if (!key.contains('X-LIC')) {
        buffer.writeln(_foldLine('$key:${customProperties![key]}'));
      }
    }
  }

  if (customProperties != null && customProperties!.isNotEmpty) {
    for (final key in customProperties!.keys) {
      if (key.contains('X-LIC')) {
        buffer.writeln(_foldLine('$key:${customProperties![key]}'));
      }
    }
  }

  if (attachFiles != null && attachFiles!.isNotEmpty) {
    for (final attachFile in attachFiles!) {
      buffer.writeln(_foldLine(attachFile.toICalString()));
    }
  }

  buffer.writeln(_foldLine('END:VEVENT'));
  return buffer.toString();
}