From 085975ebe99fa80e888593b59b97ea4a873d3261 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 16 Jan 2018 16:24:11 -0500 Subject: [PATCH] Prepopulate caption field with URL. --- .../AttachmentApprovalViewController.swift | 28 +++++++++++++------ .../attachments/MediaMessageView.swift | 18 ++++++++++-- .../attachments/SignalAttachment.swift | 5 ++++ .../ShareViewController.swift | 4 +-- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/SignalMessaging/attachments/AttachmentApprovalViewController.swift b/SignalMessaging/attachments/AttachmentApprovalViewController.swift index 23e0fe0d6..abd0c9f5e 100644 --- a/SignalMessaging/attachments/AttachmentApprovalViewController.swift +++ b/SignalMessaging/attachments/AttachmentApprovalViewController.swift @@ -129,7 +129,9 @@ public class AttachmentApprovalViewController: OWSViewController, CaptioningTool scrollView.autoPinEdgesToSuperviewEdges() - let backgroundColor = UIColor.black + let defaultCaption = self.defaultCaption() + let isUrlShare = defaultCaption != nil + let backgroundColor = isUrlShare ? UIColor.ows_signalBrandBlue : UIColor.black self.view.backgroundColor = backgroundColor // Create full screen container view so the scrollView @@ -168,9 +170,7 @@ public class AttachmentApprovalViewController: OWSViewController, CaptioningTool topToolbar.items = [cancelButton] // Bottom Toolbar - // - // Don't add a caption to text messages. - let captioningToolbar = CaptioningToolbar(allowCaptions:!self.attachment.isOversizeText) + let captioningToolbar = CaptioningToolbar(defaultCaption:defaultCaption) captioningToolbar.captioningToolbarDelegate = self self.bottomToolbar = captioningToolbar @@ -236,6 +236,18 @@ public class AttachmentApprovalViewController: OWSViewController, CaptioningTool self.pauseVideo() } + private func defaultCaption() -> String? { + guard self.attachment.isUrl else { + return nil + } + let data = self.attachment.data + guard let messageText = String(data: data, encoding: String.Encoding.utf8) else { + Logger.error("\(self.logTag) Couldn't load url strubg") + return nil + } + return messageText + } + override public var inputAccessoryView: UIView? { self.bottomToolbar.layoutIfNeeded() return self.bottomToolbar @@ -559,7 +571,6 @@ class CaptioningToolbar: UIView, UITextViewDelegate { private let sendButton: UIButton private let textView: UITextView private let bottomGradient: GradientView - private let allowCaptions: Bool // Layout Constants var maxTextViewHeight: CGFloat { @@ -590,12 +601,11 @@ class CaptioningToolbar: UIView, UITextViewDelegate { } let kSendButtonShadowOffset: CGFloat = 1 - init(allowCaptions: Bool) { + init(defaultCaption: String?) { self.sendButton = UIButton(type: .system) self.bottomGradient = GradientView(from: UIColor.clear, to: UIColor.black) self.textView = MessageTextView() self.textViewHeight = kMinTextViewHeight - self.allowCaptions = allowCaptions super.init(frame: CGRect.zero) @@ -607,8 +617,8 @@ class CaptioningToolbar: UIView, UITextViewDelegate { textView.addBorder(with: UIColor.lightGray) textView.font = UIFont.ows_dynamicTypeBody() textView.returnKeyType = .done - if !allowCaptions { - textView.isHidden = true + if let defaultCaption = defaultCaption { + textView.text = defaultCaption } let sendTitle = NSLocalizedString("ATTACHMENT_APPROVAL_SEND_BUTTON", comment: "Label for 'send' button in the 'attachment approval' dialog.") diff --git a/SignalMessaging/attachments/MediaMessageView.swift b/SignalMessaging/attachments/MediaMessageView.swift index fcba7ce5e..0c515af70 100644 --- a/SignalMessaging/attachments/MediaMessageView.swift +++ b/SignalMessaging/attachments/MediaMessageView.swift @@ -112,6 +112,8 @@ public class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate { createAudioPreview() } else if attachment.isOversizeText { createTextPreview() + } else if attachment.isUrl { + createUrlPreview() } else { createGenericPreview() } @@ -306,7 +308,7 @@ public class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate { let messageTextView = UITextView() messageTextView.font = UIFont.ows_dynamicTypeBody() - messageTextView.backgroundColor = UIColor.clear + messageTextView.backgroundColor = UIColor.clear messageTextView.isOpaque = false messageTextView.isEditable = false messageTextView.isSelectable = false @@ -320,7 +322,7 @@ public class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate { messageTextView.linkTextAttributes = [NSForegroundColorAttributeName : textColor, NSUnderlineStyleAttributeName : [NSUnderlineStyle.styleSingle, NSUnderlineStyle.patternSolid] - ] + ] messageTextView.dataDetectorTypes = [.link, .address, .calendarEvent] messageTextView.text = messageText @@ -340,6 +342,18 @@ public class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate { messageTextView.autoPinTrailingToSuperview(withMargin:15) } + private func createUrlPreview() { + + let data = attachment.data + guard let messageText = String(data: data, encoding: String.Encoding.utf8) else { + createGenericPreview() + return + } + + // Show nothing; URLs should only appear in the attachment approval view + // of the SAE and in this context the URL will be placed in the caption field. + } + private func createGenericPreview() { var subviews = [UIView]() diff --git a/SignalMessaging/attachments/SignalAttachment.swift b/SignalMessaging/attachments/SignalAttachment.swift index 53a2942be..253a67cf4 100644 --- a/SignalMessaging/attachments/SignalAttachment.swift +++ b/SignalMessaging/attachments/SignalAttachment.swift @@ -419,6 +419,11 @@ public class SignalAttachment: NSObject { return dataUTI == kOversizeTextAttachmentUTI } + @objc + public var isUrl: Bool { + return dataUTI == (kUTTypeURL as String) + } + @objc public class func pasteboardHasPossibleAttachment() -> Bool { return UIPasteboard.general.numberOfItems > 0 diff --git a/SignalShareExtension/ShareViewController.swift b/SignalShareExtension/ShareViewController.swift index 442cde640..8a63eeec5 100644 --- a/SignalShareExtension/ShareViewController.swift +++ b/SignalShareExtension/ShareViewController.swift @@ -605,9 +605,7 @@ public class ShareViewController: UINavigationController, ShareViewDelegate, SAE // start with base utiType, but it might be something generic like "image" var specificUTIType = utiType if utiType == (kUTTypeURL as String) { - // Share URLs as oversize text messages whose text content is the URL. - Logger.debug("\(self.logTag) using text UTI type for URL.") - specificUTIType = kOversizeTextAttachmentUTI as String + // Use kUTTypeURL for URLs. } else if url.pathExtension.count > 0 { // Determine a more specific utiType based on file extension if let typeExtension = MIMETypeUtil.utiType(forFileExtension: url.pathExtension) {