Exemple #1
0
        /// <summary>
        /// Gets a single calendar by platform-specific ID.
        /// </summary>
        /// <param name="externalId">Platform-specific calendar identifier</param>
        /// <returns>The corresponding calendar, or null if not found</returns>
        /// <exception cref="System.UnauthorizedAccessException">Calendar access denied</exception>
        /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public async Task <Calendar> GetCalendarByIdAsync(string externalId)
        {
            if (string.IsNullOrWhiteSpace(externalId))
            {
                return(null);
            }

            await EnsureInitializedAsync().ConfigureAwait(false);

            bool writeable = true;

            //var calendar = await _localApptStore.GetAppointmentCalendarAsync(externalId).ConfigureAwait(false);
            var calendar = await GetLocalCalendarAsync(externalId).ConfigureAwait(false);

            if (calendar == null)
            {
                writeable = false;

                // This throws an ArgumentException if externalId is not in the valid
                // WinPhone calendar ID format. Oddly, the above otherwise-identical call to
                // the local appointment store does not...
                //
                calendar = await _apptStore.GetAppointmentCalendarAsync(externalId).ConfigureAwait(false);
            }

            return(calendar == null ? null : calendar.ToCalendar(writeable));
        }
Exemple #2
0
        /// <summary>
        /// Sets/replaces the event reminder for the specified calendar event
        /// </summary>
        /// <param name="calendarEvent">Event to add the reminder to</param>
        /// <param name="reminder">The reminder</param>
        /// <returns>If successful</returns>
        /// <exception cref="ArgumentException">Calendar event is not created or not valid</exception>
        /// <exception cref="System.InvalidOperationException">Editing recurring events is not supported</exception>
        /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public async Task <bool> AddEventReminderAsync(CalendarEvent calendarEvent, CalendarEventReminder reminder)
        {
            if (string.IsNullOrEmpty(calendarEvent.ExternalID))
            {
                throw new ArgumentException("Missing calendar event identifier", "calendarEvent");
            }

            await EnsureInitializedAsync().ConfigureAwait(false);

            var existingAppt = await _localApptStore.GetAppointmentAsync(calendarEvent.ExternalID);

            if (existingAppt == null)
            {
                throw new ArgumentException("Specified calendar event not found on device");
            }

            if (existingAppt.Recurrence != null)
            {
                throw new InvalidOperationException("Editing recurring events is not supported");
            }

            var appCalendar = await _localApptStore.GetAppointmentCalendarAsync(existingAppt.CalendarId);

            if (appCalendar == null)
            {
                throw new ArgumentException("Event does not have a valid calendar.");
            }

            existingAppt.Reminder = reminder?.TimeBefore ?? TimeSpan.FromMinutes(15);

            await appCalendar.SaveAppointmentAsync(existingAppt);

            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Adds an event reminder to specified calendar event
        /// </summary>
        /// <param name="calendarEvent">Event to add the reminder to</param>
        /// <param name="reminder">The reminder</param>
        /// <returns>Success or failure</returns>
        /// <exception cref="ArgumentException">If calendar event is not created or not valid</exception>
        /// <exception cref="Calendars.Plugin.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public async Task <bool> AddEventReminderAsync(CalendarEvent calendarEvent, CalendarEventReminder reminder)
        {
            if (string.IsNullOrEmpty(calendarEvent.ExternalID))
            {
                throw new ArgumentException("Missing calendar event identifier", "calendarEvent");
            }


            var existingAppt = await _localApptStore.GetAppointmentAsync(calendarEvent.ExternalID);


            if (existingAppt == null)
            {
                throw new ArgumentException("Specified calendar event not found on device");
            }


            var appCalendar = await _localApptStore.GetAppointmentCalendarAsync(existingAppt.CalendarId);

            if (appCalendar == null)
            {
                throw new ArgumentException("Event does not have a valid calendar.");
            }

            existingAppt.Reminder = reminder?.TimeBefore ?? TimeSpan.FromMinutes(15);

            await appCalendar.SaveAppointmentAsync(existingAppt);

            return(true);
        }
Exemple #4
0
            public async Task <Event[]> GetEventsAsync(Calendar calendar, DateTime startTime, DateTime endTime)
            {
                if (appointStore == null)
                {
                    return(new Event[0]);
                }
                var cal = await appointStore.GetAppointmentCalendarAsync(calendar.id).AsTask().ConfigureAwait(false);

                var src = await cal.FindAppointmentsAsync(startTime, endTime - startTime).AsTask().ConfigureAwait(false);

                var dst = new Event[src.Count];

                for (int i = 0; i < src.Count; i++)
                {
                    var ev = src[i];
                    if (ev.IsCanceledMeeting)
                    {
                        continue;
                    }
                    dst[i] = new Event()
                    {
                        subject   = ev.Subject,
                        startTime = ev.StartTime.DateTime,
                        duration  = ev.Duration,
                        allDay    = ev.AllDay,
                        uri       = String.IsNullOrEmpty(ev.OnlineMeetingLink) ? ev.Uri : new Uri(ev.OnlineMeetingLink)
                    };
                }
                return(dst);
            }
Exemple #5
0
        /// <summary>
        /// This is to handle a difference in the behavior of Windows UWP and WinPhone 8.1.
        /// On UWP, GetAppointmentCalendarAsync for a store created with AppCalendarsReadWrite
        /// access will still return non-app calendars that the app does not have write access
        /// to. FindAppointmentCalendarsAsync, however, does still respect the access type,
        /// so we just iterate.
        /// </summary>
        /// <remarks>
        /// Trying to save changes to the calendar would have thrown an appropriate
        /// UnauthorizedAccessException, but that would be inconsistent with our
        /// behavior on other platforms and wouldn't help with setting the
        /// CanEditCalendar/CanEditEvents properties.
        /// </remarks>
        /// <param name="id">Local calendar ID</param>
        /// <returns>App calendar with write access, or null if not found.</returns>
        private async Task <AppointmentCalendar> GetLocalCalendarAsync(string id)
        {
#if WINDOWS_UWP
            var calendars = await _localApptStore.FindAppointmentCalendarsAsync().ConfigureAwait(false);

            return(calendars.FirstOrDefault(cal => cal.LocalId == id));
#else
            return(await _localApptStore.GetAppointmentCalendarAsync(id).ConfigureAwait(false));
#endif
        }