display_room_avatars :

Display the room members avatars in the member lists.
This commit is contained in:
yannick
2015-12-03 18:09:32 +01:00
parent b12c5d5ebd
commit fa0cb4001b
6 changed files with 179 additions and 15 deletions
+23
View File
@@ -0,0 +1,23 @@
/*
Copyright 2015 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import <MatrixKit/MatrixKit.h>
@interface Contact : MXKContact
@property (nonatomic) MXRoomMember* mxMember;
@end
+46
View File
@@ -0,0 +1,46 @@
/*
Copyright 2015 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "Contact.h"
#import "AvatarGenerator.h"
@implementation Contact
- (UIImage*)thumbnailWithPreferedSize:(CGSize)size
{
UIImage* thumbnail = nil;
// replace the identicon icon by the Vector style one
if (_mxMember && ([_mxMember.avatarUrl rangeOfString:@"identicon"].location != NSNotFound))
{
thumbnail = [AvatarGenerator generateRoomMemberAvatar:_mxMember];
}
else
{
thumbnail = [super thumbnailWithPreferedSize:size];
}
// ensure that the thumbnail will have a vector style.
if (!thumbnail)
{
thumbnail = [AvatarGenerator generateAvatarForText:self.displayName];
}
return thumbnail;
}
@end
+20 -2
View File
@@ -17,11 +17,29 @@
#import <MatrixKit/MatrixKit.h>
/**
`AvatarGenerator` class generate an avatar image from a string
`AvatarGenerator` class generate an avatar image from objects
*/
@interface AvatarGenerator : NSObject
/**
Generate an avatar for a room.
@param room the room
@return the avatar image
*/
+ (UIImage*)generateRoomAvatar:(MXRoom*)room;
/**
Generate an avatar for a room member.
@param mxMember the room member
@return the avatar image
*/
+ (UIImage*)generateRoomMemberAvatar:(MXRoomMember*)mxMember;
/**
Generate an avatar for a text.
@param text the text.
@return the avatar image
*/
+ (UIImage*)generateAvatarForText:(NSString*)text;
@end
+64 -7
View File
@@ -28,6 +28,11 @@ static UILabel* backgroundLabel = nil;
blue:((float)((rgbValue & 0x0000FF) >> 0))/255.0 \
alpha:1.0]
/**
Init the generated avatar colors.
Should be the same as the webclient.
*/
+ (void)initColorList
{
if (!colorsList)
@@ -39,6 +44,9 @@ static UILabel* backgroundLabel = nil;
}
}
/**
Generate the selected color index in colorsList list.
*/
+ (NSUInteger)colorIndexForText:(NSString*)text
{
[AvatarGenerator initColorList];
@@ -60,6 +68,9 @@ static UILabel* backgroundLabel = nil;
return colorIndex;
}
/**
Create an UIImage with the text and the background color.
*/
+ (UIImage *)imageFromText:(NSString*)text withBackgroundColor:(UIColor*)color
{
if (!backgroundLabel)
@@ -79,20 +90,18 @@ static UILabel* backgroundLabel = nil;
// set to the top quality
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
// Make the CALayer to draw in our "canvas".
[[backgroundLabel layer] renderInContext: UIGraphicsGetCurrentContext()];
// Fetch an UIImage of our "canvas".
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Stop the "canvas" from accepting any input.
UIGraphicsEndImageContext();
// Return the image.
return image;
}
/**
Returns the UIImage for the text and a selected color.
It checks first if it is not yet cached before generating one.
*/
+ (UIImage*)avatarForText:(NSString*)text andColorIndex:(NSUInteger)colorIndex
{
// the images are cached to avoid create them several times
@@ -116,16 +125,64 @@ static UILabel* backgroundLabel = nil;
return image;
}
/**
Generate an avatar for a text.
@param text the text.
@return the avatar image
*/
+ (UIImage*)generateAvatarForText:(NSString*)text
{
NSUInteger index = [AvatarGenerator colorIndexForText:text];
if (text.length > 0)
{
text = [[text substringToIndex:1] uppercaseString];
}
return [AvatarGenerator avatarForText:text andColorIndex:index];
}
/**
Generate an avatar for a room member.
@param mxMember the room member
@return the avatar image
*/
+ (UIImage*)generateRoomMemberAvatar:(MXRoomMember*)mxMember
{
// the selected color is based on the userId
NSUInteger index = [AvatarGenerator colorIndexForText:mxMember.userId];
NSString* text = mxMember.displayname ? mxMember.displayname : mxMember.userId;
// if the displayname is the userID
// skip the @
if (!mxMember.displayname && [text hasPrefix:@"@"])
{
text = [text substringFromIndex:1];
}
if (text.length > 0)
{
text = [[text substringToIndex:1] uppercaseString];
}
return [AvatarGenerator avatarForText:text andColorIndex:index];
}
/**
Generate an avatar for a room.
@param room the room
@return the avatar image
*/
+ (UIImage*)generateRoomAvatar:(MXRoom*)room
{
NSString* displayName = room.state.displayname;
NSString* roomId = room.state.roomId;
// the selected color is based on the roomId
NSUInteger index = [AvatarGenerator colorIndexForText:roomId];
NSString* text = displayName;
// ignore the first #
// useful for the room
if ([text hasPrefix:@"#"])
{
text = [text substringFromIndex:1];
@@ -22,6 +22,8 @@
#import "VectorDesignValues.h"
#import "Contact.h"
@interface RoomParticipantsViewController ()
{
// Add participants section
@@ -370,7 +372,8 @@
}
// Create the contact related to this member
MXKContact *contact = [[MXKContact alloc] initMatrixContactWithDisplayName:displayName andMatrixID:mxMember.userId];
Contact *contact = [[Contact alloc] initMatrixContactWithDisplayName:displayName andMatrixID:mxMember.userId];
contact.mxMember = mxMember;
[mxkContactsById setObject:contact forKey:mxMember.userId];
// Add this participant (admin is in first position, the other are sorted in alphabetical order).
@@ -567,10 +570,11 @@
if (userMatrixId && indexPath.row == 0)
{
MXKContact *contact = [mxkContactsById objectForKey:userMatrixId];
Contact *contact = [mxkContactsById objectForKey:userMatrixId];
if (! contact)
{
contact = [[MXKContact alloc] initMatrixContactWithDisplayName:NSLocalizedStringFromTable(@"you", @"Vector", nil) andMatrixID:userMatrixId];
contact = [[Contact alloc] initMatrixContactWithDisplayName:NSLocalizedStringFromTable(@"you", @"Vector", nil) andMatrixID:userMatrixId];
contact.mxMember = [self.mxRoom.state memberWithUserId:userMatrixId];
[mxkContactsById setObject:contact forKey:userMatrixId];
}
@@ -613,7 +617,7 @@
if (index < mutableParticipants.count)
{
NSString *userId = mutableParticipants[index];
MXKContact *contact = [mxkContactsById objectForKey:userId];
Contact *contact = [mxkContactsById objectForKey:userId];
if (!contact)
{
// Create this missing contact
@@ -625,7 +629,8 @@
mxUser = [session userWithUserId:userId];
if (mxUser)
{
contact = [[MXKContact alloc] initMatrixContactWithDisplayName:((mxUser.displayname.length > 0) ? mxUser.displayname : userId) andMatrixID:userId];
contact = [[Contact alloc] initMatrixContactWithDisplayName:((mxUser.displayname.length > 0) ? mxUser.displayname : userId) andMatrixID:userId];
contact.mxMember = [self.mxRoom.state memberWithUserId:userId];
break;
}
}
@@ -947,7 +952,8 @@
{
if (![userId isEqualToString:userMatrixId])
{
MXKContact *splitContact = [[MXKContact alloc] initMatrixContactWithDisplayName:contact.displayName andMatrixID:userId];
Contact *splitContact = [[Contact alloc] initMatrixContactWithDisplayName:contact.displayName andMatrixID:userId];
splitContact.mxMember = [self.mxRoom.state memberWithUserId:userId];
[mxUsers addObject:splitContact];
}
}