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

116 lines
3.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
let initialMessageText: String
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
required public init(messageText: String, delegate: MessageApprovalViewControllerDelegate) {
self.initialMessageText = messageText
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() {
self.view = UIView()
2018-01-17 20:41:36 +01:00
self.view.backgroundColor = UIColor.white
// Top Toolbar
topToolbar = UIToolbar()
topToolbar.backgroundColor = UIColor.ows_inputToolbarBackground
self.view.addSubview(topToolbar)
topToolbar.autoPinWidthToSuperview()
topToolbar.autoPin(toTopLayoutGuideOf: self, withInset: 0)
topToolbar.setContentHuggingVerticalHigh()
topToolbar.setCompressionResistanceVerticalHigh()
// 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 20:11:55 +01:00
view.addSubview(textView)
2018-01-17 20:41:36 +01:00
textView.autoPinWidthToSuperview()
textView.autoPinEdge(.top, to: .bottom, of: topToolbar)
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
}
// 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
}