Ejemplo n.º 1
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 Task <bool> AddEventReminderAsync(CalendarEvent calendarEvent, CalendarEventReminder reminder)
        {
            if (string.IsNullOrEmpty(calendarEvent.ExternalID))
            {
                throw new ArgumentException("Missing calendar event identifier", "calendarEvent");
            }

            //Grab current event
            var existingEvent = _eventStore.EventFromIdentifier(calendarEvent.ExternalID);

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

            //
            var seconds = -reminder?.TimeBefore.TotalSeconds ?? defaultTimeBefore;
            var alarm   = EKAlarm.FromTimeInterval(seconds);

            existingEvent.AddAlarm(alarm);
            NSError error = null;

            if (!_eventStore.SaveEvent(existingEvent, EKSpan.ThisEvent, out error))
            {
                // Without this, the eventStore will continue to return the "updated"
                // event even though the save failed!
                // (this obviously also resets any other changes, but since we own the eventStore
                //  we can be pretty confident that won't be an issue)
                //
                _eventStore.Reset();


                throw new ArgumentException(error.LocalizedDescription, "reminder", new NSErrorException(error));
            }


            return(Task.FromResult(true));
        }
Ejemplo n.º 2
0
        public async Task Calendars_AddEventReminder_ReplacesExistingReminder()
        {
            var calendarEvent = GetTestEvent();
            var firstReminder = new CalendarEventReminder {
                TimeBefore = TimeSpan.FromMinutes(42)
            };
            var secondReminder = new CalendarEventReminder {
                TimeBefore = TimeSpan.FromMinutes(5)
            };
            var calendar = await _service.CreateCalendarAsync(_calendarName);

            calendarEvent.Reminders = new List <CalendarEventReminder> {
                firstReminder
            };

            await _service.AddOrUpdateEventAsync(calendar, calendarEvent);

            await _service.AddEventReminderAsync(calendarEvent, secondReminder);

            var eventFromId = await _service.GetEventByIdAsync(calendarEvent.ExternalID);

            Assert.AreEqual(secondReminder, eventFromId.Reminders.Single());
        }
Ejemplo n.º 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>
        /// <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 AddEventReminderAsync(CalendarEvent calendarEvent, CalendarEventReminder reminder)
        {
            if (string.IsNullOrEmpty(calendarEvent.ExternalID))
            {
                throw new ArgumentException("Missing calendar event identifier", nameof(calendarEvent));
            }

            await RequestCalendarAccess().ConfigureAwait(false);

            //Grab current event
            var existingEvent = _eventStore.EventFromIdentifier(calendarEvent.ExternalID);

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

            if (existingEvent.HasRecurrenceRules)
            {
                throw new InvalidOperationException("Editing recurring events is not supported");
            }

            existingEvent.AddAlarm(reminder.ToEKAlarm());

            if (!_eventStore.SaveEvent(existingEvent, EKSpan.ThisEvent, out NSError error))
            {
                // Without this, the eventStore will continue to return the "updated"
                // event even though the save failed!
                // (this obviously also resets any other changes, but since we own the eventStore
                //  we can be pretty confident that won't be an issue)
                //
                _eventStore.Reset();

                throw new ArgumentException(error.LocalizedDescription, nameof(reminder), new NSErrorException(error));
            }
        }
Ejemplo n.º 4
0
        public async void Calendars_AddOrUpdateEvents_NewEventMultipleReminders()
        {
            var calendarEvent = GetTestEvent();
            var reminder      = new CalendarEventReminder {
                TimeBefore = TimeSpan.FromMinutes(42)
            };
            var calendar = await _service.CreateCalendarAsync(_calendarName);

            calendarEvent.Reminders = new List <CalendarEventReminder>
            {
                new CalendarEventReminder {
                    TimeBefore = TimeSpan.FromMinutes(42)
                },
                new CalendarEventReminder {
                    TimeBefore = TimeSpan.FromMinutes(10)
                },
            };

            await _service.AddOrUpdateEventAsync(calendar, calendarEvent);

            var eventFromId = await _service.GetEventByIdAsync(calendarEvent.ExternalID);

            Assert.AreEqual(calendarEvent.Reminders, eventFromId.Reminders);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Not supported for Windows Store apps
 /// </summary>
 public Task <bool> AddEventReminderAsync(CalendarEvent calendarEvent, CalendarEventReminder reminder)
 {
     throw new NotSupportedException();
 }