From e54e1d11c00ef1f292925062d4d36d46ac0a6715 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 23 Aug 2017 12:03:36 -0400 Subject: [PATCH 1/5] show profile name snippet in inbox and conversation settings // FREEBIE --- .../ConversationView/MessagesViewController.m | 18 +++++--- .../src/ViewControllers/InboxTableViewCell.m | 41 ++++++++----------- Signal/src/contact/OWSContactsManager.h | 4 +- Signal/src/contact/OWSContactsManager.m | 32 +++++++++++++++ 4 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m index be1b815f2..967cd44be 100644 --- a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m +++ b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m @@ -1118,17 +1118,25 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)setNavigationTitle { - NSString *navTitle = self.thread.name; - if (self.isGroupConversation && [navTitle length] == 0) { - navTitle = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @""); + NSAttributedString *name; + if (self.thread.isGroupThread) { + if (self.thread.name.length == 0) { + name = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"")]; + } else { + name = [[NSAttributedString alloc] initWithString:self.thread.name]; + } + } else { + name = [self.contactsManager attributedStringForConversationTitleWithPhoneIdentifier:self.thread.contactIdentifier + primaryFont:self.navigationBarTitleLabel.font + secondaryFont:[UIFont ows_footnoteFont]]; } self.title = nil; - if ([navTitle isEqualToString:self.navigationBarTitleLabel.text]) { + if ([name isEqual:self.navigationBarTitleLabel.attributedText]) { return; } - self.navigationBarTitleLabel.text = navTitle; + self.navigationBarTitleLabel.attributedText = name; // Changing the title requires relayout of the nav bar contents. OWSDisappearingMessagesConfiguration *configuration = diff --git a/Signal/src/ViewControllers/InboxTableViewCell.m b/Signal/src/ViewControllers/InboxTableViewCell.m index 5f194feca..647502a26 100644 --- a/Signal/src/ViewControllers/InboxTableViewCell.m +++ b/Signal/src/ViewControllers/InboxTableViewCell.m @@ -76,6 +76,7 @@ const NSUInteger kAvatarViewDiameter = 52; self.nameLabel = [UILabel new]; self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail; + self.nameLabel.font = [UIFont ows_boldFontWithSize:14.0f]; [self.contentView addSubview:self.nameLabel]; [self.nameLabel autoPinLeadingToTrailingOfView:self.avatarView margin:13.f]; [self.nameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.avatarView]; @@ -162,11 +163,7 @@ const NSUInteger kAvatarViewDiameter = 52; NSString *contactIdentifier = thread.contactIdentifier; isBlocked = [blockedPhoneNumberSet containsObject:contactIdentifier]; } - - NSString *name = thread.name; - if (name.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) { - name = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @""); - } + self.threadId = thread.uniqueId; NSMutableAttributedString *snippetText = [NSMutableAttributedString new]; if (isBlocked) { @@ -206,18 +203,28 @@ const NSUInteger kAvatarViewDiameter = 52; NSAttributedString *attributedDate = [self dateAttributedString:thread.lastMessageDate]; NSUInteger unreadCount = [[TSMessagesManager sharedManager] unreadMessagesInThread:thread]; - self.nameLabel.text = name; + NSAttributedString *name; + if (thread.isGroupThread) { + if (thread.name.length == 0) { + name = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"")]; + } else { + name = [[NSAttributedString alloc] initWithString:thread.name]; + } + } else { + name = [contactsManager attributedStringForConversationTitleWithPhoneIdentifier:thread.contactIdentifier + primaryFont:self.nameLabel.font + secondaryFont:[UIFont ows_footnoteFont]]; + } + + self.nameLabel.attributedText = name; self.snippetLabel.attributedText = snippetText; self.timeLabel.attributedText = attributedDate; self.avatarView.image = nil; self.separatorInset = UIEdgeInsetsMake(0, self.avatarSize * 1.5f, 0, 0); - if (thread.hasUnreadMessages) { - [self updateCellForUnreadMessage]; - } else { - [self updateCellForReadMessage]; - } + _timeLabel.textColor = thread.hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor]; + if (unreadCount > 0) { self.unreadBadge.hidden = NO; self.unreadLabel.hidden = NO; @@ -231,18 +238,6 @@ const NSUInteger kAvatarViewDiameter = 52; [OWSAvatarBuilder buildImageForThread:thread diameter:kAvatarViewDiameter contactsManager:contactsManager]; } -- (void)updateCellForUnreadMessage { - _nameLabel.font = [UIFont ows_boldFontWithSize:14.0f]; - _nameLabel.textColor = [UIColor ows_blackColor]; - _timeLabel.textColor = [UIColor ows_materialBlueColor]; -} - -- (void)updateCellForReadMessage { - _nameLabel.font = [UIFont ows_boldFontWithSize:14.0f]; - _nameLabel.textColor = [UIColor ows_blackColor]; - _timeLabel.textColor = [UIColor ows_darkGrayColor]; -} - #pragma mark - Date formatting - (NSAttributedString *)dateAttributedString:(NSDate *)date { diff --git a/Signal/src/contact/OWSContactsManager.h b/Signal/src/contact/OWSContactsManager.h index 4d5e4848e..e3d4fa464 100644 --- a/Signal/src/contact/OWSContactsManager.h +++ b/Signal/src/contact/OWSContactsManager.h @@ -59,7 +59,9 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification; - (NSAttributedString *)formattedDisplayNameForSignalAccount:(SignalAccount *)signalAccount font:(UIFont *_Nonnull)font; - (NSAttributedString *)formattedFullNameForRecipientId:(NSString *)recipientId font:(UIFont *)font; - (NSAttributedString *)attributedStringForMessageFooterWithPhoneIdentifier:(NSString *)recipientId; - +- (NSAttributedString *)attributedStringForConversationTitleWithPhoneIdentifier:(NSString *)recipientId + primaryFont:(UIFont *)primaryFont + secondaryFont:(UIFont *)secondaryFont; @end NS_ASSUME_NONNULL_END diff --git a/Signal/src/contact/OWSContactsManager.m b/Signal/src/contact/OWSContactsManager.m index 49ebc5ae0..b9099fd59 100644 --- a/Signal/src/contact/OWSContactsManager.m +++ b/Signal/src/contact/OWSContactsManager.m @@ -550,6 +550,38 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account return [[NSAttributedString alloc] initWithString:recipientId]; } +- (NSAttributedString *)attributedStringForConversationTitleWithPhoneIdentifier:(NSString *)recipientId + primaryFont:(UIFont *)primaryFont + secondaryFont:(UIFont *)secondaryFont +{ + // Prefer a saved name from system contacts, if available + NSString *_Nullable savedContactName = [self cachedDisplayNameForRecipientId:recipientId]; + if (savedContactName.length > 0) { + return [[NSAttributedString alloc] initWithString:savedContactName]; + } + + NSString *_Nullable profileName = [self.profileManager profileNameForRecipientId:recipientId]; + if (profileName.length > 0) { + NSString *numberAndProfileNameFormat = NSLocalizedString(@"PROFILE_NAME_AND_PHONE_NUMBER_LABEL_FORMAT", + @"Label text combining the phone number and profile name separated by a simple demarcation character. " + @"Phone number should be most prominent. '%1$@' is replaced with {{phone number}} and '%2$@' is replaced " + @"with {{profile name}}"); + + NSString *numberAndProfileName = + [NSString stringWithFormat:numberAndProfileNameFormat, recipientId, profileName]; + + NSRange recipientIdRange = [numberAndProfileName rangeOfString:recipientId]; + NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:numberAndProfileName + attributes:@{ NSFontAttributeName: secondaryFont }]; + [attributedString addAttribute:NSFontAttributeName value:primaryFont range:recipientIdRange]; + + return [attributedString copy]; + } + + // else fall back to recipient id + return [[NSAttributedString alloc] initWithString:recipientId]; +} + - (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId { OWSAssert(recipientId.length > 0); From fd993546737e32713460b456d2b42839c7ab6809 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 23 Aug 2017 15:41:56 -0400 Subject: [PATCH 2/5] profile name vs. verified in ContactTableViewCell // FREEBIE --- .../ConversationView/MessagesViewController.m | 7 +-- .../src/ViewControllers/InboxTableViewCell.m | 8 +-- .../OWSConversationSettingsViewController.m | 5 ++ .../ShowGroupMembersViewController.m | 4 +- Signal/src/contact/OWSContactsManager.m | 21 ++++---- Signal/src/views/ContactTableViewCell.h | 3 +- Signal/src/views/ContactTableViewCell.m | 51 +++++++++---------- 7 files changed, 54 insertions(+), 45 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m index 967cd44be..66f2279f1 100644 --- a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m +++ b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m @@ -1126,9 +1126,10 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { name = [[NSAttributedString alloc] initWithString:self.thread.name]; } } else { - name = [self.contactsManager attributedStringForConversationTitleWithPhoneIdentifier:self.thread.contactIdentifier - primaryFont:self.navigationBarTitleLabel.font - secondaryFont:[UIFont ows_footnoteFont]]; + name = [self.contactsManager + attributedStringForConversationTitleWithPhoneIdentifier:self.thread.contactIdentifier + primaryFont:self.navigationBarTitleLabel.font + secondaryFont:[UIFont ows_footnoteFont]]; } self.title = nil; diff --git a/Signal/src/ViewControllers/InboxTableViewCell.m b/Signal/src/ViewControllers/InboxTableViewCell.m index 647502a26..4e6bdb917 100644 --- a/Signal/src/ViewControllers/InboxTableViewCell.m +++ b/Signal/src/ViewControllers/InboxTableViewCell.m @@ -163,7 +163,7 @@ const NSUInteger kAvatarViewDiameter = 52; NSString *contactIdentifier = thread.contactIdentifier; isBlocked = [blockedPhoneNumberSet containsObject:contactIdentifier]; } - + self.threadId = thread.uniqueId; NSMutableAttributedString *snippetText = [NSMutableAttributedString new]; if (isBlocked) { @@ -215,7 +215,7 @@ const NSUInteger kAvatarViewDiameter = 52; primaryFont:self.nameLabel.font secondaryFont:[UIFont ows_footnoteFont]]; } - + self.nameLabel.attributedText = name; self.snippetLabel.attributedText = snippetText; self.timeLabel.attributedText = attributedDate; @@ -223,8 +223,8 @@ const NSUInteger kAvatarViewDiameter = 52; self.separatorInset = UIEdgeInsetsMake(0, self.avatarSize * 1.5f, 0, 0); - _timeLabel.textColor = thread.hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor]; - + _timeLabel.textColor = thread.hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor]; + if (unreadCount > 0) { self.unreadBadge.hidden = NO; self.unreadLabel.hidden = NO; diff --git a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m index 632f4a540..264da9dc6 100644 --- a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m +++ b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m @@ -670,6 +670,11 @@ NS_ASSUME_NONNULL_BEGIN initWithString:[PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:recipientId]]; addSubtitle(subtitle); + } else { + NSString *_Nullable profileName = [self.contactsManager formattedProfileNameForRecipientId:recipientId]; + if (profileName) { + addSubtitle([[NSAttributedString alloc] initWithString:profileName]); + } } BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] diff --git a/Signal/src/ViewControllers/ShowGroupMembersViewController.m b/Signal/src/ViewControllers/ShowGroupMembersViewController.m index 445cfd1d3..98548b061 100644 --- a/Signal/src/ViewControllers/ShowGroupMembersViewController.m +++ b/Signal/src/ViewControllers/ShowGroupMembersViewController.m @@ -193,7 +193,9 @@ NS_ASSUME_NONNULL_BEGIN } if (isVerified) { - [cell addVerifiedSubtitle]; + cell.subtitle.attributedText = cell.verifiedSubtitle; + } else { + cell.subtitle.attributedText = nil; } return cell; diff --git a/Signal/src/contact/OWSContactsManager.m b/Signal/src/contact/OWSContactsManager.m index b9099fd59..735f7348a 100644 --- a/Signal/src/contact/OWSContactsManager.m +++ b/Signal/src/contact/OWSContactsManager.m @@ -559,25 +559,26 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account if (savedContactName.length > 0) { return [[NSAttributedString alloc] initWithString:savedContactName]; } - + NSString *_Nullable profileName = [self.profileManager profileNameForRecipientId:recipientId]; if (profileName.length > 0) { NSString *numberAndProfileNameFormat = NSLocalizedString(@"PROFILE_NAME_AND_PHONE_NUMBER_LABEL_FORMAT", - @"Label text combining the phone number and profile name separated by a simple demarcation character. " - @"Phone number should be most prominent. '%1$@' is replaced with {{phone number}} and '%2$@' is replaced " - @"with {{profile name}}"); - + @"Label text combining the phone number and profile name separated by a simple demarcation character. " + @"Phone number should be most prominent. '%1$@' is replaced with {{phone number}} and '%2$@' is replaced " + @"with {{profile name}}"); + NSString *numberAndProfileName = [NSString stringWithFormat:numberAndProfileNameFormat, recipientId, profileName]; - + NSRange recipientIdRange = [numberAndProfileName rangeOfString:recipientId]; - NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:numberAndProfileName - attributes:@{ NSFontAttributeName: secondaryFont }]; + NSMutableAttributedString *attributedString = + [[NSMutableAttributedString alloc] initWithString:numberAndProfileName + attributes:@{ NSFontAttributeName : secondaryFont }]; [attributedString addAttribute:NSFontAttributeName value:primaryFont range:recipientIdRange]; - + return [attributedString copy]; } - + // else fall back to recipient id return [[NSAttributedString alloc] initWithString:recipientId]; } diff --git a/Signal/src/views/ContactTableViewCell.h b/Signal/src/views/ContactTableViewCell.h index 10e273b5d..4f3da3c30 100644 --- a/Signal/src/views/ContactTableViewCell.h +++ b/Signal/src/views/ContactTableViewCell.h @@ -22,6 +22,7 @@ extern NSString *const kContactsTable_CellReuseIdentifier; @interface ContactTableViewCell : UITableViewCell @property (nonatomic, nullable) NSString *accessoryMessage; +@property (nonatomic, readonly) UILabel *subtitle; + (nullable NSString *)reuseIdentifier; @@ -33,7 +34,7 @@ extern NSString *const kContactsTable_CellReuseIdentifier; - (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager; -- (void)addVerifiedSubtitle; +- (NSAttributedString *)verifiedSubtitle; @end diff --git a/Signal/src/views/ContactTableViewCell.m b/Signal/src/views/ContactTableViewCell.m index 49ac81372..066cb5001 100644 --- a/Signal/src/views/ContactTableViewCell.m +++ b/Signal/src/views/ContactTableViewCell.m @@ -21,10 +21,11 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; @interface ContactTableViewCell () -@property (nonatomic) IBOutlet UILabel *nameLabel; -@property (nonatomic) IBOutlet UILabel *profileNameLabel; -@property (nonatomic) IBOutlet UIImageView *avatarView; -@property (nonatomic, nullable) UILabel *subtitle; +@property (nonatomic) UILabel *nameLabel; +@property (nonatomic) UILabel *profileNameLabel; +@property (nonatomic) UIImageView *avatarView; +@property (nonatomic) UILabel *subtitle; +@property (nonatomic) UIView *nameContainerView; @end @@ -61,19 +62,24 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; _avatarView = [AvatarImageView new]; [self.contentView addSubview:_avatarView]; - UIView *nameContainerView = [UIView containerView]; - [self.contentView addSubview:nameContainerView]; + _nameContainerView = [UIView containerView]; + [self.contentView addSubview:_nameContainerView]; _nameLabel = [UILabel new]; _nameLabel.lineBreakMode = NSLineBreakByTruncatingTail; _nameLabel.font = [UIFont ows_dynamicTypeBodyFont]; - [nameContainerView addSubview:_nameLabel]; + [_nameContainerView addSubview:_nameLabel]; _profileNameLabel = [UILabel new]; _profileNameLabel.lineBreakMode = NSLineBreakByTruncatingTail; _profileNameLabel.font = [UIFont ows_footnoteFont]; _profileNameLabel.textColor = [UIColor grayColor]; - [nameContainerView addSubview:_profileNameLabel]; + [_nameContainerView addSubview:_profileNameLabel]; + + _subtitle = [UILabel new]; + _subtitle.font = [UIFont ows_footnoteFont]; + _subtitle.textColor = [UIColor ows_darkGrayColor]; + [_nameContainerView addSubview:self.subtitle]; [_avatarView autoVCenterInSuperview]; [_avatarView autoPinLeadingToSuperView]; @@ -86,12 +92,15 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; // 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 autoPinEdgeToSuperviewEdge:ALEdgeBottom]; [_profileNameLabel autoPinWidthToSuperview]; - [nameContainerView autoVCenterInSuperview]; - [nameContainerView autoPinLeadingToTrailingOfView:_avatarView margin:12.f]; - [nameContainerView autoPinTrailingToSuperView]; + [_subtitle autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_profileNameLabel]; + [_subtitle autoPinWidthToSuperview]; + [_subtitle autoPinEdgeToSuperviewEdge:ALEdgeBottom]; + + [_nameContainerView autoVCenterInSuperview]; + [_nameContainerView autoPinLeadingToTrailingOfView:_avatarView margin:12.f]; + [_nameContainerView autoPinTrailingToSuperView]; // Force layout, since imageView isn't being initally rendered on App Store optimized build. [self layoutSubviews]; @@ -160,30 +169,20 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; [self layoutSubviews]; } -- (void)addVerifiedSubtitle +- (NSAttributedString *)verifiedSubtitle { - [self.subtitle removeFromSuperview]; - - const CGFloat kSubtitlePointSize = 10.f; NSMutableAttributedString *text = [NSMutableAttributedString new]; // "checkmark" [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\uf00c " attributes:@{ - NSFontAttributeName : [UIFont ows_fontAwesomeFont:kSubtitlePointSize], + NSFontAttributeName : + [UIFont ows_fontAwesomeFont:self.subtitle.font.pointSize], }]]; [text appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"PRIVACY_IDENTITY_IS_VERIFIED_BADGE", @"Badge indicating that the user is verified.")]]; - self.subtitle = [UILabel new]; - self.subtitle.font = [UIFont ows_regularFontWithSize:kSubtitlePointSize]; - self.subtitle.textColor = [UIColor ows_darkGrayColor]; - self.subtitle.attributedText = text; - [self.subtitle sizeToFit]; - [self.contentView addSubview:self.subtitle]; - [self.subtitle autoPinLeadingToView:self.nameLabel]; - [self.subtitle autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nameLabel]; - [self.subtitle autoPinEdgeToSuperviewEdge:ALEdgeBottom]; + return [text copy]; } - (void)prepareForReuse From 96f0ab215c9254842781a681fc4ac039744e35ea Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 23 Aug 2017 16:41:31 -0400 Subject: [PATCH 3/5] wip --- .../ConversationView/MessagesViewController.m | 5 + .../src/ViewControllers/InboxTableViewCell.m | 99 +++++++++++++++---- 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m index 66f2279f1..ffe90fd45 100644 --- a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m +++ b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m @@ -360,6 +360,11 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId]; OWSAssert(recipientId.length > 0); if (recipientId.length > 0 && [self.thread.recipientIdentifiers containsObject:recipientId]) { + if ([self.thread isKindOfClass:[TSContactThread class]]) { + // update title with profile name + [self setNavigationTitle]; + } + // Reload all cells. [self resetContentAndLayout]; } diff --git a/Signal/src/ViewControllers/InboxTableViewCell.m b/Signal/src/ViewControllers/InboxTableViewCell.m index 4e6bdb917..eb6698f44 100644 --- a/Signal/src/ViewControllers/InboxTableViewCell.m +++ b/Signal/src/ViewControllers/InboxTableViewCell.m @@ -31,7 +31,8 @@ const NSUInteger kAvatarViewDiameter = 52; @property (nonatomic) UIView *unreadBadge; @property (nonatomic) UILabel *unreadLabel; -@property (nonatomic) NSString *threadId; +@property (nonatomic) TSThread *thread; +@property (nonatomic) OWSContactsManager *contactsManager; @end @@ -158,13 +159,15 @@ const NSUInteger kAvatarViewDiameter = 52; OWSAssert(contactsManager); OWSAssert(blockedPhoneNumberSet); + self.thread = thread; + self.contactsManager = contactsManager; + BOOL isBlocked = NO; if (!thread.isGroupThread) { NSString *contactIdentifier = thread.contactIdentifier; isBlocked = [blockedPhoneNumberSet containsObject:contactIdentifier]; } - - self.threadId = thread.uniqueId; + NSMutableAttributedString *snippetText = [NSMutableAttributedString new]; if (isBlocked) { // If thread is blocked, don't show a snippet or mute status. @@ -203,20 +206,13 @@ const NSUInteger kAvatarViewDiameter = 52; NSAttributedString *attributedDate = [self dateAttributedString:thread.lastMessageDate]; NSUInteger unreadCount = [[TSMessagesManager sharedManager] unreadMessagesInThread:thread]; - NSAttributedString *name; - if (thread.isGroupThread) { - if (thread.name.length == 0) { - name = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"")]; - } else { - name = [[NSAttributedString alloc] initWithString:thread.name]; - } - } else { - name = [contactsManager attributedStringForConversationTitleWithPhoneIdentifier:thread.contactIdentifier - primaryFont:self.nameLabel.font - secondaryFont:[UIFont ows_footnoteFont]]; - } - - self.nameLabel.attributedText = name; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(otherUsersProfileDidChange:) + name:kNSNotificationName_OtherUsersProfileDidChange + object:nil]; + [self updateNameLabel]; + self.snippetLabel.attributedText = snippetText; self.timeLabel.attributedText = attributedDate; self.avatarView.image = nil; @@ -266,9 +262,78 @@ const NSUInteger kAvatarViewDiameter = 52; - (void)prepareForReuse { + [[NSNotificationCenter defaultCenter] removeObserver:self]; [super prepareForReuse]; } +#pragma mark - Name + +- (void)otherUsersProfileDidChange:(NSNotification *)notification +{ + OWSAssert([NSThread isMainThread]); + + NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId]; + if (recipientId.length == 0) { + return; + } + + if (![self.thread isKindOfClass:[TSContactThread class]]) { + return; + } + + if (![self.thread.contactIdentifier isEqualToString:recipientId]) { + return; + } + + [self updateNameLabel]; +} + +-(void)updateNameLabel +{ + AssertIsOnMainThread(); + + TSThread *thread = self.thread; + if (thread == nil) { + OWSFail(@"%@ thread should not be nil", self.logTag); + self.nameLabel.attributedText = nil; + return; + } + + OWSContactsManager *contactsManager = self.contactsManager; + if (contactsManager == nil) { + OWSFail(@"%@ contacts manager should not be nil", self.logTag); + self.nameLabel.attributedText = nil; + return; + } + + NSAttributedString *name; + if (thread.isGroupThread) { + if (thread.name.length == 0) { + name = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"")]; + } else { + name = [[NSAttributedString alloc] initWithString:thread.name]; + } + } else { + name = [contactsManager attributedStringForConversationTitleWithPhoneIdentifier:thread.contactIdentifier + primaryFont:self.nameLabel.font + secondaryFont:[UIFont ows_footnoteFont]]; + } + + self.nameLabel.attributedText = name; +} + +#pragma mark - Logging + ++ (NSString *)logTag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)logTag +{ + return self.class.logTag; +} + @end NS_ASSUME_NONNULL_END From f49e12256758bc3a0676bff7888beb708c999140 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 23 Aug 2017 17:09:43 -0400 Subject: [PATCH 4/5] listen for profile names change notifications // FREEBIE --- .../OWSConversationSettingsViewController.m | 17 ++++ Signal/src/views/ContactTableViewCell.m | 83 +++++++++++++++++-- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m index 264da9dc6..451ed3121 100644 --- a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m +++ b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m @@ -113,6 +113,10 @@ NS_ASSUME_NONNULL_BEGIN selector:@selector(identityStateDidChange:) name:kNSNotificationName_IdentityStateDidChange object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(otherUsersProfileDidChange:) + name:kNSNotificationName_OtherUsersProfileDidChange + object:nil]; } - (NSString *)threadName @@ -1129,6 +1133,19 @@ NS_ASSUME_NONNULL_BEGIN [self updateTableContents]; } +- (void)otherUsersProfileDidChange:(NSNotification *)notification +{ + OWSAssert([NSThread isMainThread]); + + NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId]; + OWSAssert(recipientId.length > 0); + + if (recipientId.length > 0 && [self.thread isKindOfClass:[TSContactThread class]] && + [self.thread.contactIdentifier isEqualToString:recipientId]) { + [self updateTableContents]; + } +} + #pragma mark - Logging + (NSString *)tag diff --git a/Signal/src/views/ContactTableViewCell.m b/Signal/src/views/ContactTableViewCell.m index 066cb5001..37f4b616f 100644 --- a/Signal/src/views/ContactTableViewCell.m +++ b/Signal/src/views/ContactTableViewCell.m @@ -27,6 +27,9 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; @property (nonatomic) UILabel *subtitle; @property (nonatomic) UIView *nameContainerView; +@property (nonatomic) OWSContactsManager *contactsManager; +@property (nonatomic) NSString *recipientId; + @end @implementation ContactTableViewCell @@ -115,16 +118,17 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; - (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager { + self.recipientId = recipientId; + self.contactsManager = contactsManager; + self.nameLabel.attributedText = [contactsManager formattedFullNameForRecipientId:recipientId font:self.nameLabel.font]; - 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]; - } + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(otherUsersProfileDidChange:) + name:kNSNotificationName_OtherUsersProfileDidChange + object:nil]; + [self updateProfileName]; if (self.accessoryMessage) { UILabel *blockedLabel = [[UILabel alloc] init]; @@ -148,6 +152,7 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; - (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager { OWSAssert(thread); + self.contactsManager = contactsManager; NSString *threadName = thread.name; if (threadName.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) { @@ -161,6 +166,13 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; }]; self.nameLabel.attributedText = attributedText; + if ([thread isKindOfClass:[TSContactThread class]]) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(otherUsersProfileDidChange:) + name:kNSNotificationName_OtherUsersProfileDidChange + object:nil]; + [self updateProfileName]; + } self.avatarView.image = [OWSAvatarBuilder buildImageForThread:thread diameter:kContactTableViewCellAvatarSize contactsManager:contactsManager]; @@ -185,13 +197,66 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; return [text copy]; } + +- (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]; + } +} + - (void)prepareForReuse { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + self.accessoryMessage = nil; self.accessoryView = nil; self.accessoryType = UITableViewCellAccessoryNone; - [self.subtitle removeFromSuperview]; - self.subtitle = nil; + self.nameLabel.text = nil; + self.subtitle.text = nil; + self.profileNameLabel.text = nil; +} + +- (void)otherUsersProfileDidChange:(NSNotification *)notification +{ + OWSAssert([NSThread isMainThread]); + + NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId]; + OWSAssert(recipientId.length > 0); + + if (recipientId.length > 0 && [self.recipientId isEqualToString:recipientId]) { + [self updateProfileName]; + } +} + +#pragma mark - Logging + ++ (NSString *)logTag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)logTag +{ + return self.class.logTag; } @end From 041c5a4a112e9bb771df404dbfd340ae68f55880 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 24 Aug 2017 14:43:52 -0400 Subject: [PATCH 5/5] CR: setNeedsLayout // FREEBIE --- Signal/src/views/ContactTableViewCell.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Signal/src/views/ContactTableViewCell.m b/Signal/src/views/ContactTableViewCell.m index 37f4b616f..a1caf7d88 100644 --- a/Signal/src/views/ContactTableViewCell.m +++ b/Signal/src/views/ContactTableViewCell.m @@ -221,6 +221,8 @@ const NSUInteger kContactTableViewCellAvatarSize = 40; // Use profile name, if any is available self.profileNameLabel.text = [contactsManager formattedProfileNameForRecipientId:recipientId]; } + + [self.profileNameLabel setNeedsLayout]; } - (void)prepareForReuse