public void Constructors()
 {
     using (var rule = new EKRecurrenceRule(EKRecurrenceFrequency.Daily, 9, null)) {
     }
     using (var rule = new EKRecurrenceRule(EKRecurrenceFrequency.Yearly, 8, null, null, null, null, null, null, null)) {
     }
 }
 public void DefaultProperties()
 {
     using (var rule = new EKRecurrenceRule()) {
         Assert.AreEqual("gregorian", rule.CalendarIdentifier, "CalendarIdentifier");
         Assert.IsNull(rule.RecurrenceEnd, "RecurrenceEnd");
         Assert.AreEqual(EKRecurrenceFrequency.Weekly, rule.Frequency, "Frequency");
         Assert.AreEqual(1, rule.Interval, "Interval");
         Assert.AreEqual(EKDay.Monday, rule.FirstDayOfTheWeek, "FirstDayOfTheWeek");
         Assert.IsNull(rule.DaysOfTheWeek, "DaysOfTheWeek");
         Assert.IsNull(rule.DaysOfTheMonth, "DaysOfTheMonth");
         Assert.IsNull(rule.DaysOfTheYear, "DaysOfTheYear");
         Assert.IsNull(rule.WeeksOfTheYear, "WeeksOfTheYear");
         Assert.IsNull(rule.MonthsOfTheYear, "MonthsOfTheYear");
         Assert.IsNull(rule.SetPositions, "SetPositions");
     }
 }
Beispiel #3
0
        /// <summary>
        /// Launchs the create new event controller.
        /// </summary>
        protected void LaunchCreateNewEvent(CalendarEntry entry)
        {
            EKEventStore store = IPhoneServiceLocator.CurrentDelegate.EventStore;
            EKCalendar calendar = store.DefaultCalendarForNewEvents;

            EKEvent calendarEvent = EKEvent.FromStore(store);
            // add event to the default calendar for the new events
            calendarEvent.Calendar = calendar;

            try {
                // add event details
                calendarEvent.Title = entry.Title;
                if(entry.Notes == null) {
                    entry.Notes = "";
                }
                calendarEvent.Notes = entry.Notes;
                calendarEvent.Location = entry.Location;
                calendarEvent.AllDay = entry.IsAllDayEvent;
                calendarEvent.StartDate = IPhoneUtils.DateTimeToNSDate(entry.StartDate);
                calendarEvent.EndDate = IPhoneUtils.DateTimeToNSDate(entry.EndDate);

                SystemLogger.Log(SystemLogger.Module.PLATFORM, "Creating Calendar Event: " + calendarEvent.Title);
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "Default Calendar: " + calendar.Title);
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "Event StartDate: " + calendarEvent.StartDate.ToString());
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "Event EndDate: " + calendarEvent.EndDate.ToString());
                // TODO: locate how to translate this features
                // entry.Type (birthday, exchange, etc)
                //calendarEvent.  = entry.IsEditable

                // Attendees
                if(entry.Attendees != null && entry.Attendees.Length >0) {
                    int attendeesNum = entry.Attendees.Length;
                    // TODO : check another way to add participants
                    // calendarEvent.Attendees --> READ ONLY !!!
                }

                // Alarms
                if(entry.Alarms != null && entry.Alarms.Length >0) {
                    foreach(CalendarAlarm alarm in entry.Alarms) {
                        EKAlarm eventAlarm = new EKAlarm();
                        eventAlarm.AbsoluteDate = IPhoneUtils.DateTimeToNSDate(alarm.Trigger);
                        // TODO: how to manage "action", "sound" and "emailaddress"
                        calendarEvent.AddAlarm(eventAlarm);
                    }
                }

                if (UIDevice.CurrentDevice.CheckSystemVersion (5, 0)) {
                    // Recurrence Rules
                    if(entry.IsRecurrentEvent && entry.Recurrence != null) {
                        EKRecurrenceFrequency recurrenceFrequency = EKRecurrenceFrequency.Daily;
                        if(entry.Recurrence.Type == RecurrenceType.Weekly) {
                            recurrenceFrequency = EKRecurrenceFrequency.Weekly;
                        } else if(entry.Recurrence.Type == RecurrenceType.Montly) {
                            recurrenceFrequency = EKRecurrenceFrequency.Monthly;
                        } else if(entry.Recurrence.Type == RecurrenceType.Yearly) {
                            recurrenceFrequency = EKRecurrenceFrequency.Yearly;
                        } else if(entry.Recurrence.Type == RecurrenceType.FourWeekly) {
                            recurrenceFrequency = EKRecurrenceFrequency.Weekly;
                            entry.Recurrence.Interval = 4; // force event to be repeated "every 4 weeks"
                        } else if(entry.Recurrence.Type == RecurrenceType.Fortnightly) {
                            recurrenceFrequency = EKRecurrenceFrequency.Weekly;
                            entry.Recurrence.Interval = 2; // force event to be repeated "every 2 weeks"
                        }
                        EKRecurrenceEnd recurrenceEnd = null;
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "Recurrence Frequency: " + recurrenceFrequency);
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "Recurrence Interval: " + entry.Recurrence.Interval);
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "Recurrence EndDate (requested): " + entry.Recurrence.EndDate.ToString());
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "Recurrence number: " + entry.RecurrenceNumber);
                        if(entry.Recurrence.EndDate.CompareTo(entry.EndDate)>0) {
                            recurrenceEnd = EKRecurrenceEnd.FromEndDate(IPhoneUtils.DateTimeToNSDate(entry.Recurrence.EndDate));
                            SystemLogger.Log(SystemLogger.Module.PLATFORM, "Recurrence EndDate (applied): " + recurrenceEnd.EndDate.ToString());
                        } else if(entry.RecurrenceNumber > 0) {
                            recurrenceEnd = EKRecurrenceEnd.FromOccurrenceCount((int)entry.RecurrenceNumber);
                            SystemLogger.Log(SystemLogger.Module.PLATFORM, "Recurrence OcurrenceCount: " + recurrenceEnd.OccurrenceCount);
                        } else {
                            recurrenceEnd = new EKRecurrenceEnd();
                        }
                        EKRecurrenceRule recurrenceRule = new EKRecurrenceRule(recurrenceFrequency,entry.Recurrence.Interval, recurrenceEnd);
                        if(entry.Recurrence.DayOfTheWeek > 0) {
                            EKRecurrenceDayOfWeek dayOfWeek = EKRecurrenceDayOfWeek.FromWeekDay(entry.Recurrence.DayOfTheWeek,0);
                            EKRecurrenceDayOfWeek[] arrayDayOfWeek = new EKRecurrenceDayOfWeek[1];
                            arrayDayOfWeek[0] = dayOfWeek;
                            SystemLogger.Log(SystemLogger.Module.PLATFORM, "Setting DayOfTheWeek: " + dayOfWeek.DayOfTheWeek);
                            recurrenceRule = new EKRecurrenceRule(recurrenceFrequency,entry.Recurrence.Interval, arrayDayOfWeek, null, null, null, null, null, recurrenceEnd);
                        }

                        calendarEvent.AddRecurrenceRule(recurrenceRule);
                    }
                }
            } catch (Exception e) {
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "ERROR Creating Calendar Event [" + calendarEvent.Title + "]. Error message: " + e.Message);
            }

            EKEventEditViewController eventViewController = new EKEventEditViewController();
            eventViewController.Event = calendarEvent;
            eventViewController.EventStore = store;
            eventViewController.Completed += delegate(object sender, EKEventEditEventArgs e) {
                UIApplication.SharedApplication.InvokeOnMainThread (delegate {
                    SystemLogger.Log(SystemLogger.Module.PLATFORM, "On EKEventEditViewController Completed: " + e.Action);
                    if(e.Action == EKEventEditViewAction.Saved) {
                        INotification notificationService = (INotification)IPhoneServiceLocator.GetInstance ().GetService ("notify");
                        if (notificationService != null) {
                            notificationService.StartNotifyAlert ("Calendar Alert", "Calendar Entry Saved", "OK");
                        }
                    }
                    IPhoneServiceLocator.CurrentDelegate.MainUIViewController ().DismissModalViewController(true);
                });
            };

            IPhoneServiceLocator.CurrentDelegate.MainUIViewController ().PresentModalViewController (eventViewController, true);
            IPhoneServiceLocator.CurrentDelegate.SetMainUIViewControllerAsTopController(false);
        }
Beispiel #4
0
        public async Task PushMeetingsToCalendarAsync(List <Meeting> meetings)
        {
            try
            {
                using (await _PushMeetingsToCalendarAsyncLock.LockAsync())
                {
                    if (!(await _eventStore.RequestAccessAsync(EKEntityType.Event)).Item1)
                    {
                        return;
                    }

                    var     calendar   = _eventStore.GetCalendar(AcraCalendarIdentifier);
                    CGColor colorToUse = null;
                    if (calendar != null)
                    {
                        colorToUse = calendar.CGColor;
                        NSError errorToDelete;;
                        _eventStore.RemoveCalendar(calendar, true, out errorToDelete);
                    }

                    var otherToDelete = _eventStore.GetCalendars(EKEntityType.Event).Where(c => c.Title.StartsWith("ACRA"));
                    foreach (var item in otherToDelete)
                    {
                        NSError errorToDelete;
                        _eventStore.RemoveCalendar(item, true, out errorToDelete);
                    }

                    // now recreate a calendar !
                    calendar       = EKCalendar.FromEventStore(_eventStore);
                    calendar.Title = "ACRA du " + DateTime.Now.ToString("g", new System.Globalization.CultureInfo("fr"));
                    if (colorToUse != null)
                    {
                        calendar.CGColor = colorToUse;
                    }

                    EKSource[] sources     = _eventStore.Sources;
                    var        sourceToSet = sources
                                             .FirstOrDefault(s => s.SourceType == EKSourceType.CalDav && s.Title.Equals("iCloud", StringComparison.InvariantCultureIgnoreCase));

                    if (sourceToSet == null)
                    {
                        sourceToSet = sources.FirstOrDefault(s => s.SourceType == EKSourceType.Local);
                    }

                    if (sourceToSet == null)
                    {
                        sourceToSet = _eventStore.DefaultCalendarForNewEvents.Source;
                    }

                    calendar.Source = sourceToSet;
                    NSError error;
                    _eventStore.SaveCalendar(calendar, true, out error);
                    AcraCalendarIdentifier = calendar.CalendarIdentifier;

                    var toSaves = meetings.OrderByDescending(ap => ap.StartDate).ToArray();

                    var howMuch = toSaves.Length;
                    for (int index = 0; index < howMuch; index++)
                    {
                        var appointData = toSaves[index];

                        var toSave = EKEvent.FromStore(_eventStore);

                        //if (!string.IsNullOrEmpty(appointData.))
                        //{
                        //    toSave.Location = appointData.Location;
                        //}
                        toSave.StartDate = ConvertDateTimeToNSDate(appointData.StartDate);
                        toSave.AllDay    = appointData.AllDayEvent;

                        if (appointData.IsRecurrent && appointData.AllDayEvent)
                        {
                            var end  = EKRecurrenceEnd.FromEndDate(ConvertDateTimeToNSDate(appointData.EndDate));
                            var rule = new EKRecurrenceRule(EKRecurrenceFrequency.Weekly, 1, end);
                            toSave.RecurrenceRules = new[] { rule };
                        }
                        else
                        {
                            toSave.EndDate = ConvertDateTimeToNSDate(appointData.EndDate);
                        }

                        // If set to AllDay and given an EndDate of 12am the next day, EventKit
                        // assumes that the event consumes two full days.
                        // (whereas WinPhone/Android consider that one day, and thus so do we)
                        //
                        if (appointData.AllDayEvent)
                        {
                            toSave.EndDate = ConvertDateTimeToNSDate(appointData.EndDate.AddDays(-1));
                        }

                        if (!appointData.IsHoliday)
                        {
                            if (appointData.Duration.TotalDays > 1 && !appointData.IsRecurrent)
                            {
                                toSave.Title = ((int)appointData.Duration.TotalDays + 1) + " " + appointData.Title;
                            }
                            else
                            {
                                toSave.Title = appointData.Title;
                            }
                        }
                        else
                        {
                            //TODO : localisation
                            toSave.Title = "[FERIE] " + appointData.Title;
                        }

                        toSave.Notes = appointData.Type.ToString();

                        toSave.Calendar = calendar;

                        NSError errorCommit;
                        _eventStore.SaveEvent(toSave, EKSpan.ThisEvent, out errorCommit);
                    }
                }

                NSError errorEvent;

                _eventStore.Commit(out errorEvent);
            }
            catch (Exception e)
            {
                //TODO : localisation
                await App.Instance.AlertService.ShowExceptionMessageAsync(e, "Impossible de renseigner votre calendrier iOS");
            }
        }