mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
9dfebb2d4b
// FREEBIE
197 lines
6.2 KiB
Objective-C
197 lines
6.2 KiB
Objective-C
//
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "TSAnimatedAdapter.h"
|
|
#import "AttachmentUploadView.h"
|
|
#import "FLAnimatedImage.h"
|
|
#import "JSQMediaItem+OWS.h"
|
|
#import "TSAttachmentStream.h"
|
|
#import "UIColor+OWS.h"
|
|
#import <AssetsLibrary/AssetsLibrary.h>
|
|
#import <JSQMessagesViewController/JSQMessagesMediaViewBubbleImageMasker.h>
|
|
#import <MobileCoreServices/MobileCoreServices.h>
|
|
#import <SignalServiceKit/MIMETypeUtil.h>
|
|
#import <SignalServiceKit/NSData+Image.h>
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@interface TSAnimatedAdapter ()
|
|
|
|
@property (nonatomic, nullable) FLAnimatedImageView *cachedImageView;
|
|
@property (nonatomic) TSAttachmentStream *attachment;
|
|
@property (nonatomic, nullable) AttachmentUploadView *attachmentUploadView;
|
|
@property (nonatomic) BOOL incoming;
|
|
@property (nonatomic) CGSize imageSize;
|
|
@property (nonatomic) NSString *attachmentId;
|
|
|
|
// See comments on OWSMessageMediaAdapter.
|
|
@property (nonatomic, nullable, weak) id lastPresentingCell;
|
|
|
|
@end
|
|
|
|
#pragma mark -
|
|
|
|
@implementation TSAnimatedAdapter
|
|
|
|
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming
|
|
{
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
_cachedImageView = nil;
|
|
_attachment = attachment;
|
|
_attachmentId = attachment.uniqueId;
|
|
_incoming = incoming;
|
|
_imageSize = [attachment imageSizeWithoutTransaction];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)clearAllViews
|
|
{
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
[_cachedImageView removeFromSuperview];
|
|
_cachedImageView = nil;
|
|
_attachmentUploadView = nil;
|
|
}
|
|
|
|
- (void)clearCachedMediaViews {
|
|
[super clearCachedMediaViews];
|
|
[self clearAllViews];
|
|
}
|
|
|
|
- (void)setAppliesMediaViewMaskAsOutgoing:(BOOL)appliesMediaViewMaskAsOutgoing {
|
|
[super setAppliesMediaViewMaskAsOutgoing:appliesMediaViewMaskAsOutgoing];
|
|
[self clearAllViews];
|
|
}
|
|
|
|
#pragma mark - NSObject
|
|
|
|
- (NSUInteger)hash
|
|
{
|
|
return super.hash ^ self.attachment.uniqueId.hash;
|
|
}
|
|
|
|
#pragma mark - OWSMessageMediaAdapter
|
|
|
|
- (void)setCellVisible:(BOOL)isVisible
|
|
{
|
|
if (isVisible) {
|
|
[self.cachedImageView startAnimating];
|
|
} else {
|
|
[self.cachedImageView stopAnimating];
|
|
}
|
|
}
|
|
|
|
- (void)clearCachedMediaViewsIfLastPresentingCell:(id)cell
|
|
{
|
|
OWSAssert(cell);
|
|
|
|
if (cell == self.lastPresentingCell) {
|
|
[self clearCachedMediaViews];
|
|
}
|
|
}
|
|
|
|
#pragma mark - JSQMessageMediaData protocol
|
|
|
|
- (UIView *)mediaView {
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
if (self.cachedImageView == nil) {
|
|
// Use Flipboard FLAnimatedImage library to display gifs
|
|
NSData *fileData = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
|
|
if (!fileData) {
|
|
DDLogError(@"%@ Could not load image: %@", [self tag], [self.attachment mediaURL]);
|
|
UIView *view = [UIView new];
|
|
view.backgroundColor = [UIColor colorWithWhite:0.85f alpha:1.f];
|
|
return view;
|
|
}
|
|
if (![fileData ows_isValidImage]) {
|
|
return nil;
|
|
}
|
|
FLAnimatedImage *animatedGif = [FLAnimatedImage animatedImageWithGIFData:fileData];
|
|
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
|
|
imageView.animatedImage = animatedGif;
|
|
CGSize size = [self mediaViewDisplaySize];
|
|
imageView.contentMode = UIViewContentModeScaleAspectFill;
|
|
imageView.frame = CGRectMake(0.0, 0.0, size.width, size.height);
|
|
imageView.clipsToBounds = YES;
|
|
[JSQMessagesMediaViewBubbleImageMasker applyBubbleImageMaskToMediaView:imageView
|
|
isOutgoing:self.appliesMediaViewMaskAsOutgoing];
|
|
self.cachedImageView = imageView;
|
|
|
|
if (!self.incoming) {
|
|
self.attachmentUploadView = [[AttachmentUploadView alloc] initWithAttachment:self.attachment
|
|
superview:imageView
|
|
attachmentStateCallback:nil];
|
|
}
|
|
}
|
|
|
|
return self.cachedImageView;
|
|
}
|
|
|
|
- (CGSize)mediaViewDisplaySize {
|
|
return [self ows_adjustBubbleSize:[super mediaViewDisplaySize] forImageSize:self.imageSize];
|
|
}
|
|
|
|
#pragma mark - OWSMessageEditing Protocol
|
|
|
|
- (BOOL)canPerformEditingAction:(SEL)action
|
|
{
|
|
return (action == @selector(copy:) || action == NSSelectorFromString(@"save:"));
|
|
}
|
|
|
|
- (void)performEditingAction:(SEL)action
|
|
{
|
|
if (action == @selector(copy:)) {
|
|
NSString *utiType = [MIMETypeUtil utiTypeForMIMEType:self.attachment.contentType];
|
|
if (!utiType) {
|
|
OWSFail(@"%@ Unknown MIME type: %@", self.tag, self.attachment.contentType);
|
|
utiType = (NSString *)kUTTypeGIF;
|
|
}
|
|
|
|
NSData *data = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
|
|
if (!data) {
|
|
OWSFail(@"%@ Could not load image data: %@", [self tag], [self.attachment mediaURL]);
|
|
return;
|
|
}
|
|
[UIPasteboard.generalPasteboard setData:data forPasteboardType:utiType];
|
|
} else if (action == NSSelectorFromString(@"save:")) {
|
|
NSData *data = [NSData dataWithContentsOfURL:[self.attachment mediaURL]];
|
|
if (!data) {
|
|
OWSFail(@"%@ Could not load image data: %@", [self tag], [self.attachment mediaURL]);
|
|
return;
|
|
}
|
|
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
|
|
[library writeImageDataToSavedPhotosAlbum:data
|
|
metadata:nil
|
|
completionBlock:^(NSURL *assetURL, NSError *error) {
|
|
if (error) {
|
|
DDLogWarn(@"Error Saving image to photo album: %@", error);
|
|
}
|
|
}];
|
|
} else {
|
|
// Shouldn't get here, as only supported actions should be exposed via canPerformEditingAction
|
|
NSString *actionString = NSStringFromSelector(action);
|
|
DDLogError(@"'%@' action unsupported for %@: attachmentId=%@", actionString, [self class], self.attachmentId);
|
|
}
|
|
}
|
|
|
|
#pragma mark - Logging
|
|
|
|
+ (NSString *)tag
|
|
{
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
}
|
|
|
|
- (NSString *)tag
|
|
{
|
|
return self.class.tag;
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|