Skip to content

Commit

Permalink
Merge pull request #1411 from nextcloud/file-preview-fixes
Browse files Browse the repository at this point in the history
File preview fixes
  • Loading branch information
Ivansss authored Nov 6, 2023
2 parents a737f01 + d9ae205 commit 56af8ae
Showing 1 changed file with 44 additions and 42 deletions.
86 changes: 44 additions & 42 deletions NextcloudTalk/FileMessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ - (void)prepareForReuse
[self clearFileStatusView];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
// We use a CGColor so we loose the automatic color changing of dynamic colors -> update manually
self.previewImageView.layer.borderColor = [[UIColor secondarySystemFillColor] CGColor];
}
}

- (void)setupForMessage:(NCChatMessage *)message withLastCommonReadMessage:(NSInteger)lastCommonRead
{
self.titleLabel.text = message.actorDisplayName;
Expand Down Expand Up @@ -272,62 +280,56 @@ - (void)requestPreviewForMessage:(NCChatMessage *)message withAccount:(TalkAccou
BOOL isVideoFile = [NCUtils isVideoFileType:message.file.mimetype];
BOOL isMediaFile = isVideoFile || [NCUtils isImageFileType:message.file.mimetype];


NSInteger requestedHeight = 3 * kFileMessageCellFileMaxPreviewHeight;
__weak typeof(self) weakSelf = self;

[self.previewImageView setImageWithURLRequest:[[NCAPIController sharedInstance] createPreviewRequestForFile:message.file.parameterId withMaxHeight:requestedHeight usingAccount:account]
placeholderImage:filePreviewImage success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {

//TODO: How to adjust for dark mode?
weakSelf.previewImageView.layer.borderColor = [[UIColor secondarySystemFillColor] CGColor];
weakSelf.previewImageView.layer.borderWidth = 1.0f;

dispatch_async(dispatch_get_main_queue(), ^(void){
CGFloat width = image.size.width * image.scale;
CGFloat height = image.size.height * image.scale;
CGFloat width = image.size.width * image.scale;
CGFloat height = image.size.height * image.scale;

CGFloat previewMaxHeight = isMediaFile ? kFileMessageCellMediaFilePreviewHeight : kFileMessageCellFileMaxPreviewHeight;
CGFloat previewMaxWidth = isMediaFile ? kFileMessageCellMediaFileMaxPreviewWidth : kFileMessageCellFileMaxPreviewWidth;
CGFloat previewMaxHeight = isMediaFile ? kFileMessageCellMediaFilePreviewHeight : kFileMessageCellFileMaxPreviewHeight;
CGFloat previewMaxWidth = isMediaFile ? kFileMessageCellMediaFileMaxPreviewWidth : kFileMessageCellFileMaxPreviewWidth;

if (height < kFileMessageCellMinimumHeight) {
CGFloat ratio = kFileMessageCellMinimumHeight / height;
width = width * ratio;
if (width > previewMaxWidth) {
width = previewMaxWidth;
}
height = kFileMessageCellMinimumHeight;
} else {
if (height > previewMaxHeight) {
CGFloat ratio = previewMaxHeight / height;
width = width * ratio;
height = previewMaxHeight;
}
if (width > previewMaxWidth) {
CGFloat ratio = previewMaxWidth / width;
width = previewMaxWidth;
height = height * ratio;
}
if (height < kFileMessageCellMinimumHeight) {
CGFloat ratio = kFileMessageCellMinimumHeight / height;
width = width * ratio;
if (width > previewMaxWidth) {
width = previewMaxWidth;
}
weakSelf.vPreviewSize[3].constant = height;
weakSelf.hPreviewSize[3].constant = width;
weakSelf.vGroupedPreviewSize[1].constant = height;
weakSelf.hGroupedPreviewSize[1].constant = width;
if (isVideoFile) {
// only show the play icon if there is an image preview (not on top of the default video placeholder)
weakSelf.playIconImageView.hidden = NO;
// if the video preview is very narrow, make the play icon fit inside
weakSelf.playIconImageView.frame = CGRectMake(0, 0, MIN(MIN(height, width), kFileMessageCellVideoPlayIconSize), MIN(MIN(height, width), kFileMessageCellVideoPlayIconSize));
weakSelf.playIconImageView.center = CGPointMake(width / 2.0, height / 2.0);
height = kFileMessageCellMinimumHeight;
} else {
if (height > previewMaxHeight) {
CGFloat ratio = previewMaxHeight / height;
width = width * ratio;
height = previewMaxHeight;
}
[weakSelf.previewImageView setImage:image];
[weakSelf setNeedsLayout];
[weakSelf layoutIfNeeded];

if (weakSelf.delegate) {
[weakSelf.delegate cellHasDownloadedImagePreviewWithHeight:ceil(height) forMessage:message];
if (width > previewMaxWidth) {
CGFloat ratio = previewMaxWidth / width;
width = previewMaxWidth;
height = height * ratio;
}
});
}
weakSelf.vPreviewSize[3].constant = height;
weakSelf.hPreviewSize[3].constant = width;
weakSelf.vGroupedPreviewSize[1].constant = height;
weakSelf.hGroupedPreviewSize[1].constant = width;
if (isVideoFile) {
// only show the play icon if there is an image preview (not on top of the default video placeholder)
weakSelf.playIconImageView.hidden = NO;
// if the video preview is very narrow, make the play icon fit inside
weakSelf.playIconImageView.frame = CGRectMake(0, 0, MIN(MIN(height, width), kFileMessageCellVideoPlayIconSize), MIN(MIN(height, width), kFileMessageCellVideoPlayIconSize));
weakSelf.playIconImageView.center = CGPointMake(width / 2.0, height / 2.0);
}
[weakSelf.previewImageView setImage:image];

if (weakSelf.delegate) {
[weakSelf.delegate cellHasDownloadedImagePreviewWithHeight:ceil(height) forMessage:message];
}
} failure:nil];
}

Expand Down

0 comments on commit 56af8ae

Please sign in to comment.