Skip to content

Commit

Permalink
Apply masks to media messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 18, 2017
1 parent 7788a1e commit 499cded
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
18 changes: 12 additions & 6 deletions Sources/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell

// MARK: - Properties

open var messageContainerView: MessageContainerView = MessageContainerView()
open var messageContainerView: MessageContainerView = {
let messageContainerView = MessageContainerView()
messageContainerView.clipsToBounds = true
messageContainerView.layer.masksToBounds = true
return messageContainerView
}()

open var avatarView: AvatarView = AvatarView()

Expand All @@ -41,7 +46,6 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell
open var messageContentView: ContentView = {
let contentView = ContentView()
contentView.clipsToBounds = true
contentView.layer.masksToBounds = true
contentView.isUserInteractionEnabled = true
return contentView
}()
Expand All @@ -62,7 +66,6 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell
super.init(frame: frame)
setupSubviews()
setupGestureRecognizers()
contentView.backgroundColor = .purple
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

Expand All @@ -76,7 +79,7 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell

contentView.addSubview(cellTopLabel)
contentView.addSubview(messageContainerView)
//messageContainerView.addSubview(messageContentView)
messageContainerView.addSubview(messageContentView)
contentView.addSubview(avatarView)
contentView.addSubview(cellBottomLabel)

Expand All @@ -90,7 +93,7 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell
avatarView.frame = attributes.avatarFrame

messageContainerView.frame = attributes.messageContainerFrame
messageContentView.frame = CGRect(origin: .zero, size: attributes.messageContainerFrame.size)
messageContentView.frame = messageContainerView.bounds

cellTopLabel.frame = attributes.cellTopLabelFrame
cellTopLabel.textInsets = attributes.cellTopLabelInsets
Expand All @@ -108,12 +111,15 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell
}

public func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {

// TODO: - fix delegate

if let displayDelegate = messagesCollectionView.messagesDisplayDelegate {

let messageColor = displayDelegate.backgroundColor(for: message, at: indexPath, in: messagesCollectionView)
let messageStyle = displayDelegate.messageStyle(for: message, at: indexPath, in: messagesCollectionView)

messageContainerView.messageColor = messageColor
messageContainerView.backgroundColor = messageColor
messageContainerView.style = messageStyle
}

Expand Down
63 changes: 26 additions & 37 deletions Sources/MessageContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,45 @@

import UIKit

open class MessageContainerView: UIView {
open class MessageContainerView: UIImageView {

// MARK: - Properties

open let imageView = UIImageView()
private let imageMask = UIImageView()

open var style: MessageStyle = .none {
didSet {
imageView.image = style.image
updateMessageColor()
applyMessageStyle()
}
}

open var messageColor: UIColor = .white {
didSet {
updateMessageColor()
}
}

// MARK: - Initializers

override public init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
setupConstraints()
}

public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Methods

func setupSubviews() {
imageView.isUserInteractionEnabled = true
addSubview(imageView)
}

func setupConstraints() {
imageView.fillSuperview()
}

private func updateMessageColor() {
private func applyMessageStyle() {
switch style {
case .bubble, .bubbleTail:
imageMask.image = style.image
imageMask.frame = bounds
mask = imageMask
image = nil
case .bubbleOutline(let color):
let bubbleStyle: MessageStyle = .bubble
imageMask.image = bubbleStyle.image
imageMask.frame = bounds.insetBy(dx: 1.0, dy: 1.0)
mask = imageMask
image = style.image
tintColor = color
case .bubbleTailOutline(let color, let tail, let corner):
let bubbleStyle: MessageStyle = .bubbleTailOutline(.white, tail, corner)
imageMask.image = bubbleStyle.image
imageMask.frame = bounds.insetBy(dx: 1.0, dy: 1.0)
mask = imageMask
image = style.image
tintColor = color
case .none:
backgroundColor = messageColor
imageView.tintColor = messageColor
default:
backgroundColor = superview?.backgroundColor
imageView.tintColor = messageColor
mask = nil
image = nil
tintColor = nil
}
}

Expand Down
16 changes: 16 additions & 0 deletions Sources/MessageKit+Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,19 @@ public extension MessagesLayoutDelegate {
}

}

// MARK: - MessageContainerView

public extension MessageContainerView {

@available(*, deprecated: 0.8.0, message: "Removed in MessageKit 0.8.0. Please use backgroundColor instead.")
var messageColor: UIColor {
get {
return .white
}
set {
backgroundColor = messageColor
}
}

}
8 changes: 4 additions & 4 deletions Sources/MessageStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public enum MessageStyle {

case none
case bubble
case bubbleOutline
case bubbleOutline(UIColor)
case bubbleTail(TailCorner, TailStyle)
case bubbleTailOutline(TailCorner, TailStyle)
case bubbleTailOutline(UIColor, TailCorner, TailStyle)

// MARK: - Public

Expand All @@ -83,7 +83,7 @@ public enum MessageStyle {
return nil
case .bubble, .bubbleOutline:
break
case .bubbleTail(let corner, _), .bubbleTailOutline(let corner, _):
case .bubbleTail(let corner, _), .bubbleTailOutline(_, let corner, _):
guard let cgImage = image.cgImage else { return nil }
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: corner.imageOrientation)
}
Expand All @@ -101,7 +101,7 @@ public enum MessageStyle {
return "bubble_outlined"
case .bubbleTail(_, let tailStyle):
return "bubble_full" + tailStyle.imageNameSuffix
case .bubbleTailOutline(_, let tailStyle):
case .bubbleTailOutline(_, _, let tailStyle):
return "bubble_outlined" + tailStyle.imageNameSuffix
case .none:
return nil
Expand Down
1 change: 1 addition & 0 deletions Sources/TextMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ open class TextMessageCell: MessageCollectionViewCell<MessageLabel> {

override open weak var delegate: MessageCellDelegate? {
didSet {
print("Fix this never gets set")
messageContentView.delegate = delegate
}
}
Expand Down

0 comments on commit 499cded

Please sign in to comment.