public async Task <CalendarEventModel> GetCalendarEvent(string id)
        {
            var result = await RequestAccess(EKEntityType.Event);

            if (result.granted)
            {
                var evt = CalendarEvent.EventStore.EventFromIdentifier(id);
                if (evt != null)
                {
                    var model = new CalendarEventModel()
                    {
                        Id             = evt.EventIdentifier,
                        Description    = evt.Notes,
                        HasReminder    = evt.HasAlarms,
                        Title          = evt.Title,
                        EndTime        = evt.EndDate.ToDateTime(),
                        StartTime      = evt.StartDate.ToDateTime(),
                        DeviceCalendar = new CalendarAccount()
                        {
                            Id = evt.Calendar.CalendarIdentifier
                        }
                    };
                    return(model);
                }
            }
            return(null);
        }
Esempio n. 2
0
 public void CreateCalendarEvent(CalendarEventModel calEvent, Action <bool> callback)
 {
     try
     {
         Intent intent = new Intent(Intent.ActionInsert);
         intent.SetData(Provider.CalendarContract.Events.ContentUri);
         intent.PutExtra(Provider.CalendarContract.ExtraEventBeginTime, CurrentTimeMillis(calEvent.StartTime));
         intent.PutExtra(Provider.CalendarContract.EventsColumns.AllDay, false);
         intent.PutExtra(Provider.CalendarContract.EventsColumns.HasAlarm, calEvent.HasReminder);
         intent.PutExtra(Provider.CalendarContract.EventsColumns.EventLocation, calEvent.Location);
         intent.PutExtra(Provider.CalendarContract.EventsColumns.Description, calEvent.Description);
         intent.PutExtra(Provider.CalendarContract.ExtraEventEndTime, CurrentTimeMillis(calEvent.EndTime));
         intent.PutExtra(Provider.CalendarContract.EventsColumns.Title, calEvent.Title);
         Xamarin.Forms.Forms.Context.StartActivity(intent);
         callback?.Invoke(true);
     }
     catch (Exception ex)
     {
         callback?.Invoke(false);
     }
 }
Esempio n. 3
0
        public void CreateCalendarEvent(CalendarEventModel calEvent, Action <bool> callback)
        {
            if (CalendarEvent.eventStore == null)
            {
                CalendarEvent.eventStore = new EKEventStore();
            }

            RequestAccess(EKEntityType.Event, () =>
            {
                EKEvent newEvent = EKEvent.FromStore(CalendarEvent.eventStore);

                if (calEvent.HasReminder)
                {
                    newEvent.AddAlarm(EKAlarm.FromDate(ToNSDate(calEvent.StartTime.AddHours(-1))));
                }

                newEvent.StartDate = ToNSDate(calEvent.StartTime);
                newEvent.EndDate   = ToNSDate(calEvent.EndTime);
                newEvent.Title     = calEvent.Title;
                newEvent.Notes     = calEvent.Description;
                newEvent.Calendar  = CalendarEvent.eventStore.DefaultCalendarForNewEvents;

                NSError e;
                CalendarEvent.eventStore.SaveEvent(newEvent, EKSpan.ThisEvent, out e);
                if (e != null)
                {
                    callback?.Invoke(false);
                }
                else
                {
                    callback?.Invoke(true);
                }

                // to retrieve the event you can call
                EKEvent mySavedEvent = CalendarEvent.eventStore.EventFromIdentifier(newEvent.EventIdentifier);
                Console.WriteLine("Retrieved Saved Event: " + mySavedEvent.Title);
            });
        }
        public async Task <(bool result, CalendarEventModel model)> UpdateCalendarEvent(CalendarEventModel calEvent)
        {
            (bool result, CalendarEventModel model)response = (false, calEvent);

            if (calEvent.DeviceCalendar == null)
            {
                return(response);
            }

            var result = await RequestAccess(EKEntityType.Event);

            if (result.granted)
            {
                var evt = CalendarEvent.EventStore.EventFromIdentifier(calEvent.Id);
                if (evt != null)
                {
                    evt.StartDate = calEvent.StartTime.ToNSDate();
                    evt.EndDate   = calEvent.EndTime.ToNSDate();
                    evt.Title     = calEvent.Title;
                    evt.Location  = calEvent.Location;
                    evt.Notes     = calEvent.Description;
                    evt.Calendar  = CalendarEvent.EventStore.GetCalendar(calEvent.DeviceCalendar.Id);

                    if (calEvent.HasReminder)
                    {
                        foreach (var alarm in evt.Alarms)
                        {
                            evt.RemoveAlarm(alarm);
                        }

                        var offset = calEvent.StartTime.AddMinutes(-calEvent.ReminderMinutes).ToNSDate();
                        evt.AddAlarm(EKAlarm.FromDate(offset));
                    }
                    NSError e = null;

                    var x = CalendarEvent.EventStore.SaveEvent(evt, EKSpan.ThisEvent, true, out e);

                    if (e == null)
                    {
                        response.result   = true;
                        response.model.Id = evt.EventIdentifier;
                    }
                }
            }
            return(response);
        }
        public async Task <(bool result, CalendarEventModel model)> CreateCalendarEvent(CalendarEventModel calEvent)
        {
            (bool result, CalendarEventModel model)response = (false, calEvent);

            var result = await RequestAccess(EKEntityType.Event);

            if (result.granted)
            {
                var newEvent         = EKEvent.FromStore(CalendarEvent.EventStore);
                var selectedCalendar = CalendarEvent.EventStore.DefaultCalendarForNewEvents;
                if (calEvent.DeviceCalendar != null)
                {
                    selectedCalendar = CalendarEvent.EventStore.GetCalendar(calEvent.DeviceCalendar.Id);
                }

                newEvent.StartDate = calEvent.StartTime.ToNSDate();
                newEvent.EndDate   = calEvent.EndTime.ToNSDate();
                newEvent.Title     = calEvent.Title;
                newEvent.Notes     = calEvent.Description;
                newEvent.Calendar  = selectedCalendar;

                if (calEvent.HasReminder)
                {
                    var offset = calEvent.StartTime.AddMinutes(-calEvent.ReminderMinutes).ToNSDate();
                    newEvent.AddAlarm(EKAlarm.FromDate(offset));
                }

                NSError e;

                CalendarEvent.EventStore.SaveEvent(newEvent, EKSpan.ThisEvent, true, out e);

                if (e == null)
                {
                    response.result   = true;
                    response.model.Id = newEvent.EventIdentifier;
                }
            }

            return(response);
        }