From 31b4612746ef2b1a99ffd4e60f9300122e2ec61b Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 19 Mar 2019 13:17:34 -0400 Subject: [PATCH] Rework attachment download progress for generic attachments. --- .../Cells/OWSAudioMessageView.m | 4 +- .../Cells/OWSGenericAttachmentView.h | 6 ++- .../Cells/OWSGenericAttachmentView.m | 48 ++++++++++++++++++- .../Cells/OWSMessageBubbleView.m | 9 ++-- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSAudioMessageView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSAudioMessageView.m index 3719e2322..6493dd419 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSAudioMessageView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSAudioMessageView.m @@ -136,8 +136,8 @@ NS_ASSUME_NONNULL_BEGIN switch (self.viewItem.attachmentPointer.state) { case TSAttachmentPointerStateFailed: - // TODO: - // [self addTapToRetryView:bodyMediaView]; + // We don't need to handle the "tap to retry" state here, + // only download progress. return; case TSAttachmentPointerStateEnqueued: case TSAttachmentPointerStateDownloading: diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.h b/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.h index 350b93465..a06a54494 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.h +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.h @@ -7,9 +7,13 @@ NS_ASSUME_NONNULL_BEGIN @class ConversationStyle; @class TSAttachment; +@protocol ConversationViewItem; + @interface OWSGenericAttachmentView : UIStackView -- (instancetype)initWithAttachment:(TSAttachment *)attachment isIncoming:(BOOL)isIncoming; +- (instancetype)initWithAttachment:(TSAttachment *)attachment + isIncoming:(BOOL)isIncoming + viewItem:(id)viewItem; - (void)createContentsWithConversationStyle:(ConversationStyle *)conversationStyle; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.m index b95fd220d..2508ced75 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSGenericAttachmentView.m @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) TSAttachment *attachment; @property (nonatomic, nullable) TSAttachmentStream *attachmentStream; +@property (nonatomic, weak) id viewItem; @property (nonatomic) BOOL isIncoming; @property (nonatomic) UILabel *topLabel; @property (nonatomic) UILabel *bottomLabel; @@ -30,7 +31,9 @@ NS_ASSUME_NONNULL_BEGIN @implementation OWSGenericAttachmentView -- (instancetype)initWithAttachment:(TSAttachment *)attachment isIncoming:(BOOL)isIncoming +- (instancetype)initWithAttachment:(TSAttachment *)attachment + isIncoming:(BOOL)isIncoming + viewItem:(id)viewItem { self = [super init]; @@ -40,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN _attachmentStream = (TSAttachmentStream *)attachment; } _isIncoming = isIncoming; + _viewItem = viewItem; } return self; @@ -130,6 +134,8 @@ NS_ASSUME_NONNULL_BEGIN [fileTypeLabel autoCenterInSuperview]; [fileTypeLabel autoSetDimension:ALDimensionWidth toSize:self.iconWidth - 20.f]; + [self replaceIconWithDownloadProgressIfNecessary:imageView]; + UIStackView *labelsView = [UIStackView new]; labelsView.axis = UILayoutConstraintAxisVertical; labelsView.spacing = [OWSGenericAttachmentView labelVSpacing]; @@ -175,6 +181,46 @@ NS_ASSUME_NONNULL_BEGIN [labelsView addArrangedSubview:bottomLabel]; } +- (void)replaceIconWithDownloadProgressIfNecessary:(UIView *)iconView +{ + if (!self.viewItem.attachmentPointer) { + return; + } + + switch (self.viewItem.attachmentPointer.state) { + case TSAttachmentPointerStateFailed: + // We don't need to handle the "tap to retry" state here, + // only download progress. + return; + case TSAttachmentPointerStateEnqueued: + case TSAttachmentPointerStateDownloading: + break; + } + switch (self.viewItem.attachmentPointer.pointerType) { + case TSAttachmentPointerTypeRestoring: + // TODO: Show "restoring" indicator and possibly progress. + return; + case TSAttachmentPointerTypeUnknown: + case TSAttachmentPointerTypeIncoming: + break; + } + NSString *_Nullable uniqueId = self.viewItem.attachmentPointer.uniqueId; + if (uniqueId.length < 1) { + OWSFailDebug(@"Missing uniqueId."); + return; + } + + CGSize iconViewSize = [iconView sizeThatFits:CGSizeZero]; + CGFloat downloadViewSize = MIN(iconViewSize.width, iconViewSize.height); + MediaDownloadView *downloadView = + [[MediaDownloadView alloc] initWithAttachmentId:uniqueId radius:downloadViewSize * 0.5f]; + iconView.layer.opacity = 0.01f; + [self addSubview:downloadView]; + [downloadView autoSetDimensionsToSize:CGSizeMake(downloadViewSize, downloadViewSize)]; + [downloadView autoAlignAxis:ALAxisHorizontal toSameAxisOfView:iconView]; + [downloadView autoAlignAxis:ALAxisVertical toSameAxisOfView:iconView]; +} + + (UIFont *)topLabelFont { return [UIFont ows_dynamicTypeBodyFont]; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m index a822fc7e2..d97b87c8f 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m @@ -854,8 +854,9 @@ NS_ASSUME_NONNULL_BEGIN { TSAttachment *attachment = (self.viewItem.attachmentStream ?: self.viewItem.attachmentPointer); OWSAssertDebug(attachment); - OWSGenericAttachmentView *attachmentView = - [[OWSGenericAttachmentView alloc] initWithAttachment:attachment isIncoming:self.isIncoming]; + OWSGenericAttachmentView *attachmentView = [[OWSGenericAttachmentView alloc] initWithAttachment:attachment + isIncoming:self.isIncoming + viewItem:self.viewItem]; [attachmentView createContentsWithConversationStyle:self.conversationStyle]; [self addProgressViewsIfNecessary:attachmentView shouldShowDownloadProgress:NO]; @@ -1065,7 +1066,9 @@ NS_ASSUME_NONNULL_BEGIN TSAttachment *attachment = (self.viewItem.attachmentStream ?: self.viewItem.attachmentPointer); OWSAssertDebug(attachment); OWSGenericAttachmentView *attachmentView = - [[OWSGenericAttachmentView alloc] initWithAttachment:attachment isIncoming:self.isIncoming]; + [[OWSGenericAttachmentView alloc] initWithAttachment:attachment + isIncoming:self.isIncoming + viewItem:self.viewItem]; [attachmentView createContentsWithConversationStyle:self.conversationStyle]; result = [attachmentView measureSizeWithMaxMessageWidth:maxMessageWidth]; break;