jfversluis / Plugin.Maui.CalendarStore

Plugin.Maui.CalendarStore provides the ability to access the device calendar information in your .NET MAUI application.
MIT License
79 stars 12 forks source link

Enhancement to Add Alarms #52

Open SupaStid opened 9 months ago

SupaStid commented 9 months ago

Apologies - very new to this and this is my first ever github input

public interface ICalendarStore

Add or change CreateEvent with 2 extra parameters

Task<string> CreateEvent(string calendarId, string title, string description, string location,
    DateTimeOffset startDateTime, DateTimeOffset endDateTime, bool isAllDay = false, 
bool hasAlarm = false, int AlarmMins = 0);

so the Modification CalaendarStoremacios.cs is

public async Task<string> CreateEvent(string calendarId, string title, string description,
    string location, DateTimeOffset startDateTime, DateTimeOffset endDateTime, bool isAllDay = false, 
    bool hasAlarm = false, int AlarmMins = 0)
{
    await EnsureWriteCalendarPermission();

    var platformCalendar = EventStore.GetCalendar(calendarId)
        ?? throw InvalidCalendar(calendarId);

    if (!platformCalendar.AllowsContentModifications)
    {
        throw new CalendarStoreException($"Selected calendar (id: {calendarId}) is read-only.");
    }

    var eventToSave = EKEvent.FromStore(EventStore);
    eventToSave.Calendar = platformCalendar;
    eventToSave.Title = title;
    eventToSave.Notes = description;
    eventToSave.Location = location;
    eventToSave.StartDate = (NSDate)startDateTime.LocalDateTime;
    eventToSave.EndDate = (NSDate)endDateTime.LocalDateTime;
    eventToSave.AllDay = isAllDay;

    if (hasAlarm)
    {
        // Set reminder before event 
        NSDate nsAlarmDate = (NSDate)startDateTime.LocalDateTime.AddMinutes(-1 * AlarmMins);
        eventToSave.AddAlarm(EKAlarm.FromDate(nsAlarmDate));
    }

    var saveResult = EventStore.SaveEvent(eventToSave, EKSpan.ThisEvent, true, out var error);

And in CalaendarStore.android.cs

public async Task<string> CreateEvent(string calendarId, string title, string description,
    string location, DateTimeOffset startDateTime, DateTimeOffset endDateTime,
    bool isAllDay = false, bool hasAlarm = false, int AlarmMins = 0)
{
    await EnsureWriteCalendarPermission();

    // We just want to know a calendar with this ID exists
    _ = await GetPlatformCalendar(calendarId);

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

    eventToInsert.Put(CalendarContract.Events.InterfaceConsts.Dtend,
        endDateTime.ToUnixTimeMilliseconds());

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

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

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

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

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

    eventToInsert.Put(CalendarContract.Events.InterfaceConsts.CalendarId,
        calendarId);

    eventToInsert.Put(CalendarContract.Events.InterfaceConsts.HasAlarm,
        hasAlarm);

    var idUrl = platformContentResolver?.Insert(eventsTableUri, eventToInsert);

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

    if (hasAlarm)
    {
        var reminderValues = new ContentValues();
        reminderValues.Put(CalendarContract.Reminders.InterfaceConsts.EventId, savedId);
        reminderValues.Put(CalendarContract.Reminders.InterfaceConsts.Method, (int)RemindersMethod.Alert);
        reminderValues.Put(Reminders.InterfaceConsts.Minutes, AlarmMins);
        Android.Net.Uri uri1 = platformContentResolver?.Insert(Reminders.ContentUri, reminderValues);
    }

    return savedId.ToString();
}
SupaStid commented 9 months ago

Apologies again - I know I'm doing this wrong in CalendarStoreWindows

public async Task<string> CreateEvent(string calendarId, string title, string description,
    string location, DateTimeOffset startDateTime, DateTimeOffset endDateTime,
    bool isAllDay = false, bool hasAlarm = false, int AlarmMins = 0)
{
    await EnsureWriteCalendarPermission();

    var platformCalendarManager = await GetAppointmentStore(true)
        .ConfigureAwait(false);

    var platformCalendar = await platformCalendarManager
        .GetAppointmentCalendarAsync(calendarId)
        .AsTask().ConfigureAwait(false);

    var eventToSave = new Appointment
    {
        Subject = title,
        Details = description,
        Location = location,
        StartTime = startDateTime.LocalDateTime,
        Duration = endDateTime.Subtract(startDateTime),
        AllDay = isAllDay,          
    };

    if (hasAlarm)
    {
        eventToSave.Reminder = new TimeSpan(0, AlarmMins, 0);
    }

    await platformCalendar.SaveAppointmentAsync(eventToSave)
        .AsTask().ConfigureAwait(false);

    return eventToSave.LocalId;
}