initCalendarSync function

Future<void> initCalendarSync({
  1. bool ignoreRejection = false,
})

Implementation

Future<void> initCalendarSync({bool ignoreRejection = false}) async {
  if (!await _isEnabled()) {
    _log.warning('Calendar Sync disabled');
    return;
  }
  if (!isSupportedPlatform) {
    _log.warning('Calendar Sync not available on this device');
    return;
  }
  final SharedPreferences preferences = await sharedPrefs();

  final hasPermission = await deviceCalendar.hasPermissions();

  if (hasPermission.data == false) {
    if (!ignoreRejection && (preferences.getBool(rejectionKey) ?? false)) {
      _log.warning('user previously rejected calendar sync. quitting');
      return;
    }
    final requesting = await deviceCalendar.requestPermissions();
    if (requesting.data == false) {
      await preferences.setBool(rejectionKey, true);
      _log.warning('user rejected calendar sync. quitting');
      return;
    }
  }
  // FOR DEBUGGING CLEAR Acter CALENDARS VIA:
  // await clearActerCalendars();

  final calendarId = await _getOrCreateCalendar();
  // clear if it existed before
  _subscription?.close();
  // start listening
  _subscription =
      ProviderScope.containerOf(rootNavKey.currentContext!, listen: true)
          .listen(
    eventsToSyncProvider,
    (prev, next) async {
      if (!next.hasValue) {
        _log.info('ignoring state change without value');
        return;
      }
      // FIXME: we probably want to debounce this ...
      await _refreshCalendar(calendarId, next.valueOrNull ?? []);
    },
    fireImmediately: true,
  );
}