Skip to content

Commit

Permalink
feat: use new calendar and reminders access APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 14, 2024
1 parent e406d65 commit fd7129c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ function askForFoldersAccess(folder) {
return permissions.askForFoldersAccess.call(this, folder)
}

function askForCalendarAccess(type) {
if (!['write-only', 'full'].includes(type)) {
throw new TypeError(`${type} must be one of either 'write-only' or 'full'`)
}

return permissions.askForCalendarAccess.call(this, type)
}

function askForScreenCaptureAccess(openPreferences = false) {
if (typeof openPreferences !== 'boolean') {
throw new TypeError('openPreferences must be a boolean')
Expand All @@ -62,7 +70,7 @@ function askForInputMonitoringAccess(accessLevel = 'listen') {

module.exports = {
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
askForCalendarAccess: permissions.askForCalendarAccess,
askForCalendarAccess: askForCalendarAccess,
askForCameraAccess: permissions.askForCameraAccess,
askForContactsAccess: permissions.askForContactsAccess,
askForFoldersAccess,
Expand Down
72 changes: 49 additions & 23 deletions permissions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -510,17 +510,38 @@ bool HasOpenSystemPreferencesDialog() {
env, Napi::Function::New(env, NoOp), "calendarCallback", 0, 1);

__block Napi::ThreadSafeFunction tsfn = ts_fn;
[[EKEventStore new]
requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
auto callback = [=](Napi::Env env, Napi::Function js_cb,
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(granted ? "authorized" : "denied",
callback);
tsfn.Release();
}];
if (@available(macOS 14.0, *)) {
const std::string type = info[0].As<Napi::String>().Utf8Value();

EKEventStoreRequestAccessCompletionHandler handler =
^(BOOL granted, NSError *error) {
auto callback = [=](Napi::Env env, Napi::Function js_cb,
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
tsfn.Release();
};

if (type == "full") {
[[EKEventStore new] requestFullAccessToEventsWithCompletion:handler];
} else {
[[EKEventStore new] requestWriteOnlyAccessToEventsWithCompletion:handler];
}
} else {
[[EKEventStore new]
requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
auto callback = [=](Napi::Env env,
Napi::Function js_cb,
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(granted ? "authorized" : "denied",
callback);
tsfn.Release();
}];
}

return deferred.Promise();
}
Expand All @@ -533,18 +554,23 @@ bool HasOpenSystemPreferencesDialog() {
env, Napi::Function::New(env, NoOp), "remindersCallback", 0, 1);

__block Napi::ThreadSafeFunction tsfn = ts_fn;
[[EKEventStore new]
requestAccessToEntityType:EKEntityTypeReminder
completion:^(BOOL granted, NSError *error) {
auto callback = [=](Napi::Env env,
Napi::Function prom_cb,
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(granted ? "authorized" : "denied",
callback);
tsfn.Release();
}];

EKEventStoreRequestAccessCompletionHandler handler =
^(BOOL granted, NSError *error) {
auto callback = [=](Napi::Env env, Napi::Function prom_cb,
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
tsfn.Release();
};

if (@available(macOS 14.0, *)) {
[[EKEventStore new] requestFullAccessToRemindersWithCompletion:handler];
} else {
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeReminder
completion:handler];
}

return deferred.Promise();
}
Expand Down

0 comments on commit fd7129c

Please sign in to comment.