use block::{ConcreteBlock, IntoConcreteBlock};
use objc::{msg_send, sel, sel_impl};
use crate::{
foundation::{Int, NSArray, NSError, NSSet, NSString},
object,
objective_c_runtime::{
id,
macros::interface_impl,
traits::{FromId, PNSObject},
},
utils::to_bool,
};
use super::{
UNNotification, UNNotificationCategory, UNNotificationRequest, UNNotificationSettings,
};
object! {
unsafe pub struct UNUserNotificationCenter;
}
pub static UNUSER_NOTIFICATION_CENTER_PTR: &str = "rstUNUserNotificationCenterPtr";
#[repr(u64)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum UNAuthorizationOptions {
None = 0,
Badge = (1 << 0),
Sound = (1 << 1),
Alert = (1 << 2),
CarPlay = (1 << 3),
#[cfg(target_os = "ios")]
CriticalAlert = (1 << 4),
#[cfg(target_os = "ios")]
ProvidesAppNotificationSettings = (1 << 5),
#[cfg(target_os = "ios")]
Provisional = (1 << 6),
#[deprecated]
#[cfg(target_os = "ios")]
Announcement = (1 << 7),
#[deprecated]
#[cfg(any(target_os = "ios", target_os = "macos"))]
TimeSensitive = (1 << 8),
}
#[repr(i64)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum UNErrorCode {
NotificationsNotAllowed = 1,
AttachmentInvalidUrl = 100,
AttachmentUnrecognizedType,
AttachmentInvalidFileSize,
AttachmentNotInDataStore,
AttachmentMoveIntoDataStoreFailed,
AttachmentCorrupt,
NotificationInvalidNoDate = 1400,
NotificationInvalidNoContent,
ContentProvidingObjectNotAllowed = 1500,
ContentProvidingInvalid = 1501,
}
#[interface_impl(NSObject)]
impl UNUserNotificationCenter {
#[method]
pub fn current_notification_center() -> UNUserNotificationCenter
where
Self: Sized + FromId,
{
unsafe { msg_send![Self::m_class(), currentNotificationCenter] }
}
#[method]
pub fn get_notification_settings_with_completion_handler<F>(&self, completion_handler: F)
where
F: IntoConcreteBlock<(UNNotificationSettings,), Ret = ()> + 'static,
{
let block = ConcreteBlock::new(completion_handler);
let block = block.copy();
unsafe {
msg_send![
self.m_self(),
getNotificationSettingsWithCompletionHandler: block
]
}
}
#[method]
pub fn set_badge_count_with_completion_handler<F>(
&mut self,
new_badge_count: Int,
completion_handler: F,
) where
F: IntoConcreteBlock<(NSError,), Ret = ()> + 'static,
{
let block = ConcreteBlock::new(completion_handler);
let block = block.copy();
unsafe {
msg_send![self.m_self(), setBadgeCount: new_badge_count withCompletionHandler: block]
}
}
#[method]
pub fn request_authorization_with_options_completion_handler<F>(
&mut self,
options: &[UNAuthorizationOptions],
completion_handler: F,
) where
F: IntoConcreteBlock<(bool, Option<NSError>), Ret = ()> + 'static,
{
let options = options
.iter()
.fold(0u64, |init, option| init | *option as u64);
let block = ConcreteBlock::new(completion_handler);
let block = block.copy();
unsafe {
msg_send![self.m_self(), requestAuthorizationWithOptions:options completionHandler: block]
}
}
#[property]
pub fn delegate(&self) -> Option<id> {
unsafe {
let ptr: id = msg_send![self.m_self(), delegate];
if ptr.is_null() {
None
} else {
Some(ptr)
}
}
}
#[property]
pub fn set_delegate(&self, delegate: id) {
unsafe { msg_send![self.m_self(), setDelegate: delegate] }
}
#[property]
pub fn supports_content_extensions(&self) -> bool {
unsafe { to_bool(msg_send![self.m_self(), supportsContentExtensions]) }
}
#[method]
pub fn add_notification_request_with_completion_handler<F>(
&mut self,
request: &UNNotificationRequest,
completion_handler: F,
) where
F: IntoConcreteBlock<(NSError,), Ret = ()> + 'static,
{
let block = ConcreteBlock::new(completion_handler);
let block = block.copy();
unsafe {
msg_send![self.m_self(), addNotificationRequest: request.m_self() withCompletionHandler: block]
}
}
#[method]
pub fn get_pending_notification_requests_with_completion_handler<F>(
&self,
completion_handler: F,
) where
F: IntoConcreteBlock<(NSArray<UNNotificationRequest>,)>,
{
unsafe {
msg_send![
self.m_self(),
getPendingNotificationRequestsWithCompletionHandler: completion_handler
]
}
}
#[method]
pub fn remove_pending_notification_requests_with_identifiers(
&mut self,
identifiers: &NSArray<NSString>,
) {
unsafe {
msg_send![
self.m_self(),
removePendingNotificationRequestsWithIdentifiers: identifiers.m_self()
]
}
}
#[method]
pub fn remove_all_pending_notification_requests(&mut self) {
unsafe { msg_send![self.m_self(), removeAllPendingNotificationRequests] }
}
#[method]
pub fn get_delivered_notifications_with_completion_handler<F>(&self, completion_handler: F)
where
F: IntoConcreteBlock<(NSArray<UNNotification>,)>,
{
unsafe {
msg_send![
self.m_self(),
getDeliveredNotificationsWithCompletionHandler: completion_handler
]
}
}
#[method]
pub fn remove_delivered_notifications_with_identifiers(
&mut self,
identifiers: &NSArray<NSString>,
) {
unsafe {
msg_send![
self.m_self(),
removeDeliveredNotificationsWithIdentifiers: identifiers.m_self()
]
}
}
#[method]
pub fn remove_all_delivered_notifications(&mut self) {
unsafe { msg_send![self.m_self(), removeAllDeliveredNotifications] }
}
#[method]
pub fn set_notification_categories(&mut self, categories: &NSSet<UNNotificationCategory>) {
unsafe { msg_send![self.m_self(), setNotificationCategories: categories.m_self()] }
}
#[method]
pub fn get_notification_categories_with_completion_handler<F>(&self, completion_handler: F)
where
F: IntoConcreteBlock<(NSSet<UNNotificationCategory>,)> + 'static,
{
unsafe {
msg_send![
self.m_self(),
getNotificationCategoriesWithCompletionHandler: completion_handler
]
}
}
}