Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to dark/light themed server-side avatars when using NC v25+ #945

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NextcloudTalk/AddParticipantsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.labelTitle.text = participant.name;

if ([participant.source isEqualToString:kParticipantTypeUser]) {
[cell.contactImage setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:participant.userId andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[cell.contactImage setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:participant.userId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
[cell.contactImage setContentMode:UIViewContentModeScaleToFill];
} else if ([participant.source isEqualToString:kParticipantTypeEmail]) {
Expand Down
4 changes: 2 additions & 2 deletions NextcloudTalk/CallParticipantViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ - (void)setUserAvatar:(NSString *)userId
_backgroundImageView = [[AvatarBackgroundImageView alloc] initWithFrame:self.bounds];
__weak UIImageView *weakBGView = _backgroundImageView;
self.backgroundView = _backgroundImageView;
[_backgroundImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:userId andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[_backgroundImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:userId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
NSDictionary *headers = [response allHeaderFields];
id customAvatarHeader = [headers objectForKey:@"X-NC-IsCustomAvatar"];
Expand Down Expand Up @@ -130,7 +130,7 @@ - (void)setUserAvatar:(NSString *)userId
}

if (userId && userId.length > 0) {
[self.peerAvatarImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:userId andSize:256 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[self.peerAvatarImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:userId withStyle:self.traitCollection.userInterfaceStyle andSize:256 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
} else {
UIColor *guestAvatarColor = [UIColor colorWithRed:0.73 green:0.73 blue:0.73 alpha:1.0]; /*#b9b9b9*/
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/CallViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ - (void)createWaitingScreen
{
if (_room.type == kNCRoomTypeOneToOne) {
__weak AvatarBackgroundImageView *weakBGView = self.avatarBackgroundImageView;
[self.avatarBackgroundImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:_room.name andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[self.avatarBackgroundImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:_room.name withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
NSDictionary *headers = [response allHeaderFields];
id customAvatarHeader = [headers objectForKey:@"X-NC-IsCustomAvatar"];
Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/ChatMessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ - (void)setupForMessage:(NCChatMessage *)message withLastCommonReadMessage:(NSIn
[self.avatarView
setImageWithURLRequest:[[NCAPIController sharedInstance]
createAvatarRequestForUser:message.actorId
withStyle:self.traitCollection.userInterfaceStyle
andSize:96
usingAccount:activeAccount]
placeholderImage:nil success:nil failure:nil];
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/FileMessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ - (void)setupForMessage:(NCChatMessage *)message withLastCommonReadMessage:(NSIn
self.dateLabel.text = [NCUtils getTimeFromDate:date];

TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
[self.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:message.actorId andSize:96 usingAccount:activeAccount]
[self.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:message.actorId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:activeAccount]
placeholderImage:nil success:nil failure:nil];

NSString *imageName = [[NCUtils previewImageForFileMIMEType:message.file.mimetype] stringByAppendingString:@"-chat-preview"];
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/LocationMessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ - (void)setupForMessage:(NCChatMessage *)message withLastCommonReadMessage:(NSIn
self.dateLabel.text = [NCUtils getTimeFromDate:date];

TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
[self.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:message.actorId andSize:96 usingAccount:activeAccount]
[self.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:message.actorId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:activeAccount]
placeholderImage:nil success:nil failure:nil];


Expand Down
4 changes: 2 additions & 2 deletions NextcloudTalk/NCAPIController.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ extern NSInteger const kReceivedChatMessagesLimit;
- (void)checkOrCreateAttachmentFolderForAccount:(TalkAccount *)account withCompletionBlock:(CheckAttachmentFolderCompletionBlock)block;

// User avatars
- (NSURLRequest *)createAvatarRequestForUser:(NSString *)userId andSize:(NSInteger)size usingAccount:(TalkAccount *)account;
- (NSURLRequest *)createAvatarRequestForUser:(NSString *)userId withStyle:(UIUserInterfaceStyle) style andSize:(NSInteger)size usingAccount:(TalkAccount *)account;
- (void)getUserAvatarForUser:(NSString *)userId andSize:(NSInteger)size usingAccount:(TalkAccount *)account withCompletionBlock:(GetUserAvatarImageForUserCompletionBlock)block;

// User actions
Expand All @@ -231,7 +231,7 @@ extern NSInteger const kReceivedChatMessagesLimit;
- (NSURLSessionDataTask *)removeUserProfileImageForAccount:(TalkAccount *)account withCompletionBlock:(SetUserProfileFieldCompletionBlock)block;
- (NSURLSessionDataTask *)setUserProfileImage:(UIImage *)image forAccount:(TalkAccount *)account withCompletionBlock:(SetUserProfileFieldCompletionBlock)block;
- (void)saveProfileImageForAccount:(TalkAccount *)account;
- (UIImage *)userProfileImageForAccount:(TalkAccount *)account withSize:(CGSize)size;
- (UIImage *)userProfileImageForAccount:(TalkAccount *)account withStyle:(UIUserInterfaceStyle)style andSize:(CGSize)size;
- (void)removeProfileImageForAccount:(TalkAccount *)account;

// User Status
Expand Down
50 changes: 41 additions & 9 deletions NextcloudTalk/NCAPIController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1995,23 +1995,29 @@ - (NSString *)alternativeNameForFileName:(NSString *)fileName original:(BOOL)isO

#pragma mark - User avatars

- (NSURLRequest *)createAvatarRequestForUser:(NSString *)userId andSize:(NSInteger)size usingAccount:(TalkAccount *)account
- (NSURLRequest *)createAvatarRequestForUser:(NSString *)userId withStyle:(UIUserInterfaceStyle)style andSize:(NSInteger)size usingAccount:(TalkAccount *)account
{
return [self createAvatarRequestForUser:userId withCachePolicy:NSURLRequestReturnCacheDataElseLoad andSize:size usingAccount:account];
return [self createAvatarRequestForUser:userId withCachePolicy:NSURLRequestReturnCacheDataElseLoad style:style andSize:size usingAccount:account];
}

- (NSURLRequest *)createAvatarRequestForUser:(NSString *)userId withCachePolicy:(NSURLRequestCachePolicy)cachePolicy andSize:(NSInteger)size usingAccount:(TalkAccount *)account
- (NSURLRequest *)createAvatarRequestForUser:(NSString *)userId withCachePolicy:(NSURLRequestCachePolicy)cachePolicy style:(UIUserInterfaceStyle)style andSize:(NSInteger)size usingAccount:(TalkAccount *)account
{
NSString *encodedUser = [userId stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *urlString = [NSString stringWithFormat:@"%@/index.php/avatar/%@/%ld", account.server, encodedUser, (long)size];
ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:account.accountId];
NSString *urlString;
if (style == UIUserInterfaceStyleDark && serverCapabilities.versionMajor >= 25) {
urlString = [NSString stringWithFormat:@"%@/index.php/avatar/%@/%ld/dark", account.server, encodedUser, (long)size];
} else {
urlString = [NSString stringWithFormat:@"%@/index.php/avatar/%@/%ld", account.server, encodedUser, (long)size];
}
NSMutableURLRequest *avatarRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:cachePolicy timeoutInterval:60];
[avatarRequest setValue:[self authHeaderForAccount:account] forHTTPHeaderField:@"Authorization"];
return avatarRequest;
}

- (void)getUserAvatarForUser:(NSString *)userId andSize:(NSInteger)size usingAccount:(TalkAccount *)account withCompletionBlock:(GetUserAvatarImageForUserCompletionBlock)block
{
NSURLRequest *request = [self createAvatarRequestForUser:userId andSize:size usingAccount:account];
NSURLRequest *request = [self createAvatarRequestForUser:userId withStyle:UIUserInterfaceStyleLight andSize:size usingAccount:account];
[_imageDownloader downloadImageForURLRequest:request success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
NSData *pngData = UIImagePNGRepresentation(responseObject);
UIImage *image = [UIImage imageWithData:pngData];
Expand Down Expand Up @@ -2184,7 +2190,12 @@ - (NSURLSessionDataTask *)removeUserProfileImageForAccount:(TalkAccount *)accoun

- (void)saveProfileImageForAccount:(TalkAccount *)account
{
NSURLRequest *request = [self createAvatarRequestForUser:account.userId withCachePolicy:NSURLRequestReloadIgnoringCacheData andSize:160 usingAccount:account];
[self getAndStoreProfileImageForAccount:account withStyle:UIUserInterfaceStyleLight];
}

- (void)getAndStoreProfileImageForAccount:(TalkAccount *)account withStyle:(UIUserInterfaceStyle)style
{
NSURLRequest *request = [self createAvatarRequestForUser:account.userId withCachePolicy:NSURLRequestReloadIgnoringCacheData style:style andSize:160 usingAccount:account];
[_imageDownloader downloadImageForURLRequest:request success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {

NSDictionary *headers = [response allHeaderFields];
Expand All @@ -2197,20 +2208,38 @@ - (void)saveProfileImageForAccount:(TalkAccount *)account

NSData *pngData = UIImagePNGRepresentation(responseObject);
NSString *documentsPath = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupIdentifier] path];
NSString *fileName = [NSString stringWithFormat:@"%@-%@.png", account.userId, [[NSURL URLWithString:account.server] host]];
NSString *fileName;
if (style == UIUserInterfaceStyleDark) {
fileName = [NSString stringWithFormat:@"%@-%@-dark.png", account.userId, [[NSURL URLWithString:account.server] host]];
} else {
fileName = [NSString stringWithFormat:@"%@-%@.png", account.userId, [[NSURL URLWithString:account.server] host]];
}
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
[pngData writeToFile:filePath atomically:YES];

ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:account.accountId];
if (style == UIUserInterfaceStyleLight && !managedAccount.hasCustomAvatar && serverCapabilities.versionMajor >= 25) {
[self getAndStoreProfileImageForAccount:account withStyle:UIUserInterfaceStyleDark];
return;
}

[[NSNotificationCenter defaultCenter] postNotificationName:NCUserProfileImageUpdatedNotification object:self userInfo:nil];
} failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"Could not download user profile image");
}];
}

- (UIImage *)userProfileImageForAccount:(TalkAccount *)account withSize:(CGSize)size
- (UIImage *)userProfileImageForAccount:(TalkAccount *)account withStyle:(UIUserInterfaceStyle)style andSize:(CGSize)size
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [[fileManager containerURLForSecurityApplicationGroupIdentifier:groupIdentifier] path];
NSString *fileName = [NSString stringWithFormat:@"%@-%@.png", account.userId, [[NSURL URLWithString:account.server] host]];
ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:account.accountId];
NSString *fileName;
if (style == UIUserInterfaceStyleDark && !account.hasCustomAvatar && serverCapabilities.versionMajor >= 25) {
fileName = [NSString stringWithFormat:@"%@-%@-dark.png", account.userId, [[NSURL URLWithString:account.server] host]];
} else {
fileName = [NSString stringWithFormat:@"%@-%@.png", account.userId, [[NSURL URLWithString:account.server] host]];
}
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];

// Migrate to app group directory
Expand All @@ -2233,6 +2262,9 @@ - (void)removeProfileImageForAccount:(TalkAccount *)account
NSString *fileName = [NSString stringWithFormat:@"%@-%@.png", account.userId, [[NSURL URLWithString:account.server] host]];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
fileName = [NSString stringWithFormat:@"%@-%@-dark.png", account.userId, [[NSURL URLWithString:account.server] host]];
filePath = [documentsPath stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
// Legacy
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *oldDocumentsPath = [paths objectAtIndex:0];
Expand Down
4 changes: 2 additions & 2 deletions NextcloudTalk/NCChatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ - (void)setTitleView
case kNCRoomTypeOneToOne:
{
// Request user avatar to the server and set it if exist
[_titleView.image setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:_room.name andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[_titleView.image setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:_room.name withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
}
break;
Expand Down Expand Up @@ -3366,7 +3366,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
NSString *name = ([suggestionName isEqualToString:@"Guest"]) ? @"?" : suggestionName;
[suggestionCell.avatarView setImageWithString:name color:guestAvatarColor circular:true];
} else {
[suggestionCell.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:suggestionId andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[suggestionCell.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:suggestionId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
}
return suggestionCell;
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/NewRoomTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.labelTitle.accessibilityLabel = [NSString stringWithFormat:NSLocalizedString(@"Create a conversation with %@", nil), contact.name];
cell.labelTitle.accessibilityHint = [NSString stringWithFormat:NSLocalizedString(@"Double tap to create a conversation with %@", nil), contact.name];

[cell.contactImage setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:contact.userId andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
[cell.contactImage setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:contact.userId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
[cell.contactImage setContentMode:UIViewContentModeScaleToFill];

Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/ObjectShareMessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ - (void)setupForMessage:(NCChatMessage *)message withLastCommonReadMessage:(NSIn
self.dateLabel.text = [NCUtils getTimeFromDate:date];

TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
[self.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:message.actorId andSize:96 usingAccount:activeAccount]
[self.avatarView setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:message.actorId withStyle:self.traitCollection.userInterfaceStyle andSize:96 usingAccount:activeAccount]
placeholderImage:nil success:nil failure:nil];


Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/PollResultsDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ import UIKit
// Actor avatar
if detail.actorType == "users" {
let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
if let request = NCAPIController.sharedInstance().createAvatarRequest(forUser: detail.actorId, andSize: 96, using: activeAccount) {
if let request = NCAPIController.sharedInstance().createAvatarRequest(forUser: detail.actorId, with: self.traitCollection.userInterfaceStyle, andSize: 96, using: activeAccount) {
cell.avatarImageView.setImageWith(request, placeholderImage: nil, success: nil, failure: nil)
cell.avatarImageView.contentMode = .scaleToFill
}
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/ReactionsSummaryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ import UIKit
let actorType = actor?["actorType"] as? String
if actorId != nil && actorType == "users" {
let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
if let request = NCAPIController.sharedInstance().createAvatarRequest(forUser: actorId, andSize: 96, using: activeAccount) {
if let request = NCAPIController.sharedInstance().createAvatarRequest(forUser: actorId, with: self.traitCollection.userInterfaceStyle, andSize: 96, using: activeAccount) {
cell.avatarImageView.setImageWith(request, placeholderImage: nil, success: nil, failure: nil)
cell.avatarImageView.contentMode = .scaleToFill
}
Expand Down
Loading