builttoroam / device_calendar

A cross platform plugin for modifying calendars on the user's device
https://pub.dev/packages/device_calendar
BSD 3-Clause "New" or "Revised" License
259 stars 258 forks source link

Use the retrieveEvents method in the ios device to query the events of the system calendar #452

Open w9623 opened 1 year ago

w9623 commented 1 year ago

Describe the bug When I retrieve the schedule of the system calendar using an ios device, I call the retrieveEvents method, set the start time DateTime(1971, 1,1), DateTime(2049, 12,31), and the query result is empty

To Reproduce Steps to reproduce the behavior: 1.Creates a schedule in the device's system calendar with a schedule time between the start time of the call parameter. 2.Here's the code

final calendarsResult = await _deviceCalendarPlugin.retrieveCalendars();

      var _calendars = calendarsResult.data as List<Calendar>;
      for (var _calendar in _calendars) {
        var calendarEventsResult = await _deviceCalendarPlugin.retrieveEvents(
          _calendar.id,
          RetrieveEventsParams(
            startDate: DateTime(1971, 1, 1),
            endDate: DateTime(2049, 12, 31),
          ),
        );
        var _calendarEvents = calendarEventsResult.data as List<Event>;
      }

3._calendarEvents is empty

Expected behavior It seems that the start time needs to be set to 2019 before the query succeeds. I need to set a slightly distant historical time.

Screenshots

Device(s) tested This can be very important as not all device vendors do calendar in the same way.

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.0.5, on macOS 12.6.1 21G217 darwin-x64, locale zh-Hans-CN) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.2) [✓] Connected device (4 available) [✓] HTTP Host Availability

Additional context

IVLIVS-III commented 1 year ago

Without looking into this on the code-level, I can confirm from personal experience, that the retrieved List of Events is empty if the time range is too big.

It might be possible to query for past Events (even those in the distant past) by specifying a smaller time range, that also has the end date in the past. I didn't have time to test this workaround tho.

@w9623 in case you try this proposed potential workaround, could you please reply with the results. This would help us to find a solution for this issue.

w9623 commented 1 year ago

I tried to start before 2019 and the time range is very small, but I still can't retrieve the results

IVLIVS-III commented 1 year ago

Hmmm interesting. Thank you for trying it out.

VladyslavBilomeria commented 1 year ago

This is expected behavior for iOS, here's what the official documentation says about it:

For performance reasons, this method matches only those events within a four-year time span. If the date range between startDate and endDate is greater than four years, it’s shortened to the first four years.

As a workaround, we can do something like (example for the previous 50 years and the next 50 years):

int rangeSize = 4;
List<Event> events = [];
DateTime currentDate = DateTime.now();
DateTime startDate = DateTime(currentDate.year - 50, currentDate.month, currentDate.day);
DateTime endDate = DateTime(currentDate.year + 50, currentDate.month, currentDate.day);

for (int year = startDate.year; year <= endDate.year; year += rangeSize) {
  DateTime start = DateTime(year, startDate.month, startDate.day);
  DateTime end = DateTime(year + rangeSize, endDate.month, endDate.day);
  final eventsResult = await _deviceCalendarPlugin.retrieveEvents(
    calendar.id!,
    RetrieveEventsParams(startDate: start, endDate: end),
  );
  if (eventsResult.isSuccess) {
    events.addAll(eventsResult.data!);
  }
}

It might be better to make it part of a plugin