MediaMessageCell.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /// A subclass of `MessageContentCell` used to display video and audio messages.
  22. open class MediaMessageCell: MessageContentCell {
  23. /// The play button view to display on video messages.
  24. open lazy var playButtonView: PlayButtonView = {
  25. let playButtonView = PlayButtonView()
  26. return playButtonView
  27. }()
  28. /// The image view display the media content.
  29. open var imageView: UIImageView = {
  30. let imageView = UIImageView()
  31. imageView.contentMode = .scaleAspectFill
  32. return imageView
  33. }()
  34. // MARK: - Methods
  35. /// Responsible for setting up the constraints of the cell's subviews.
  36. open func setupConstraints() {
  37. imageView.fillSuperview()
  38. playButtonView.centerInSuperview()
  39. playButtonView.constraint(equalTo: CGSize(width: 35, height: 35))
  40. }
  41. open override func setupSubviews() {
  42. super.setupSubviews()
  43. messageContainerView.addSubview(imageView)
  44. messageContainerView.addSubview(playButtonView)
  45. setupConstraints()
  46. }
  47. open override func prepareForReuse() {
  48. super.prepareForReuse()
  49. self.imageView.image = nil
  50. }
  51. open override func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
  52. super.configure(with: message, at: indexPath, and: messagesCollectionView)
  53. guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
  54. fatalError(MessageKitError.nilMessagesDisplayDelegate)
  55. }
  56. switch message.kind {
  57. case .photo(let mediaItem):
  58. imageView.image = mediaItem.image ?? mediaItem.placeholderImage
  59. playButtonView.isHidden = true
  60. case .video(let mediaItem):
  61. let image = mediaItem.image ?? mediaItem.placeholderImage
  62. imageView.image = image //mediaItem.image ?? mediaItem.placeholderImage
  63. playButtonView.isHidden = false
  64. default:
  65. break
  66. }
  67. displayDelegate.configureMediaMessageImageView(imageView, for: message, at: indexPath, in: messagesCollectionView)
  68. }
  69. }