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:)
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

@ -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

Loading…
Cancel
Save