session-ios/Signal/src/views/QuotedReplyPreview.swift

97 lines
3.0 KiB
Swift
Raw Normal View History

2018-04-04 03:59:19 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
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?
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?
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) {
fatalError("init(coder:) has not been implemented")
}
2018-05-25 23:28:36 +02:00
@objc
2018-06-28 19:28:14 +02:00
init(quotedReply: OWSQuotedReplyModel, conversationStyle: ConversationStyle) {
self.quotedReply = quotedReply
2018-06-28 19:28:14 +02:00
self.conversationStyle = conversationStyle
2018-04-04 03:59:19 +02:00
super.init(frame: .zero)
self.heightConstraint = self.autoSetDimension(.height, toSize: 0)
updateContents()
2018-04-04 03:59:19 +02:00
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: .UIContentSizeCategoryDidChange, object: nil)
}
2018-04-04 03:59:19 +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
quotedMessageView.backgroundColor = .clear
2018-04-04 03:59:19 +02:00
let cancelButton: UIButton = UIButton(type: .custom)
2018-04-04 03:59:19 +02:00
let buttonImage: UIImage = #imageLiteral(resourceName: "quoted-message-cancel").withRenderingMode(.alwaysTemplate)
cancelButton.setImage(buttonImage, for: .normal)
cancelButton.imageView?.tintColor = .darkGray
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))
updateHeight()
2018-04-04 03:59:19 +02:00
}
// MARK: Actions
2018-04-04 03:59:19 +02:00
@objc
func didTapCancel(_ sender: Any) {
self.delegate?.quotedReplyPreviewDidPressCancel(self)
}
// MARK: Sizing
func updateHeight() {
2018-04-13 21:10:16 +02:00
guard let quotedMessageView = quotedMessageView else {
owsFail("\(logTag) missing quotedMessageView")
return
}
let size = quotedMessageView.size(forMaxWidth: CGFloat.infinity)
self.heightConstraint.constant = size.height
}
2018-05-25 18:54:25 +02:00
@objc func contentSizeCategoryDidChange(_ notification: Notification) {
Logger.debug("\(self.logTag) in \(#function)")
updateContents()
}
2018-04-04 03:59:19 +02:00
}