mirror of https://github.com/oxen-io/session-ios
Fix 'contact cell vs. message details layout' issue.
parent
1a57fe631c
commit
2ecbf1bb65
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
extern const NSUInteger kContactCellAvatarSize;
|
||||
extern const CGFloat kContactCellAvatarTextMargin;
|
||||
|
||||
@class OWSContactsManager;
|
||||
@class SignalAccount;
|
||||
@class TSThread;
|
||||
|
||||
@interface ContactCellView : UIView
|
||||
|
||||
@property (nonatomic, nullable) NSString *accessoryMessage;
|
||||
|
||||
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager;
|
||||
|
||||
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager;
|
||||
|
||||
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager;
|
||||
|
||||
- (void)prepareForReuse;
|
||||
|
||||
- (NSAttributedString *)verifiedSubtitle;
|
||||
|
||||
- (void)setAttributedSubtitle:(nullable NSAttributedString *)attributedSubtitle;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,277 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ContactCellView.h"
|
||||
#import "OWSContactAvatarBuilder.h"
|
||||
#import "OWSContactsManager.h"
|
||||
#import "UIFont+OWS.h"
|
||||
#import "UIView+OWS.h"
|
||||
#import <SignalMessaging/SignalMessaging-Swift.h>
|
||||
#import <SignalServiceKit/SignalAccount.h>
|
||||
#import <SignalServiceKit/TSContactThread.h>
|
||||
#import <SignalServiceKit/TSGroupThread.h>
|
||||
#import <SignalServiceKit/TSThread.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
const NSUInteger kContactCellAvatarSize = 48;
|
||||
const CGFloat kContactCellAvatarTextMargin = 12;
|
||||
|
||||
@interface ContactCellView ()
|
||||
|
||||
@property (nonatomic) UILabel *nameLabel;
|
||||
@property (nonatomic) UILabel *profileNameLabel;
|
||||
@property (nonatomic) UIImageView *avatarView;
|
||||
@property (nonatomic) UILabel *subtitleLabel;
|
||||
@property (nonatomic) UILabel *ows_accessoryView;
|
||||
@property (nonatomic) UIStackView *nameContainerView;
|
||||
|
||||
@property (nonatomic) OWSContactsManager *contactsManager;
|
||||
@property (nonatomic) NSString *recipientId;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation ContactCellView
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
[self configure];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)configure
|
||||
{
|
||||
OWSAssert(!self.nameLabel);
|
||||
|
||||
self.layoutMargins = UIEdgeInsetsZero;
|
||||
|
||||
_avatarView = [AvatarImageView new];
|
||||
[_avatarView autoSetDimension:ALDimensionWidth toSize:kContactCellAvatarSize];
|
||||
[_avatarView autoSetDimension:ALDimensionHeight toSize:kContactCellAvatarSize];
|
||||
|
||||
self.nameLabel = [UILabel new];
|
||||
self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
||||
self.nameLabel.textColor = [UIColor blackColor];
|
||||
|
||||
self.profileNameLabel = [UILabel new];
|
||||
self.profileNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
||||
self.profileNameLabel.textColor = [UIColor grayColor];
|
||||
|
||||
self.subtitleLabel = [UILabel new];
|
||||
self.subtitleLabel.textColor = [UIColor ows_darkGrayColor];
|
||||
|
||||
self.ows_accessoryView = [[UILabel alloc] init];
|
||||
self.ows_accessoryView.textAlignment = NSTextAlignmentRight;
|
||||
self.ows_accessoryView.textColor = [UIColor colorWithWhite:0.5f alpha:1.f];
|
||||
|
||||
self.nameContainerView = [[UIStackView alloc] initWithArrangedSubviews:@[
|
||||
self.nameLabel,
|
||||
self.profileNameLabel,
|
||||
self.subtitleLabel,
|
||||
]];
|
||||
self.nameContainerView.axis = UILayoutConstraintAxisVertical;
|
||||
self.nameContainerView.alignment = UIStackViewAlignmentFill;
|
||||
|
||||
UIStackView *hStackView = [[UIStackView alloc] initWithArrangedSubviews:@[
|
||||
self.avatarView,
|
||||
self.nameContainerView,
|
||||
self.ows_accessoryView,
|
||||
]];
|
||||
hStackView.axis = UILayoutConstraintAxisHorizontal;
|
||||
hStackView.spacing = kContactCellAvatarTextMargin;
|
||||
hStackView.distribution = UIStackViewDistributionFill;
|
||||
[self addSubview:hStackView];
|
||||
[hStackView autoVCenterInSuperview];
|
||||
[hStackView autoPinLeadingToSuperviewMargin];
|
||||
[hStackView autoPinTrailingToSuperviewMargin];
|
||||
// Ensure that the cell's contents never overflow the cell bounds.
|
||||
[hStackView autoPinEdgeToSuperviewMargin:ALEdgeTop relation:NSLayoutRelationGreaterThanOrEqual];
|
||||
[hStackView autoPinEdgeToSuperviewMargin:ALEdgeBottom relation:NSLayoutRelationGreaterThanOrEqual];
|
||||
|
||||
[self configureFonts];
|
||||
}
|
||||
|
||||
- (void)configureFonts
|
||||
{
|
||||
self.nameLabel.font = [UIFont ows_dynamicTypeBodyFont];
|
||||
self.profileNameLabel.font = [UIFont ows_regularFontWithSize:11.f];
|
||||
self.subtitleLabel.font = [UIFont ows_regularFontWithSize:11.f];
|
||||
self.ows_accessoryView.font = [UIFont ows_mediumFontWithSize:13.f];
|
||||
}
|
||||
|
||||
- (void)configureWithSignalAccount:(SignalAccount *)signalAccount contactsManager:(OWSContactsManager *)contactsManager
|
||||
{
|
||||
[self configureWithRecipientId:signalAccount.recipientId contactsManager:contactsManager];
|
||||
}
|
||||
|
||||
- (void)configureWithRecipientId:(NSString *)recipientId contactsManager:(OWSContactsManager *)contactsManager
|
||||
{
|
||||
OWSAssert(recipientId.length > 0);
|
||||
OWSAssert(contactsManager);
|
||||
|
||||
// Update fonts to reflect changes to dynamic type.
|
||||
[self configureFonts];
|
||||
|
||||
self.recipientId = recipientId;
|
||||
self.contactsManager = contactsManager;
|
||||
|
||||
self.nameLabel.attributedText =
|
||||
[contactsManager formattedFullNameForRecipientId:recipientId font:self.nameLabel.font];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(otherUsersProfileDidChange:)
|
||||
name:kNSNotificationName_OtherUsersProfileDidChange
|
||||
object:nil];
|
||||
[self updateProfileName];
|
||||
[self updateAvatar];
|
||||
|
||||
if (self.accessoryMessage) {
|
||||
self.ows_accessoryView.text = self.accessoryMessage;
|
||||
}
|
||||
|
||||
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
||||
[self layoutSubviews];
|
||||
}
|
||||
|
||||
- (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager
|
||||
{
|
||||
OWSAssert(thread);
|
||||
|
||||
// Update fonts to reflect changes to dynamic type.
|
||||
[self configureFonts];
|
||||
|
||||
self.contactsManager = contactsManager;
|
||||
|
||||
NSString *threadName = thread.name;
|
||||
if (threadName.length == 0 && [thread isKindOfClass:[TSGroupThread class]]) {
|
||||
threadName = [MessageStrings newGroupDefaultTitle];
|
||||
}
|
||||
|
||||
NSAttributedString *attributedText =
|
||||
[[NSAttributedString alloc] initWithString:threadName
|
||||
attributes:@{
|
||||
NSForegroundColorAttributeName : [UIColor blackColor],
|
||||
}];
|
||||
self.nameLabel.attributedText = attributedText;
|
||||
|
||||
if ([thread isKindOfClass:[TSContactThread class]]) {
|
||||
self.recipientId = thread.contactIdentifier;
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(otherUsersProfileDidChange:)
|
||||
name:kNSNotificationName_OtherUsersProfileDidChange
|
||||
object:nil];
|
||||
[self updateProfileName];
|
||||
}
|
||||
self.avatarView.image =
|
||||
[OWSAvatarBuilder buildImageForThread:thread diameter:kContactCellAvatarSize contactsManager:contactsManager];
|
||||
|
||||
if (self.accessoryMessage) {
|
||||
self.ows_accessoryView.text = self.accessoryMessage;
|
||||
}
|
||||
|
||||
// Force layout, since imageView isn't being initally rendered on App Store optimized build.
|
||||
[self layoutSubviews];
|
||||
}
|
||||
|
||||
- (void)updateAvatar
|
||||
{
|
||||
OWSContactsManager *contactsManager = self.contactsManager;
|
||||
if (contactsManager == nil) {
|
||||
OWSFail(@"%@ contactsManager should not be nil", self.logTag);
|
||||
self.avatarView.image = nil;
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *recipientId = self.recipientId;
|
||||
if (recipientId.length == 0) {
|
||||
OWSFail(@"%@ recipientId should not be nil", self.logTag);
|
||||
self.avatarView.image = nil;
|
||||
return;
|
||||
}
|
||||
|
||||
self.avatarView.image = [[[OWSContactAvatarBuilder alloc] initWithSignalId:recipientId
|
||||
diameter:kContactCellAvatarSize
|
||||
contactsManager:contactsManager] build];
|
||||
}
|
||||
|
||||
- (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];
|
||||
}
|
||||
|
||||
[self.profileNameLabel setNeedsLayout];
|
||||
}
|
||||
|
||||
- (void)prepareForReuse
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
|
||||
self.accessoryMessage = nil;
|
||||
self.nameLabel.text = nil;
|
||||
self.subtitleLabel.text = nil;
|
||||
self.profileNameLabel.text = nil;
|
||||
self.ows_accessoryView.text = nil;
|
||||
}
|
||||
|
||||
- (void)otherUsersProfileDidChange:(NSNotification *)notification
|
||||
{
|
||||
OWSAssertIsOnMainThread();
|
||||
|
||||
NSString *recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId];
|
||||
OWSAssert(recipientId.length > 0);
|
||||
|
||||
if (recipientId.length > 0 && [self.recipientId isEqualToString:recipientId]) {
|
||||
[self updateProfileName];
|
||||
[self updateAvatar];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSAttributedString *)verifiedSubtitle
|
||||
{
|
||||
NSMutableAttributedString *text = [NSMutableAttributedString new];
|
||||
// "checkmark"
|
||||
[text appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:@"\uf00c "
|
||||
attributes:@{
|
||||
NSFontAttributeName :
|
||||
[UIFont ows_fontAwesomeFont:self.subtitleLabel.font.pointSize],
|
||||
}]];
|
||||
[text appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:NSLocalizedString(@"PRIVACY_IDENTITY_IS_VERIFIED_BADGE",
|
||||
@"Badge indicating that the user is verified.")]];
|
||||
return [text copy];
|
||||
}
|
||||
|
||||
- (void)setAttributedSubtitle:(nullable NSAttributedString *)attributedSubtitle
|
||||
{
|
||||
self.subtitleLabel.attributedText = attributedSubtitle;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue