Skip to content

Instantly share code, notes, and snippets.

@arpit
Created July 11, 2024 16:40
Show Gist options
  • Save arpit/d8457b7e87b71d0ca0e1f410d7b9ff9c to your computer and use it in GitHub Desktop.
Save arpit/d8457b7e87b71d0ca0e1f410d7b9ff9c to your computer and use it in GitHub Desktop.
Dart code for calendar event creation
import 'package:app_client/shared/models/video_asset_preview.dart';
import 'package:intl/intl.dart';
class CalendarHelpers{
static String _formatDateTime(DateTime dateTime) {
final DateFormat formatter = DateFormat('yyyyMMddTHHmmss');
return '${formatter.format(dateTime)}Z';
}
static String createICalString(VideoAssetPreview mediaAsset){
String eventName = mediaAsset.name;
DateTime eventStart = mediaAsset.localStartTime;
DateTime eventEnd = mediaAsset.localStartTime.add(const Duration(minutes: 30));
String location = mediaAsset.providerName;
// Create the iCal content
final StringBuffer icalContent = StringBuffer()
..writeln('BEGIN:VCALENDAR')
..writeln('VERSION:2.0')
..writeln('PRODID:-//Xfinity//Xfinity Rewards//EN')
..writeln('BEGIN:VEVENT')
..writeln('UID:${DateTime.now().millisecondsSinceEpoch}@comcast.com')
..writeln('DTSTAMP:${_formatDateTime(DateTime.now().toUtc())}')
..writeln('DTSTART:${_formatDateTime(eventStart.toUtc())}')
..writeln('DTEND:${_formatDateTime(eventEnd.toUtc())}')
..writeln('LOCATION:$location')
..writeln('SUMMARY:$eventName')
..writeln('END:VEVENT')
..writeln('END:VCALENDAR');
return icalContent.toString().trim().replaceAll('\n', '\r\n');
}
}
//
void addCalendarEvent(String calendarString) {
// Convert the iCal content to a Blob
final bytes = utf8.encode(calendarString);
final blob = Blob([bytes], 'text/calendar');
// Create a URL for the Blob
final url = Url.createObjectUrlFromBlob(blob);
//Create an anchor element and set its href to the Blob URL
final anchor = AnchorElement(href: url)
..setAttribute('download', 'event.ics')
..click();
//Revoke the Blob URL to free up memory
Url.revokeObjectUrl(url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment