Prepopulate caption field with URL.

This commit is contained in:
Matthew Chen 2018-01-16 16:24:11 -05:00
parent 9c4ce3d304
commit 085975ebe9
4 changed files with 41 additions and 14 deletions

View File

@ -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.")

View File

@ -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]()

View File

@ -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

View File

@ -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) {