From 39eac9129ba777597ee6c1e8ba7a669f94e16215 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 2 Jul 2018 09:42:48 -0400 Subject: [PATCH] Respond to CR. --- .../Cells/OWSMessageBubbleView.h | 3 ++ .../Cells/OWSMessageBubbleView.m | 25 ++++++++- .../ConversationViewController.m | 17 +++--- .../ConversationView/ConversationViewItem.h | 2 +- .../ConversationView/ConversationViewItem.m | 2 +- .../ViewControllers/HomeView/HomeViewCell.m | 6 +-- SignalMessaging/contacts/OWSContactsManager.h | 9 ++-- SignalMessaging/contacts/OWSContactsManager.m | 53 ++++++++++++------- SignalMessaging/utils/ConversationStyle.swift | 5 +- 9 files changed, 85 insertions(+), 37 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.h b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.h index fc036b8da..83b9dd688 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.h +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.h @@ -83,6 +83,9 @@ typedef NS_ENUM(NSUInteger, OWSMessageGestureLocation) { - (void)prepareForReuse; ++ (NSDictionary *)senderNamePrimaryAttributes; ++ (NSDictionary *)senderNameSecondaryAttributes; + #pragma mark - Gestures - (OWSMessageGestureLocation)gestureLocationForLocation:(CGPoint)locationInMessageBubble; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m index d3169abb3..58cc0ecc7 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m @@ -683,12 +683,33 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(self.senderNameLabel); OWSAssert(self.shouldShowSenderName); - self.senderNameLabel.text = self.viewItem.senderName.uppercaseString; self.senderNameLabel.textColor = self.bodyTextColor; - self.senderNameLabel.font = UIFont.ows_dynamicTypeCaption2Font; + self.senderNameLabel.font = OWSMessageBubbleView.senderNameFont; + self.senderNameLabel.attributedText = self.viewItem.senderName; self.senderNameLabel.lineBreakMode = NSLineBreakByTruncatingTail; } ++ (UIFont *)senderNameFont +{ + return UIFont.ows_dynamicTypeCaption2Font; +} + ++ (NSDictionary *)senderNamePrimaryAttributes +{ + return @{ + NSFontAttributeName : self.senderNameFont, + NSForegroundColorAttributeName : ConversationStyle.bubbleTextColorIncoming, + }; +} + ++ (NSDictionary *)senderNameSecondaryAttributes +{ + return @{ + NSFontAttributeName : self.senderNameFont, + NSForegroundColorAttributeName : [ConversationStyle.bubbleTextColorIncoming colorWithAlphaComponent:0.75f], + }; +} + - (BOOL)hasTapForMore { if (!self.hasBodyText) { diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index bba4740c0..b4ae37c5e 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -1206,10 +1206,10 @@ typedef enum : NSUInteger { } } else { OWSAssert(self.thread.contactIdentifier); - name = [self.contactsManager - attributedStringForConversationTitleWithPhoneIdentifier:self.thread.contactIdentifier - primaryFont:self.headerView.titlePrimaryFont - secondaryFont:self.headerView.titleSecondaryFont]; + name = + [self.contactsManager attributedContactOrProfileNameForPhoneIdentifier:self.thread.contactIdentifier + primaryFont:self.headerView.titlePrimaryFont + secondaryFont:self.headerView.titleSecondaryFont]; } self.title = nil; @@ -4856,7 +4856,7 @@ typedef enum : NSUInteger { ConversationViewItem *_Nullable nextViewItem = (i + 1 < viewItems.count ? viewItems[i + 1] : nil); BOOL shouldShowSenderAvatar = NO; BOOL shouldHideFooter = NO; - NSString *_Nullable senderName = nil; + NSAttributedString *_Nullable senderName = nil; OWSInteractionType interactionType = viewItem.interaction.interactionType; NSString *timestampText = [DateUtil formatTimestampShort:viewItem.interaction.timestamp]; @@ -4910,7 +4910,12 @@ typedef enum : NSUInteger { || viewItem.shouldShowDate); } if (shouldShowSenderName) { - senderName = [self.contactsManager contactOrProfileNameForPhoneIdentifier:incomingSenderId]; + senderName = [self.contactsManager + attributedContactOrProfileNameForPhoneIdentifier:incomingSenderId + primaryAttributes:[OWSMessageBubbleView + senderNamePrimaryAttributes] + secondaryAttributes:[OWSMessageBubbleView + senderNameSecondaryAttributes]]; } // Show the sender avatar for incoming group messages unless diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h index d69ce4b24..20051dd00 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h @@ -56,7 +56,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType); @property (nonatomic) BOOL shouldShowDate; @property (nonatomic) BOOL shouldShowSenderAvatar; -@property (nonatomic, nullable) NSString *senderName; +@property (nonatomic, nullable) NSAttributedString *senderName; @property (nonatomic) BOOL shouldHideFooter; @property (nonatomic, readonly) ConversationStyle *conversationStyle; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index 0928447a2..af8e585d0 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -160,7 +160,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) [self clearCachedLayoutState]; } -- (void)setSenderName:(nullable NSString *)senderName +- (void)setSenderName:(nullable NSAttributedString *)senderName { if ([NSObject isNullableObject:senderName equalTo:_senderName]) { return; diff --git a/Signal/src/ViewControllers/HomeView/HomeViewCell.m b/Signal/src/ViewControllers/HomeView/HomeViewCell.m index ffa9e4988..efc8f5bcf 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewCell.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewCell.m @@ -470,9 +470,9 @@ NS_ASSUME_NONNULL_BEGIN name = [[NSAttributedString alloc] initWithString:thread.name]; } } else { - name = [contactsManager attributedStringForConversationTitleWithPhoneIdentifier:thread.contactIdentifier - primaryFont:self.nameFont - secondaryFont:self.nameSecondaryFont]; + name = [contactsManager attributedContactOrProfileNameForPhoneIdentifier:thread.contactIdentifier + primaryFont:self.nameFont + secondaryFont:self.nameSecondaryFont]; } self.nameLabel.attributedText = name; diff --git a/SignalMessaging/contacts/OWSContactsManager.h b/SignalMessaging/contacts/OWSContactsManager.h index 3dee348d5..b1b5f06a4 100644 --- a/SignalMessaging/contacts/OWSContactsManager.h +++ b/SignalMessaging/contacts/OWSContactsManager.h @@ -90,9 +90,12 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification; - (NSAttributedString *)formattedFullNameForRecipientId:(NSString *)recipientId font:(UIFont *)font; - (NSString *)contactOrProfileNameForPhoneIdentifier:(NSString *)recipientId; - (NSAttributedString *)attributedContactOrProfileNameForPhoneIdentifier:(NSString *)recipientId; -- (NSAttributedString *)attributedStringForConversationTitleWithPhoneIdentifier:(NSString *)recipientId - primaryFont:(UIFont *)primaryFont - secondaryFont:(UIFont *)secondaryFont; +- (NSAttributedString *)attributedContactOrProfileNameForPhoneIdentifier:(NSString *)recipientId + primaryFont:(UIFont *)primaryFont + secondaryFont:(UIFont *)secondaryFont; +- (NSAttributedString *)attributedContactOrProfileNameForPhoneIdentifier:(NSString *)recipientId + primaryAttributes:(NSDictionary *)primaryAttributes + secondaryAttributes:(NSDictionary *)secondaryAttributes; @end NS_ASSUME_NONNULL_END diff --git a/SignalMessaging/contacts/OWSContactsManager.m b/SignalMessaging/contacts/OWSContactsManager.m index ad28c4018..189b11369 100644 --- a/SignalMessaging/contacts/OWSContactsManager.m +++ b/SignalMessaging/contacts/OWSContactsManager.m @@ -4,6 +4,7 @@ #import "OWSContactsManager.h" #import "Environment.h" +#import "NSAttributedString+OWS.h" #import "OWSFormat.h" #import "OWSProfileManager.h" #import "OWSUserProfile.h" @@ -737,37 +738,49 @@ NSString *const OWSContactsManagerSignalAccountsDidChangeNotification return [[NSAttributedString alloc] initWithString:[self contactOrProfileNameForPhoneIdentifier:recipientId]]; } -- (NSAttributedString *)attributedStringForConversationTitleWithPhoneIdentifier:(NSString *)recipientId - primaryFont:(UIFont *)primaryFont - secondaryFont:(UIFont *)secondaryFont +- (NSAttributedString *)attributedContactOrProfileNameForPhoneIdentifier:(NSString *)recipientId + primaryFont:(UIFont *)primaryFont + secondaryFont:(UIFont *)secondaryFont +{ + OWSAssert(primaryFont); + OWSAssert(secondaryFont); + + return [self attributedContactOrProfileNameForPhoneIdentifier:(NSString *)recipientId + primaryAttributes:@{ + NSFontAttributeName : primaryFont, + } + secondaryAttributes:@{ + NSFontAttributeName : secondaryFont, + }]; +} + +- (NSAttributedString *)attributedContactOrProfileNameForPhoneIdentifier:(NSString *)recipientId + primaryAttributes:(NSDictionary *)primaryAttributes + secondaryAttributes:(NSDictionary *)secondaryAttributes { + OWSAssert(primaryAttributes.count > 0); + OWSAssert(secondaryAttributes.count > 0); + // Prefer a saved name from system contacts, if available NSString *_Nullable savedContactName = [self cachedContactNameForRecipientId:recipientId]; if (savedContactName.length > 0) { - return [[NSAttributedString alloc] initWithString:savedContactName]; + return [[NSAttributedString alloc] initWithString:savedContactName attributes:primaryAttributes]; } 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]; + NSAttributedString *result = + [[NSAttributedString alloc] initWithString:recipientId attributes:primaryAttributes]; + result = [result rtlSafeAppend:[[NSAttributedString alloc] initWithString:@" "] referenceView:[UIView new]]; + result = [result rtlSafeAppend:[[NSAttributedString alloc] initWithString:@"~"] referenceView:[UIView new]]; + result = + [result rtlSafeAppend:[[NSAttributedString alloc] initWithString:profileName attributes:secondaryAttributes] + referenceView:[UIView new]]; + return [result copy]; } // else fall back to recipient id - return [[NSAttributedString alloc] initWithString:recipientId]; + return [[NSAttributedString alloc] initWithString:recipientId attributes:primaryAttributes]; } // TODO refactor attributed counterparts to use this as a helper method? diff --git a/SignalMessaging/utils/ConversationStyle.swift b/SignalMessaging/utils/ConversationStyle.swift index d0d9d50e1..ea54e89e7 100644 --- a/SignalMessaging/utils/ConversationStyle.swift +++ b/SignalMessaging/utils/ConversationStyle.swift @@ -161,10 +161,13 @@ public class ConversationStyle: NSObject { } } + @objc + public static var bubbleTextColorIncoming = UIColor.ows_white + @objc public func bubbleTextColor(message: TSMessage) -> UIColor { if message is TSIncomingMessage { - return UIColor.ows_white + return ConversationStyle.bubbleTextColorIncoming } else if let outgoingMessage = message as? TSOutgoingMessage { switch outgoingMessage.messageState { case .failed: