Exemplo n.º 1
0
        EKCalendar SaveEKCalendar(EKSource source, string calendarName, string color = null)
        {
            var calendar = EKCalendar.Create(EKEntityType.Event, _eventStore);

            //Setup calendar to be inserted
            calendar.Title = calendarName;

            NSError error = null;

            if (!string.IsNullOrEmpty(color))
            {
                calendar.CGColor = ColorConversion.ToCGColor(color);
            }

            calendar.Source = source;

            if (_eventStore.SaveCalendar(calendar, true, out error))
            {
                return(calendar);
            }

            _eventStore.Reset();

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to create a new calendar with the specified source/name/color.
        /// May fail depending on source.
        /// </summary>
        /// <remarks>
        /// This is only intended as a helper method for CreateEKCalendar,
        /// not to be called independently.
        /// </remarks>
        /// <returns>The created native calendar, or null on failure</returns>
        /// <param name="source">Calendar source (e.g. iCloud vs local vs gmail)</param>
        /// <param name="calendarName">Calendar name.</param>
        /// <param name="color">Calendar color.</param>
        private EKCalendar SaveEKCalendar(EKSource source, string calendarName, string color = null)
        {
            var calendar = EKCalendar.Create(EKEntityType.Event, _eventStore);

            // Setup calendar to be inserted
            //
            calendar.Title = calendarName;

            if (!string.IsNullOrEmpty(color))
            {
                calendar.CGColor = ColorConversion.ToCGColor(color);
            }

            calendar.Source = source;

            NSError error = null;

            if (_eventStore.SaveCalendar(calendar, true, out error))
            {
                Console.WriteLine($"Successfully saved calendar with source {source.Title}");

                // TODO: Should we try calling GetCalendars to make sure that
                //       the calendar isn't hidden??
                //
                return(calendar);
            }
            else
            {
                Console.WriteLine($"Tried and failed to save calendar with source {source.Title}");
            }

            _eventStore.Reset();

            return(null);
        }
Exemplo n.º 3
0
        static EKCalendar CreateNewCalendar()
        {
            NSError    err;
            EKCalendar cal = EKCalendar.Create(EKEntityType.Event, EventStore);

            cal.Title = "Evènements Epitech";
            foreach (EKSource s in EventStore.Sources)
            {
                if (s.SourceType == EKSourceType.CalDav)
                {
                    cal.Source = s;
                    break;
                }
            }
            if (cal.Source == null)
            {
                foreach (EKSource s in EventStore.Sources)
                {
                    if (s.SourceType == EKSourceType.Local)
                    {
                        cal.Source = s;
                        break;
                    }
                }
            }
            bool didwell = EventStore.SaveCalendar(cal, true, out err);

            if (!didwell)
            {
                throw new Exception("SaveCalendar failed");
            }
            NSUserDefaults.StandardUserDefaults.SetString(cal.CalendarIdentifier, CalendarIDKey);
            NSUserDefaults.StandardUserDefaults.Synchronize();
            return(cal);
        }
Exemplo n.º 4
0
        public void FromEventStoreWithReminder()
        {
            if (!TestRuntime.CheckXcodeVersion(4, 5))
            {
                Assert.Inconclusive("+[EKCalendar calendarForEntityType:eventStore:]: unrecognized selector before 6.0");
            }

            var c = EKCalendar.Create(EKEntityType.Reminder, new EKEventStore());

            // defaults
#if __WATCHOS__
            Assert.False(c.AllowsContentModifications, "AllowsContentModifications");
#else
            Assert.True(c.AllowsContentModifications, "AllowsContentModifications");
#endif
            Assert.NotNull(c.CalendarIdentifier, "CalendarIdentifier");
            Assert.Null(c.CGColor, "CGColor");

#if __WATCHOS__
            Assert.True(c.Immutable, "Immutable");
#else
            Assert.False(c.Immutable, "Immutable");
#endif
            Assert.Null(c.Source, "Source");
            Assert.False(c.Subscribed, "Subscribed");
            Assert.That(c.SupportedEventAvailabilities, Is.EqualTo(EKCalendarEventAvailability.None), "SupportedEventAvailabilities");
            Assert.Null(c.Title, "Title");
            Assert.That(c.Type, Is.EqualTo(EKCalendarType.Local), "Type");
            Assert.AreEqual(EKEntityMask.Reminder, c.AllowedEntityTypes, "AllowedEntityTypes");
            Assert.IsNotNull(c.CalendarIdentifier, "CalendarIdentifier");
        }
        private EKCalendar CreateEKCalendar(string calendarName, string color = null)
        {
            var calendar = EKCalendar.Create(EKEntityType.Event, _eventStore);

            calendar.Source = _eventStore.Sources.First(source => source.SourceType == EKSourceType.Local);
            calendar.Title  = calendarName;

            if (!string.IsNullOrEmpty(color))
            {
                calendar.CGColor = ColorConversion.ToCGColor(color);
            }

            NSError error = null;

            if (!_eventStore.SaveCalendar(calendar, true, out error))
            {
                // Without this, the eventStore may return the new calendar 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 PlatformException(error.LocalizedDescription, new NSErrorException(error));
            }

            return(calendar);
        }
Exemplo n.º 6
0
        public void FromEventStore_Null()
        {
#if MONOMAC
            EKCalendar.Create(EKEntityType.Event, null);
#else
            EKCalendar.FromEventStore(null);
#endif
        }
Exemplo n.º 7
0
        public void FromEventStore_Null()
        {
#if MONOMAC
            Assert.Throws <ArgumentNullException> (() => EKCalendar.Create(EKEntityType.Event, null));
#else
            Assert.Throws <ArgumentNullException> (() => EKCalendar.FromEventStore(null));
#endif
        }
Exemplo n.º 8
0
        public void FromEventStore()
        {
            RequestPermission();

            EKEventStore store = new EKEventStore();

#if MONOMAC || __MACCATALYST__
            var c = EKCalendar.Create(EKEntityType.Event, store);
#else
            var c = EKCalendar.FromEventStore(store);
#endif
            // defaults
#if __WATCHOS__
            Assert.False(c.AllowsContentModifications, "AllowsContentModifications");
#else
            Assert.True(c.AllowsContentModifications, "AllowsContentModifications");
#endif
            Assert.NotNull(c.CalendarIdentifier, "CalendarIdentifier");
#if MONOMAC
            Assert.Null(c.Color, "Color");
#else
            Assert.Null(c.CGColor, "CGColor");
#endif

            if (TestRuntime.CheckXcodeVersion(4, 5))
            {
                // default value changed for iOS 6.0 beta 1
#if __WATCHOS__
                Assert.True(c.Immutable, "Immutable");
#else
                Assert.False(c.Immutable, "Immutable");
#endif
                // new in 6.0
                Assert.AreEqual(EKEntityMask.Event, c.AllowedEntityTypes, "AllowedEntityTypes");
            }
            else
            {
                Assert.True(c.Immutable, "Immutable");
            }

            Assert.Null(c.Source, "Source");
            Assert.False(c.Subscribed, "Subscribed");
#if MONOMAC || __MACCATALYST__
            Assert.That(c.SupportedEventAvailabilities, Is.EqualTo(EKCalendarEventAvailability.Busy | EKCalendarEventAvailability.Free), "SupportedEventAvailabilities");
            Assert.That(c.Title, Is.EqualTo(string.Empty), "Title");
#else
            Assert.That(c.SupportedEventAvailabilities, Is.EqualTo(EKCalendarEventAvailability.None), "SupportedEventAvailabilities");
            if (TestRuntime.CheckXcodeVersion(13, 2))
            {
                Assert.That(c.Title, Is.EqualTo(string.Empty), "Title");
            }
            else
            {
                Assert.Null(c.Title, "Title");
            }
#endif
            Assert.That(c.Type, Is.EqualTo(EKCalendarType.Local), "Type");
        }
Exemplo n.º 9
0
        public void Title()
        {
            EKEventStore store = new EKEventStore();

#if MONOMAC
            var c = EKCalendar.Create(EKEntityType.Event, store);
#else
            var c = EKCalendar.FromEventStore(store);
#endif
            c.Title = "my title";
            Assert.That(c.Title, Is.EqualTo("my title"), "Title");
        }
Exemplo n.º 10
0
        public void Title()
        {
            RequestPermission();

            EKEventStore store = new EKEventStore();

#if MONOMAC || __MACCATALYST__
            var c = EKCalendar.Create(EKEntityType.Event, store);
#else
            var c = EKCalendar.FromEventStore(store);
#endif
            c.Title = "my title";
            Assert.That(c.Title, Is.EqualTo("my title"), "Title");
        }
Exemplo n.º 11
0
        public async Task <bool> CreateCalendarForAppAlarmsAsync()
        {
            bool hasPermission = await ASkForPermissionsAsync();

            if (hasPermission && appCalendar == null)
            {
                appCalendar         = EKCalendar.Create(EKEntityType.Event, eventsStore);
                appCalendar.Title   = "My App Calendar";
                appCalendar.Source  = eventsStore.Sources.Where(s => s.SourceType == EKSourceType.CalDav).FirstOrDefault();
                appCalendar.CGColor = UIColor.Purple.CGColor;
                bool saved = eventsStore.SaveCalendar(appCalendar, true, out NSError e);
                return(saved);
            }
            return(true);
        }
Exemplo n.º 12
0
        private void AddGoHejaCalendarToDevice()
        {
            try
            {
                NSError error;

                ////remove existing descending events from now in "goHeja Events" calendar of device.
                var calendars = App.Current.EventStore.GetCalendars(EKEntityType.Event);
                foreach (var calendar in calendars)
                {
                    if (calendar.Title == PortableLibrary.Constants.DEVICE_CALENDAR_TITLE)
                    {
                        goHejaCalendar = calendar;

                        EKCalendar[] calendarArray = new EKCalendar[1];
                        calendarArray[0] = calendar;
                        NSPredicate pEvents   = App.Current.EventStore.PredicateForEvents(NSDate.Now.AddSeconds(-(3600 * 10000)), NSDate.Now.AddSeconds(3600 * 10000), calendarArray);
                        EKEvent[]   allEvents = App.Current.EventStore.EventsMatching(pEvents);
                        if (allEvents == null)
                        {
                            continue;
                        }
                        foreach (var pEvent in allEvents)
                        {
                            NSError  pE;
                            DateTime now         = DateTime.Now;
                            DateTime startNow    = new DateTime(now.Year, now.Month, now.Day);
                            var      startString = baseVC.ConvertDateTimeToNSDate(startNow);
                            if (pEvent.StartDate.Compare(startString) == NSComparisonResult.Descending)
                            {
                                App.Current.EventStore.RemoveEvent(pEvent, EKSpan.ThisEvent, true, out pE);
                            }
                        }
                    }
                }

                if (goHejaCalendar == null)
                {
                    goHejaCalendar = EKCalendar.Create(EKEntityType.Event, App.Current.EventStore);
                    EKSource goHejaSource = null;

                    foreach (EKSource source in App.Current.EventStore.Sources)
                    {
                        if (source.SourceType == EKSourceType.CalDav && source.Title == "iCloud")
                        {
                            goHejaSource = source;
                            break;
                        }
                    }
                    if (goHejaSource == null)
                    {
                        foreach (EKSource source in App.Current.EventStore.Sources)
                        {
                            if (source.SourceType == EKSourceType.Local)
                            {
                                goHejaSource = source;
                                break;
                            }
                        }
                    }
                    if (goHejaSource == null)
                    {
                        return;
                    }
                    goHejaCalendar.Title  = PortableLibrary.Constants.DEVICE_CALENDAR_TITLE;
                    goHejaCalendar.Source = goHejaSource;
                }

                App.Current.EventStore.SaveCalendar(goHejaCalendar, true, out error);

                if (error == null)
                {
                    AddEvents();
                }
            }
            catch (Exception e)
            {
                new UIAlertView("add events process", e.Message, null, "ok", null).Show();
            }
        }