|
| 1 | +# firstfloor_calendar |
| 2 | + |
| 3 | +[](https://pub.dev/packages/firstfloor_calendar) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +A Dart library for parsing iCalendar (.ics) files with RFC 5545 support. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- Parse iCalendar files into strongly typed models |
| 11 | +- Support for events, todos, journals, and timezones |
| 12 | +- Full RRULE recurrence expansion |
| 13 | +- Stream large files without loading into memory |
| 14 | +- Extensible with custom property parsers |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```yaml |
| 19 | +dependencies: |
| 20 | + firstfloor_calendar: ^1.0.0 |
| 21 | +``` |
| 22 | +
|
| 23 | +## Usage |
| 24 | +
|
| 25 | +### Basic Parsing |
| 26 | +
|
| 27 | +Parse iCalendar text into a strongly typed `Calendar` object. The parser handles all RFC 5545 components including events, todos, journals, and timezones. |
| 28 | + |
| 29 | +```dart |
| 30 | +import 'package:firstfloor_calendar/firstfloor_calendar.dart'; |
| 31 | +
|
| 32 | +final icsContent = ''' |
| 33 | +BEGIN:VCALENDAR |
| 34 | +VERSION:2.0 |
| 35 | +PRODID:-//Example//EN |
| 36 | +BEGIN:VEVENT |
| 37 | +UID:event-123@example.com |
| 38 | +DTSTART:20240315T100000Z |
| 39 | +DTEND:20240315T110000Z |
| 40 | +SUMMARY:Team Meeting |
| 41 | +END:VEVENT |
| 42 | +END:VCALENDAR'''; |
| 43 | +
|
| 44 | +final parser = CalendarParser(); |
| 45 | +final calendar = parser.parseFromString(icsContent); |
| 46 | +
|
| 47 | +for (final event in calendar.events) { |
| 48 | + print('${event.summary}: ${event.dtstart}'); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Working with Events |
| 53 | + |
| 54 | +Access event properties with full type safety. Required properties like `uid` and `dtstart` are non-nullable, while optional properties return nullable values. |
| 55 | + |
| 56 | +```dart |
| 57 | +final event = calendar.events.first; |
| 58 | +
|
| 59 | +// Required properties |
| 60 | +print('UID: ${event.uid}'); |
| 61 | +print('Start: ${event.dtstart}'); |
| 62 | +
|
| 63 | +// Optional properties |
| 64 | +print('Summary: ${event.summary ?? "Untitled"}'); |
| 65 | +print('Location: ${event.location ?? "No location"}'); |
| 66 | +print('Description: ${event.description ?? ""}'); |
| 67 | +
|
| 68 | +// Attendees |
| 69 | +for (final attendee in event.attendees) { |
| 70 | + print('Attendee: ${attendee.address}'); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Recurring Events |
| 75 | + |
| 76 | +Generate occurrences from recurrence rules (RRULE). The `occurrences()` method returns a lazy stream that handles both recurring and non-recurring events gracefully. |
| 77 | + |
| 78 | +```dart |
| 79 | +final event = calendar.events.first; |
| 80 | +
|
| 81 | +// Get first 10 occurrences |
| 82 | +for (final occurrence in event.occurrences().take(10)) { |
| 83 | + print('Occurrence: $occurrence'); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Streaming Large Files |
| 88 | + |
| 89 | +Parse large iCalendar files efficiently using the streaming parser. Components are processed one at a time without loading the entire file into memory. |
| 90 | + |
| 91 | +```dart |
| 92 | +import 'dart:io'; |
| 93 | +
|
| 94 | +final file = File('large-calendar.ics'); |
| 95 | +final streamParser = DocumentStreamParser(); |
| 96 | +
|
| 97 | +await for (final component in streamParser.streamComponents(file.openRead())) { |
| 98 | + if (component.name == 'VEVENT') { |
| 99 | + final summary = component.properties |
| 100 | + .where((p) => p.name == 'SUMMARY') |
| 101 | + .firstOrNull |
| 102 | + ?.value; |
| 103 | + print('Event: ${summary ?? "Untitled"}'); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Custom Property Parsers |
| 109 | + |
| 110 | +Extend the parser with custom property handlers for vendor-specific or experimental properties. Register custom parsers before parsing your calendar data. |
| 111 | + |
| 112 | +```dart |
| 113 | +final parser = CalendarParser(); |
| 114 | +
|
| 115 | +parser.registerPropertyRule( |
| 116 | + componentName: 'VEVENT', |
| 117 | + propertyName: 'X-CUSTOM-PRIORITY', |
| 118 | + rule: PropertyRule( |
| 119 | + parser: (property) { |
| 120 | + final value = int.tryParse(property.value); |
| 121 | + if (value == null || value < 1 || value > 10) { |
| 122 | + throw ParseException( |
| 123 | + 'X-CUSTOM-PRIORITY must be between 1-10', |
| 124 | + lineNumber: property.lineNumber, |
| 125 | + ); |
| 126 | + } |
| 127 | + return value; |
| 128 | + }, |
| 129 | + ), |
| 130 | +); |
| 131 | +
|
| 132 | +final calendar = parser.parseFromString(icsContent); |
| 133 | +final priority = calendar.events.first.value<int>('X-CUSTOM-PRIORITY'); |
| 134 | +``` |
| 135 | + |
| 136 | +## Architecture |
| 137 | + |
| 138 | +The library uses a two-layer architecture: |
| 139 | + |
| 140 | +- **Document Layer** (`DocumentParser`): Parses raw .ics text into an untyped tree structure |
| 141 | +- **Semantic Layer** (`CalendarParser`): Converts the document tree into strongly typed models with validation |
| 142 | + |
| 143 | +Use `DocumentParser` for low-level access, and `CalendarParser` for most applications. |
| 144 | + |
| 145 | +## License |
| 146 | + |
| 147 | +MIT License - see [LICENSE](LICENSE) file for details. |
0 commit comments