예제 #1
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");
            }
        }
예제 #2
0
        public void PoppillReminder(int _ID, string _name, string _day, int _count, TimeSpan _time)
        {
            long     EventID      = _ID;
            TimeSpan Time         = _time;
            string   Day          = _day;
            string   Title        = Day;
            int      Count        = _count;
            string   PillName     = _name;
            int      recurringday = 0;
            var      Nowdtime     = DateTime.Today;

            int day = GetDay(Day, Nowdtime, ref recurringday);

            Nowdtime = Nowdtime.AddDays(day);
            var Datewithtime = Nowdtime.Add(Time);


            NSDate newNSdatetime = ConvertDateTimeToNSDate(Datewithtime);
            NSDate nsEndDate     = NSDate.DistantFuture;


            CalendarHelper.Current.EventStore.RequestAccess(EKEntityType.Event,
                                                            (bool granted, NSError e) =>
            {
                if (granted)
                {
                    EKEvent newEvent   = EKEvent.FromStore(CalendarHelper.Current.EventStore);
                    newEvent.StartDate = newNSdatetime;
                    newEvent.AddRecurrenceRule(new EKRecurrenceRule(EKRecurrenceFrequency.Weekly, 1, EKRecurrenceEnd.FromEndDate(nsEndDate)));
                    newEvent.Title    = "Take your " + Title;
                    newEvent.Notes    = "Time to take " + Count + " " + PillName + " pill/s";
                    newEvent.Calendar = CalendarHelper.Current.EventStore.DefaultCalendarForNewEvents;

                    NSError a;
                    try
                    {   // Save Note to Calendar to get UUID
                        CalendarHelper.Current.EventStore.SaveEvent(newEvent, EKSpan.ThisEvent, true, out a);
                        if (a != null)
                        {
                            // Disable UIKit thread checks for a couple of methods
                            var previous = UIApplication.CheckForIllegalCrossThreadCalls;
                            UIApplication.CheckForIllegalCrossThreadCalls = false;

                            var issueView = new UIAlertView("Issues saving Reminder", a.ToString(), null, "ok", null);
                            issueView.Show();


                            // Restore
                            UIApplication.CheckForIllegalCrossThreadCalls = previous;
                            return;
                        }
                        else
                        {
                            // Disable UIKit thread checks for a couple of methods
                            var previous = UIApplication.CheckForIllegalCrossThreadCalls;
                            UIApplication.CheckForIllegalCrossThreadCalls = false;

                            // Test: Show EventIdentifier
                            var SavedView = new UIAlertView(newEvent.EventIdentifier, "The event has been saved", null, "ok", null);
                            SavedView.Show();
                            //ViewModel.DataManager.InsertCalID(e.PTPEvent.Id, e.PTPEvent.FilmId, newEvent.Eventidentifier);
                            // ViewModel.DataManager.InsertCalID(e.PTPEvent.Id, e.PTPEvent.FilmId, newEvent.UUID);

                            // Restore
                            UIApplication.CheckForIllegalCrossThreadCalls = previous;
                        }
                    }
                    catch
                    {
                        // Disable UIKit thread checks for a couple of methods
                        var previous = UIApplication.CheckForIllegalCrossThreadCalls;
                        UIApplication.CheckForIllegalCrossThreadCalls = false;

                        var ErrorView = new UIAlertView("Event", "Issues accessing the Calendar to save reminder", null, "ok", null);
                        ErrorView.Show();

                        // Restore
                        UIApplication.CheckForIllegalCrossThreadCalls = previous;
                    }
                }
                else
                {
                    // Disable UIKit thread checks for a couple of methods
                    var previous = UIApplication.CheckForIllegalCrossThreadCalls;
                    UIApplication.CheckForIllegalCrossThreadCalls = false;

                    var accessDeniedView = new UIAlertView("Access Denied", "user Denied Access to Calendar Data", null, "ok", null);
                    accessDeniedView.Show();

                    // Restore
                    UIApplication.CheckForIllegalCrossThreadCalls = previous;
                }
            });
        }