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

99 lines
3.2 KiB
Swift
Raw Normal View History

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, OWSQuotedMessageViewDelegate {
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!
@available(*, unavailable, message:"use other constructor instead.")
required init(coder aDecoder: NSCoder) {
notImplemented()
}
@available(*, unavailable, message:"use other constructor instead.")
override init(frame: CGRect) {
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) {
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
2019-03-30 14:22:31 +01:00
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil)
}
2018-04-04 03:59:19 +02:00
private let draftMarginTop: CGFloat = 6
func updateContents() {
subviews.forEach { $0.removeFromSuperview() }
2018-04-04 03:59:19 +02:00
let hMargin: CGFloat = 6
self.layoutMargins = UIEdgeInsets(top: draftMarginTop,
left: hMargin,
bottom: 0,
right: hMargin)
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)
quotedMessageView.delegate = self
2018-04-13 21:10:16 +02:00
self.quotedMessageView = quotedMessageView
2019-01-25 17:33:09 +01:00
quotedMessageView.setContentHuggingHorizontalLow()
quotedMessageView.setCompressionResistanceHorizontalLow()
quotedMessageView.backgroundColor = .clear
self.addSubview(quotedMessageView)
quotedMessageView.ows_autoPinToSuperviewMargins()
updateHeight()
2018-04-04 03:59:19 +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)
self.heightConstraint.constant = size.height + draftMarginTop
}
2018-05-25 18:54:25 +02:00
@objc func contentSizeCategoryDidChange(_ notification: Notification) {
2018-08-23 16:37:34 +02:00
Logger.debug("")
updateContents()
}
// MARK: - OWSQuotedMessageViewDelegate
@objc public func didTapQuotedReply(_ quotedReply: OWSQuotedReplyModel, failedThumbnailDownloadAttachmentPointer attachmentPointer: TSAttachmentPointer) {
// Do nothing.
}
@objc public func didCancelQuotedReply() {
self.delegate?.quotedReplyPreviewDidPressCancel(self)
}
2018-04-04 03:59:19 +02:00
}