2017-03-15 14:23:21 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "AttachmentUploadView.h"
|
|
|
|
#import "OWSProgressView.h"
|
|
|
|
#import "OWSUploadingService.h"
|
|
|
|
#import "TSAttachmentStream.h"
|
|
|
|
|
2017-03-15 16:00:32 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
@interface AttachmentUploadView ()
|
|
|
|
|
|
|
|
@property (nonatomic) TSAttachmentStream *attachment;
|
|
|
|
|
|
|
|
@property (nonatomic) OWSProgressView *progressView;
|
|
|
|
|
|
|
|
@property (nonatomic) CALayer *maskLayer;
|
|
|
|
|
2017-03-15 16:00:32 +01:00
|
|
|
@property (nonatomic) AttachmentStateBlock _Nullable attachmentStateCallback;
|
2017-03-15 14:23:21 +01:00
|
|
|
|
|
|
|
@property (nonatomic) BOOL isAttachmentReady;
|
|
|
|
|
2017-04-10 17:35:52 +02:00
|
|
|
@property (nonatomic) CGFloat lastProgress;
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
@implementation AttachmentUploadView
|
|
|
|
|
|
|
|
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment
|
|
|
|
superview:(UIView *)superview
|
2017-03-15 16:00:32 +01:00
|
|
|
attachmentStateCallback:(AttachmentStateBlock _Nullable)attachmentStateCallback
|
2017-03-15 14:23:21 +01:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
OWSAssert(attachment);
|
|
|
|
OWSAssert(superview);
|
|
|
|
|
|
|
|
self.attachment = attachment;
|
|
|
|
self.attachmentStateCallback = attachmentStateCallback;
|
|
|
|
|
|
|
|
_maskLayer = [CALayer layer];
|
|
|
|
[_maskLayer setBackgroundColor:[UIColor blackColor].CGColor];
|
|
|
|
[_maskLayer setOpacity:0.4f];
|
|
|
|
[_maskLayer setFrame:superview.frame];
|
|
|
|
[superview.layer addSublayer:_maskLayer];
|
|
|
|
|
|
|
|
const CGFloat progressWidth = round(superview.frame.size.width * 0.45f);
|
2017-03-25 16:39:13 +01:00
|
|
|
const CGFloat progressHeight = round(MIN(superview.frame.size.height * 0.5f,
|
|
|
|
progressWidth * 0.09f));
|
2017-03-15 14:23:21 +01:00
|
|
|
CGRect progressFrame = CGRectMake(round((superview.frame.size.width - progressWidth) * 0.5f),
|
|
|
|
round((superview.frame.size.height - progressHeight) * 0.5f),
|
|
|
|
progressWidth,
|
|
|
|
progressHeight);
|
|
|
|
// The progress view is white. It will only be shown
|
|
|
|
// while the mask layer is visible, so it will show up
|
|
|
|
// even against all-white attachments.
|
|
|
|
_progressView = [OWSProgressView new];
|
|
|
|
_progressView.color = [UIColor whiteColor];
|
|
|
|
_progressView.frame = progressFrame;
|
|
|
|
[superview addSubview:_progressView];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(attachmentUploadProgress:)
|
|
|
|
name:kAttachmentUploadProgressNotification
|
|
|
|
object:nil];
|
|
|
|
|
|
|
|
_isAttachmentReady = self.attachment.isUploaded;
|
|
|
|
|
|
|
|
[self ensureViewState];
|
|
|
|
|
|
|
|
if (attachmentStateCallback) {
|
|
|
|
self.attachmentStateCallback(_isAttachmentReady);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2017-03-28 21:29:08 +02:00
|
|
|
[_maskLayer removeFromSuperlayer];
|
|
|
|
[_progressView removeFromSuperview];
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setIsAttachmentReady:(BOOL)isAttachmentReady
|
|
|
|
{
|
|
|
|
if (_isAttachmentReady == isAttachmentReady) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isAttachmentReady = isAttachmentReady;
|
|
|
|
|
|
|
|
[self ensureViewState];
|
|
|
|
|
|
|
|
if (self.attachmentStateCallback) {
|
|
|
|
self.attachmentStateCallback(isAttachmentReady);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:35:52 +02:00
|
|
|
- (void)setLastProgress:(CGFloat)lastProgress
|
|
|
|
{
|
|
|
|
_lastProgress = lastProgress;
|
|
|
|
|
|
|
|
[self ensureViewState];
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
- (void)ensureViewState
|
|
|
|
{
|
2017-04-10 17:35:52 +02:00
|
|
|
_maskLayer.hidden = self.isAttachmentReady || self.lastProgress == 0;
|
|
|
|
_progressView.hidden = self.isAttachmentReady || self.lastProgress == 0;
|
2017-03-15 14:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)attachmentUploadProgress:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
NSDictionary *userinfo = [notification userInfo];
|
|
|
|
double progress = [[userinfo objectForKey:kAttachmentUploadProgressKey] doubleValue];
|
|
|
|
NSString *attachmentID = [userinfo objectForKey:kAttachmentUploadAttachmentIDKey];
|
2017-03-20 18:37:41 +01:00
|
|
|
if ([self.attachment.uniqueId isEqual:attachmentID]) {
|
2017-03-15 14:23:21 +01:00
|
|
|
if (!isnan(progress)) {
|
2017-03-15 16:05:10 +01:00
|
|
|
[_progressView setProgress:progress];
|
2017-04-10 17:35:52 +02:00
|
|
|
self.lastProgress = progress;
|
2017-03-15 14:23:21 +01:00
|
|
|
self.isAttachmentReady = self.attachment.isUploaded;
|
|
|
|
} else {
|
2017-09-08 21:34:07 +02:00
|
|
|
OWSFail(@"%@ Invalid attachment progress.", self.tag);
|
2017-03-15 14:23:21 +01:00
|
|
|
self.isAttachmentReady = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2017-03-15 16:00:32 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|