From b1624d68182a2e0cad2735aefaa7cf92c47030ce Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 12 Oct 2017 13:48:09 -0400 Subject: [PATCH] Respond to CR. // FREEBIE --- Signal/src/UIView+OWS.m | 26 ++++++++-------- .../src/ViewControllers/AttachmentSharing.m | 18 +++++------ .../Cells/AttachmentUploadView.h | 2 +- .../ConversationView/Cells/OWSMessageCell.m | 19 ------------ .../ConversationView/ConversationViewItem.h | 2 ++ .../ConversationView/ConversationViewItem.m | 29 ++++++++++++++++++ Signal/src/views/AttachmentPointerView.swift | 30 +++++++++++++++++++ Signal/src/views/OWSProgressView.m | 12 ++++++++ 8 files changed, 96 insertions(+), 42 deletions(-) diff --git a/Signal/src/UIView+OWS.m b/Signal/src/UIView+OWS.m index 7c2e238fa..81afc13dd 100644 --- a/Signal/src/UIView+OWS.m +++ b/Signal/src/UIView+OWS.m @@ -2,8 +2,8 @@ // Copyright (c) 2017 Open Whisper Systems. All rights reserved. // -#import "OWSMath.h" #import "UIView+OWS.h" +#import "OWSMath.h" NS_ASSUME_NONNULL_BEGIN @@ -36,8 +36,8 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) - (NSArray *)autoPinWidthToSuperviewWithMargin:(CGFloat)margin { NSArray *result = @[ - [self autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.superview withOffset:+margin], - [self autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.superview withOffset:-margin], + [self autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:margin], + [self autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:margin], ]; return result; } @@ -45,8 +45,8 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) - (NSArray *)autoPinWidthToSuperview { NSArray *result = @[ - [self autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.superview], - [self autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.superview], + [self autoPinEdgeToSuperviewEdge:ALEdgeLeft], + [self autoPinEdgeToSuperviewEdge:ALEdgeRight], ]; return result; } @@ -63,8 +63,8 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) - (NSArray *)autoPinHeightToSuperviewWithMargin:(CGFloat)margin { NSArray *result = @[ - [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.superview withOffset:+margin], - [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.superview withOffset:-margin], + [self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:margin], + [self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:margin], ]; return result; } @@ -72,8 +72,8 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) - (NSArray *)autoPinHeightToSuperview { NSArray *result = @[ - [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.superview], - [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.superview], + [self autoPinEdgeToSuperviewEdge:ALEdgeTop], + [self autoPinEdgeToSuperviewEdge:ALEdgeBottom], ]; return result; } @@ -81,10 +81,10 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) - (NSArray *)autoPinToSuperviewEdges { NSArray *result = @[ - [self autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.superview], - [self autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.superview], - [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.superview], - [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.superview], + [self autoPinEdgeToSuperviewEdge:ALEdgeLeft ], + [self autoPinEdgeToSuperviewEdge:ALEdgeRight ], + [self autoPinEdgeToSuperviewEdge:ALEdgeTop ], + [self autoPinEdgeToSuperviewEdge:ALEdgeBottom ], ]; return result; } diff --git a/Signal/src/ViewControllers/AttachmentSharing.m b/Signal/src/ViewControllers/AttachmentSharing.m index 7dcba4ba3..60f294344 100644 --- a/Signal/src/ViewControllers/AttachmentSharing.m +++ b/Signal/src/ViewControllers/AttachmentSharing.m @@ -23,6 +23,15 @@ ]]; } ++ (void)showShareUIForText:(NSString *)text +{ + OWSAssert(text); + + [AttachmentSharing showShareUIForActivityItems:@[ + text, + ]]; +} + + (void)showShareUIForActivityItems:(NSArray *)activityItems { OWSAssert(activityItems); @@ -63,15 +72,6 @@ }); } -+ (void)showShareUIForText:(NSString *)text -{ - OWSAssert(text); - - [AttachmentSharing showShareUIForActivityItems:@[ - text, - ]]; -} - #pragma mark - Logging + (NSString *)tag diff --git a/Signal/src/ViewControllers/ConversationView/Cells/AttachmentUploadView.h b/Signal/src/ViewControllers/ConversationView/Cells/AttachmentUploadView.h index bc669f74d..3d08c5f78 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/AttachmentUploadView.h +++ b/Signal/src/ViewControllers/ConversationView/Cells/AttachmentUploadView.h @@ -8,7 +8,7 @@ NS_ASSUME_NONNULL_BEGIN typedef void (^AttachmentStateBlock)(BOOL isAttachmentReady); -// This entity is used by display download progress for incoming +// This entity is used to display download progress for incoming // attachments in conversation view cells. // // During attachment uploads we want to: diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m index b8cbba012..fabdcb066 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m @@ -292,10 +292,6 @@ NS_ASSUME_NONNULL_BEGIN = (self.isIncoming ? [UIColor jsq_messageBubbleLightGrayColor] : [UIColor ows_fadedBlueColor]); break; case TSAttachmentPointerStateDownloading: - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(attachmentDownloadProgress:) - name:kAttachmentDownloadProgressNotification - object:nil]; self.customView.backgroundColor = (self.isIncoming ? [UIColor jsq_messageBubbleLightGrayColor] : [UIColor ows_fadedBlueColor]); break; @@ -491,7 +487,6 @@ NS_ASSUME_NONNULL_BEGIN { [super prepareForReuse]; - [[NSNotificationCenter defaultCenter] removeObserver:self]; [NSLayoutConstraint deactivateConstraints:self.contentConstraints]; self.contentConstraints = nil; @@ -575,20 +570,6 @@ NS_ASSUME_NONNULL_BEGIN // [self.expirationTimerView stopTimer]; //} -#pragma mark - Notifications: - -// TODO: Move this logic into AttachmentPointerView. -- (void)attachmentDownloadProgress:(NSNotification *)notification -{ - NSNumber *progress = notification.userInfo[kAttachmentDownloadProgressKey]; - NSString *attachmentId = notification.userInfo[kAttachmentDownloadAttachmentIDKey]; - if (!self.attachmentPointer || ![self.attachmentPointer.uniqueId isEqualToString:attachmentId]) { - OWSFail(@"%@ Unexpected attachment progress notification: %@", self.logTag, attachmentId); - return; - } - self.attachmentPointerView.progress = progress.floatValue; -} - #pragma mark - Gesture recognizers - (void)handleTapGesture:(UITapGestureRecognizer *)sender diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h index 1aa88c595..9f2c45aca 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.h @@ -20,6 +20,8 @@ typedef NS_ENUM(NSInteger, OWSMessageCellType) { OWSMessageCellType_Unknown = OWSMessageCellType_TextMessage, }; +NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType); + @class ConversationViewCell; @class OWSAudioMessageView; @class TSAttachmentPointer; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index 9b6b365ce..3592fa02f 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -15,6 +15,28 @@ NS_ASSUME_NONNULL_BEGIN +NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) +{ + switch (cellType) { + case OWSMessageCellType_TextMessage: + return @"OWSMessageCellType_TextMessage"; + case OWSMessageCellType_OversizeTextMessage: + return @"OWSMessageCellType_OversizeTextMessage"; + case OWSMessageCellType_StillImage: + return @"OWSMessageCellType_StillImage"; + case OWSMessageCellType_AnimatedImage: + return @"OWSMessageCellType_AnimatedImage"; + case OWSMessageCellType_Audio: + return @"OWSMessageCellType_Audio"; + case OWSMessageCellType_Video: + return @"OWSMessageCellType_Video"; + case OWSMessageCellType_GenericAttachment: + return @"OWSMessageCellType_GenericAttachment"; + case OWSMessageCellType_DownloadingAttachment: + return @"OWSMessageCellType_DownloadingAttachment"; + } +} + @interface ConversationViewItem () @property (nonatomic, nullable) NSValue *cachedCellSize; @@ -58,6 +80,13 @@ NS_ASSUME_NONNULL_BEGIN _interaction = interaction; + self.hasViewState = NO; + self.messageCellType = OWSMessageCellType_Unknown; + self.textMessage = nil; + self.attachmentStream = nil; + self.attachmentPointer = nil; + self.contentSize = CGSizeZero; + [self clearCachedLayoutState]; } diff --git a/Signal/src/views/AttachmentPointerView.swift b/Signal/src/views/AttachmentPointerView.swift index 14366f444..0c6a92116 100644 --- a/Signal/src/views/AttachmentPointerView.swift +++ b/Signal/src/views/AttachmentPointerView.swift @@ -37,6 +37,36 @@ class AttachmentPointerView: UIView { createSubviews() updateViews() + + if attachmentPointer.state == .downloading { + NotificationCenter.default.addObserver(self, + selector:#selector(attachmentDownloadProgress(_:)), + name:NSNotification.Name.attachmentDownloadProgress, + object:nil) + } + } + + deinit { + NotificationCenter.default.removeObserver(self) + } + + internal func attachmentDownloadProgress(_ notification: Notification) { + guard let attachmentId = attachmentPointer.uniqueId else { + owsFail("Missing attachment id.") + return + } + guard let progress = (notification as NSNotification).userInfo?[kAttachmentDownloadProgressKey] as? NSNumber else { + owsFail("Attachment download notification missing progress.") + return + } + guard let notificationAttachmentId = (notification as NSNotification).userInfo?[kAttachmentDownloadAttachmentIDKey] as? String else { + owsFail("Attachment download notification missing attachment id.") + return + } + guard notificationAttachmentId == attachmentId else { + return + } + self.progress = CGFloat(progress.floatValue) } @available(*, unavailable) diff --git a/Signal/src/views/OWSProgressView.m b/Signal/src/views/OWSProgressView.m index 554f2e4c1..a47e50831 100644 --- a/Signal/src/views/OWSProgressView.m +++ b/Signal/src/views/OWSProgressView.m @@ -119,6 +119,18 @@ NS_ASSUME_NONNULL_BEGIN return CGSizeMake(UIViewNoIntrinsicMetric, 16); } +#pragma mark - Logging + ++ (NSString *)logTag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)logTag +{ + return self.class.logTag; +} + @end NS_ASSUME_NONNULL_END