2016-11-23 17:07:38 +01:00
// O r i g i n a l l y b a s e d o n E P C o n t a c t s
//
// C r e a t e d b y P r a b a h a r a n E l a n g o v a n o n 1 3 / 1 0 / 1 5 .
// C o p y r i g h t © 2 0 1 5 P r a b a h a r a n E l a n g o v a n . A l l r i g h t s r e s e r v e d .
//
// M o d i f i e d f o r S i g n a l b y M i c h a e l K i r k o n 1 1 / 2 5 / 2 0 1 6
// P a r t s C o p y r i g h t © 2 0 1 6 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
import UIKit
@ available ( iOS 9.0 , * )
class ContactCell : UITableViewCell {
static let nib = UINib ( nibName : " ContactCell " , bundle : nil )
@IBOutlet weak var contactTextLabel : UILabel !
@IBOutlet weak var contactDetailTextLabel : UILabel !
@IBOutlet weak var contactImageView : UIImageView !
@IBOutlet weak var contactContainerView : UIView !
var contact : Contact ?
override func awakeFromNib ( ) {
super . awakeFromNib ( )
// I n i t i a l i z a t i o n c o d e
selectionStyle = UITableViewCellSelectionStyle . none
contactContainerView . layer . masksToBounds = true
contactContainerView . layer . cornerRadius = contactContainerView . frame . size . width / 2
NotificationCenter . default . addObserver ( self , selector : #selector ( self . didChangePreferredContentSize ) , name : NSNotification . Name . UIContentSizeCategoryDidChange , object : nil )
}
override func prepareForReuse ( ) {
accessoryType = . none
}
override func setSelected ( _ selected : Bool , animated : Bool ) {
super . setSelected ( selected , animated : animated )
accessoryType = selected ? . checkmark : . none
}
func didChangePreferredContentSize ( ) {
contactTextLabel . font = UIFont . preferredFont ( forTextStyle : . body )
contactDetailTextLabel . font = UIFont . preferredFont ( forTextStyle : . subheadline )
}
func updateContactsinUI ( _ contact : Contact , subtitleType : SubtitleCellValue , contactsManager : OWSContactsManager ) {
self . contact = contact
if contactTextLabel != nil {
contactTextLabel . attributedText = contact . cnContact ? . formattedFullName ( font : contactTextLabel . font )
}
updateSubtitleBasedonType ( subtitleType , contact : contact )
if contact . image = = nil {
let contactIdForDeterminingBackgroundColor : String
if let signalId = contact . parsedPhoneNumbers . first ? . toE164 ( ) {
contactIdForDeterminingBackgroundColor = signalId
} else {
contactIdForDeterminingBackgroundColor = contact . fullName
}
let avatarBuilder = OWSContactAvatarBuilder ( contactId : contactIdForDeterminingBackgroundColor ,
name : contact . fullName ,
contactsManager : contactsManager )
self . contactImageView ? . image = avatarBuilder . buildDefaultImage ( ) ;
} else {
self . contactImageView ? . image = contact . image
}
}
func updateSubtitleBasedonType ( _ subtitleType : SubtitleCellValue , contact : Contact ) {
switch subtitleType {
case SubtitleCellValue . phoneNumber :
if contact . userTextPhoneNumbers . count > 0 {
self . contactDetailTextLabel . text = " \( contact . userTextPhoneNumbers [ 0 ] ) "
} else {
self . contactDetailTextLabel . text = NSLocalizedString ( " CONTACT_PICKER_NO_PHONE_NUMBERS_AVAILABLE " , comment : " table cell subtitle when contact card has no known phone number " )
}
case SubtitleCellValue . email :
if contact . emails . count > 0 {
self . contactDetailTextLabel . text = " \( contact . emails [ 0 ] ) "
} else {
self . contactDetailTextLabel . text = NSLocalizedString ( " CONTACT_PICKER_NO_EMAILS_AVAILABLE " , comment : " table cell subtitle when contact card has no email " )
}
}
}
}
@ available ( iOS 9.0 , * )
fileprivate extension CNContact {
/* *
* Bold the sorting portion of the name . e . g . if we sort by family name , bold the family name .
*/
func formattedFullName ( font : UIFont ) -> NSAttributedString ? {
let keyToHighlight = ContactSortOrder = = . familyName ? CNContactFamilyNameKey : CNContactGivenNameKey
2016-12-09 20:19:18 +01:00
let boldDescriptor = font . fontDescriptor . withSymbolicTraits ( . traitBold )
let boldAttributes = [
NSFontAttributeName : UIFont ( descriptor : boldDescriptor ! , size : 0 )
]
2016-11-23 17:07:38 +01:00
if let attributedName = CNContactFormatter . attributedString ( from : self , style : . fullName , defaultAttributes : nil ) {
let highlightedName = attributedName . mutableCopy ( ) as ! NSMutableAttributedString
highlightedName . enumerateAttributes ( in : NSMakeRange ( 0 , highlightedName . length ) , options : [ ] , using : { ( attrs , range , stop ) in
if let property = attrs [ CNContactPropertyAttribute ] as ? String , property = = keyToHighlight {
highlightedName . addAttributes ( boldAttributes , range : range )
}
} )
return highlightedName
}
2016-12-09 20:19:18 +01:00
if let emailAddress = ( self . emailAddresses . first ? . value as String ? ) {
return NSAttributedString ( string : emailAddress , attributes : boldAttributes )
}
2016-11-23 17:07:38 +01:00
return nil
}
}