persistent_device_calendar 0.0.1 copy "persistent_device_calendar: ^0.0.1" to clipboard
persistent_device_calendar: ^0.0.1 copied to clipboard

Persistence wrapper for device_calendar

Persistent Device Calendar #

Features #

This package contains a wrapper for the device_calendar plugin which adds persistence and quality-of-life improvements, reducing boilerplate code in common use-cases, as well as improving code readability and simplicity.

NOTE: This package currently only aims to provide basic calendar functionality, recurrence for events has not been tested with this package and is considered not supported.

Example #

void usage() async {
  var calPlugin = PersistentDeviceCalendar();
  if (!await calPlugin.setup()) return; // Setup permissions or return if not granted
  
  var start = TZDateTime.now(getLocation("America/Detroit")); // Constant debug times
  var end = start.add(const Duration(hours: 2));
  
  var calendar = await calPlugin.getCalendar<CustomEvent>(name: "Custom Events",
      builder: (event, calendarId, eventId) => Event(calendarId, eventId: eventId, title: event.name, start: start, end: end),
      idSelector: (event) => event.id
  );
  await calendar.prune(); // Optionally remove registered events which have been deleted on the device

  var events = [
    CustomEvent(id: "a", name: "Event A"),
    CustomEvent(id: "b", name: "Event B"), 
    CustomEvent(id: "c", name: "Event C")
  ];
  await calendar.put(events); // Put events, possibly replacing old version
}

class CustomEvent {

  String id;
  String name;

  CustomEvent({
    required this.id,
    required this.name,
  });
}

Timezones with TZDateTime (from device_calendar) #

Due to feedback we received, starting from 4.0.0 we will be using the timezone package to better handle all timezone data.

This is already included in this package. However, you need to add this line whenever the package is needed.

import 'package:timezone/timezone.dart';

If you don't need any timezone specific features in your app, you may use flutter_native_timezone to get your devices' current timezone, then convert your previous DateTime with it.

import 'package:flutter_native_timezone/flutter_native_timezone.dart';

// As an example, our default timezone is UTC.
Location _currentLocation = getLocation('Etc/UTC');

Future setCurentLocation() async {
  String timezone = 'Etc/UTC';
  try {
    timezone = await FlutterNativeTimezone.getLocalTimezone();
  } catch (e) {
    print('Could not get the local timezone');
  }
  _currentLocation = getLocation(timezone);
  setLocalLocation(_currentLocation);
}

...

event.start = TZDateTime.from(oldDateTime, _currentLocation);

For other use cases, feedback or future developments on the feature, feel free to open a discussion on GitHub.

Platform Setup (from device_calendar) #

Android Integration #

The following will need to be added to the AndroidManifest.xml file for your application to indicate permissions to modify calendars are needed

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

Proguard / R8 exceptions

By default, all android apps go through R8 for file shrinking when building a release version. Currently, it interferes with some functions such as retrieveCalendars().

You may add the following setting to the ProGuard rules file proguard-rules.pro (thanks to Britannio Jarrett). Read more about the issue here

-keep class com.builttoroam.devicecalendar.** { *; }

See here for an example setup.

For more information, refer to the guide at Android Developer

AndroidX migration

Since v.1.0, this version has migrated to use AndroidX instead of the deprecated Android support libraries. When using 0.10.0 and onwards for this plugin, please ensure your application has been migrated following the guide here

iOS Integration #

For iOS 10+ support, you'll need to modify the Info.plist to add the following key/value pair

<key>NSCalendarsUsageDescription</key>
<string>Access most functions for calendar viewing and editing.</string>

<key>NSContactsUsageDescription</key>
<string>Access contacts for event attendee editing.</string>

Note that on iOS, this is a Swift plugin. There is a known issue being tracked here by the Flutter team, where adding a plugin developed in Swift to an Objective-C project causes problems. If you run into such issues, please look at the suggested workarounds there.