Skip to content

Commit

Permalink
#89 Added Ability to SetEventRecurrenceEndDate
Browse files Browse the repository at this point in the history
which allows stopping event recurrences going from a specific date (end recurrence rule from date)
  • Loading branch information
ScottBTR committed Feb 6, 2020
1 parent ca626cd commit 33bc648
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Samples/Samples/View/CalendarEventPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,31 @@ async void OnDeleteEventButtonClicked(object sender, EventArgs e)
{
if (calendarEvent.RecurrancePattern != null)
{
if (await DisplayAlert("Warning!", $"Do you want to delete all instances of this event?", "Yes All", "Just this one"))
var action = await DisplayActionSheet("Do you want to delete all instances of this event?", "Cancel", null, "Yes to All", "Just this one", "From this date forward");

if (action == "Yes to All")
{
if (await Calendars.DeleteCalendarEventById(eventId, CalendarId.Text))
{
await DisplayAlert("Info", "Deleted event id: " + eventId, "Ok");
await Navigation.PopAsync();
}
}
else if (await Calendars.DeleteCalendarEventInstanceByDate(eventId, CalendarId.Text, calendarEvent.StartDate))
else if (action == "Just this one")
{
await DisplayAlert("Info", "Deleted event id: " + eventId, "Ok");
await Navigation.PopAsync();
if (await Calendars.DeleteCalendarEventInstanceByDate(eventId, CalendarId.Text, calendarEvent.StartDate))
{
await DisplayAlert("Info", "Deleted instance of event id: " + eventId, "Ok");
await Navigation.PopAsync();
}
}
else if (action == "From this date forward")
{
if (await Calendars.SetEventRecurrenceEndDate(eventId, calendarEvent.StartDate.AddDays(-1)))
{
await DisplayAlert("Info", "Deleted all future instances of event id: " + eventId, "Ok");
await Navigation.PopAsync();
}
}
}
else if (await Calendars.DeleteCalendarEventById(eventId, CalendarId.Text))
Expand Down
28 changes: 28 additions & 0 deletions Xamarin.Essentials/Calendars/Calendars.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,34 @@ static async Task<bool> PlatformUpdateCalendarEvent(CalendarEvent eventToUpdate)
throw new ArgumentException("[Android]: Could not update appointment with supplied parameters");
}

static async Task<bool> PlatformSetEventRecurrenceEndDate(string eventId, DateTimeOffset recurrenceEndDate)
{
await Permissions.RequestAsync<Permissions.CalendarWrite>();

var existingEvent = await GetEventByIdAsync(eventId);
CalendarEvent thisEvent;
if (existingEvent == null || string.IsNullOrEmpty(existingEvent.CalendarId))
{
return false;
}
else
{
thisEvent = await GetEventByIdAsync(eventId);
}

thisEvent.RecurrancePattern.EndDate = recurrenceEndDate;

var eventUri = CalendarContract.Events.ContentUri;
var eventValues = SetupContentValues(thisEvent);

if (Platform.AppContext.ApplicationContext.ContentResolver.Update(eventUri, eventValues, $"{CalendarContract.Attendees.InterfaceConsts.Id}={eventId}", null) > 0)
{
return true;
}
throw new ArgumentException("[Android]: Could not update appointment with supplied parameters");
}


static ContentValues SetupContentValues(CalendarEvent newEvent, bool existingEvent = false)
{
var eventValues = new ContentValues();
Expand Down
26 changes: 26 additions & 0 deletions Xamarin.Essentials/Calendars/Calendars.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,32 @@ static async Task<bool> PlatformUpdateCalendarEvent(CalendarEvent eventToUpdate)
throw new ArgumentException("[iOS]: Could not update appointment with supplied parameters");
}

static async Task<bool> PlatformSetEventRecurrenceEndDate(string eventId, DateTimeOffset recurrenceEndDate)
{
await Permissions.RequestAsync<Permissions.CalendarWrite>();

var existingEvent = await GetEventByIdAsync(eventId);
EKEvent thisEvent;
if (existingEvent == null || string.IsNullOrEmpty(existingEvent.CalendarId))
{
return false;
}
else
{
thisEvent = CalendarRequest.Instance.GetCalendarItem(eventId) as EKEvent;
}

existingEvent.RecurrancePattern.EndDate = recurrenceEndDate;

thisEvent = SetUpEvent(thisEvent, existingEvent);

if (CalendarRequest.Instance.SaveEvent(thisEvent, EKSpan.FutureEvents, true, out var error))
{
return true;
}
throw new ArgumentException("[iOS]: Could not update appointments recurrence dates with supplied parameters");
}

static EKEvent SetUpEvent(EKEvent eventToUpdate, CalendarEvent eventToUpdateFrom)
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(NSTimeZone.LocalTimeZone.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static partial class Calendars

static Task<bool> PlatformUpdateCalendarEvent(CalendarEvent eventToUpdate) => throw ExceptionUtils.NotSupportedOrImplementedException;

static Task<bool> PlatformSetEventRecurrenceEndDate(string eventId, DateTimeOffset recurrenceEndDate) => throw ExceptionUtils.NotSupportedOrImplementedException;

static Task<bool> PlatformDeleteCalendarEventInstanceByDate(string eventId, string calendarId, DateTimeOffset dateOfInstanceUtc) => throw ExceptionUtils.NotSupportedOrImplementedException;

static Task<bool> PlatformDeleteCalendarEventById(string eventId, string calendarId) => throw ExceptionUtils.NotSupportedOrImplementedException;
Expand Down
2 changes: 2 additions & 0 deletions Xamarin.Essentials/Calendars/Calendars.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static partial class Calendars

public static Task<bool> UpdateCalendarEvent(CalendarEvent eventToUpdate) => PlatformUpdateCalendarEvent(eventToUpdate);

public static Task<bool> SetEventRecurrenceEndDate(string eventId, DateTimeOffset recurrenceEndDate) => PlatformSetEventRecurrenceEndDate(eventId, recurrenceEndDate);

public static Task<bool> DeleteCalendarEventInstanceByDate(string eventId, string calendarId, DateTimeOffset dateOfInstanceUtc) => PlatformDeleteCalendarEventInstanceByDate(eventId, calendarId, dateOfInstanceUtc);

public static Task<bool> DeleteCalendarEventById(string eventId, string calendarId) => PlatformDeleteCalendarEventById(eventId, calendarId);
Expand Down
33 changes: 33 additions & 0 deletions Xamarin.Essentials/Calendars/Calendars.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,39 @@ static async Task<bool> PlatformUpdateCalendarEvent(CalendarEvent eventToUpdate)
throw new ArgumentException("[UWP]: Could not update appointment with supplied parameters");
}

static async Task<bool> PlatformSetEventRecurrenceEndDate(string eventId, DateTimeOffset recurrenceEndDate)
{
await Permissions.RequestAsync<Permissions.CalendarWrite>();

var existingEvent = await GetEventByIdAsync(eventId);

Appointment thisEvent = null;
var instance = await CalendarRequest.GetInstanceAsync();
if (existingEvent == null || string.IsNullOrEmpty(existingEvent?.CalendarId))
{
return false;
}
else
{
thisEvent = await instance.GetAppointmentAsync(existingEvent.Id);
}

if (existingEvent.RecurrancePattern != null)
{
existingEvent.RecurrancePattern.EndDate = recurrenceEndDate;
thisEvent.Recurrence = existingEvent.RecurrancePattern.ConvertRule();
}

var calendar = await instance.GetAppointmentCalendarAsync(existingEvent.CalendarId);
await calendar.SaveAppointmentAsync(thisEvent);

if (!string.IsNullOrEmpty(thisEvent.LocalId))
{
return true;
}
throw new ArgumentException("[UWP]: Could not update appointments Recurrence End Date with supplied parameters");
}

static AppointmentRecurrence ConvertRule(this RecurrenceRule recurrenceRule)
{
var eventRecurrence = new AppointmentRecurrence();
Expand Down

0 comments on commit 33bc648

Please sign in to comment.