Exemple #1
0
        /// <summary>
        /// Gets a list of all calendars on the device.
        /// </summary>
        /// <returns>Calendars</returns>
        /// <exception cref="System.UnauthorizedAccessException">Calendar access denied</exception>
        /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public Task <IList <Calendar> > GetCalendarsAsync()
        {
            var            calendars = new List <Calendar>();
            CalendarRecord calRecord = null;

            try
            {
                calRecord = new CalendarRecord(Book.Uri);
                calRecord.Set <string>(Book.Name, "org.tizen.calendar");
                calendars.Add(
                    new Calendar
                {
                    Name            = calRecord.Get <string>(Book.Name),
                    ExternalID      = calRecord.Get <int>(Book.Id).ToString(),
                    CanEditEvents   = true,
                    CanEditCalendar = false,
                    AccountName     = calRecord.Get <int>(Book.AccountId).ToString(),
                    Color           = calRecord.Get <string>(Book.Color),
                }
                    );
            }
            finally
            {
                calRecord?.Dispose();
                calRecord = null;
            }
            return(Task.FromResult <IList <Calendar> >(calendars));
        }
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");
            }

            var existingEvt = await GetEventByIdAsync(calendarEvent.ExternalID).ConfigureAwait(false);

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

            CalendarManager calManager = null;
            CalendarRecord  calRecord = null, calAlarm = null;
            CalendarTime    startTime = null, calTime = null;

            try
            {
                calManager = new CalendarManager();
                calRecord  = calManager.Database.Get(Event.Uri, Convert.ToInt32(calendarEvent.ExternalID));
                startTime  = calRecord.Get <CalendarTime>(Event.Start);
                calTime    = new CalendarTime(startTime.UtcTime.AddMinutes(reminder?.TimeBefore.TotalMinutes ?? 15).Ticks);
                calAlarm   = new CalendarRecord(Alarm.Uri);

                calAlarm.Set <CalendarTime>(Alarm.AlarmTime, calTime);
                calAlarm.Set <int>(Alarm.TickUnit, (int)CalendarTypes.TickUnit.Specific);
                calRecord.AddChildRecord(Event.Alarm, calAlarm);
                calManager.Database.Update(calRecord);
            }
            finally
            {
                calAlarm?.Dispose();
                calAlarm = null;

                calRecord?.Dispose();
                calRecord = null;

                calManager?.Dispose();
                calManager = null;
            }
            return(true);
        }
Exemple #3
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 Task <Calendar> GetCalendarByIdAsync(string externalId)
        {
            Calendar       calendar  = null;
            CalendarRecord calRecord = null;

            try
            {
                calendar  = new Calendar();
                calRecord = new CalendarRecord(Book.Uri);
                calRecord.Set <string>(Book.Name, "org.tizen.calendar");
                calendar.Name            = calRecord.Get <string>(Book.Name);
                calendar.ExternalID      = calRecord.Get <int>(Book.Id).ToString();
                calendar.CanEditEvents   = true;
                calendar.CanEditCalendar = false;
                calendar.AccountName     = calRecord.Get <int>(Book.AccountId).ToString();
                calendar.Color           = calRecord.Get <string>(Book.Color);
            }
            finally
            {
                calRecord?.Dispose();
                calRecord = null;
            }
            return(Task.FromResult(calendar));
        }
Exemple #4
0
        /// <summary>
        /// Add new event to a calendar or update an existing event.
        /// Throws if Calendar ID is empty, calendar does not exist, or calendar is read-only.
        /// </summary>
        /// <param name="calendar">Destination calendar</param>
        /// <param name="calendarEvent">Event to add or update</param>
        /// <exception cref="System.ArgumentException">Calendar is not specified, does not exist on device, or is read-only</exception>
        /// <exception cref="System.UnauthorizedAccessException">Calendar access denied</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 AddOrUpdateEventAsync(Calendar calendar, CalendarEvent calendarEvent)
        {
            Calendar existingCal = null;

            if (!string.IsNullOrEmpty(calendar.ExternalID) || !string.IsNullOrEmpty(calendarEvent.ExternalID))
            {
                existingCal = await GetCalendarByIdAsync(calendar.ExternalID).ConfigureAwait(false);
            }

            if (calendarEvent.End < calendarEvent.Start)
            {
                throw new ArgumentException("End time may not precede start time", "calendarEvent");
            }

            if (!existingCal.CanEditEvents)
            {
                throw new ArgumentException("Cannot delete event from readonly calendar", "calendar");
            }
            else
            {
                CalendarManager calManager   = null;
                CalendarRecord  updateRecord = null;

                try
                {
                    calManager   = new CalendarManager();
                    updateRecord = calManager.Database.Get(Event.Uri, Convert.ToInt32(calendarEvent.ExternalID));

                    if (updateRecord != null)
                    {
                        try
                        {
                            DateTime startTime = calendarEvent.AllDay
                                                                        ? DateTime.SpecifyKind(calendarEvent.Start, DateTimeKind.Utc)
                                                                        : calendarEvent.Start;
                            DateTime endTime = calendarEvent.AllDay
                                                                        ? DateTime.SpecifyKind(calendarEvent.End, DateTimeKind.Utc)
                                                                        : calendarEvent.End;
                            updateRecord.Set(Event.Summary, calendarEvent.Name);
                            updateRecord.Set(Event.Start, ConvertIntPtrToCalendarTime(startTime));
                            updateRecord.Set(Event.End, ConvertIntPtrToCalendarTime(endTime));
                            updateRecord.Set(Event.Location, calendarEvent.Location);
                            updateRecord.Set(Event.Description, calendarEvent.Description);

                            calManager.Database.Update(updateRecord);
                        }
                        finally
                        {
                            updateRecord?.Dispose();
                            updateRecord = null;
                        }
                    }
                    else
                    {
                        CalendarRecord addRecord = null;

                        try
                        {
                            addRecord = new CalendarRecord(Event.Uri);

                            var start = DateTime.SpecifyKind(calendarEvent.Start, DateTimeKind.Utc);
                            var end   = DateTime.SpecifyKind(calendarEvent.End, DateTimeKind.Utc);
                            addRecord.Set(Event.Summary, calendarEvent.Name);
                            addRecord.Set(Event.Start, ConvertIntPtrToCalendarTime(start));
                            addRecord.Set(Event.End, ConvertIntPtrToCalendarTime(end));
                            addRecord.Set(Event.Location, calendarEvent.Location);
                            addRecord.Set(Event.Description, calendarEvent.Description);

                            calManager.Database.Insert(addRecord);
                        }
                        finally
                        {
                            addRecord?.Dispose();
                            addRecord = null;
                        }
                    }
                }
                finally
                {
                    calManager?.Dispose();
                    calManager = null;
                }
            }
        }