2017-03-23 14:55:39 +01:00
|
|
|
//
|
2018-01-17 23:45:47 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-03-23 14:55:39 +01:00
|
|
|
//
|
|
|
|
|
2014-05-06 19:41:08 +02:00
|
|
|
#import "ContactTableViewCell.h"
|
2014-11-29 19:54:33 +01:00
|
|
|
#import "Environment.h"
|
2016-11-18 23:11:56 +01:00
|
|
|
#import "OWSContactAvatarBuilder.h"
|
|
|
|
#import "OWSContactsManager.h"
|
2017-12-08 19:45:24 +01:00
|
|
|
#import "OWSUserProfile.h"
|
2017-03-31 23:43:42 +02:00
|
|
|
#import "UIFont+OWS.h"
|
2016-11-18 23:11:56 +01:00
|
|
|
#import "UIUtil.h"
|
2017-03-31 23:43:42 +02:00
|
|
|
#import "UIView+OWS.h"
|
2017-12-04 23:17:44 +01:00
|
|
|
#import <SignalMessaging/SignalMessaging-Swift.h>
|
2017-05-05 18:33:10 +02:00
|
|
|
#import <SignalServiceKit/SignalAccount.h>
|
2017-12-04 23:17:44 +01:00
|
|
|
#import <SignalServiceKit/TSContactThread.h>
|
2017-04-26 19:03:51 +02:00
|
|
|
#import <SignalServiceKit/TSGroupThread.h>
|
|
|
|
#import <SignalServiceKit/TSThread.h>
|
2014-11-29 19:54:33 +01:00
|
|
|
|
2016-11-22 21:19:49 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-04-18 22:08:01 +02:00
|
|
|
NSString *const kContactsTable_CellReuseIdentifier = @"kContactsTable_CellReuseIdentifier";
|
2017-05-27 17:56:08 +02:00
|
|
|
const NSUInteger kContactTableViewCellAvatarSize = 40;
|
2017-10-14 19:20:46 +02:00
|
|
|
const CGFloat kContactTableViewCellAvatarTextMargin = 12;
|
2017-04-18 22:08:01 +02:00
|
|
|
|
2016-06-28 02:07:18 +02:00
|
|
|
@interface ContactTableViewCell ()
|
2014-05-06 19:41:08 +02:00
|
|
|
|
2017-08-23 21:41:56 +02:00
|
|
|
@property (nonatomic) UILabel *nameLabel;
|
|
|
|
@property (nonatomic) UILabel *profileNameLabel;
|
|
|
|
@property (nonatomic) UIImageView *avatarView;
|
|
|
|
@property (nonatomic) UILabel *subtitle;
|
|
|
|
@property (nonatomic) UIView *nameContainerView;
|
2016-06-28 02:07:18 +02:00
|
|
|
|
2017-08-23 23:09:43 +02:00
|
|
|
@property (nonatomic) OWSContactsManager *contactsManager;
|
|
|
|
@property (nonatomic) NSString *recipientId;
|
|
|
|
|
2014-10-29 21:58:58 +01:00
|
|
|
@end
|
|
|
|
|
2014-05-06 19:41:08 +02:00
|
|
|
@implementation ContactTableViewCell
|
|
|
|
|
2017-03-31 23:43:42 +02:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
[self configureProgrammatically];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-04-06 17:44:03 +02:00
|
|
|
+ (nullable NSString *)reuseIdentifier
|
|
|
|
{
|
|
|
|
return NSStringFromClass(self.class);
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:19:49 +01:00
|
|
|
- (nullable NSString *)reuseIdentifier
|
|
|
|
{
|
2014-09-08 01:31:05 +02:00
|
|
|
return NSStringFromClass(self.class);
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 22:16:56 +02:00
|
|
|
- (CGSize)intrinsicContentSize
|
|
|
|
{
|
|
|
|
return CGSizeMake(self.width, ContactTableViewCell.rowHeight);
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:43:42 +02:00
|
|
|
+ (CGFloat)rowHeight
|
|
|
|
{
|
|
|
|
return 59.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)configureProgrammatically
|
|
|
|
{
|
2017-07-11 00:44:46 +02:00
|
|
|
self.preservesSuperviewLayoutMargins = YES;
|
|
|
|
self.contentView.preservesSuperviewLayoutMargins = YES;
|
|
|
|
|
2017-05-13 23:36:12 +02:00
|
|
|
_avatarView = [AvatarImageView new];
|
2017-03-31 23:43:42 +02:00
|
|
|
[self.contentView addSubview:_avatarView];
|
|
|
|
|
2017-08-23 21:41:56 +02:00
|
|
|
_nameContainerView = [UIView containerView];
|
|
|
|
[self.contentView addSubview:_nameContainerView];
|
2017-08-16 21:14:52 +02:00
|
|
|
|
2017-03-31 23:43:42 +02:00
|
|
|
_nameLabel = [UILabel new];
|
|
|
|
_nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
|
|
_nameLabel.font = [UIFont ows_dynamicTypeBodyFont];
|
2017-08-23 21:41:56 +02:00
|
|
|
[_nameContainerView addSubview:_nameLabel];
|
2017-08-16 21:14:52 +02:00
|
|
|
|
|
|
|
_profileNameLabel = [UILabel new];
|
|
|
|
_profileNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
2018-04-09 22:13:48 +02:00
|
|
|
_profileNameLabel.font = [UIFont ows_regularFontWithSize:11.f];
|
2017-08-16 21:14:52 +02:00
|
|
|
_profileNameLabel.textColor = [UIColor grayColor];
|
2017-08-23 21:41:56 +02:00
|
|
|
[_nameContainerView addSubview:_profileNameLabel];
|
|
|
|
|
|
|
|
_subtitle = [UILabel new];
|
2018-04-09 22:13:48 +02:00
|
|
|
_subtitle.font = [UIFont ows_regularFontWithSize:11.f];
|
2017-08-23 21:41:56 +02:00
|
|
|
_subtitle.textColor = [UIColor ows_darkGrayColor];
|
|
|
|
[_nameContainerView addSubview:self.subtitle];
|
2017-03-31 23:43:42 +02:00
|
|
|
|
|
|
|
[_avatarView autoVCenterInSuperview];
|
2018-04-02 21:31:32 +02:00
|
|
|
[_avatarView autoPinLeadingToSuperviewMargin];
|
2017-05-27 17:56:08 +02:00
|
|
|
[_avatarView autoSetDimension:ALDimensionWidth toSize:kContactTableViewCellAvatarSize];
|
|
|
|
[_avatarView autoSetDimension:ALDimensionHeight toSize:kContactTableViewCellAvatarSize];
|
2017-03-31 23:43:42 +02:00
|
|
|
|
2017-08-16 21:14:52 +02:00
|
|
|
[_nameLabel autoPinEdgeToSuperviewEdge:ALEdgeTop];
|
|
|
|
[_nameLabel autoPinWidthToSuperview];
|
|
|
|
|
|
|
|
// profileNameLabel can be zero sized, in which case nameLabel essentially occupies the totality of
|
|
|
|
// nameContainerView's frame.
|
|
|
|
[_profileNameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_nameLabel];
|
|
|
|
[_profileNameLabel autoPinWidthToSuperview];
|
|
|
|
|
2017-08-23 21:41:56 +02:00
|
|
|
[_subtitle autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_profileNameLabel];
|
|
|
|
[_subtitle autoPinWidthToSuperview];
|
|
|
|
[_subtitle autoPinEdgeToSuperviewEdge:ALEdgeBottom];
|
|
|
|
|
|
|
|
[_nameContainerView autoVCenterInSuperview];
|
2018-04-02 21:31:32 +02:00
|
|
|
[_nameContainerView autoPinLeadingToTrailingEdgeOfView:_avatarView offset:kContactTableViewCellAvatarTextMargin];
|
|
|
|
[_nameContainerView autoPinTrailingToSuperviewMargin];
|
2017-03-31 23:43:42 +02:00
|
|
|
|
|
|
|
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
|
|
|
[self layoutSubviews];
|
|
|
|
}
|
|
|
|
|
2017-05-01 18:51:59 +02:00
|
|
|
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager
|
2017-04-30 16:34:28 +02:00
|
|
|
{
|
2017-12-04 23:17:44 +01:00
|
|
|
[self configureWithRecipientId:signalAccount.recipientId contactsManager:contactsManager];
|
2017-04-30 16:34:28 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager
|
2017-04-18 22:08:01 +02:00
|
|
|
{
|
2017-08-23 23:09:43 +02:00
|
|
|
self.recipientId = recipientId;
|
|
|
|
self.contactsManager = contactsManager;
|
|
|
|
|
2017-08-16 21:14:52 +02:00
|
|
|
self.nameLabel.attributedText =
|
|
|
|
[contactsManager formattedFullNameForRecipientId:recipientId font:self.nameLabel.font];
|
|
|
|
|
2017-08-23 23:09:43 +02:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(otherUsersProfileDidChange:)
|
|
|
|
name:kNSNotificationName_OtherUsersProfileDidChange
|
|
|
|
object:nil];
|
|
|
|
[self updateProfileName];
|
2017-08-25 18:39:27 +02:00
|
|
|
[self updateAvatar];
|
2017-08-16 21:14:52 +02:00
|
|
|
|
2017-04-06 17:44:03 +02:00
|
|
|
if (self.accessoryMessage) {
|
2017-04-05 22:32:34 +02:00
|
|
|
UILabel *blockedLabel = [[UILabel alloc] init];
|
|
|
|
blockedLabel.textAlignment = NSTextAlignmentRight;
|
2017-04-06 17:44:03 +02:00
|
|
|
blockedLabel.text = self.accessoryMessage;
|
2017-04-06 03:37:26 +02:00
|
|
|
blockedLabel.font = [UIFont ows_mediumFontWithSize:13.f];
|
|
|
|
blockedLabel.textColor = [UIColor colorWithWhite:0.5f alpha:1.f];
|
2017-04-05 22:32:34 +02:00
|
|
|
[blockedLabel sizeToFit];
|
|
|
|
|
|
|
|
self.accessoryView = blockedLabel;
|
2017-04-05 19:27:29 +02:00
|
|
|
}
|
2017-08-16 21:14:52 +02:00
|
|
|
|
2016-11-22 21:19:49 +01:00
|
|
|
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
|
|
|
[self layoutSubviews];
|
2016-11-18 23:11:56 +01:00
|
|
|
}
|
|
|
|
|
2017-04-26 19:03:51 +02:00
|
|
|
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager
|
|
|
|
{
|
|
|
|
OWSAssert(thread);
|
2017-08-23 23:09:43 +02:00
|
|
|
self.contactsManager = contactsManager;
|
2017-04-26 19:03:51 +02:00
|
|
|
|
|
|
|
NSString *threadName = thread.name;
|
|
|
|
if (threadName.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) {
|
2017-10-14 19:20:46 +02:00
|
|
|
threadName = [MessageStrings newGroupDefaultTitle];
|
2017-04-26 19:03:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:17:44 +01:00
|
|
|
NSAttributedString *attributedText =
|
|
|
|
[[NSAttributedString alloc] initWithString:threadName
|
|
|
|
attributes:@{
|
|
|
|
NSForegroundColorAttributeName : [UIColor blackColor],
|
|
|
|
}];
|
2017-04-26 19:03:51 +02:00
|
|
|
self.nameLabel.attributedText = attributedText;
|
|
|
|
|
2017-08-23 23:09:43 +02:00
|
|
|
if ([thread isKindOfClass:[TSContactThread class]]) {
|
2017-12-04 17:26:17 +01:00
|
|
|
self.recipientId = thread.contactIdentifier;
|
|
|
|
|
2017-08-23 23:09:43 +02:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(otherUsersProfileDidChange:)
|
|
|
|
name:kNSNotificationName_OtherUsersProfileDidChange
|
|
|
|
object:nil];
|
|
|
|
[self updateProfileName];
|
|
|
|
}
|
2017-05-27 17:56:08 +02:00
|
|
|
self.avatarView.image = [OWSAvatarBuilder buildImageForThread:thread
|
2017-07-31 22:07:38 +02:00
|
|
|
diameter:kContactTableViewCellAvatarSize
|
|
|
|
contactsManager:contactsManager];
|
2017-04-26 19:03:51 +02:00
|
|
|
|
2018-01-17 23:45:47 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:03:51 +02:00
|
|
|
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
|
|
|
[self layoutSubviews];
|
|
|
|
}
|
|
|
|
|
2017-08-23 21:41:56 +02:00
|
|
|
- (NSAttributedString *)verifiedSubtitle
|
2017-06-09 22:21:59 +02:00
|
|
|
{
|
|
|
|
NSMutableAttributedString *text = [NSMutableAttributedString new];
|
|
|
|
// "checkmark"
|
|
|
|
[text appendAttributedString:[[NSAttributedString alloc]
|
|
|
|
initWithString:@"\uf00c "
|
|
|
|
attributes:@{
|
2017-08-23 21:41:56 +02:00
|
|
|
NSFontAttributeName :
|
|
|
|
[UIFont ows_fontAwesomeFont:self.subtitle.font.pointSize],
|
2017-06-09 22:21:59 +02:00
|
|
|
}]];
|
|
|
|
[text appendAttributedString:[[NSAttributedString alloc]
|
|
|
|
initWithString:NSLocalizedString(@"PRIVACY_IDENTITY_IS_VERIFIED_BADGE",
|
|
|
|
@"Badge indicating that the user is verified.")]];
|
2017-08-23 21:41:56 +02:00
|
|
|
return [text copy];
|
2017-06-09 22:21:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 18:39:27 +02:00
|
|
|
- (void)updateAvatar
|
|
|
|
{
|
|
|
|
OWSContactsManager *contactsManager = self.contactsManager;
|
|
|
|
if (contactsManager == nil) {
|
|
|
|
OWSFail(@"%@ contactsManager should not be nil", self.logTag);
|
|
|
|
self.avatarView.image = nil;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *recipientId = self.recipientId;
|
|
|
|
if (recipientId.length == 0) {
|
|
|
|
OWSFail(@"%@ recipientId should not be nil", self.logTag);
|
|
|
|
self.avatarView.image = nil;
|
|
|
|
return;
|
|
|
|
}
|
2017-08-23 23:09:43 +02:00
|
|
|
|
2017-08-25 18:39:27 +02:00
|
|
|
self.avatarView.image = [[[OWSContactAvatarBuilder alloc] initWithSignalId:recipientId
|
|
|
|
diameter:kContactTableViewCellAvatarSize
|
|
|
|
contactsManager:contactsManager] build];
|
|
|
|
}
|
2017-12-04 17:26:17 +01:00
|
|
|
|
2017-08-23 23:09:43 +02:00
|
|
|
- (void)updateProfileName
|
|
|
|
{
|
|
|
|
OWSContactsManager *contactsManager = self.contactsManager;
|
|
|
|
if (contactsManager == nil) {
|
|
|
|
OWSFail(@"%@ contactsManager should not be nil", self.logTag);
|
|
|
|
self.profileNameLabel.text = nil;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *recipientId = self.recipientId;
|
|
|
|
if (recipientId.length == 0) {
|
|
|
|
OWSFail(@"%@ recipientId should not be nil", self.logTag);
|
|
|
|
self.profileNameLabel.text = nil;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([contactsManager hasNameInSystemContactsForRecipientId:recipientId]) {
|
|
|
|
// Don't display profile name when we have a veritas name in system Contacts
|
|
|
|
self.profileNameLabel.text = nil;
|
|
|
|
} else {
|
|
|
|
// Use profile name, if any is available
|
|
|
|
self.profileNameLabel.text = [contactsManager formattedProfileNameForRecipientId:recipientId];
|
|
|
|
}
|
2017-08-24 20:43:52 +02:00
|
|
|
|
|
|
|
[self.profileNameLabel setNeedsLayout];
|
2017-08-23 23:09:43 +02:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:44:03 +02:00
|
|
|
- (void)prepareForReuse
|
|
|
|
{
|
2017-11-08 18:56:55 +01:00
|
|
|
[super prepareForReuse];
|
|
|
|
|
2017-08-23 23:09:43 +02:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
2017-04-06 17:44:03 +02:00
|
|
|
self.accessoryMessage = nil;
|
|
|
|
self.accessoryView = nil;
|
|
|
|
self.accessoryType = UITableViewCellAccessoryNone;
|
2017-08-23 23:09:43 +02:00
|
|
|
self.nameLabel.text = nil;
|
|
|
|
self.subtitle.text = nil;
|
|
|
|
self.profileNameLabel.text = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherUsersProfileDidChange:(NSNotification *)notification
|
|
|
|
{
|
2017-12-19 17:38:25 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
2017-08-23 23:09:43 +02:00
|
|
|
|
|
|
|
NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId];
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
|
|
|
if (recipientId.length > 0 && [self.recipientId isEqualToString:recipientId]) {
|
|
|
|
[self updateProfileName];
|
2017-08-25 18:39:27 +02:00
|
|
|
[self updateAvatar];
|
2017-08-23 23:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 19:41:08 +02:00
|
|
|
@end
|
2016-11-22 21:19:49 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|