317 lines
10 KiB
Objective-C
317 lines
10 KiB
Objective-C
//
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "ContactCellView.h"
|
|
#import "OWSContactAvatarBuilder.h"
|
|
#import "OWSContactsManager.h"
|
|
#import "UIFont+OWS.h"
|
|
#import "UIView+OWS.h"
|
|
#import <SignalMessaging/SignalMessaging-Swift.h>
|
|
#import <SignalServiceKit/SignalAccount.h>
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
|
#import <SignalServiceKit/TSContactThread.h>
|
|
#import <SignalServiceKit/TSGroupThread.h>
|
|
#import <SignalServiceKit/TSThread.h>
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
const CGFloat kContactCellAvatarTextMargin = 12;
|
|
|
|
@interface ContactCellView ()
|
|
|
|
@property (nonatomic) UILabel *nameLabel;
|
|
@property (nonatomic) UIImageView *avatarView;
|
|
@property (nonatomic) UILabel *subtitleLabel;
|
|
@property (nonatomic) UILabel *profileNameLabel;
|
|
@property (nonatomic) UILabel *accessoryLabel;
|
|
@property (nonatomic) UIStackView *nameContainerView;
|
|
@property (nonatomic) UIView *accessoryViewContainer;
|
|
|
|
@property (nonatomic, nullable) TSThread *thread;
|
|
@property (nonatomic) SignalServiceAddress *address;
|
|
|
|
@end
|
|
|
|
#pragma mark -
|
|
|
|
@implementation ContactCellView
|
|
|
|
- (instancetype)init
|
|
{
|
|
if (self = [super init]) {
|
|
[self configure];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - Dependencies
|
|
|
|
- (OWSContactsManager *)contactsManager
|
|
{
|
|
OWSAssertDebug(Environment.shared.contactsManager);
|
|
|
|
return Environment.shared.contactsManager;
|
|
}
|
|
|
|
- (SDSDatabaseStorage *)databaseStorage
|
|
{
|
|
return SDSDatabaseStorage.shared;
|
|
}
|
|
|
|
- (TSAccountManager *)tsAccountManager
|
|
{
|
|
OWSAssertDebug(SSKEnvironment.shared.tsAccountManager);
|
|
|
|
return SSKEnvironment.shared.tsAccountManager;
|
|
}
|
|
|
|
#pragma mark -
|
|
|
|
- (void)configure
|
|
{
|
|
OWSAssertDebug(!self.nameLabel);
|
|
|
|
self.layoutMargins = UIEdgeInsetsZero;
|
|
|
|
_avatarView = [AvatarImageView new];
|
|
[_avatarView autoSetDimension:ALDimensionWidth toSize:kStandardAvatarSize];
|
|
[_avatarView autoSetDimension:ALDimensionHeight toSize:kStandardAvatarSize];
|
|
|
|
self.nameLabel = [UILabel new];
|
|
self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
|
|
self.subtitleLabel = [UILabel new];
|
|
|
|
self.profileNameLabel = [UILabel new];
|
|
|
|
self.accessoryLabel = [[UILabel alloc] init];
|
|
self.accessoryLabel.textAlignment = NSTextAlignmentRight;
|
|
|
|
self.accessoryViewContainer = [UIView containerView];
|
|
|
|
self.nameContainerView = [[UIStackView alloc] initWithArrangedSubviews:@[
|
|
self.nameLabel,
|
|
self.profileNameLabel,
|
|
self.subtitleLabel,
|
|
]];
|
|
self.nameContainerView.axis = UILayoutConstraintAxisVertical;
|
|
|
|
[self.avatarView setContentHuggingHorizontalHigh];
|
|
[self.nameContainerView setContentHuggingHorizontalLow];
|
|
[self.accessoryViewContainer setContentHuggingHorizontalHigh];
|
|
|
|
self.axis = UILayoutConstraintAxisHorizontal;
|
|
self.spacing = kContactCellAvatarTextMargin;
|
|
self.alignment = UIStackViewAlignmentCenter;
|
|
[self addArrangedSubview:self.avatarView];
|
|
[self addArrangedSubview:self.nameContainerView];
|
|
[self addArrangedSubview:self.accessoryViewContainer];
|
|
|
|
[self configureFontsAndColors];
|
|
}
|
|
|
|
- (void)configureFontsAndColors
|
|
{
|
|
self.nameLabel.font = [UIFont ows_dynamicTypeBodyFont];
|
|
self.profileNameLabel.font = [UIFont ows_regularFontWithSize:11.f];
|
|
self.subtitleLabel.font = [UIFont ows_regularFontWithSize:11.f];
|
|
self.accessoryLabel.font = [UIFont ows_mediumFontWithSize:13.f];
|
|
|
|
self.nameLabel.textColor = [Theme primaryColor];
|
|
self.profileNameLabel.textColor = [Theme secondaryColor];
|
|
self.subtitleLabel.textColor = [Theme secondaryColor];
|
|
self.accessoryLabel.textColor = Theme.middleGrayColor;
|
|
}
|
|
|
|
- (void)configureWithRecipientAddress:(SignalServiceAddress *)address
|
|
{
|
|
OWSAssertDebug(address.isValid);
|
|
|
|
// Update fonts to reflect changes to dynamic type.
|
|
[self configureFontsAndColors];
|
|
|
|
self.address = address;
|
|
|
|
// TODO remove sneaky transaction.
|
|
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
|
|
self.thread = [TSContactThread getThreadWithContactAddress:address transaction:transaction];
|
|
}];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(otherUsersProfileDidChange:)
|
|
name:kNSNotificationName_OtherUsersProfileDidChange
|
|
object:nil];
|
|
[self updateProfileName];
|
|
[self updateAvatar];
|
|
|
|
if (self.accessoryMessage) {
|
|
self.accessoryLabel.text = self.accessoryMessage;
|
|
[self setAccessoryView:self.accessoryLabel];
|
|
}
|
|
|
|
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
|
[self layoutSubviews];
|
|
}
|
|
|
|
- (void)configureWithThread:(TSThread *)thread transaction:(SDSAnyReadTransaction *)transaction
|
|
{
|
|
OWSAssertDebug(thread);
|
|
self.thread = thread;
|
|
|
|
// Update fonts to reflect changes to dynamic type.
|
|
[self configureFontsAndColors];
|
|
|
|
NSString *threadName = [self.contactsManager displayNameForThread:thread transaction:transaction];
|
|
TSContactThread *_Nullable contactThread;
|
|
if ([self.thread isKindOfClass:[TSContactThread class]]) {
|
|
contactThread = (TSContactThread *)self.thread;
|
|
}
|
|
|
|
BOOL isNoteToSelf = contactThread && contactThread.contactAddress.isLocalAddress;
|
|
if (isNoteToSelf) {
|
|
threadName = MessageStrings.noteToSelf;
|
|
}
|
|
|
|
NSAttributedString *attributedText =
|
|
[[NSAttributedString alloc] initWithString:threadName
|
|
attributes:@{
|
|
NSForegroundColorAttributeName : [Theme primaryColor],
|
|
}];
|
|
self.nameLabel.attributedText = attributedText;
|
|
|
|
if (contactThread != nil) {
|
|
self.address = contactThread.contactAddress;
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(otherUsersProfileDidChange:)
|
|
name:kNSNotificationName_OtherUsersProfileDidChange
|
|
object:nil];
|
|
[self updateProfileName];
|
|
}
|
|
self.avatarView.image = [OWSAvatarBuilder buildImageForThread:thread diameter:kStandardAvatarSize];
|
|
|
|
if (self.accessoryMessage) {
|
|
self.accessoryLabel.text = self.accessoryMessage;
|
|
[self setAccessoryView:self.accessoryLabel];
|
|
}
|
|
|
|
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
|
[self layoutSubviews];
|
|
}
|
|
|
|
- (void)updateAvatar
|
|
{
|
|
SignalServiceAddress *address = self.address;
|
|
if (!address.isValid) {
|
|
OWSFailDebug(@"address should not be invalid");
|
|
self.avatarView.image = nil;
|
|
return;
|
|
}
|
|
|
|
ConversationColorName colorName = ^{
|
|
if (self.thread) {
|
|
return self.thread.conversationColorName;
|
|
} else {
|
|
return [TSThread stableColorNameForNewConversationWithString:address.stringForDisplay];
|
|
}
|
|
}();
|
|
|
|
OWSContactAvatarBuilder *avatarBuilder = [[OWSContactAvatarBuilder alloc] initWithAddress:address
|
|
colorName:colorName
|
|
diameter:kStandardAvatarSize];
|
|
|
|
self.avatarView.image = [avatarBuilder build];
|
|
}
|
|
|
|
- (void)updateProfileName
|
|
{
|
|
BOOL isNoteToSelf = IsNoteToSelfEnabled() && self.address.isLocalAddress;
|
|
if (isNoteToSelf) {
|
|
self.nameLabel.text = MessageStrings.noteToSelf;
|
|
} else {
|
|
self.nameLabel.text = [self.contactsManager displayNameForAddress:self.address];
|
|
}
|
|
|
|
if (!SSKFeatureFlags.profileDisplayChanges
|
|
&& ![self.contactsManager hasNameInSystemContactsForAddress:self.address]) {
|
|
self.profileNameLabel.text = [self.contactsManager formattedProfileNameForAddress:self.address];
|
|
[self.profileNameLabel setNeedsLayout];
|
|
}
|
|
|
|
[self.nameLabel setNeedsLayout];
|
|
}
|
|
|
|
- (void)prepareForReuse
|
|
{
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
self.thread = nil;
|
|
self.accessoryMessage = nil;
|
|
self.nameLabel.text = nil;
|
|
self.subtitleLabel.text = nil;
|
|
self.profileNameLabel.text = nil;
|
|
self.accessoryLabel.text = nil;
|
|
for (UIView *subview in self.accessoryViewContainer.subviews) {
|
|
[subview removeFromSuperview];
|
|
}
|
|
}
|
|
|
|
- (void)otherUsersProfileDidChange:(NSNotification *)notification
|
|
{
|
|
OWSAssertIsOnMainThread();
|
|
|
|
SignalServiceAddress *address = notification.userInfo[kNSNotificationKey_ProfileAddress];
|
|
OWSAssertDebug(address.isValid);
|
|
|
|
if (address.isValid && [self.address isEqualToAddress:address]) {
|
|
[self updateProfileName];
|
|
[self updateAvatar];
|
|
}
|
|
}
|
|
|
|
- (NSAttributedString *)verifiedSubtitle
|
|
{
|
|
NSMutableAttributedString *text = [NSMutableAttributedString new];
|
|
// "checkmark"
|
|
[text appendAttributedString:[[NSAttributedString alloc]
|
|
initWithString:@"\uf00c "
|
|
attributes:@{
|
|
NSFontAttributeName :
|
|
[UIFont ows_fontAwesomeFont:self.subtitleLabel.font.pointSize],
|
|
}]];
|
|
[text appendAttributedString:[[NSAttributedString alloc]
|
|
initWithString:NSLocalizedString(@"PRIVACY_IDENTITY_IS_VERIFIED_BADGE",
|
|
@"Badge indicating that the user is verified.")]];
|
|
return [text copy];
|
|
}
|
|
|
|
- (void)setAttributedSubtitle:(nullable NSAttributedString *)attributedSubtitle
|
|
{
|
|
self.subtitleLabel.attributedText = attributedSubtitle;
|
|
}
|
|
|
|
- (BOOL)hasAccessoryText
|
|
{
|
|
return self.accessoryMessage.length > 0;
|
|
}
|
|
|
|
- (void)setAccessoryView:(UIView *)accessoryView
|
|
{
|
|
OWSAssertDebug(accessoryView);
|
|
OWSAssertDebug(self.accessoryViewContainer);
|
|
OWSAssertDebug(self.accessoryViewContainer.subviews.count < 1);
|
|
|
|
[self.accessoryViewContainer addSubview:accessoryView];
|
|
|
|
// Trailing-align the accessory view.
|
|
[accessoryView autoPinEdgeToSuperviewMargin:ALEdgeTop];
|
|
[accessoryView autoPinEdgeToSuperviewMargin:ALEdgeBottom];
|
|
[accessoryView autoPinEdgeToSuperviewMargin:ALEdgeTrailing];
|
|
[accessoryView autoPinEdgeToSuperviewMargin:ALEdgeLeading relation:NSLayoutRelationGreaterThanOrEqual];
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|