mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.2 KiB
Matlab
149 lines
4.2 KiB
Matlab
8 years ago
|
//
|
||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "AttachmentUploadView.h"
|
||
|
#import "OWSProgressView.h"
|
||
|
#import "OWSUploadingService.h"
|
||
|
#import "TSAttachmentStream.h"
|
||
|
|
||
8 years ago
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
8 years ago
|
@interface AttachmentUploadView ()
|
||
|
|
||
|
@property (nonatomic) TSAttachmentStream *attachment;
|
||
|
|
||
|
@property (nonatomic) OWSProgressView *progressView;
|
||
|
|
||
|
@property (nonatomic) CALayer *maskLayer;
|
||
|
|
||
8 years ago
|
@property (nonatomic) AttachmentStateBlock _Nullable attachmentStateCallback;
|
||
8 years ago
|
|
||
|
@property (nonatomic) BOOL isAttachmentReady;
|
||
|
|
||
8 years ago
|
@property (nonatomic) CGFloat lastProgress;
|
||
|
|
||
8 years ago
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
|
@implementation AttachmentUploadView
|
||
|
|
||
|
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachment
|
||
|
superview:(UIView *)superview
|
||
8 years ago
|
attachmentStateCallback:(AttachmentStateBlock _Nullable)attachmentStateCallback
|
||
8 years ago
|
{
|
||
|
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);
|
||
8 years ago
|
const CGFloat progressHeight = round(MIN(superview.frame.size.height * 0.5f, progressWidth * 0.09f));
|
||
8 years ago
|
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
|
||
|
{
|
||
8 years ago
|
[_maskLayer removeFromSuperlayer];
|
||
|
[_progressView removeFromSuperview];
|
||
|
|
||
8 years ago
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||
|
}
|
||
|
|
||
|
- (void)setIsAttachmentReady:(BOOL)isAttachmentReady
|
||
|
{
|
||
|
if (_isAttachmentReady == isAttachmentReady) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
_isAttachmentReady = isAttachmentReady;
|
||
|
|
||
|
[self ensureViewState];
|
||
|
|
||
|
if (self.attachmentStateCallback) {
|
||
|
self.attachmentStateCallback(isAttachmentReady);
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
- (void)setLastProgress:(CGFloat)lastProgress
|
||
|
{
|
||
|
_lastProgress = lastProgress;
|
||
|
|
||
|
[self ensureViewState];
|
||
|
}
|
||
|
|
||
8 years ago
|
- (void)ensureViewState
|
||
|
{
|
||
8 years ago
|
_maskLayer.hidden = self.isAttachmentReady || self.lastProgress == 0;
|
||
|
_progressView.hidden = self.isAttachmentReady || self.lastProgress == 0;
|
||
8 years ago
|
}
|
||
|
|
||
|
- (void)attachmentUploadProgress:(NSNotification *)notification
|
||
|
{
|
||
|
NSDictionary *userinfo = [notification userInfo];
|
||
|
double progress = [[userinfo objectForKey:kAttachmentUploadProgressKey] doubleValue];
|
||
|
NSString *attachmentID = [userinfo objectForKey:kAttachmentUploadAttachmentIDKey];
|
||
8 years ago
|
if ([self.attachment.uniqueId isEqual:attachmentID]) {
|
||
8 years ago
|
if (!isnan(progress)) {
|
||
8 years ago
|
[_progressView setProgress:progress];
|
||
8 years ago
|
self.lastProgress = progress;
|
||
8 years ago
|
self.isAttachmentReady = self.attachment.isUploaded;
|
||
|
} else {
|
||
8 years ago
|
OWSFail(@"%@ Invalid attachment progress.", self.tag);
|
||
8 years ago
|
self.isAttachmentReady = YES;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
#pragma mark - Logging
|
||
|
|
||
|
+ (NSString *)tag
|
||
|
{
|
||
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
||
|
}
|
||
|
|
||
|
- (NSString *)tag
|
||
|
{
|
||
|
return self.class.tag;
|
||
|
}
|
||
|
|
||
8 years ago
|
@end
|
||
8 years ago
|
|
||
|
NS_ASSUME_NONNULL_END
|