session-ios/SignalUtilitiesKit/Media Viewing & Editing/MessageApprovalViewControll...

211 lines
8.0 KiB
Swift
Raw Normal View History

2018-01-17 20:11:55 +01:00
//
2019-01-16 16:04:33 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2018-01-17 20:11:55 +01:00
//
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
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
private(set) var textView: UITextView!
private var sendButton: UIBarButtonItem!
2018-01-17 20:11:55 +01:00
// MARK: Initializers
@available(*, unavailable, message:"use attachment: constructor instead.")
required public init?(coder aDecoder: NSCoder) {
2018-08-27 16:21:03 +02:00
notImplemented()
2018-01-17 20:11:55 +01:00
}
@objc
2020-11-16 00:34:47 +01:00
required public init(messageText: String, thread: TSThread, delegate: MessageApprovalViewControllerDelegate) {
2018-01-17 20:11:55 +01:00
self.initialMessageText = messageText
2018-01-17 21:38:36 +01:00
self.thread = thread
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()
2018-01-17 20:11:55 +01:00
self.navigationItem.title = NSLocalizedString("MESSAGE_APPROVAL_DIALOG_TITLE",
comment: "Title for the 'message approval' dialog.")
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(cancelPressed))
2019-01-31 03:11:56 +01:00
sendButton = UIBarButtonItem(title: MessageStrings.sendButton,
style: .plain,
target: self,
action: #selector(sendPressed))
self.navigationItem.rightBarButtonItem = sendButton
}
2018-01-17 20:41:36 +01:00
private func updateSendButton() {
sendButton.isEnabled = textView.text.count > 0
2018-01-17 20:41:36 +01:00
}
2018-01-17 20:11:55 +01:00
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateSendButton()
}
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-08-08 17:07:05 +02:00
self.view.backgroundColor = Theme.backgroundColor
2018-01-17 20:41:36 +01:00
2018-01-17 21:38:36 +01:00
// Recipient Row
let recipientRow = createRecipientRow()
view.addSubview(recipientRow)
2019-01-16 16:04:33 +01:00
recipientRow.autoPinEdge(toSuperviewSafeArea: .leading)
recipientRow.autoPinEdge(toSuperviewSafeArea: .trailing)
2020-06-05 05:43:06 +02:00
recipientRow.autoPinEdge(.bottom, to: .bottom, of: view)
2018-01-17 21:38:36 +01:00
2018-01-17 20:41:36 +01:00
// Text View
2018-08-15 23:09:59 +02:00
textView = OWSTextView()
2018-01-17 20:41:36 +01:00
textView.delegate = self
2018-08-08 17:07:05 +02:00
textView.backgroundColor = Theme.backgroundColor
textView.textColor = Theme.primaryColor
2018-04-04 03:59:19 +02:00
textView.font = UIFont.ows_dynamicTypeBody
2018-01-17 20:41:36 +01:00
textView.text = self.initialMessageText
2018-04-02 21:34:10 +02: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)
2019-01-16 16:04:33 +01:00
textView.autoPinEdge(toSuperviewSafeArea: .leading)
textView.autoPinEdge(toSuperviewSafeArea: .trailing)
2018-01-17 21:38:36 +01:00
textView.autoPinEdge(.top, to: .bottom, of: recipientRow)
2020-06-05 05:43:06 +02:00
textView.autoPinEdge(.bottom, to: .bottom, of: view)
2018-01-17 20:11:55 +01:00
}
2018-01-17 21:38:36 +01:00
private func createRecipientRow() -> UIView {
let recipientRow = UIView.container()
2018-07-13 15:50:49 +02:00
recipientRow.backgroundColor = Theme.toolbarBackgroundColor
2018-01-17 21:38:36 +01:00
// Hairline borders should be 1 pixel, not 1 point.
let borderThickness = 1.0 / UIScreen.main.scale
2018-08-08 17:07:05 +02:00
let borderColor = Theme.middleGrayColor
2018-01-17 21:38:36 +01:00
let topBorder = UIView.container()
topBorder.backgroundColor = borderColor
recipientRow.addSubview(topBorder)
topBorder.autoPinWidthToSuperview()
topBorder.autoPinTopToSuperviewMargin()
2018-01-17 21:38:36 +01:00
topBorder.autoSetDimension(.height, toSize: borderThickness)
let bottomBorder = UIView.container()
bottomBorder.backgroundColor = borderColor
recipientRow.addSubview(bottomBorder)
bottomBorder.autoPinWidthToSuperview()
bottomBorder.autoPinBottomToSuperviewMargin()
2018-01-17 21:38:36 +01:00
bottomBorder.autoSetDimension(.height, toSize: borderThickness)
2018-04-02 21:34:10 +02: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.")
2018-08-08 17:07:05 +02:00
toLabel.textColor = Theme.secondaryColor
2018-01-17 21:38:36 +01:00
toLabel.font = font
recipientRow.addSubview(toLabel)
2018-01-17 21:52:41 +01:00
let nameLabel = UILabel()
2018-08-08 17:07:05 +02:00
nameLabel.textColor = Theme.primaryColor
2018-01-17 21:52:41 +01:00
nameLabel.font = font
nameLabel.lineBreakMode = .byTruncatingTail
recipientRow.addSubview(nameLabel)
toLabel.autoPinLeadingToSuperviewMargin(withInset: hMargin)
2018-01-17 21:38:36 +01:00
toLabel.setContentHuggingHorizontalHigh()
toLabel.setCompressionResistanceHorizontalHigh()
2018-01-17 21:52:41 +01:00
toLabel.autoAlignAxis(.horizontal, toSameAxisOf: nameLabel)
2018-04-02 21:34:10 +02:00
nameLabel.autoPinLeading(toTrailingEdgeOf: toLabel, offset: hSpacing)
nameLabel.autoPinTrailingToSuperviewMargin(withInset: hMargin)
2018-01-17 21:52:41 +01:00
nameLabel.setContentHuggingHorizontalLow()
nameLabel.setCompressionResistanceHorizontalLow()
nameLabel.autoPinTopToSuperviewMargin(withInset: vMargin)
2018-01-17 21:52:41 +01:00
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
nameLabel.autoPinBottomToSuperviewMargin(withInset: vMargin)
2018-01-17 21:38:36 +01:00
return recipientRow
}
guard let contactThread = self.thread as? TSContactThread else {
2018-08-27 16:27:48 +02:00
owsFailDebug("Unexpected thread type")
2018-01-17 21:38:36 +01:00
return recipientRow
}
2020-11-16 00:34:47 +01:00
let publicKey = thread.contactIdentifier()!
2021-02-26 05:56:41 +01:00
nameLabel.text = Storage.shared.getContact(with: publicKey)?.displayName(for: .regular) ?? publicKey
2018-08-08 17:07:05 +02:00
nameLabel.textColor = Theme.primaryColor
2018-01-17 21:52:41 +01:00
2018-04-02 21:34:10 +02:00
if let profileName = self.profileName(contactThread: contactThread) {
2018-01-17 21:52:41 +01:00
// If there's a profile name worth showing, add it as a second line below the name.
let profileNameLabel = UILabel()
2018-08-08 17:07:05 +02:00
profileNameLabel.textColor = Theme.secondaryColor
2018-01-17 21:52:41 +01:00
profileNameLabel.font = font
profileNameLabel.text = profileName
profileNameLabel.lineBreakMode = .byTruncatingTail
recipientRow.addSubview(profileNameLabel)
profileNameLabel.autoPinEdge(.top, to: .bottom, of: nameLabel, withOffset: vSpacing)
profileNameLabel.autoPinLeading(toTrailingEdgeOf: toLabel, offset: hSpacing)
profileNameLabel.autoPinTrailingToSuperviewMargin(withInset: hMargin)
2018-01-17 21:52:41 +01:00
profileNameLabel.setContentHuggingHorizontalLow()
profileNameLabel.setCompressionResistanceHorizontalLow()
profileNameLabel.autoPinBottomToSuperviewMargin(withInset: vMargin)
2018-01-17 21:52:41 +01:00
} else {
nameLabel.autoPinBottomToSuperviewMargin(withInset: vMargin)
2018-01-17 21:52:41 +01:00
}
2018-01-17 21:38:36 +01:00
return recipientRow
}
2018-01-17 21:52:41 +01:00
private func profileName(contactThread: TSContactThread) -> String? {
2020-11-16 00:34:47 +01:00
let publicKey = contactThread.contactIdentifier()
2021-02-26 05:56:41 +01:00
return Storage.shared.getContact(with: publicKey)?.displayName(for: .regular) ?? publicKey
2018-01-17 21:52:41 +01:00
}
2018-01-17 20:11:55 +01:00
// MARK: - Event Handlers
2018-05-25 18:54:25 +02:00
@objc func cancelPressed(sender: UIButton) {
2018-01-17 20:41:36 +01:00
delegate?.messageApprovalDidCancel(self)
2018-01-17 20:11:55 +01:00
}
2018-05-25 18:54:25 +02:00
@objc func sendPressed(sender: UIButton) {
2018-01-17 20:41:36 +01:00
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) {
updateSendButton()
2018-01-17 20:41:36 +01:00
}
2018-01-17 20:11:55 +01:00
}