session-ios/SignalMessaging/attachments/MessageApprovalViewControll...

229 lines
8.7 KiB
Swift
Raw Normal View History

2018-01-17 20:11:55 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@objc
public protocol MessageApprovalViewControllerDelegate: class {
func messageApproval(_ messageApproval: MessageApprovalViewController, didApproveMessage messageText: String)
func messageApprovalDidCancel(_ messageApproval: MessageApprovalViewController)
}
@objc
2018-01-17 20:41:36 +01:00
public class MessageApprovalViewController: OWSViewController, UITextViewDelegate {
2018-01-17 20:11:55 +01:00
let TAG = "[MessageApprovalViewController]"
weak var delegate: MessageApprovalViewControllerDelegate?
// MARK: Properties
2018-01-17 21:38:36 +01:00
let thread: TSThread
2018-01-17 20:11:55 +01:00
let initialMessageText: String
2018-01-17 21:38:36 +01:00
let contactsManager: OWSContactsManager
2018-01-17 20:11:55 +01:00
private(set) var textView: UITextView!
2018-01-17 20:41:36 +01:00
private(set) var topToolbar: UIToolbar!
2018-01-17 20:11:55 +01:00
// MARK: Initializers
@available(*, unavailable, message:"use attachment: constructor instead.")
required public init?(coder aDecoder: NSCoder) {
fatalError("unimplemented")
}
@objc
2018-01-17 21:38:36 +01:00
required public init(messageText: String, thread: TSThread, contactsManager: OWSContactsManager, delegate: MessageApprovalViewControllerDelegate) {
2018-01-17 20:11:55 +01:00
self.initialMessageText = messageText
2018-01-17 21:38:36 +01:00
self.thread = thread
self.contactsManager = contactsManager
2018-01-17 20:11:55 +01:00
self.delegate = delegate
super.init(nibName: nil, bundle: nil)
}
// MARK: View Lifecycle
override public func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = NSLocalizedString("MESSAGE_APPROVAL_DIALOG_TITLE",
comment: "Title for the 'message approval' dialog.")
}
2018-01-17 20:41:36 +01:00
private func updateToolbar() {
var items = [UIBarButtonItem]()
2018-01-17 20:11:55 +01:00
2018-01-17 20:41:36 +01:00
let cancelButton = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(cancelPressed))
items.append(cancelButton)
2018-01-17 20:11:55 +01:00
2018-01-17 20:41:36 +01:00
if textView.text.count > 0 {
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
items.append(spacer)
let sendButton = UIBarButtonItem(title: NSLocalizedString("SEND_BUTTON_TITLE",
comment:"Label for the send button in the conversation view."),
style:.plain,
target: self,
action: #selector(sendPressed))
items.append(sendButton)
}
topToolbar.items = items
}
2018-01-17 20:11:55 +01:00
// MARK: - Create Views
public override func loadView() {
2018-01-17 21:38:36 +01:00
self.view = UIView.container()
2018-01-17 20:41:36 +01:00
self.view.backgroundColor = UIColor.white
// Top Toolbar
topToolbar = UIToolbar()
2018-01-17 21:38:36 +01:00
topToolbar.backgroundColor = UIColor.ows_toolbarBackground
2018-01-17 20:41:36 +01:00
self.view.addSubview(topToolbar)
topToolbar.autoPinWidthToSuperview()
topToolbar.autoPin(toTopLayoutGuideOf: self, withInset: 0)
topToolbar.setContentHuggingVerticalHigh()
topToolbar.setCompressionResistanceVerticalHigh()
2018-01-17 21:38:36 +01:00
// Recipient Row
let recipientRow = createRecipientRow()
view.addSubview(recipientRow)
recipientRow.autoPinWidthToSuperview()
recipientRow.autoPinEdge(.top, to: .bottom, of: topToolbar)
2018-01-17 20:41:36 +01:00
// Text View
2018-01-17 20:11:55 +01:00
textView = UITextView()
2018-01-17 20:41:36 +01:00
textView.delegate = self
textView.backgroundColor = UIColor.white
textView.textColor = UIColor.black
textView.font = UIFont.ows_dynamicTypeBody()
textView.text = self.initialMessageText
2018-01-17 21:55:57 +01:00
textView.contentInset = UIEdgeInsets(top:0.0, left:0.0, bottom:0.0, right:0.0)
textView.textContainerInset = UIEdgeInsets(top:10.0, left:10.0, bottom:10.0, right:10.0)
2018-01-17 20:11:55 +01:00
view.addSubview(textView)
2018-01-17 20:41:36 +01:00
textView.autoPinWidthToSuperview()
2018-01-17 21:38:36 +01:00
textView.autoPinEdge(.top, to: .bottom, of: recipientRow)
2018-01-17 20:41:36 +01:00
textView.autoPin(toBottomLayoutGuideOf: self, withInset: 0)
2018-01-17 20:11:55 +01:00
2018-01-17 20:41:36 +01:00
updateToolbar()
2018-01-17 20:11:55 +01:00
}
2018-01-17 21:38:36 +01:00
private func createRecipientRow() -> UIView {
let recipientRow = UIView.container()
recipientRow.backgroundColor = UIColor.ows_toolbarBackground
// Hairline borders should be 1 pixel, not 1 point.
let borderThickness = 1.0 / UIScreen.main.scale
let borderColor = UIColor(white:135 / 255.0, alpha:1.0)
let topBorder = UIView.container()
topBorder.backgroundColor = borderColor
recipientRow.addSubview(topBorder)
topBorder.autoPinWidthToSuperview()
topBorder.autoPinTopToSuperview()
topBorder.autoSetDimension(.height, toSize: borderThickness)
let bottomBorder = UIView.container()
bottomBorder.backgroundColor = borderColor
recipientRow.addSubview(bottomBorder)
bottomBorder.autoPinWidthToSuperview()
bottomBorder.autoPinBottomToSuperview()
bottomBorder.autoSetDimension(.height, toSize: borderThickness)
2018-01-25 19:53:39 +01:00
let font = UIFont.ows_regularFont(withSize:ScaleFromIPhone5To7Plus(14.0, 18.0))
2018-01-17 21:38:36 +01:00
let hSpacing = CGFloat(10)
let hMargin = CGFloat(15)
2018-01-17 21:52:41 +01:00
let vSpacing = CGFloat(5)
let vMargin = CGFloat(10)
2018-01-17 21:38:36 +01:00
let toLabel = UILabel()
toLabel.text = NSLocalizedString("MESSAGE_APPROVAL_RECIPIENT_LABEL",
comment: "Label for the recipient name in the 'message approval' dialog.")
toLabel.textColor = UIColor.ows_darkGray
toLabel.font = font
recipientRow.addSubview(toLabel)
2018-01-17 21:52:41 +01:00
let nameLabel = UILabel()
nameLabel.textColor = UIColor.black
nameLabel.font = font
nameLabel.lineBreakMode = .byTruncatingTail
recipientRow.addSubview(nameLabel)
2018-01-17 21:38:36 +01:00
toLabel.autoPinLeadingToSuperview(withMargin: hMargin)
toLabel.setContentHuggingHorizontalHigh()
toLabel.setCompressionResistanceHorizontalHigh()
2018-01-17 21:52:41 +01:00
toLabel.autoAlignAxis(.horizontal, toSameAxisOf: nameLabel)
nameLabel.autoPinLeading(toTrailingOf: toLabel, margin:hSpacing)
nameLabel.autoPinTrailingToSuperview(withMargin: hMargin)
nameLabel.setContentHuggingHorizontalLow()
nameLabel.setCompressionResistanceHorizontalLow()
nameLabel.autoPinTopToSuperview(withMargin: vMargin)
2018-01-17 21:38:36 +01:00
if let groupThread = self.thread as? TSGroupThread {
let groupName = (groupThread.name().count > 0
? groupThread.name()
: MessageStrings.newGroupDefaultTitle)
nameLabel.text = groupName
2018-01-17 21:52:41 +01:00
nameLabel.autoPinBottomToSuperview(withMargin: vMargin)
2018-01-17 21:38:36 +01:00
return recipientRow
}
guard let contactThread = self.thread as? TSContactThread else {
owsFail("Unexpected thread type")
return recipientRow
}
2018-01-17 21:52:41 +01:00
nameLabel.attributedText = contactsManager.formattedFullName(forRecipientId:contactThread.contactIdentifier(), font:font)
nameLabel.textColor = UIColor.black
if let profileName = self.profileName(contactThread:contactThread) {
// If there's a profile name worth showing, add it as a second line below the name.
let profileNameLabel = UILabel()
profileNameLabel.textColor = UIColor.ows_darkGray
profileNameLabel.font = font
profileNameLabel.text = profileName
profileNameLabel.lineBreakMode = .byTruncatingTail
recipientRow.addSubview(profileNameLabel)
profileNameLabel.autoPinEdge(.top, to: .bottom, of: nameLabel, withOffset: vSpacing)
profileNameLabel.autoPinLeading(toTrailingOf: toLabel, margin:hSpacing)
profileNameLabel.autoPinTrailingToSuperview(withMargin: hMargin)
profileNameLabel.setContentHuggingHorizontalLow()
profileNameLabel.setCompressionResistanceHorizontalLow()
profileNameLabel.autoPinBottomToSuperview(withMargin: vMargin)
} else {
nameLabel.autoPinBottomToSuperview(withMargin: vMargin)
}
2018-01-17 21:38:36 +01:00
return recipientRow
}
2018-01-17 21:52:41 +01:00
private func profileName(contactThread: TSContactThread) -> String? {
let recipientId = contactThread.contactIdentifier()
if contactsManager.hasNameInSystemContacts(forRecipientId:recipientId) {
// Don't display profile name when we have a veritas name in system Contacts
return nil
}
return contactsManager.formattedProfileName(forRecipientId:recipientId)
}
2018-01-17 20:11:55 +01:00
// MARK: - Event Handlers
func cancelPressed(sender: UIButton) {
2018-01-17 20:41:36 +01:00
delegate?.messageApprovalDidCancel(self)
2018-01-17 20:11:55 +01:00
}
2018-01-17 20:41:36 +01:00
func sendPressed(sender: UIButton) {
delegate?.messageApproval(self, didApproveMessage: self.textView.text)
}
2018-01-17 20:11:55 +01:00
2018-01-17 20:41:36 +01:00
// MARK: - UITextViewDelegate
2018-01-17 20:11:55 +01:00
2018-01-17 20:41:36 +01:00
public func textViewDidChange(_ textView: UITextView) {
updateToolbar()
}
2018-01-17 20:11:55 +01:00
}