2018-04-04 03:59:19 +02:00
|
|
|
//
|
2019-01-25 17:33:09 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2018-04-04 03:59:19 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc
|
|
|
|
protocol QuotedReplyPreviewDelegate: class {
|
|
|
|
func quotedReplyPreviewDidPressCancel(_ preview: QuotedReplyPreview)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
class QuotedReplyPreview: UIView {
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-04-04 03:59:19 +02:00
|
|
|
public weak var delegate: QuotedReplyPreviewDelegate?
|
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
private let quotedReply: OWSQuotedReplyModel
|
2018-06-28 19:28:14 +02:00
|
|
|
private let conversationStyle: ConversationStyle
|
2018-04-13 21:10:16 +02:00
|
|
|
private var quotedMessageView: OWSQuotedMessageView?
|
2018-04-10 00:13:44 +02:00
|
|
|
private var heightConstraint: NSLayoutConstraint!
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-04-04 03:59:19 +02:00
|
|
|
required init?(coder aDecoder: NSCoder) {
|
2018-08-27 16:21:03 +02:00
|
|
|
notImplemented()
|
2018-04-04 03:59:19 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 23:28:36 +02:00
|
|
|
@objc
|
2018-06-28 19:28:14 +02:00
|
|
|
init(quotedReply: OWSQuotedReplyModel, conversationStyle: ConversationStyle) {
|
2018-04-10 00:13:44 +02:00
|
|
|
self.quotedReply = quotedReply
|
2018-06-28 19:28:14 +02:00
|
|
|
self.conversationStyle = conversationStyle
|
2018-04-10 00:13:44 +02:00
|
|
|
|
2018-04-04 03:59:19 +02:00
|
|
|
super.init(frame: .zero)
|
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
self.heightConstraint = self.autoSetDimension(.height, toSize: 0)
|
2018-04-09 23:00:44 +02:00
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
updateContents()
|
2018-04-04 03:59:19 +02:00
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: .UIContentSizeCategoryDidChange, object: nil)
|
|
|
|
}
|
2018-04-04 03:59:19 +02:00
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
func updateContents() {
|
|
|
|
subviews.forEach { $0.removeFromSuperview() }
|
2018-04-04 03:59:19 +02:00
|
|
|
|
2018-04-13 21:10:16 +02:00
|
|
|
// We instantiate quotedMessageView late to ensure that it is updated
|
|
|
|
// every time contentSizeCategoryDidChange (i.e. when dynamic type
|
|
|
|
// sizes changes).
|
2018-06-28 19:28:14 +02:00
|
|
|
let quotedMessageView = OWSQuotedMessageView(forPreview: quotedReply, conversationStyle: conversationStyle)
|
2018-04-13 21:10:16 +02:00
|
|
|
self.quotedMessageView = quotedMessageView
|
2019-01-25 17:33:09 +01:00
|
|
|
quotedMessageView.setContentHuggingHorizontalLow()
|
|
|
|
quotedMessageView.setCompressionResistanceHorizontalLow()
|
2018-04-13 21:10:16 +02:00
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
quotedMessageView.backgroundColor = .clear
|
2018-04-04 03:59:19 +02:00
|
|
|
|
|
|
|
let cancelButton: UIButton = UIButton(type: .custom)
|
2018-04-10 00:13:44 +02:00
|
|
|
|
2018-04-04 03:59:19 +02:00
|
|
|
let buttonImage: UIImage = #imageLiteral(resourceName: "quoted-message-cancel").withRenderingMode(.alwaysTemplate)
|
|
|
|
cancelButton.setImage(buttonImage, for: .normal)
|
2018-08-08 16:17:31 +02:00
|
|
|
cancelButton.imageView?.tintColor = Theme.secondaryColor
|
2018-04-04 03:59:19 +02:00
|
|
|
cancelButton.addTarget(self, action: #selector(didTapCancel), for: .touchUpInside)
|
|
|
|
|
2018-04-09 23:15:56 +02:00
|
|
|
self.layoutMargins = .zero
|
|
|
|
|
|
|
|
self.addSubview(quotedMessageView)
|
|
|
|
self.addSubview(cancelButton)
|
|
|
|
|
|
|
|
quotedMessageView.autoPinEdges(toSuperviewMarginsExcludingEdge: .trailing)
|
|
|
|
cancelButton.autoPinEdges(toSuperviewMarginsExcludingEdge: .leading)
|
|
|
|
cancelButton.autoPinEdge(.leading, to: .trailing, of: quotedMessageView)
|
2018-04-04 03:59:19 +02:00
|
|
|
|
|
|
|
cancelButton.autoSetDimensions(to: CGSize(width: 40, height: 40))
|
2018-04-09 23:48:31 +02:00
|
|
|
|
2018-04-10 00:13:44 +02:00
|
|
|
updateHeight()
|
2018-04-04 03:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
2018-04-10 00:13:44 +02:00
|
|
|
|
2018-04-04 03:59:19 +02:00
|
|
|
@objc
|
|
|
|
func didTapCancel(_ sender: Any) {
|
|
|
|
self.delegate?.quotedReplyPreviewDidPressCancel(self)
|
|
|
|
}
|
2018-04-10 00:13:44 +02:00
|
|
|
|
|
|
|
// MARK: Sizing
|
|
|
|
|
|
|
|
func updateHeight() {
|
2018-04-13 21:10:16 +02:00
|
|
|
guard let quotedMessageView = quotedMessageView else {
|
2018-08-27 16:27:48 +02:00
|
|
|
owsFailDebug("missing quotedMessageView")
|
2018-04-13 21:10:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let size = quotedMessageView.size(forMaxWidth: CGFloat.infinity)
|
2018-04-10 00:13:44 +02:00
|
|
|
self.heightConstraint.constant = size.height
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:54:25 +02:00
|
|
|
@objc func contentSizeCategoryDidChange(_ notification: Notification) {
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.debug("")
|
2018-04-10 00:13:44 +02:00
|
|
|
|
|
|
|
updateContents()
|
|
|
|
}
|
2018-04-04 03:59:19 +02:00
|
|
|
}
|