Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UpdateEvent should use Event table #34

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 107 additions & 103 deletions src/Plugin.Maui.CalendarStore/CalendarStore.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Platform;
using static Plugin.Maui.CalendarStore.CalendarStore;

namespace Plugin.Maui.CalendarStore;

Expand Down Expand Up @@ -47,19 +48,19 @@ partial class CalendarStoreImplementation : ICalendarStore
public CalendarStoreImplementation()
{
calendarsTableUri = CalendarContract.Calendars.ContentUri
?? throw new CalendarStore.CalendarStoreException(
?? throw new CalendarStoreException(
"Could not determine Android calendars table URI.");

eventsTableUri = CalendarContract.Events.ContentUri
?? throw new CalendarStore.CalendarStoreException(
?? throw new CalendarStoreException(
"Could not determine Android events table URI.");

attendeesTableUri = CalendarContract.Attendees.ContentUri
?? throw new CalendarStore.CalendarStoreException(
?? throw new CalendarStoreException(
"Could not determine Android attendees table URI.");

platformContentResolver = Platform.AppContext.ApplicationContext?.ContentResolver
?? throw new CalendarStore.CalendarStoreException(
?? throw new CalendarStoreException(
"Could not determine Android events table URI.");
}

Expand All @@ -73,7 +74,7 @@ public async Task<IEnumerable<Calendar>> GetCalendars()

using var cursor = platformContentResolver?.Query(calendarsTableUri,
calendarColumns.ToArray(), queryConditions, null, null)
?? throw new CalendarStore.CalendarStoreException("Error while querying calendars");
?? throw new CalendarStoreException("Error while querying calendars");

return ToCalendars(cursor, calendarColumns).ToList();
}
Expand Down Expand Up @@ -108,7 +109,7 @@ public async Task CreateCalendar(string name, Color? color = null)

if (!long.TryParse(idUrl?.LastPathSegment, out _))
{
throw new CalendarStore.CalendarStoreException(
throw new CalendarStoreException(
"There was an error saving the calendar.");
}
}
Expand Down Expand Up @@ -144,7 +145,7 @@ public async Task UpdateCalendar(string calendarId, string newName, Color? newCo

if (updateCount != 1)
{
throw new CalendarStore.CalendarStoreException(
throw new CalendarStoreException(
"There was an error updating the calendar.");
}
}
Expand All @@ -158,7 +159,7 @@ public async Task UpdateCalendar(string calendarId, string newName, Color? newCo
// if (string.IsNullOrEmpty(calendarId) ||
// !long.TryParse(calendarId, out long platformCalendarId))
// {
// throw CalendarStore.InvalidCalendar(calendarId);
// throw InvalidCalendar(calendarId);
// }

// // We just want to know a calendar with this ID exists
Expand All @@ -169,7 +170,7 @@ public async Task UpdateCalendar(string calendarId, string newName, Color? newCo

// if (deleteCount != 1)
// {
// throw new CalendarStore.CalendarStoreException(
// throw new CalendarStoreException(
// "There was an error deleting the calendar.");
// }
//}
Expand All @@ -188,11 +189,11 @@ public async Task<IEnumerable<CalendarEvent>> GetEvents(
// Android ids are always integers
if (!string.IsNullOrEmpty(calendarId) && !int.TryParse(calendarId, out _))
{
throw CalendarStore.InvalidCalendar(calendarId);
throw InvalidCalendar(calendarId);
}

var sDate = startDate ?? DateTimeOffset.Now.Add(CalendarStore.defaultStartTimeFromNow);
var eDate = endDate ?? sDate.Add(CalendarStore.defaultEndTimeFromStartTime);
var sDate = startDate ?? DateTimeOffset.Now.Add(defaultStartTimeFromNow);
var eDate = endDate ?? sDate.Add(defaultEndTimeFromStartTime);

var calendarSpecificEvent =
$"{CalendarContract.Events.InterfaceConsts.Dtend} >= " +
Expand All @@ -211,7 +212,7 @@ public async Task<IEnumerable<CalendarEvent>> GetEvents(

using var cursor = platformContentResolver.Query(eventsTableUri,
eventsColumns.ToArray(), calendarSpecificEvent, null, sortOrder)
?? throw new CalendarStore.CalendarStoreException("Error while querying events");
?? throw new CalendarStoreException("Error while querying events");

// Confirm the calendar exists if no events were found
if (cursor.Count == 0 && !string.IsNullOrEmpty(calendarId))
Expand All @@ -230,19 +231,19 @@ public async Task<CalendarEvent> GetEvent(string eventId)
// Android ids are always integers
if (!string.IsNullOrEmpty(eventId) && !long.TryParse(eventId, out _))
{
throw CalendarStore.InvalidCalendar(eventId);
throw InvalidCalendar(eventId);
}

var calendarSpecificEvent =
$"{CalendarContract.Events.InterfaceConsts.Id} = {eventId}";

using var cursor = platformContentResolver.Query(eventsTableUri,
eventsColumns.ToArray(), calendarSpecificEvent, null, null)
?? throw new CalendarStore.CalendarStoreException("Error while querying events");
?? throw new CalendarStoreException("Error while querying events");

if (cursor.Count <= 0)
{
throw CalendarStore.InvalidEvent(eventId);
throw InvalidEvent(eventId);
}

cursor.MoveToNext();
Expand All @@ -257,66 +258,69 @@ public async Task CreateEvent(string calendarId, string title, string descriptio
{
await EnsureWriteCalendarPermission();

using var cursor = platformContentResolver.Query(
calendarsTableUri, calendarColumns.ToArray(), null, null, null);
using var cursor = await GetPlatformCalendar(calendarId);

long platformCalendarId = 0;

if (cursor != null)
if (cursor is null)
{
if (!long.TryParse(calendarId, out long virtualCalendarId))
{
throw CalendarStore.InvalidCalendar(calendarId);
}
throw new CalendarStoreException("Cursor is null.");
}

while (cursor.MoveToNext())
{
long id = cursor.GetLong(cursor.GetColumnIndex(calendarColumns[0]));
if (!long.TryParse(calendarId, out long virtualCalendarId))
{
throw InvalidCalendar(calendarId);
}

if (id == virtualCalendarId)
{
platformCalendarId = cursor.GetLong(
cursor.GetColumnIndex(calendarColumns[0]));
while (cursor.MoveToNext())
{
long id = cursor.GetLong(cursor.GetColumnIndex(calendarColumns[0]));

break;
}
if (id == virtualCalendarId)
{
platformCalendarId = cursor.GetLong(
cursor.GetColumnIndex(calendarColumns[0]));

break;
}
}

if (platformCalendarId != 0)
{
ContentValues eventToInsert = new();
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Dtstart,
startDateTime.ToUnixTimeMilliseconds());
if (platformCalendarId <= 0)
{
throw new CalendarStoreException("Could not determine platform calendar ID.");
}

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Dtend,
endDateTime.ToUnixTimeMilliseconds());
ContentValues eventToInsert = new();
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Dtstart,
startDateTime.ToUnixTimeMilliseconds());

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.EventTimezone,
TimeZoneInfo.Local.StandardName);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Dtend,
endDateTime.ToUnixTimeMilliseconds());

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.AllDay,
isAllDay);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.EventTimezone,
TimeZoneInfo.Local.StandardName);

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Title,
title);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.AllDay,
isAllDay);

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Description,
description);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Title,
title);

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.EventLocation,
location);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Description,
description);

eventToInsert.Put(CalendarContract.Events.InterfaceConsts.CalendarId,
platformCalendarId);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.EventLocation,
location);

var idUrl = platformContentResolver?.Insert(eventsTableUri, eventToInsert);
eventToInsert.Put(CalendarContract.Events.InterfaceConsts.CalendarId,
platformCalendarId);

if (!long.TryParse(idUrl?.LastPathSegment, out _))
{
throw new CalendarStore.CalendarStoreException(
"There was an error saving the event.");
}
}
var idUrl = platformContentResolver?.Insert(eventsTableUri, eventToInsert);

if (!long.TryParse(idUrl?.LastPathSegment, out _))
{
throw new CalendarStoreException(
"There was an error saving the event.");
}
}

Expand All @@ -343,60 +347,60 @@ public async Task UpdateEvent(string eventId, string title, string description,
await EnsureWriteCalendarPermission();

using var cursor = platformContentResolver.Query(
calendarsTableUri, calendarColumns.ToArray(), null, null, null);
eventsTableUri, calendarColumns.ToArray(), null, null, null)
?? throw new CalendarStoreException("Error while querying events");

long platformEventId = 0;

if (cursor != null)
if (!long.TryParse(eventId, out long virtualEventId))
{
if (!long.TryParse(eventId, out long virtualEventId))
{
throw CalendarStore.InvalidEvent(eventId);
}
throw InvalidEvent(eventId);
}

while (cursor.MoveToNext())
{
long id = cursor.GetLong(cursor.GetColumnIndex(calendarColumns[0]));
while (cursor.MoveToNext())
{
long id = cursor.GetLong(cursor.GetColumnIndex(calendarColumns[0]));

if (id == virtualEventId)
{
platformEventId = cursor.GetLong(
cursor.GetColumnIndex(calendarColumns[0]));
if (id == virtualEventId)
{
platformEventId = cursor.GetLong(
cursor.GetColumnIndex(calendarColumns[0]));

break;
}
break;
}
}

if (platformEventId != 0)
{
ContentValues eventToUpdate = new();
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Dtstart,
startDateTime.ToUnixTimeMilliseconds());
if (platformEventId <= 0)
{
throw new CalendarStoreException("Could not determine platform event ID.");
}

eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Dtend,
endDateTime.ToUnixTimeMilliseconds());
ContentValues eventToUpdate = new();
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Dtstart,
startDateTime.ToUnixTimeMilliseconds());

eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.AllDay,
isAllDay);
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Dtend,
endDateTime.ToUnixTimeMilliseconds());

eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Title,
title);
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.AllDay,
isAllDay);

eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Description,
description);
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Title,
title);

eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.EventLocation,
location);
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.Description,
description);

var updateCount = platformContentResolver?.Update(
ContentUris.WithAppendedId(eventsTableUri, platformEventId), eventToUpdate, null, null);
eventToUpdate.Put(CalendarContract.Events.InterfaceConsts.EventLocation,
location);

if (updateCount != 1)
{
throw new CalendarStore.CalendarStoreException(
"There was an error updating the event.");
}
}
var updateCount = platformContentResolver?.Update(
ContentUris.WithAppendedId(eventsTableUri, platformEventId), eventToUpdate, null, null);

if (updateCount != 1)
{
throw new CalendarStoreException(
"There was an error updating the event.");
}
}

Expand All @@ -414,7 +418,7 @@ public async Task DeleteEvent(string eventId)
if (string.IsNullOrEmpty(eventId) ||
!long.TryParse(eventId, out long platformEventId))
{
throw CalendarStore.InvalidEvent(eventId);
throw InvalidEvent(eventId);
}

ContentValues eventToRemove = new();
Expand All @@ -426,7 +430,7 @@ public async Task DeleteEvent(string eventId)

if (deleteCount != 1)
{
throw new CalendarStore.CalendarStoreException(
throw new CalendarStoreException(
"There was an error deleting the event.");
}
}
Expand All @@ -451,15 +455,15 @@ IEnumerable<CalendarEventAttendee> GetAttendees(string eventId)
// Android ids are always integers
if (!string.IsNullOrEmpty(eventId) && !long.TryParse(eventId, out _))
{
throw CalendarStore.InvalidCalendar(eventId);
throw InvalidCalendar(eventId);
}

var attendeeFilter =
$"{CalendarContract.Attendees.InterfaceConsts.EventId}={eventId}";

using var cursor = platformContentResolver.Query(attendeesTableUri,
attendeesColumns.ToArray(), attendeeFilter, null, null)
?? throw new CalendarStore.CalendarStoreException("Error while querying attendees");
?? throw new CalendarStoreException("Error while querying attendees");

return ToAttendees(cursor, attendeesColumns).ToList();
}
Expand All @@ -473,7 +477,7 @@ async Task<ICursor> GetPlatformCalendar(string calendarId)
// Android ids are always integers
if (!long.TryParse(calendarId, out _))
{
throw CalendarStore.InvalidCalendar(calendarId);
throw InvalidCalendar(calendarId);
}

var queryConditions =
Expand All @@ -482,11 +486,11 @@ async Task<ICursor> GetPlatformCalendar(string calendarId)

using var cursor = platformContentResolver.Query(calendarsTableUri,
calendarColumns.ToArray(), queryConditions, null, null)
?? throw new CalendarStore.CalendarStoreException("Error while querying calendars");
?? throw new CalendarStoreException("Error while querying calendars");

if (cursor.Count <= 0)
{
throw CalendarStore.InvalidCalendar(calendarId);
throw InvalidCalendar(calendarId);
}

cursor.MoveToNext();
Expand Down