From c02d633272652389a9d42a28d2e631af5f5797a9 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 25 Jan 2019 16:43:16 -0500 Subject: [PATCH] Align draft view of link preview and draft view of quoted reply. --- .../Cells/OWSQuotedMessageView.m | 2 +- .../ConversationInputToolbar.m | 33 ++++++++---- Signal/src/views/LinkPreviewView.swift | 32 ++++++++++-- Signal/src/views/QuotedReplyPreview.swift | 51 +++++++++++++------ SignalMessaging/categories/UIView+OWS.m | 3 +- 5 files changed, 89 insertions(+), 32 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m index fe9798308..c3637e18b 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m @@ -130,7 +130,7 @@ const CGFloat kRemotelySourcedContentRowSpacing = 3; - (CGFloat)bubbleHMargin { - return 6.f; + return (self.isForPreview ? 0.f : 6.f); } - (CGFloat)hSpacing diff --git a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m index f4795b9ab..64a9d9b31 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m @@ -73,6 +73,7 @@ const CGFloat kMaxTextViewHeight = 98; @property (nonatomic) UIEdgeInsets receivedSafeAreaInsets; @property (nonatomic, nullable) InputLinkPreview *inputLinkPreview; @property (nonatomic) BOOL wasLinkPreviewCancelled; +@property (nonatomic, nullable, weak) LinkPreviewView *linkPreviewView; @end @@ -303,9 +304,11 @@ const CGFloat kMaxTextViewHeight = 98; [quotedMessagePreview setCompressionResistanceHorizontalLow]; self.quotedReplyWrapper.hidden = NO; - self.quotedReplyWrapper.layoutMargins = UIEdgeInsetsMake(self.quotedMessageTopMargin, 0, 0, 0); + self.quotedReplyWrapper.layoutMargins = UIEdgeInsetsZero; [self.quotedReplyWrapper addSubview:quotedMessagePreview]; [quotedMessagePreview ows_autoPinToSuperviewMargins]; + + self.linkPreviewView.hasAsymmetricalRounding = !self.quotedReply; } - (CGFloat)quotedMessageTopMargin @@ -716,31 +719,27 @@ const CGFloat kMaxTextViewHeight = 98; OWSAssertIsOnMainThread(); if (self.wasLinkPreviewCancelled) { - self.inputLinkPreview = nil; - [self clearLinkPreviewView]; + [self clearLinkPreviewStateAndView]; return; } NSString *body = [[self messageText] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (body.length < 1) { - self.inputLinkPreview = nil; - [self clearLinkPreviewView]; + [self clearLinkPreviewStateAndView]; self.wasLinkPreviewCancelled = NO; return; } // Don't include link previews for oversize text messages. if ([body lengthOfBytesUsingEncoding:NSUTF8StringEncoding] >= kOversizeTextMessageSizeThreshold) { - self.inputLinkPreview = nil; - [self clearLinkPreviewView]; + [self clearLinkPreviewStateAndView]; return; } NSString *_Nullable previewUrl = [OWSLinkPreview previewUrlForMessageBodyText:body]; if (previewUrl.length < 1) { - self.inputLinkPreview = nil; - [self clearLinkPreviewView]; + [self clearLinkPreviewStateAndView]; return; } @@ -784,12 +783,24 @@ const CGFloat kMaxTextViewHeight = 98; LinkPreviewView *linkPreviewView = [[LinkPreviewView alloc] initWithDraftDelegate:self]; linkPreviewView.state = state; + linkPreviewView.hasAsymmetricalRounding = !self.quotedReply; + self.linkPreviewView = linkPreviewView; self.linkPreviewWrapper.hidden = NO; [self.linkPreviewWrapper addSubview:linkPreviewView]; [linkPreviewView ows_autoPinToSuperviewMargins]; } +- (void)clearLinkPreviewStateAndView +{ + OWSAssertIsOnMainThread(); + + self.inputLinkPreview = nil; + self.linkPreviewView = nil; + + [self clearLinkPreviewView]; +} + - (void)clearLinkPreviewView { OWSAssertIsOnMainThread(); @@ -829,8 +840,8 @@ const CGFloat kMaxTextViewHeight = 98; self.wasLinkPreviewCancelled = YES; - self.self.inputLinkPreview = nil; - [self clearLinkPreviewView]; + self.inputLinkPreview = nil; + [self clearLinkPreviewStateAndView]; } @end diff --git a/Signal/src/views/LinkPreviewView.swift b/Signal/src/views/LinkPreviewView.swift index 2f1448ca0..fe7a80034 100644 --- a/Signal/src/views/LinkPreviewView.swift +++ b/Signal/src/views/LinkPreviewView.swift @@ -231,14 +231,20 @@ public protocol LinkPreviewViewDraftDelegate { public class LinkPreviewImageView: UIImageView { private let maskLayer = CAShapeLayer() + private let hasAsymmetricalRounding: Bool + @objc - public init() { + public init(hasAsymmetricalRounding: Bool) { + self.hasAsymmetricalRounding = hasAsymmetricalRounding + super.init(frame: .zero) self.layer.mask = maskLayer } public required init?(coder aDecoder: NSCoder) { + self.hasAsymmetricalRounding = false + super.init(coder: aDecoder) } @@ -273,8 +279,15 @@ public class LinkPreviewImageView: UIImageView { let bigRounding: CGFloat = 14 let smallRounding: CGFloat = 4 - let upperLeftRounding = CurrentAppContext().isRTL ? smallRounding : bigRounding - let upperRightRounding = CurrentAppContext().isRTL ? bigRounding : smallRounding + let upperLeftRounding: CGFloat + let upperRightRounding: CGFloat + if hasAsymmetricalRounding { + upperLeftRounding = CurrentAppContext().isRTL ? smallRounding : bigRounding + upperRightRounding = CurrentAppContext().isRTL ? bigRounding : smallRounding + } else { + upperLeftRounding = smallRounding + upperRightRounding = smallRounding + } let lowerRightRounding = smallRounding let lowerLeftRounding = smallRounding @@ -325,6 +338,17 @@ public class LinkPreviewView: UIStackView { } } + @objc + public var hasAsymmetricalRounding: Bool = false { + didSet { + AssertIsOnMainThread() + + if hasAsymmetricalRounding != oldValue { + updateContents() + } + } + } + @available(*, unavailable, message:"use other constructor instead.") required init(coder aDecoder: NSCoder) { notImplemented() @@ -672,7 +696,7 @@ public class LinkPreviewView: UIStackView { owsFailDebug("Could not load image.") return nil } - let imageView = LinkPreviewImageView() + let imageView = LinkPreviewImageView(hasAsymmetricalRounding: self.hasAsymmetricalRounding) imageView.image = image return imageView } diff --git a/Signal/src/views/QuotedReplyPreview.swift b/Signal/src/views/QuotedReplyPreview.swift index c806b7b2a..7354f6d11 100644 --- a/Signal/src/views/QuotedReplyPreview.swift +++ b/Signal/src/views/QuotedReplyPreview.swift @@ -10,7 +10,7 @@ protocol QuotedReplyPreviewDelegate: class { } @objc -class QuotedReplyPreview: UIView { +class QuotedReplyPreview: UIStackView { @objc public weak var delegate: QuotedReplyPreviewDelegate? @@ -19,8 +19,13 @@ class QuotedReplyPreview: UIView { private var quotedMessageView: OWSQuotedMessageView? private var heightConstraint: NSLayoutConstraint! - @objc - required init?(coder aDecoder: NSCoder) { + @available(*, unavailable, message:"use other constructor instead.") + required init(coder aDecoder: NSCoder) { + notImplemented() + } + + @available(*, unavailable, message:"use other constructor instead.") + override init(frame: CGRect) { notImplemented() } @@ -38,6 +43,8 @@ class QuotedReplyPreview: UIView { NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: .UIContentSizeCategoryDidChange, object: nil) } + private let draftMarginTop: CGFloat = 6 + func updateContents() { subviews.forEach { $0.removeFromSuperview() } @@ -53,21 +60,35 @@ class QuotedReplyPreview: UIView { let cancelButton: UIButton = UIButton(type: .custom) - let buttonImage: UIImage = #imageLiteral(resourceName: "quoted-message-cancel").withRenderingMode(.alwaysTemplate) - cancelButton.setImage(buttonImage, for: .normal) + let cancelImage = UIImage(named: "compose-cancel")?.withRenderingMode(.alwaysTemplate) + cancelButton.setImage(cancelImage, for: .normal) cancelButton.imageView?.tintColor = Theme.secondaryColor cancelButton.addTarget(self, action: #selector(didTapCancel), for: .touchUpInside) + if let cancelSize = cancelImage?.size { + cancelButton.autoSetDimensions(to: cancelSize) + } - self.layoutMargins = .zero - - self.addSubview(quotedMessageView) - self.addSubview(cancelButton) - - quotedMessageView.autoPinEdges(toSuperviewMarginsExcludingEdge: .trailing) - cancelButton.autoPinEdges(toSuperviewMarginsExcludingEdge: .leading) - cancelButton.autoPinEdge(.leading, to: .trailing, of: quotedMessageView) - - cancelButton.autoSetDimensions(to: CGSize(width: 40, height: 40)) + self.axis = .horizontal + self.alignment = .fill + self.distribution = .fill + self.spacing = 8 + self.isLayoutMarginsRelativeArrangement = true + let hMarginLeading: CGFloat = 6 + let hMarginTrailing: CGFloat = 12 + self.layoutMargins = UIEdgeInsets(top: draftMarginTop, + left: CurrentAppContext().isRTL ? hMarginTrailing : hMarginLeading, + bottom: 0, + right: CurrentAppContext().isRTL ? hMarginLeading : hMarginTrailing) + + self.addArrangedSubview(quotedMessageView) + + let cancelStack = UIStackView() + cancelStack.axis = .horizontal + cancelStack.alignment = .top + cancelStack.setContentHuggingHigh() + cancelStack.setCompressionResistanceHigh() + cancelStack.addArrangedSubview(cancelButton) + self.addArrangedSubview(cancelStack) updateHeight() } diff --git a/SignalMessaging/categories/UIView+OWS.m b/SignalMessaging/categories/UIView+OWS.m index b0c0f6eb8..babbd3f9f 100644 --- a/SignalMessaging/categories/UIView+OWS.m +++ b/SignalMessaging/categories/UIView+OWS.m @@ -567,7 +567,8 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) UIView *borderView = [UIView new]; borderView.userInteractionEnabled = NO; - borderView.backgroundColor = nil; + borderView.backgroundColor = UIColor.clearColor; + borderView.opaque = NO; borderView.layer.borderColor = color.CGColor; borderView.layer.borderWidth = strokeWidth; borderView.layer.cornerRadius = cornerRadius;