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.selectionStyle = UITableViewCellSelectionStyleNone;
const NSUInteger kAvatarSize = 68;
// TODO: Replace this icon.
UIImage *_Nullable localProfileAvatarImage = [OWSProfileManager.sharedManager localProfileAvatarImage];
UIImage *avatarImage = (localProfileAvatarImage
@ -295,8 +294,8 @@
[cell.contentView addSubview:avatarView];
[avatarView autoVCenterInSuperview];
[avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kAvatarSize];
[avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
if (!localProfileAvatarImage) {
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");
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
= blockedGroup.groupName.length > 0 ? blockedGroup.groupName : TSGroupThread.defaultGroupName;

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

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

@ -179,15 +179,14 @@ NS_ASSUME_NONNULL_BEGIN
[threadInfoView autoPinWidthToSuperviewWithMargin:16.f];
[threadInfoView autoPinHeightToSuperviewWithMargin:16.f];
const CGFloat kAvatarSize = 68.f;
AvatarImageView *avatarView = [AvatarImageView new];
_avatarView = avatarView;
[threadInfoView addSubview:avatarView];
[avatarView autoVCenterInSuperview];
[avatarView autoPinLeadingToSuperviewMargin];
[avatarView autoSetDimension:ALDimensionWidth toSize:kAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kAvatarSize];
[avatarView autoSetDimension:ALDimensionWidth toSize:kLargeAvatarSize];
[avatarView autoSetDimension:ALDimensionHeight toSize:kLargeAvatarSize];
_groupAvatar = self.thread.groupModel.groupImage;
[self updateAvatarView];
@ -397,7 +396,11 @@ NS_ASSUME_NONNULL_BEGIN
- (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

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

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

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

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

@ -45,51 +45,42 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable UIImage *)buildDefaultImage
{
NSString *cacheKey = [NSString stringWithFormat:@"%@-%d", self.thread.uniqueId, Theme.isDarkThemeEnabled];
UIImage *cachedAvatar =
[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;
return [self.class defaultAvatarForGroupId:self.thread.groupModel.groupId
conversationColorName:self.thread.conversationColorName
diameter:self.diameter];
}
+ (nullable UIImage *)defaultGroupAvatarImage
+ (nullable UIImage *)defaultAvatarForGroupId:(NSData *)groupId
conversationColorName:(NSString *)conversationColorName
diameter:(NSUInteger)diameter
{
NSUInteger diameter = 200;
NSString *cacheKey = [NSString stringWithFormat:@"default-group-avatar-%d", Theme.isDarkThemeEnabled];
NSString *cacheKey = [NSString stringWithFormat:@"%@-%d", groupId.hexadecimalString, 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) {
return cachedAvatar;
}
// TODO: Verify with Myles.
UIColor *backgroundColor = UIColor.ows_signalBlueColor;
UIImage *_Nullable image = [self groupAvatarImageWithBackgroundColor:backgroundColor diameter:diameter];
UIColor *backgroundColor =
[UIColor ows_conversationColorForColorName:conversationColorName isShaded:Theme.isDarkThemeEnabled];
UIImage *_Nullable image =
[OWSGroupAvatarBuilder groupAvatarImageWithBackgroundColor:backgroundColor diameter:diameter];
if (!image) {
return nil;
}
[self.contactsManager.avatarCache setImage:image forKey:cacheKey diameter:diameter];
[OWSGroupAvatarBuilder.contactsManager.avatarCache setImage:image forKey:cacheKey diameter:diameter];
return image;
}
+ (nullable UIImage *)groupAvatarImageWithBackgroundColor:(UIColor *)backgroundColor diameter:(NSUInteger)diameter
{
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
[OWSAvatarBuilder avatarImageWithIcon:icon iconSize:iconSize backgroundColor:backgroundColor diameter:diameter];
}

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

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

Loading…
Cancel
Save