use std::fmt::Debug;
use crate::{
foundation::{NSArray, NSComparator, NSData, NSDateComponents, NSPredicate, NSString},
object,
objective_c_runtime::{
id,
macros::interface_impl,
nil,
traits::{FromId, PNSObject},
},
utils::to_bool,
};
use objc::{msg_send, sel, sel_impl};
use super::{
CNContactRelation, CNInstantMessageAddress, CNLabeledValue, CNPhoneNumber, CNPostalAddress,
CNSocialProfile,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(i64)]
pub enum CNContactType {
Person,
Organization,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(i64)]
pub enum CNContactSortOrder {
None,
UserDefault,
GivenName,
FamilyName,
}
object! {
unsafe pub struct CNContact;
}
#[interface_impl(NSObject)]
impl CNContact {
#[property]
pub fn identifier(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), identifier]) }
}
#[property]
pub fn contact_type(&self) -> CNContactType {
unsafe { msg_send![self.m_self(), contactType] }
}
#[property]
pub fn name_prefix(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), namePrefix]) }
}
#[property]
pub fn given_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), givenName]) }
}
#[property]
pub fn middle_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), middleName]) }
}
#[property]
pub fn family_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), familyName]) }
}
#[property]
pub fn previous_family_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), previousFamilyName]) }
}
#[property]
pub fn name_suffix(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), nameSuffix]) }
}
#[property]
pub fn nickname(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), nickname]) }
}
#[property]
pub fn phonetic_given_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), phoneticGivenName]) }
}
#[property]
pub fn phonetic_middle_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), phoneticMiddleName]) }
}
#[property]
pub fn phonetic_family_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), phoneticFamilyName]) }
}
#[property]
pub fn job_title(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), jobTitle]) }
}
#[property]
pub fn department_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), departmentName]) }
}
#[property]
pub fn organization_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), organizationName]) }
}
#[property]
pub fn phonetic_organization_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), phoneticOrganizationName]) }
}
#[property]
pub fn postal_addresses(&self) -> NSArray<CNLabeledValue<CNPostalAddress>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), postalAddresses]) }
}
#[property]
pub fn email_addresses(&self) -> NSArray<CNLabeledValue<NSString>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), emailAddresses]) }
}
#[property]
pub fn url_addresses(&self) -> NSArray<CNLabeledValue<NSString>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), urlAddresses]) }
}
#[property]
pub fn phone_numbers(&self) -> NSArray<CNLabeledValue<CNPhoneNumber>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), phoneNumbers]) }
}
#[property]
pub fn social_profiles(&self) -> NSArray<CNLabeledValue<CNSocialProfile>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), socialProfiles]) }
}
#[property]
pub fn birthday(&self) -> Option<NSDateComponents> {
unsafe {
let ptr = msg_send![self.m_self(), birthday];
if ptr != nil {
Some(NSDateComponents::from_id(ptr))
} else {
None
}
}
}
#[property]
pub fn non_gregorian_birthday(&self) -> Option<NSDateComponents> {
unsafe {
let ptr = msg_send![self.m_self(), nonGregorianBirthday];
if ptr != nil {
Some(NSDateComponents::from_id(ptr))
} else {
None
}
}
}
#[property]
pub fn dates(&self) -> NSArray<CNLabeledValue<NSDateComponents>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), dates]) }
}
#[property]
pub fn note(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), note]) }
}
#[property]
pub fn image_data(&self) -> Option<NSData> {
unsafe {
let ptr = msg_send![self.m_self(), imageData];
if ptr != nil {
Some(NSData::from_id(ptr))
} else {
None
}
}
}
#[property]
pub fn thumbnail_image_data(&self) -> Option<NSData> {
unsafe {
let ptr = msg_send![self.m_self(), thumbnailImageData];
if ptr != nil {
Some(NSData::from_id(ptr))
} else {
None
}
}
}
#[property]
pub fn image_data_available(&self) -> bool {
unsafe { to_bool(msg_send![self.m_self(), imageDataAvailable]) }
}
#[property]
pub fn contact_relations(&self) -> NSArray<CNLabeledValue<CNContactRelation>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), contactRelations]) }
}
#[property]
pub fn instant_messaging_addresses(&self) -> NSArray<CNLabeledValue<CNInstantMessageAddress>> {
unsafe { NSArray::from_id(msg_send![self.m_self(), instantMessageAddresses]) }
}
#[method]
pub fn localized_string_for_key(property: NSString) -> NSString {
unsafe { NSString::from_id(msg_send![Self::m_class(), localizedStringForKey: property]) }
}
#[method]
pub fn descriptor_for_all_comparator_keys() -> id {
unsafe { msg_send![Self::m_class(), descriptorForAllComparatorKeys] }
}
#[method]
pub fn comparator_for_name_sort_order(sort_order: CNContactSortOrder) -> NSComparator {
unsafe { msg_send![Self::m_class(), comparatorForNameSortOrder: sort_order] }
}
#[method]
pub fn is_unified_with_contact_with_identifier(&self, identifier: NSString) -> bool {
unsafe {
to_bool(msg_send![
self.m_self(),
isUnifiedWithContactWithIdentifier: identifier
])
}
}
#[method]
pub fn is_key_available(&self, key: NSString) -> bool {
unsafe { to_bool(msg_send![self.m_self(), isKeyAvailable: key]) }
}
#[method]
pub fn are_keys_available(&self, key_descriptors: NSArray<id>) -> bool {
unsafe { to_bool(msg_send![self.m_self(), areKeysAvailable: key_descriptors]) }
}
#[method]
pub fn predicate_for_contacts_matching_name(name: NSString) -> NSPredicate {
unsafe {
NSPredicate::from_id(msg_send![
Self::m_class(),
predicateForContactsMatchingName: name
])
}
}
#[method]
pub fn predicate_for_contacts_with_identifiers(identifiers: NSArray<String>) -> NSPredicate {
unsafe {
NSPredicate::from_id(msg_send![
Self::m_class(),
predicateForContactsWithIdentifiers: identifiers
])
}
}
#[method]
pub fn predicate_for_contacts_in_group_with_identifier(group: NSString) -> NSPredicate {
unsafe {
NSPredicate::from_id(msg_send![
Self::m_class(),
predicateForContactsInGroupWithIdentifier: group
])
}
}
#[method]
pub fn predicate_for_contacts_in_container_with_identifier(container: NSString) -> NSPredicate {
unsafe {
NSPredicate::from_id(msg_send![
Self::m_class(),
predicateForContactsInContainerWithIdentifier: container
])
}
}
#[method]
pub fn predicate_for_contacts_matching_phone_number(
phone_number: CNPhoneNumber,
) -> NSPredicate {
unsafe {
NSPredicate::from_id(msg_send![
Self::m_class(),
predicateForContactsMatchingPhoneNumber: phone_number
])
}
}
#[method]
pub fn predicate_for_contacts_matching_email_address(email_address: NSString) -> NSPredicate {
unsafe {
NSPredicate::from_id(msg_send![
Self::m_class(),
predicateForContactsMatchingEmailAddress: email_address
])
}
}
}
#[cfg(test)]
mod tests {
use crate::{contacts::CNContactType, objective_c_runtime::traits::PNSObject};
use super::CNContact;
#[test]
fn test_contact() {
let contact = CNContact::m_new();
assert!(contact.birthday().is_none());
assert!(contact.contact_relations().count() == 0);
assert!(contact.contact_type() == CNContactType::Person);
assert!(contact.dates().count() == 0);
assert!(contact.department_name() == "");
assert!(contact.email_addresses().count() == 0);
assert!(contact.family_name() == "");
assert!(contact.given_name() == "");
assert!(contact.identifier() != "");
assert!(contact.image_data().is_none());
assert!(!contact.image_data_available());
assert!(contact.instant_messaging_addresses().count() == 0);
assert!(contact.job_title() == "");
assert!(contact.middle_name() == "");
assert!(contact.name_prefix() == "");
assert!(contact.name_suffix() == "");
assert!(contact.nickname() == "");
assert!(contact.non_gregorian_birthday().is_none());
assert!(contact.note() == "");
assert!(contact.organization_name() == "");
assert!(contact.phone_numbers().count() == 0);
assert!(contact.phonetic_given_name() == "");
assert!(contact.phonetic_middle_name() == "");
assert!(contact.phonetic_family_name() == "");
assert!(contact.postal_addresses().count() == 0);
assert!(contact.previous_family_name() == "");
assert!(contact.social_profiles().count() == 0);
assert!(contact.thumbnail_image_data().is_none());
assert!(contact.url_addresses().count() == 0);
}
}