Rework group avatars to reflect conversation colors.

pull/1/head
Matthew Chen 7 years ago
parent 1c920c6be6
commit 8db4595bdc

@ -280,7 +280,6 @@
cell.contentView.preservesSuperviewLayoutMargins = YES; cell.contentView.preservesSuperviewLayoutMargins = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.selectionStyle = UITableViewCellSelectionStyleNone;
const NSUInteger kAvatarSize = 68;
// TODO: Replace this icon. // TODO: Replace this icon.
UIImage *_Nullable localProfileAvatarImage = [OWSProfileManager.sharedManager localProfileAvatarImage]; UIImage *_Nullable localProfileAvatarImage = [OWSProfileManager.sharedManager localProfileAvatarImage];
UIImage *avatarImage = (localProfileAvatarImage UIImage *avatarImage = (localProfileAvatarImage
@ -295,8 +294,8 @@
[cell.contentView addSubview:avatarView]; [cell.contentView addSubview:avatarView];
[avatarView autoVCenterInSuperview]; [avatarView autoVCenterInSuperview];
[avatarView autoPinLeadingToSuperviewMargin]; [avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kAvatarSize]; [avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kAvatarSize]; [avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
if (!localProfileAvatarImage) { if (!localProfileAvatarImage) {
UIImage *cameraImage = [UIImage imageNamed:@"settings-avatar-camera"]; UIImage *cameraImage = [UIImage imageNamed:@"settings-avatar-camera"];

@ -117,7 +117,14 @@ NS_ASSUME_NONNULL_BEGIN
@"BLOCK_LIST_BLOCKED_GROUPS_SECTION", @"Section header for groups that have been blocked"); @"BLOCK_LIST_BLOCKED_GROUPS_SECTION", @"Section header for groups that have been blocked");
for (TSGroupModel *blockedGroup in blockedGroups) { for (TSGroupModel *blockedGroup in blockedGroups) {
UIImage *image = blockedGroup.groupImage ?: OWSGroupAvatarBuilder.defaultGroupAvatarImage; UIImage *_Nullable image = blockedGroup.groupImage;
if (!image) {
NSString *conversationColorName =
[TSGroupThread defaultConversationColorNameForGroupId:blockedGroup.groupId];
image = [OWSGroupAvatarBuilder defaultAvatarForGroupId:blockedGroup.groupId
conversationColorName:conversationColorName
diameter:kStandardAvatarSize];
}
NSString *groupName NSString *groupName
= blockedGroup.groupName.length > 0 ? blockedGroup.groupName : TSGroupThread.defaultGroupName; = blockedGroup.groupName.length > 0 ? blockedGroup.groupName : TSGroupThread.defaultGroupName;

@ -29,8 +29,6 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
@interface NewGroupViewController () <UIImagePickerControllerDelegate, @interface NewGroupViewController () <UIImagePickerControllerDelegate,
UITextFieldDelegate, UITextFieldDelegate,
ContactsViewHelperDelegate, ContactsViewHelperDelegate,
@ -48,6 +46,8 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
@property (nonatomic, readonly) AvatarImageView *avatarView; @property (nonatomic, readonly) AvatarImageView *avatarView;
@property (nonatomic, readonly) UITextField *groupNameTextField; @property (nonatomic, readonly) UITextField *groupNameTextField;
@property (nonatomic) NSData *groupId;
@property (nonatomic, nullable) UIImage *groupAvatar; @property (nonatomic, nullable) UIImage *groupAvatar;
@property (nonatomic) NSMutableSet<NSString *> *memberRecipientIds; @property (nonatomic) NSMutableSet<NSString *> *memberRecipientIds;
@ -86,6 +86,8 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
- (void)commonInit - (void)commonInit
{ {
self.groupId = [Randomness generateRandomBytes:16];
_messageSender = SSKEnvironment.shared.messageSender; _messageSender = SSKEnvironment.shared.messageSender;
_contactsViewHelper = [[ContactsViewHelper alloc] initWithDelegate:self]; _contactsViewHelper = [[ContactsViewHelper alloc] initWithDelegate:self];
_avatarViewHelper = [AvatarViewHelper new]; _avatarViewHelper = [AvatarViewHelper new];
@ -151,8 +153,8 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
[threadInfoView addSubview:avatarView]; [threadInfoView addSubview:avatarView];
[avatarView autoVCenterInSuperview]; [avatarView autoVCenterInSuperview];
[avatarView autoPinLeadingToSuperviewMargin]; [avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kNewGroupViewControllerAvatarWidth]; [avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kNewGroupViewControllerAvatarWidth]; [avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
[self updateAvatarView]; [self updateAvatarView];
UITextField *groupNameTextField = [OWSTextField new]; UITextField *groupNameTextField = [OWSTextField new];
@ -505,8 +507,10 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
NSString *groupName = [self.groupNameTextField.text ows_stripped]; NSString *groupName = [self.groupNameTextField.text ows_stripped];
NSMutableArray<NSString *> *recipientIds = [self.memberRecipientIds.allObjects mutableCopy]; NSMutableArray<NSString *> *recipientIds = [self.memberRecipientIds.allObjects mutableCopy];
[recipientIds addObject:[self.contactsViewHelper localNumber]]; [recipientIds addObject:[self.contactsViewHelper localNumber]];
NSData *groupId = [Randomness generateRandomBytes:16]; return [[TSGroupModel alloc] initWithTitle:groupName
return [[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:self.groupAvatar groupId:groupId]; memberIds:recipientIds
image:self.groupAvatar
groupId:self.groupId];
} }
#pragma mark - Group Avatar #pragma mark - Group Avatar
@ -529,7 +533,14 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
- (void)updateAvatarView - (void)updateAvatarView
{ {
self.avatarView.image = (self.groupAvatar ?: [OWSGroupAvatarBuilder defaultGroupAvatarImage]); UIImage *_Nullable groupAvatar = self.groupAvatar;
if (!groupAvatar) {
NSString *conversationColorName = [TSGroupThread defaultConversationColorNameForGroupId:self.groupId];
groupAvatar = [OWSGroupAvatarBuilder defaultAvatarForGroupId:self.groupId
conversationColorName:conversationColorName
diameter:kLargeAvatarSize];
}
self.avatarView.image = groupAvatar;
} }
#pragma mark - Event Handling #pragma mark - Event Handling

@ -758,8 +758,7 @@ const CGFloat kIconViewLength = 24;
[threadInfoView autoPinWidthToSuperviewWithMargin:16.f]; [threadInfoView autoPinWidthToSuperviewWithMargin:16.f];
[threadInfoView autoPinHeightToSuperviewWithMargin:16.f]; [threadInfoView autoPinHeightToSuperviewWithMargin:16.f];
const NSUInteger kAvatarSize = 68; UIImage *avatarImage = [OWSAvatarBuilder buildImageForThread:self.thread diameter:kLargeAvatarSize];
UIImage *avatarImage = [OWSAvatarBuilder buildImageForThread:self.thread diameter:kAvatarSize];
OWSAssertDebug(avatarImage); OWSAssertDebug(avatarImage);
AvatarImageView *avatarView = [[AvatarImageView alloc] initWithImage:avatarImage]; AvatarImageView *avatarView = [[AvatarImageView alloc] initWithImage:avatarImage];
@ -767,8 +766,8 @@ const CGFloat kIconViewLength = 24;
[threadInfoView addSubview:avatarView]; [threadInfoView addSubview:avatarView];
[avatarView autoVCenterInSuperview]; [avatarView autoVCenterInSuperview];
[avatarView autoPinLeadingToSuperviewMargin]; [avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kAvatarSize]; [avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kAvatarSize]; [avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
UIView *threadNameView = [UIView containerView]; UIView *threadNameView = [UIView containerView];
[threadInfoView addSubview:threadNameView]; [threadInfoView addSubview:threadNameView];

@ -179,15 +179,14 @@ NS_ASSUME_NONNULL_BEGIN
[threadInfoView autoPinWidthToSuperviewWithMargin:16.f]; [threadInfoView autoPinWidthToSuperviewWithMargin:16.f];
[threadInfoView autoPinHeightToSuperviewWithMargin:16.f]; [threadInfoView autoPinHeightToSuperviewWithMargin:16.f];
const CGFloat kAvatarSize = 68.f;
AvatarImageView *avatarView = [AvatarImageView new]; AvatarImageView *avatarView = [AvatarImageView new];
_avatarView = avatarView; _avatarView = avatarView;
[threadInfoView addSubview:avatarView]; [threadInfoView addSubview:avatarView];
[avatarView autoVCenterInSuperview]; [avatarView autoVCenterInSuperview];
[avatarView autoPinLeadingToSuperviewMargin]; [avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kAvatarSize]; [avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kAvatarSize]; [avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
_groupAvatar = self.thread.groupModel.groupImage; _groupAvatar = self.thread.groupModel.groupImage;
[self updateAvatarView]; [self updateAvatarView];
@ -397,7 +396,11 @@ NS_ASSUME_NONNULL_BEGIN
- (void)updateAvatarView - (void)updateAvatarView
{ {
self.avatarView.image = (self.groupAvatar ?: [OWSGroupAvatarBuilder defaultGroupAvatarImage]); UIImage *_Nullable groupAvatar = self.groupAvatar;
if (!groupAvatar) {
groupAvatar = [[[OWSGroupAvatarBuilder alloc] initWithThread:self.thread diameter:kLargeAvatarSize] build];
}
self.avatarView.image = groupAvatar;
} }
#pragma mark - Event Handling #pragma mark - Event Handling

@ -5,6 +5,7 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
extern const NSUInteger kStandardAvatarSize; extern const NSUInteger kStandardAvatarSize;
extern const NSUInteger kLargeAvatarSize;
@class TSThread; @class TSThread;
@class UIImage; @class UIImage;

@ -14,6 +14,7 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
const NSUInteger kStandardAvatarSize = 48; const NSUInteger kStandardAvatarSize = 48;
const NSUInteger kLargeAvatarSize = 68;
typedef void (^OWSAvatarDrawBlock)(CGContextRef context); typedef void (^OWSAvatarDrawBlock)(CGContextRef context);

@ -90,7 +90,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable UIImage *)buildDefaultImage - (nullable UIImage *)buildDefaultImage
{ {
UIImage *cachedAvatar = UIImage *_Nullable cachedAvatar =
[OWSContactAvatarBuilder.contactsManager.avatarCache imageForKey:self.cacheKey diameter:(CGFloat)self.diameter]; [OWSContactAvatarBuilder.contactsManager.avatarCache imageForKey:self.cacheKey diameter:(CGFloat)self.diameter];
if (cachedAvatar) { if (cachedAvatar) {
return cachedAvatar; return cachedAvatar;

@ -12,7 +12,9 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithThread:(TSGroupThread *)thread diameter:(NSUInteger)diameter; - (instancetype)initWithThread:(TSGroupThread *)thread diameter:(NSUInteger)diameter;
+ (nullable UIImage *)defaultGroupAvatarImage; + (nullable UIImage *)defaultAvatarForGroupId:(NSData *)groupId
conversationColorName:(NSString *)conversationColorName
diameter:(NSUInteger)diameter;
@end @end

@ -45,51 +45,42 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable UIImage *)buildDefaultImage - (nullable UIImage *)buildDefaultImage
{ {
NSString *cacheKey = [NSString stringWithFormat:@"%@-%d", self.thread.uniqueId, Theme.isDarkThemeEnabled]; return [self.class defaultAvatarForGroupId:self.thread.groupModel.groupId
conversationColorName:self.thread.conversationColorName
UIImage *cachedAvatar = diameter:self.diameter];
[OWSGroupAvatarBuilder.contactsManager.avatarCache imageForKey:cacheKey diameter:(CGFloat)self.diameter];
if (cachedAvatar) {
return cachedAvatar;
}
UIColor *backgroundColor =
[UIColor ows_conversationColorForColorName:self.thread.conversationColorName isShaded:Theme.isDarkThemeEnabled];
UIImage *_Nullable image =
[OWSGroupAvatarBuilder groupAvatarImageWithBackgroundColor:backgroundColor diameter:self.diameter];
if (!image) {
return nil;
}
[OWSGroupAvatarBuilder.contactsManager.avatarCache setImage:image forKey:cacheKey diameter:self.diameter];
return image;
} }
+ (nullable UIImage *)defaultGroupAvatarImage + (nullable UIImage *)defaultAvatarForGroupId:(NSData *)groupId
conversationColorName:(NSString *)conversationColorName
diameter:(NSUInteger)diameter
{ {
NSUInteger diameter = 200; NSString *cacheKey = [NSString stringWithFormat:@"%@-%d", groupId.hexadecimalString, Theme.isDarkThemeEnabled];
NSString *cacheKey = [NSString stringWithFormat:@"default-group-avatar-%d", Theme.isDarkThemeEnabled];
UIImage *cachedAvatar = [self.contactsManager.avatarCache imageForKey:cacheKey diameter:(CGFloat)diameter]; UIImage *_Nullable cachedAvatar =
[OWSGroupAvatarBuilder.contactsManager.avatarCache imageForKey:cacheKey diameter:(CGFloat)diameter];
if (cachedAvatar) { if (cachedAvatar) {
return cachedAvatar; return cachedAvatar;
} }
// TODO: Verify with Myles. UIColor *backgroundColor =
UIColor *backgroundColor = UIColor.ows_signalBlueColor; [UIColor ows_conversationColorForColorName:conversationColorName isShaded:Theme.isDarkThemeEnabled];
UIImage *_Nullable image = [self groupAvatarImageWithBackgroundColor:backgroundColor diameter:diameter]; UIImage *_Nullable image =
[OWSGroupAvatarBuilder groupAvatarImageWithBackgroundColor:backgroundColor diameter:diameter];
if (!image) { if (!image) {
return nil; return nil;
} }
[self.contactsManager.avatarCache setImage:image forKey:cacheKey diameter:diameter]; [OWSGroupAvatarBuilder.contactsManager.avatarCache setImage:image forKey:cacheKey diameter:diameter];
return image; return image;
} }
+ (nullable UIImage *)groupAvatarImageWithBackgroundColor:(UIColor *)backgroundColor diameter:(NSUInteger)diameter + (nullable UIImage *)groupAvatarImageWithBackgroundColor:(UIColor *)backgroundColor diameter:(NSUInteger)diameter
{ {
UIImage *icon = [UIImage imageNamed:@"group-avatar"]; UIImage *icon = [UIImage imageNamed:@"group-avatar"];
CGSize iconSize = CGSizeScale(icon.size, diameter / kStandardAvatarSize); // The group-avatar asset is designed for the kStandardAvatarSize.
// Adjust its size to reflect the actual output diameter.
CGFloat scaling = diameter / (CGFloat)kStandardAvatarSize;
CGSize iconSize = CGSizeScale(icon.size, scaling);
return return
[OWSAvatarBuilder avatarImageWithIcon:icon iconSize:iconSize backgroundColor:backgroundColor diameter:diameter]; [OWSAvatarBuilder avatarImageWithIcon:icon iconSize:iconSize backgroundColor:backgroundColor diameter:diameter];
} }

@ -44,6 +44,8 @@ extern NSString *const TSGroupThread_NotificationKey_UniqueId;
- (void)fireAvatarChangedNotification; - (void)fireAvatarChangedNotification;
+ (NSString *)defaultConversationColorNameForGroupId:(NSData *)groupId;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -242,6 +242,13 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific
userInfo:userInfo]; userInfo:userInfo];
} }
+ (NSString *)defaultConversationColorNameForGroupId:(NSData *)groupId
{
OWSAssertDebug(groupId.length > 0);
return [self.class stableConversationColorNameForString:[self threadIdFromGroupId:groupId]];
}
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

Loading…
Cancel
Save