listen for profile names change notifications

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent 96f0ab215c
commit f49e122567

@ -113,6 +113,10 @@ NS_ASSUME_NONNULL_BEGIN
selector:@selector(identityStateDidChange:) selector:@selector(identityStateDidChange:)
name:kNSNotificationName_IdentityStateDidChange name:kNSNotificationName_IdentityStateDidChange
object:nil]; object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(otherUsersProfileDidChange:)
name:kNSNotificationName_OtherUsersProfileDidChange
object:nil];
} }
- (NSString *)threadName - (NSString *)threadName
@ -1129,6 +1133,19 @@ NS_ASSUME_NONNULL_BEGIN
[self updateTableContents]; [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 #pragma mark - Logging
+ (NSString *)tag + (NSString *)tag

@ -27,6 +27,9 @@ const NSUInteger kContactTableViewCellAvatarSize = 40;
@property (nonatomic) UILabel *subtitle; @property (nonatomic) UILabel *subtitle;
@property (nonatomic) UIView *nameContainerView; @property (nonatomic) UIView *nameContainerView;
@property (nonatomic) OWSContactsManager *contactsManager;
@property (nonatomic) NSString *recipientId;
@end @end
@implementation ContactTableViewCell @implementation ContactTableViewCell
@ -115,16 +118,17 @@ const NSUInteger kContactTableViewCellAvatarSize = 40;
- (void)configureWithRecipientId:(NSString *)recipientId - (void)configureWithRecipientId:(NSString *)recipientId
contactsManager:(OWSContactsManager *)contactsManager contactsManager:(OWSContactsManager *)contactsManager
{ {
self.recipientId = recipientId;
self.contactsManager = contactsManager;
self.nameLabel.attributedText = self.nameLabel.attributedText =
[contactsManager formattedFullNameForRecipientId:recipientId font:self.nameLabel.font]; [contactsManager formattedFullNameForRecipientId:recipientId font:self.nameLabel.font];
if ([contactsManager hasNameInSystemContactsForRecipientId:recipientId]) { [[NSNotificationCenter defaultCenter] addObserver:self
// Don't display profile name when we have a veritas name in system Contacts selector:@selector(otherUsersProfileDidChange:)
self.profileNameLabel.text = nil; name:kNSNotificationName_OtherUsersProfileDidChange
} else { object:nil];
// Use profile name, if any is available [self updateProfileName];
self.profileNameLabel.text = [contactsManager formattedProfileNameForRecipientId:recipientId];
}
if (self.accessoryMessage) { if (self.accessoryMessage) {
UILabel *blockedLabel = [[UILabel alloc] init]; UILabel *blockedLabel = [[UILabel alloc] init];
@ -148,6 +152,7 @@ const NSUInteger kContactTableViewCellAvatarSize = 40;
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager - (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager
{ {
OWSAssert(thread); OWSAssert(thread);
self.contactsManager = contactsManager;
NSString *threadName = thread.name; NSString *threadName = thread.name;
if (threadName.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) { if (threadName.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) {
@ -161,6 +166,13 @@ const NSUInteger kContactTableViewCellAvatarSize = 40;
}]; }];
self.nameLabel.attributedText = attributedText; 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 self.avatarView.image = [OWSAvatarBuilder buildImageForThread:thread
diameter:kContactTableViewCellAvatarSize diameter:kContactTableViewCellAvatarSize
contactsManager:contactsManager]; contactsManager:contactsManager];
@ -185,13 +197,66 @@ const NSUInteger kContactTableViewCellAvatarSize = 40;
return [text copy]; 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 - (void)prepareForReuse
{ {
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.accessoryMessage = nil; self.accessoryMessage = nil;
self.accessoryView = nil; self.accessoryView = nil;
self.accessoryType = UITableViewCellAccessoryNone; self.accessoryType = UITableViewCellAccessoryNone;
[self.subtitle removeFromSuperview]; self.nameLabel.text = nil;
self.subtitle = 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 @end

Loading…
Cancel
Save