From f49e12256758bc3a0676bff7888beb708c999140 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 23 Aug 2017 17:09:43 -0400 Subject: [PATCH] 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