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

175 lines
6.1 KiB
Mathematica
Raw Normal View History

2017-03-23 14:55:39 +01:00
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
2014-05-06 19:41:08 +02:00
#import "ContactTableViewCell.h"
#import "ContactAccount.h"
#import "Environment.h"
#import "OWSContactAvatarBuilder.h"
#import "OWSContactsManager.h"
#import "UIFont+OWS.h"
#import "UIUtil.h"
#import "UIView+OWS.h"
2017-04-26 19:03:51 +02:00
#import <SignalServiceKit/TSGroupThread.h>
#import <SignalServiceKit/TSThread.h>
NS_ASSUME_NONNULL_BEGIN
2017-04-18 22:08:01 +02:00
NSString *const kContactsTable_CellReuseIdentifier = @"kContactsTable_CellReuseIdentifier";
@interface ContactTableViewCell ()
2014-05-06 19:41:08 +02:00
@property (nonatomic) IBOutlet UILabel *nameLabel;
@property (nonatomic) IBOutlet UIImageView *avatarView;
2014-10-29 21:58:58 +01:00
@end
2014-05-06 19:41:08 +02:00
@implementation ContactTableViewCell
- (instancetype)init
{
if (self = [super init]) {
[self configureProgrammatically];
}
return self;
}
+ (nullable NSString *)reuseIdentifier
{
return NSStringFromClass(self.class);
}
- (nullable NSString *)reuseIdentifier
{
return NSStringFromClass(self.class);
2014-05-06 19:41:08 +02:00
}
+ (CGFloat)rowHeight
{
return 59.f;
}
- (void)configureProgrammatically
{
2017-04-07 03:54:43 +02:00
const CGFloat kAvatarSize = 40.f;
_avatarView = [UIImageView new];
_avatarView.image = [UIImage imageNamed:@"empty-group-avatar"];
2017-04-26 21:55:01 +02:00
_avatarView.contentMode = UIViewContentModeScaleToFill;
2017-04-07 03:54:43 +02:00
// applyRoundedBorderToImageView requires the avatar to have
// the correct size.
_avatarView.frame = CGRectMake(0, 0, kAvatarSize, kAvatarSize);
2017-04-18 22:08:01 +02:00
_avatarView.layer.minificationFilter = kCAFilterTrilinear;
_avatarView.layer.magnificationFilter = kCAFilterTrilinear;
[self.contentView addSubview:_avatarView];
_nameLabel = [UILabel new];
_nameLabel.contentMode = UIViewContentModeLeft;
_nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_nameLabel.font = [UIFont ows_dynamicTypeBodyFont];
[self.contentView addSubview:_nameLabel];
[_avatarView autoVCenterInSuperview];
[_avatarView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:ScaleFromIPhone5To7Plus(14.f, 20.f)];
2017-04-07 03:54:43 +02:00
[_avatarView autoSetDimension:ALDimensionWidth toSize:kAvatarSize];
[_avatarView autoSetDimension:ALDimensionHeight toSize:kAvatarSize];
[_nameLabel autoPinEdgeToSuperviewEdge:ALEdgeRight];
[_nameLabel autoPinEdgeToSuperviewEdge:ALEdgeTop];
[_nameLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom];
[_nameLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:_avatarView withOffset:12.f];
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
[self layoutSubviews];
}
- (void)configureWithContact:(Contact *)contact contactsManager:(OWSContactsManager *)contactsManager
{
2017-04-18 22:08:01 +02:00
[self configureWithRecipientId:contact.textSecureIdentifiers.firstObject
avatarName:contact.fullName
displayName:[contactsManager formattedFullNameForContact:contact font:self.nameLabel.font]
contactsManager:contactsManager];
}
- (void)configureWithContactAccount:(ContactAccount *)contactAccount
contactsManager:(OWSContactsManager *)contactsManager
{
[self configureWithRecipientId:contactAccount.recipientId
avatarName:contactAccount.contact.fullName
displayName:[contactsManager formattedDisplayNameForContactAccount:contactAccount
font:self.nameLabel.font]
contactsManager:contactsManager];
}
2017-04-18 22:08:01 +02:00
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager
{
[self
configureWithRecipientId:recipientId
avatarName:@""
displayName:[contactsManager formattedFullNameForRecipientId:recipientId font:self.nameLabel.font]
contactsManager:contactsManager];
}
- (void)configureWithRecipientId:(NSString *)recipientId
avatarName:(NSString *)avatarName
displayName:(NSAttributedString *)displayName
contactsManager:(OWSContactsManager *)contactsManager
{
NSMutableAttributedString *attributedText = [displayName mutableCopy];
if (self.accessoryMessage) {
UILabel *blockedLabel = [[UILabel alloc] init];
blockedLabel.textAlignment = NSTextAlignmentRight;
blockedLabel.text = self.accessoryMessage;
blockedLabel.font = [UIFont ows_mediumFontWithSize:13.f];
blockedLabel.textColor = [UIColor colorWithWhite:0.5f alpha:1.f];
[blockedLabel sizeToFit];
self.accessoryView = blockedLabel;
}
self.nameLabel.attributedText = attributedText;
self.avatarView.image =
2017-04-18 22:08:01 +02:00
[[[OWSContactAvatarBuilder alloc] initWithContactId:recipientId name:avatarName contactsManager:contactsManager]
build];
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
[self layoutSubviews];
}
2017-04-26 19:03:51 +02:00
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager
{
OWSAssert(thread);
NSString *threadName = thread.name;
if (threadName.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) {
threadName = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"");
}
NSAttributedString *attributedText = [[NSAttributedString alloc]
initWithString:threadName
attributes:@{
NSForegroundColorAttributeName : [UIColor blackColor],
}];
self.nameLabel.attributedText = attributedText;
self.avatarView.image = [OWSAvatarBuilder buildImageForThread:thread contactsManager:contactsManager];
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
[self layoutSubviews];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[UIUtil applyRoundedBorderToImageView:self.avatarView];
2014-05-06 19:41:08 +02:00
}
- (void)prepareForReuse
{
self.accessoryMessage = nil;
self.accessoryView = nil;
self.accessoryType = UITableViewCellAccessoryNone;
}
2014-05-06 19:41:08 +02:00
@end
NS_ASSUME_NONNULL_END