session-ios/Signal/src/views/ContactTableViewCell.m

124 lines
4.7 KiB
Mathematica
Raw Normal View History

2014-05-06 19:41:08 +02:00
#import "ContactTableViewCell.h"
2014-11-24 21:51:43 +01:00
#import "UIUtil.h"
2014-05-06 19:41:08 +02:00
#import "Environment.h"
#import "PhoneManager.h"
#import "DJWActionSheet.h"
2014-05-06 19:41:08 +02:00
#define CONTACT_TABLE_CELL_BORDER_WIDTH 1.0f
2014-10-29 21:58:58 +01:00
@interface ContactTableViewCell() {
}
@property(strong,nonatomic) Contact* associatedContact;
@end
2014-05-06 19:41:08 +02:00
@implementation ContactTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [NSBundle.mainBundle loadNibNamed:NSStringFromClass(self.class) owner:self options:nil][0];
2014-05-06 19:41:08 +02:00
_contactPictureView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
_contactPictureView.layer.masksToBounds = YES;
self.selectionStyle = UITableViewCellSelectionStyleGray;
2014-11-24 21:51:43 +01:00
_shouldShowContactButtons = YES;
2014-05-06 19:41:08 +02:00
return self;
}
- (NSString *)reuseIdentifier {
return NSStringFromClass(self.class);
2014-05-06 19:41:08 +02:00
}
2014-11-24 21:51:43 +01:00
-(void)showContactButtons:(BOOL)enabled
{
_callButton.hidden = !enabled;
_callButton.enabled = enabled;
_messageButton.hidden = !enabled;
_callButton.enabled = enabled;
}
2014-05-06 19:41:08 +02:00
- (void)configureWithContact:(Contact *)contact {
2014-11-24 21:51:43 +01:00
[self showContactButtons:_shouldShowContactButtons];
2014-10-29 21:58:58 +01:00
_associatedContact = contact;
2014-05-06 19:41:08 +02:00
_nameLabel.attributedText = [self attributedStringForContact:contact];
UIImage *image = contact.image;
BOOL imageNotNil = image != nil;
[self configureBorder:imageNotNil];
if (imageNotNil) {
_contactPictureView.image = image;
} else {
_contactPictureView.image = nil;
2014-10-29 21:58:58 +01:00
[_contactPictureView addConstraint:[NSLayoutConstraint constraintWithItem:_contactPictureView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:0]];
}
if (contact.isRedPhoneContact && _shouldShowContactButtons)
2014-10-29 21:58:58 +01:00
{
UIImage * callImage = [[UIImage imageNamed:@"call_dark"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_callButton setImage:callImage forState:UIControlStateNormal];
_callButton.tintColor = [UIColor ows_blueColor];
2014-10-29 21:58:58 +01:00
} else {
[_callButton setImage:[UIImage imageNamed:@"call_dotted"] forState:UIControlStateNormal];
2014-10-29 21:58:58 +01:00
}
if (contact.isTextSecureContact && _shouldShowContactButtons)
2014-10-29 21:58:58 +01:00
{
UIImage * messageImage = [[UIImage imageNamed:@"signal"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_messageButton setImage:messageImage forState:UIControlStateNormal];
_messageButton.tintColor = [UIColor ows_blueColor];
2014-10-29 21:58:58 +01:00
} else {
[_messageButton setImage:[UIImage imageNamed:@"signal_dotted"] forState:UIControlStateNormal];
2014-05-06 19:41:08 +02:00
}
}
- (void)configureBorder:(BOOL)show {
_contactPictureView.layer.borderWidth = show ? CONTACT_TABLE_CELL_BORDER_WIDTH : 0;
_contactPictureView.layer.cornerRadius = show ? CGRectGetWidth(_contactPictureView.frame)/2 : 0;
}
- (NSAttributedString *)attributedStringForContact:(Contact *)contact {
NSMutableAttributedString *fullNameAttributedString = [[NSMutableAttributedString alloc] initWithString:contact.fullName];
2014-05-06 19:41:08 +02:00
UIFont *firstNameFont;
UIFont *lastNameFont;
if (ABPersonGetSortOrdering() == kABPersonCompositeNameFormatFirstNameFirst) {
2014-11-24 21:51:43 +01:00
firstNameFont = [UIFont ows_thinFontWithSize:_nameLabel.font.pointSize];
2014-05-06 19:41:08 +02:00
lastNameFont = [UIFont systemFontOfSize:_nameLabel.font.pointSize];
} else{
2014-11-24 21:51:43 +01:00
firstNameFont = [UIFont ows_thinFontWithSize:_nameLabel.font.pointSize];
2014-10-29 21:58:58 +01:00
lastNameFont = [UIFont systemFontOfSize:_nameLabel.font.pointSize];
2014-05-06 19:41:08 +02:00
}
[fullNameAttributedString addAttribute:NSFontAttributeName value:firstNameFont range:NSMakeRange(0, contact.firstName.length)];
[fullNameAttributedString addAttribute:NSFontAttributeName value:lastNameFont range:NSMakeRange(contact.firstName.length + 1, contact.lastName.length)];
2014-05-06 19:41:08 +02:00
[fullNameAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, contact.fullName.length)];
2014-05-06 19:41:08 +02:00
return fullNameAttributedString;
}
2014-10-29 21:58:58 +01:00
-(IBAction)callContact:(id)sender
{
if (_associatedContact.isRedPhoneContact) {
NSArray *redPhoneIdentifiers = [_associatedContact redPhoneIdentifiers];
[Environment.phoneManager initiateOutgoingCallToContact:_associatedContact atRemoteNumber:[redPhoneIdentifiers firstObject]];
} else{
DDLogWarn(@"Tried to intiate a call but contact has no RedPhone identifier");
}
2014-10-29 21:58:58 +01:00
}
-(IBAction)messageContact:(id)sender
{
if (_associatedContact.isTextSecureContact) {
NSArray *textSecureIdentifiers = [_associatedContact textSecureIdentifiers];
[Environment messageIdentifier:[textSecureIdentifiers firstObject]];
} else{
DDLogWarn(@"Tried to intiate a call but contact has no RedPhone identifier");
}
2014-10-29 21:58:58 +01:00
}
2014-05-06 19:41:08 +02:00
@end