2017-03-15 14:23:21 +01:00
|
|
|
//
|
2019-03-15 17:34:33 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2017-03-15 14:23:21 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "AttachmentUploadView.h"
|
2017-10-23 23:23:41 +02:00
|
|
|
#import "OWSBezierPathView.h"
|
2017-03-15 14:23:21 +01:00
|
|
|
#import "OWSProgressView.h"
|
2018-07-09 22:15:46 +02:00
|
|
|
#import <SignalMessaging/UIFont+OWS.h>
|
2018-04-04 22:26:41 +02:00
|
|
|
#import <SignalMessaging/UIView+OWS.h>
|
2018-07-02 21:33:21 +02:00
|
|
|
#import <SignalServiceKit/AppContext.h>
|
2018-04-04 22:26:41 +02:00
|
|
|
#import <SignalServiceKit/OWSUploadOperation.h>
|
|
|
|
#import <SignalServiceKit/TSAttachmentStream.h>
|
2017-03-15 14:23:21 +01:00
|
|
|
|
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;
|
|
|
|
|
2017-10-23 23:23:41 +02:00
|
|
|
@property (nonatomic) OWSProgressView *progressView;
|
2017-03-15 14:23:21 +01:00
|
|
|
|
2018-07-09 22:15:46 +02:00
|
|
|
@property (nonatomic) UILabel *progressLabel;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(attachment);
|
2017-03-15 14:23:21 +01:00
|
|
|
|
|
|
|
self.attachment = attachment;
|
|
|
|
|
2018-07-09 22:15:46 +02:00
|
|
|
[self createContents];
|
2017-03-15 14:23:21 +01:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(attachmentUploadProgress:)
|
|
|
|
name:kAttachmentUploadProgressNotification
|
|
|
|
object:nil];
|
|
|
|
|
|
|
|
_isAttachmentReady = self.attachment.isUploaded;
|
|
|
|
|
|
|
|
[self ensureViewState];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2018-07-09 22:15:46 +02:00
|
|
|
- (void)createContents
|
|
|
|
{
|
|
|
|
// 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];
|
|
|
|
self.progressView.color = [UIColor whiteColor];
|
|
|
|
[self.progressView autoSetDimension:ALDimensionWidth toSize:80.f];
|
|
|
|
[self.progressView autoSetDimension:ALDimensionHeight toSize:6.f];
|
|
|
|
|
|
|
|
self.progressLabel = [UILabel new];
|
|
|
|
self.progressLabel.text = NSLocalizedString(
|
|
|
|
@"MESSAGE_METADATA_VIEW_MESSAGE_STATUS_UPLOADING", @"Status label for messages which are uploading.")
|
2018-07-10 18:09:31 +02:00
|
|
|
.localizedUppercaseString;
|
2018-07-09 22:15:46 +02:00
|
|
|
self.progressLabel.textColor = UIColor.whiteColor;
|
|
|
|
self.progressLabel.font = [UIFont ows_dynamicTypeCaption1Font];
|
|
|
|
self.progressLabel.textAlignment = NSTextAlignmentCenter;
|
|
|
|
|
|
|
|
UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[
|
|
|
|
self.progressView,
|
|
|
|
self.progressLabel,
|
|
|
|
]];
|
|
|
|
stackView.axis = UILayoutConstraintAxisVertical;
|
|
|
|
stackView.spacing = 4;
|
|
|
|
stackView.layoutMargins = UIEdgeInsetsMake(4, 4, 4, 4);
|
|
|
|
stackView.layoutMarginsRelativeArrangement = YES;
|
|
|
|
[self addSubview:stackView];
|
|
|
|
[stackView autoCenterInSuperview];
|
2018-07-10 15:58:45 +02:00
|
|
|
[stackView autoPinEdgeToSuperviewMargin:ALEdgeTop relation:NSLayoutRelationGreaterThanOrEqual];
|
|
|
|
[stackView autoPinEdgeToSuperviewMargin:ALEdgeBottom relation:NSLayoutRelationGreaterThanOrEqual];
|
|
|
|
[stackView autoPinEdgeToSuperviewMargin:ALEdgeLeading relation:NSLayoutRelationGreaterThanOrEqual];
|
|
|
|
[stackView autoPinEdgeToSuperviewMargin:ALEdgeTrailing relation:NSLayoutRelationGreaterThanOrEqual];
|
2018-07-09 22:15:46 +02:00
|
|
|
}
|
|
|
|
|
2017-03-15 14:23:21 +01:00
|
|
|
- (void)setIsAttachmentReady:(BOOL)isAttachmentReady
|
|
|
|
{
|
|
|
|
if (_isAttachmentReady == isAttachmentReady) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isAttachmentReady = isAttachmentReady;
|
|
|
|
|
|
|
|
[self ensureViewState];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-07-09 22:15:46 +02:00
|
|
|
BOOL isUploading = !self.isAttachmentReady && self.lastProgress != 0;
|
|
|
|
self.backgroundColor = (isUploading ? [UIColor colorWithWhite:0.f alpha:0.2f] : nil);
|
|
|
|
self.progressView.hidden = !isUploading;
|
|
|
|
self.progressLabel.hidden = !isUploading;
|
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)) {
|
2018-07-18 03:08:53 +02:00
|
|
|
[self.progressView setProgress:(CGFloat)progress];
|
|
|
|
self.lastProgress = (CGFloat)progress;
|
2017-03-15 14:23:21 +01:00
|
|
|
self.isAttachmentReady = self.attachment.isUploaded;
|
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Invalid attachment progress.");
|
2017-03-15 14:23:21 +01:00
|
|
|
self.isAttachmentReady = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2017-03-15 16:00:32 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|