use std::ffi::c_void;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use doom_fish_utils::completion::{error_from_cstr, AsyncCompletion, AsyncCompletionFuture};
use doom_fish_utils::panic_safe::catch_user_panic;
use crate::error::EventKitError;
use crate::event::EKEvent;
use crate::event_store::{EKEventStore, EKReminderPredicate, EKSpan};
use crate::ffi;
use crate::private::{json_cstring, parse_json_ptr};
use crate::reminder::EKReminder;
extern "C" fn access_cb(result: *const c_void, error: *const i8, ctx: *mut c_void) {
catch_user_panic("eventkit::async_api::access_cb", || {
if error.is_null() {
let granted = !result.is_null();
unsafe { AsyncCompletion::complete_ok(ctx, granted) };
} else {
let msg = unsafe { error_from_cstr(error) };
unsafe { AsyncCompletion::<bool>::complete_err(ctx, msg) };
}
});
}
pub struct RequestAccessFuture {
inner: AsyncCompletionFuture<bool>,
}
impl std::fmt::Debug for RequestAccessFuture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RequestAccessFuture").finish_non_exhaustive()
}
}
impl Future for RequestAccessFuture {
type Output = Result<bool, EventKitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.inner)
.poll(cx)
.map(|r| r.map_err(EventKitError::from_error_json))
}
}
extern "C" fn fetch_reminders_cb(result: *const c_void, error: *const i8, ctx: *mut c_void) {
catch_user_panic("eventkit::async_api::fetch_reminders_cb", || {
if error.is_null() {
if result.is_null() {
unsafe { AsyncCompletion::<Vec<EKReminder>>::complete_ok(ctx, vec![]) };
} else {
let json_ptr = result as *mut core::ffi::c_char;
match unsafe { parse_json_ptr::<Vec<EKReminder>>(json_ptr, "fetchReminders") } {
Ok(reminders) => unsafe { AsyncCompletion::complete_ok(ctx, reminders) },
Err(err) => unsafe {
AsyncCompletion::<Vec<EKReminder>>::complete_err(ctx, err.to_string());
},
}
}
} else {
let msg = unsafe { error_from_cstr(error) };
unsafe { AsyncCompletion::<Vec<EKReminder>>::complete_err(ctx, msg) };
}
});
}
pub struct FetchRemindersFuture {
inner: AsyncCompletionFuture<Vec<EKReminder>>,
}
impl std::fmt::Debug for FetchRemindersFuture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FetchRemindersFuture").finish_non_exhaustive()
}
}
impl Future for FetchRemindersFuture {
type Output = Result<Vec<EKReminder>, EventKitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.inner)
.poll(cx)
.map(|r| r.map_err(EventKitError::from_error_json))
}
}
pub struct AsyncEventStore {
store: EKEventStore,
}
impl std::fmt::Debug for AsyncEventStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AsyncEventStore").finish_non_exhaustive()
}
}
impl AsyncEventStore {
pub const fn new(store: EKEventStore) -> Self {
Self { store }
}
pub const fn inner(&self) -> &EKEventStore {
&self.store
}
pub fn into_inner(self) -> EKEventStore {
self.store
}
pub fn request_full_access_to_events(&self) -> RequestAccessFuture {
let (future, ctx) = AsyncCompletion::create();
unsafe {
ffi::event_store::ek_store_request_full_access_events_async(
self.store.as_raw_ptr(),
access_cb,
ctx,
);
}
RequestAccessFuture { inner: future }
}
pub fn request_full_access_to_reminders(&self) -> RequestAccessFuture {
let (future, ctx) = AsyncCompletion::create();
unsafe {
ffi::event_store::ek_store_request_full_access_reminders_async(
self.store.as_raw_ptr(),
access_cb,
ctx,
);
}
RequestAccessFuture { inner: future }
}
pub fn request_write_only_access_to_events(&self) -> RequestAccessFuture {
let (future, ctx) = AsyncCompletion::create();
unsafe {
ffi::event_store::ek_store_request_write_only_access_events_async(
self.store.as_raw_ptr(),
access_cb,
ctx,
);
}
RequestAccessFuture { inner: future }
}
pub fn fetch_reminders(
&self,
predicate: &EKReminderPredicate,
) -> Result<FetchRemindersFuture, EventKitError> {
let predicate_json = json_cstring(predicate, "EKReminderPredicate")?;
let (future, ctx) = AsyncCompletion::create();
unsafe {
ffi::event_store::ek_store_fetch_reminders_async(
self.store.as_raw_ptr(),
predicate_json.as_ptr(),
fetch_reminders_cb,
ctx,
);
}
Ok(FetchRemindersFuture { inner: future })
}
pub fn save_event(
&self,
event: &EKEvent,
span: EKSpan,
commit: bool,
) -> impl Future<Output = Result<(), EventKitError>> + '_ {
std::future::ready(self.store.save_event(event, span, commit))
}
pub fn remove_event(
&self,
event: &EKEvent,
span: EKSpan,
commit: bool,
) -> impl Future<Output = Result<(), EventKitError>> + '_ {
std::future::ready(self.store.remove_event(event, span, commit))
}
pub fn save_reminder(
&self,
reminder: &EKReminder,
commit: bool,
) -> impl Future<Output = Result<(), EventKitError>> + '_ {
std::future::ready(self.store.save_reminder(reminder, commit))
}
pub fn remove_reminder(
&self,
reminder: &EKReminder,
commit: bool,
) -> impl Future<Output = Result<(), EventKitError>> + '_ {
std::future::ready(self.store.remove_reminder(reminder, commit))
}
}