2015-09-01 04:51:39 +02:00
|
|
|
//
|
2017-03-15 14:23:21 +01:00
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
2015-09-01 04:51:39 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "TSAnimatedAdapter.h"
|
2017-03-15 14:23:21 +01:00
|
|
|
#import "AttachmentUploadView.h"
|
2015-09-01 04:51:39 +02:00
|
|
|
#import "FLAnimatedImage.h"
|
2016-10-19 06:32:48 +02:00
|
|
|
#import "JSQMediaItem+OWS.h"
|
2017-03-15 14:23:21 +01:00
|
|
|
#import "TSAttachmentStream.h"
|
2016-03-11 19:34:39 +01:00
|
|
|
#import <AssetsLibrary/AssetsLibrary.h>
|
2016-07-13 20:03:50 +02:00
|
|
|
#import <JSQMessagesViewController/JSQMessagesMediaViewBubbleImageMasker.h>
|
2016-03-11 19:34:39 +01:00
|
|
|
#import <MobileCoreServices/MobileCoreServices.h>
|
2015-09-01 04:51:39 +02:00
|
|
|
|
|
|
|
@interface TSAnimatedAdapter ()
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
@property (nonatomic) UIImageView *cachedImageView;
|
|
|
|
@property (nonatomic) UIImage *image;
|
|
|
|
@property (nonatomic) TSAttachmentStream *attachment;
|
|
|
|
@property (nonatomic) AttachmentUploadView *attachmentUploadView;
|
|
|
|
@property (nonatomic) BOOL incoming;
|
2015-09-01 04:51:39 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TSAnimatedAdapter
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment incoming:(BOOL)incoming
|
|
|
|
{
|
2015-09-01 04:51:39 +02:00
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
_cachedImageView = nil;
|
|
|
|
_attachment = attachment;
|
|
|
|
_attachmentId = attachment.uniqueId;
|
|
|
|
_image = [attachment image];
|
|
|
|
_fileData = [NSData dataWithContentsOfURL:[attachment mediaURL]];
|
2017-03-15 14:23:21 +01:00
|
|
|
_incoming = incoming;
|
2015-09-01 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)dealloc {
|
2015-09-01 04:51:39 +02:00
|
|
|
_cachedImageView = nil;
|
2015-12-22 12:45:09 +01:00
|
|
|
_attachment = nil;
|
|
|
|
_attachmentId = nil;
|
|
|
|
_image = nil;
|
|
|
|
_fileData = nil;
|
2015-09-01 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)clearCachedMediaViews {
|
2015-09-01 04:51:39 +02:00
|
|
|
[super clearCachedMediaViews];
|
|
|
|
_cachedImageView = nil;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)setAppliesMediaViewMaskAsOutgoing:(BOOL)appliesMediaViewMaskAsOutgoing {
|
2015-09-01 04:51:39 +02:00
|
|
|
[super setAppliesMediaViewMaskAsOutgoing:appliesMediaViewMaskAsOutgoing];
|
|
|
|
_cachedImageView = nil;
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:39:46 +02:00
|
|
|
#pragma mark - NSObject
|
|
|
|
|
|
|
|
- (NSUInteger)hash
|
|
|
|
{
|
|
|
|
return super.hash ^ self.image.hash;
|
|
|
|
}
|
|
|
|
|
2015-09-01 04:51:39 +02:00
|
|
|
#pragma mark - JSQMessageMediaData protocol
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (UIView *)mediaView {
|
2015-09-01 04:51:39 +02:00
|
|
|
if (self.cachedImageView == nil) {
|
|
|
|
// Use Flipboard FLAnimatedImage library to display gifs
|
2015-12-22 12:45:09 +01:00
|
|
|
FLAnimatedImage *animatedGif = [FLAnimatedImage animatedImageWithGIFData:self.fileData];
|
2015-09-01 04:51:39 +02:00
|
|
|
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
|
2015-12-22 12:45:09 +01:00
|
|
|
imageView.animatedImage = animatedGif;
|
|
|
|
CGSize size = [self mediaViewDisplaySize];
|
|
|
|
imageView.frame = CGRectMake(0.0, 0.0, size.width, size.height);
|
|
|
|
imageView.contentMode = UIViewContentModeScaleAspectFill;
|
|
|
|
imageView.clipsToBounds = YES;
|
|
|
|
[JSQMessagesMediaViewBubbleImageMasker applyBubbleImageMaskToMediaView:imageView
|
|
|
|
isOutgoing:self.appliesMediaViewMaskAsOutgoing];
|
2015-09-01 04:51:39 +02:00
|
|
|
self.cachedImageView = imageView;
|
2017-03-15 14:23:21 +01:00
|
|
|
|
|
|
|
if (!self.incoming) {
|
|
|
|
self.attachmentUploadView = [[AttachmentUploadView alloc] initWithAttachment:self.attachment
|
|
|
|
superview:imageView
|
|
|
|
attachmentStateCallback:nil];
|
|
|
|
}
|
2015-09-01 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.cachedImageView;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (CGSize)mediaViewDisplaySize {
|
2016-10-19 06:32:48 +02:00
|
|
|
return [self ows_adjustBubbleSize:[super mediaViewDisplaySize] forImage:self.image];
|
2015-09-01 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:34:39 +01:00
|
|
|
#pragma mark - OWSMessageEditing Protocol
|
|
|
|
|
|
|
|
- (BOOL)canPerformEditingAction:(SEL)action
|
|
|
|
{
|
|
|
|
return (action == @selector(copy:) || action == NSSelectorFromString(@"save:"));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performEditingAction:(SEL)action
|
|
|
|
{
|
|
|
|
if (action == @selector(copy:)) {
|
|
|
|
UIPasteboard *pasteBoard = UIPasteboard.generalPasteboard;
|
|
|
|
[pasteBoard setData:self.fileData forPasteboardType:(__bridge NSString *)kUTTypeGIF];
|
|
|
|
} else if (action == NSSelectorFromString(@"save:")) {
|
|
|
|
NSData *photoData = self.fileData;
|
|
|
|
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
|
|
|
|
[library writeImageDataToSavedPhotosAlbum:photoData
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 04:51:39 +02:00
|
|
|
@end
|