rust_macios/contacts/
cn_save_request.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    foundation::NSString,
5    object,
6    objective_c_runtime::{
7        macros::interface_impl,
8        nil,
9        traits::{FromId, PNSObject},
10    },
11    utils::to_bool,
12};
13
14use super::{CNMutableContact, CNMutableGroup, ICNContact, ICNGroup};
15
16object! {
17    /// An object that collects the changes you want to save to the user's contacts database.
18    unsafe pub struct CNSaveRequest;
19}
20
21#[interface_impl(NSObject)]
22impl CNSaveRequest {
23    /* Saving a Contact Changes
24     */
25
26    /// Adds the specified contact to the contact store.
27    ///
28    /// # Arguments
29    ///
30    /// * `contact` - The contact to add to the contact store.
31    /// * `container_identifier` - The identifier of the container to add the new contact. To add the new contact to the default container set identifier to [`None`].
32    #[method]
33    pub fn add_contact_to_container_with_identifier<Contact>(
34        &self,
35        contact: Contact,
36        identifier: Option<NSString>,
37    ) where
38        Contact: ICNContact,
39    {
40        match identifier {
41            Some(identifier) => unsafe {
42                msg_send![self.m_self(), addContact: contact.m_self() toContainerWithIdentifier: identifier]
43            },
44            None => unsafe {
45                msg_send![self.m_self(), addContact: contact.m_self() toContainerWithIdentifier: nil]
46            },
47        }
48    }
49
50    /// Updates an existing contact in the contact store.
51    #[method]
52    pub fn update_contact(&self, contact: CNMutableContact) {
53        unsafe { msg_send![self.m_self(), updateContact: contact] }
54    }
55
56    /// Removes the specified contact from the contact store.
57    #[method]
58    pub fn delete_contact(&self, contact: CNMutableContact) {
59        unsafe { msg_send![self.m_self(), deleteContact: contact] }
60    }
61
62    /* Saving Group Changes */
63
64    /// Adds a group to the contact store.
65    #[method]
66    pub fn add_group_to_container_with_identifier(
67        &self,
68        group: CNMutableGroup,
69        identifier: NSString,
70    ) {
71        unsafe { msg_send![self.m_self(), addGroup: group toContainerWithIdentifier: identifier] }
72    }
73
74    /// Updates an existing group in the contact store.
75    #[method]
76    pub fn update_group(&self, group: CNMutableGroup) {
77        unsafe { msg_send![self.m_self(), updateGroup: group] }
78    }
79
80    /// Deletes a group from the contact store.
81    #[method]
82    pub fn delete_group(&self, group: CNMutableGroup) {
83        unsafe { msg_send![self.m_self(), deleteGroup: group] }
84    }
85
86    /// Adds a contact as a member of a group.
87    #[method]
88    pub fn add_member_to_group<Contact, Group>(&self, contact: Contact, group: Group)
89    where
90        Contact: ICNContact,
91        Group: ICNGroup,
92    {
93        unsafe { msg_send![self.m_self(), addMember: contact toGroup: group] }
94    }
95
96    /// Removes a contact from a group.
97    #[method]
98    pub fn remove_member_from_group<Contact, Group>(&self, contact: Contact, group: Group)
99    where
100        Contact: ICNContact,
101        Group: ICNGroup,
102    {
103        unsafe { msg_send![self.m_self(), removeMember: contact fromGroup: group] }
104    }
105
106    /* Adding and Removing Subgroups
107     */
108
109    /// Add the specified group to a parent group.
110    #[method]
111    pub fn add_subgroup_to_group<Group, ParentGroup>(&self, group: Group, parent_group: ParentGroup)
112    where
113        Group: ICNGroup,
114        ParentGroup: ICNGroup,
115    {
116        unsafe { msg_send![self.m_self(), addSubgroup: group toGroup: parent_group] }
117    }
118
119    /// Remove a subgroup from the specified parent group.
120    #[method]
121    pub fn remove_subgroup_from_group<Group, ParentGroup>(
122        &self,
123        group: Group,
124        parent_group: ParentGroup,
125    ) where
126        Group: ICNGroup,
127        ParentGroup: ICNGroup,
128    {
129        unsafe { msg_send![self.m_self(), removeSubgroup: group fromGroup: parent_group] }
130    }
131
132    /* Instance Properties
133     */
134
135    ///
136    #[property]
137    pub fn should_refetch_contacts(&self) -> bool {
138        unsafe { to_bool(msg_send![self.m_self(), shouldRefetchContacts]) }
139    }
140
141    ///
142    #[property]
143    pub fn transaction_author(&self) -> NSString {
144        unsafe { NSString::from_id(msg_send![self.m_self(), transactionAuthor]) }
145    }
146}