AudioMessageCell.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2019 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import UIKit
  21. import AVFoundation
  22. /// A subclass of `MessageContentCell` used to display video and audio messages.
  23. open class AudioMessageCell: MessageContentCell {
  24. public static let insetTop: CGFloat = 12
  25. public static let insetBottom: CGFloat = 12
  26. public static let insetHorizontalBig: CGFloat = 23
  27. public static let insetHorizontalSmall: CGFloat = 12
  28. // MARK: - Properties
  29. /// The `MessageCellDelegate` for the cell.
  30. open override weak var delegate: MessageCellDelegate? {
  31. didSet {
  32. messageLabel.delegate = delegate
  33. }
  34. }
  35. /// The label used to display the message's text.
  36. open var messageLabel = MessageLabel()
  37. public lazy var audioPlayerView: AudioPlayerView = {
  38. let audioPlayerView = AudioPlayerView()
  39. audioPlayerView.translatesAutoresizingMaskIntoConstraints = false
  40. return audioPlayerView
  41. }()
  42. // MARK: - Methods
  43. /// Responsible for setting up the constraints of the cell's subviews.
  44. open func setupConstraints() {
  45. messageContainerView.removeConstraints(messageContainerView.constraints)
  46. let audioPlayerHeight = messageContainerView.frame.height - getMessageLabelHeight()
  47. let audioPlayerConstraints = [ audioPlayerView.constraintHeightTo(audioPlayerHeight),
  48. audioPlayerView.constraintAlignLeadingTo(messageContainerView),
  49. audioPlayerView.constraintAlignTrailingTo(messageContainerView),
  50. audioPlayerView.constraintAlignTopTo(messageContainerView)
  51. ]
  52. messageContainerView.addConstraints(audioPlayerConstraints)
  53. messageLabel.frame = CGRect(x: 0,
  54. y: messageContainerView.frame.height - getMessageLabelHeight(),
  55. width: messageContainerView.frame.width,
  56. height: getMessageLabelHeight())
  57. }
  58. func getMessageLabelHeight() -> CGFloat {
  59. if let text = messageLabel.attributedText {
  60. let height = (text.height(withConstrainedWidth:
  61. messageContainerView.frame.width -
  62. TextMediaMessageCell.insetHorizontalSmall -
  63. TextMediaMessageCell.insetHorizontalBig))
  64. return height + TextMediaMessageCell.insetBottom + TextMediaMessageCell.insetTop
  65. }
  66. return 0
  67. }
  68. open override func setupSubviews() {
  69. super.setupSubviews()
  70. messageContainerView.addSubview(audioPlayerView)
  71. messageContainerView.addSubview(messageLabel)
  72. }
  73. open override func prepareForReuse() {
  74. super.prepareForReuse()
  75. audioPlayerView.reset()
  76. messageLabel.attributedText = nil
  77. }
  78. /// Handle tap gesture on contentView and its subviews.
  79. open override func handleTapGesture(_ gesture: UIGestureRecognizer) {
  80. if audioPlayerView.didTapPlayButton(gesture) {
  81. delegate?.didTapPlayButton(in: self)
  82. } else {
  83. super.handleTapGesture(gesture)
  84. }
  85. }
  86. // MARK: - Configure Cell
  87. open override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
  88. super.apply(layoutAttributes)
  89. if let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes {
  90. messageLabel.textInsets = attributes.messageLabelInsets
  91. messageLabel.messageLabelFont = attributes.messageLabelFont
  92. }
  93. }
  94. open override func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
  95. super.configure(with: message, at: indexPath, and: messagesCollectionView)
  96. guard messagesCollectionView.messagesDataSource != nil else {
  97. fatalError(MessageKitError.nilMessagesDataSource)
  98. }
  99. guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
  100. fatalError(MessageKitError.nilMessagesDisplayDelegate)
  101. }
  102. let tintColor = displayDelegate.audioTintColor(for: message, at: indexPath, in: messagesCollectionView)
  103. audioPlayerView.setTintColor(tintColor)
  104. if case let .audio(audioItem) = message.kind {
  105. audioPlayerView.setDuration(formattedText: displayDelegate.audioProgressTextFormat(audioItem.duration,
  106. for: self,
  107. in: messagesCollectionView))
  108. configureMessageLabel(for: audioItem,
  109. with: displayDelegate,
  110. message: message,
  111. at: indexPath, in: messagesCollectionView)
  112. }
  113. setupConstraints()
  114. displayDelegate.configureAudioCell(self, message: message)
  115. }
  116. func configureMessageLabel(for audioItem: AudioItem,
  117. with displayDelegate: MessagesDisplayDelegate,
  118. message: MessageType,
  119. at indexPath: IndexPath,
  120. in messagesCollectionView: MessagesCollectionView) {
  121. let enabledDetectors = displayDelegate.enabledDetectors(for: message, at: indexPath, in: messagesCollectionView)
  122. messageLabel.configure {
  123. messageLabel.enabledDetectors = enabledDetectors
  124. for detector in enabledDetectors {
  125. let attributes = displayDelegate.detectorAttributes(for: detector, and: message, at: indexPath)
  126. messageLabel.setAttributes(attributes, detector: detector)
  127. }
  128. messageLabel.attributedText = audioItem.text
  129. }
  130. }
  131. /// Used to handle the cell's contentView's tap gesture.
  132. /// Return false when the contentView does not need to handle the gesture.
  133. open override func cellContentView(canHandle touchPoint: CGPoint) -> Bool {
  134. return messageLabel.handleGesture(touchPoint)
  135. }
  136. }