icalendar.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class ICalendar {
  2. final List<ICalendarEvent> _events = [];
  3. void addEvent({
  4. required String summary,
  5. required String description,
  6. required DateTime startTime,
  7. required DateTime endTime,
  8. required List<dynamic> attendees,
  9. }) {
  10. _events.add(
  11. ICalendarEvent(
  12. summary: summary,
  13. description: description,
  14. startTime: startTime,
  15. endTime: endTime,
  16. attendees: attendees,
  17. ),
  18. );
  19. }
  20. String serialize() {
  21. final dtstamp = DateTime.now();
  22. final buffer = StringBuffer();
  23. buffer.writeln('BEGIN:VCALENDAR');
  24. buffer.writeln('VERSION:2.0');
  25. buffer.writeln('PRODID:-//Your Organization//Your Product//EN');
  26. for (final event in _events) {
  27. buffer.writeln('BEGIN:VEVENT');
  28. buffer.writeln(
  29. 'UID:${DateTime.now().millisecondsSinceEpoch}-${event.hashCode}');
  30. buffer.writeln('DTSTAMP:${_formatDateTime(dtstamp)}');
  31. buffer.writeln('DTSTART:${_formatDateTime(event.startTime)}');
  32. buffer.writeln('DTEND:${_formatDateTime(event.endTime)}');
  33. buffer.writeln('SUMMARY:${event.summary}');
  34. buffer.writeln('DESCRIPTION:${event.description}');
  35. for (final attendee in event.attendees) {
  36. buffer.writeln(
  37. 'ATTENDEE;CN="${attendee.toString()}";ROLE=REQ-PARTICIPANT;RSVP=TRUE:mailto:test@me.com');
  38. }
  39. buffer.writeln('END:VEVENT');
  40. }
  41. buffer.writeln('END:VCALENDAR');
  42. return buffer.toString();
  43. }
  44. String _formatDateTime(DateTime dt) {
  45. return '${dt.toUtc().toString().replaceAll('-', '').replaceAll(':', '').replaceAll(' ', 'T').split('.')[0]}Z';
  46. }
  47. }
  48. class ICalendarEvent {
  49. final String summary;
  50. final String description;
  51. final DateTime startTime;
  52. final DateTime endTime;
  53. final List<dynamic> attendees;
  54. ICalendarEvent({
  55. required this.summary,
  56. required this.description,
  57. required this.startTime,
  58. required this.endTime,
  59. required this.attendees,
  60. });
  61. }