Skip to content

Commit

Permalink
Add video message
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 18, 2017
1 parent ba0ef16 commit 4caea44
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Example/Sources/MockMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@ struct MockMessage: MessageType {
init(image: UIImage, sender: Sender, messageId: String) {
self.init(data: .photo(image), sender: sender, messageId: messageId)
}


init(thumbnail: UIImage, sender: Sender, messageId: String) {
let url = URL(fileURLWithPath: "")
self.init(data: .video(file: url, thumbnail: thumbnail), sender: sender, messageId: messageId)
}

}
3 changes: 2 additions & 1 deletion Example/Sources/SampleData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct SampleData {
var msg11 = MockMessage(text: "One Infinite Loop Cupertino, CA 95014 This is some extra text that should not be detected.", sender: Cook, messageId: UUID().uuidString)
let msg12 = MockMessage(text: "This is an example of the date detector 11/11/2017. April 1st is April Fools Day. Next Friday is not Friday the 13th.", sender: Dan, messageId: UUID().uuidString)
let msg13 = MockMessage(text: "https//:github.com/SD10", sender: Steven, messageId: UUID().uuidString)
let msg14 = MockMessage(thumbnail: #imageLiteral(resourceName: "Tim-Cook"), sender: Jobs, messageId: UUID().uuidString)

msg2.sentDate = Calendar.current.date(byAdding: .hour, value: 2, to: msg1.sentDate)!
msg3.sentDate = Calendar.current.date(byAdding: .minute, value: 37, to: msg2.sentDate)!
Expand All @@ -68,7 +69,7 @@ struct SampleData {
msg10.sentDate = Calendar.current.date(byAdding: .minute, value: 59, to: msg9.sentDate)!
msg11.sentDate = Calendar.current.date(byAdding: .hour, value: 7, to: msg10.sentDate)!

return [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13].map { msg -> MockMessage in
return [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13, msg14].map { msg -> MockMessage in
var msg = msg
msg.sender = msg.sender == Dan ? Steven : Dan
return msg
Expand Down
76 changes: 76 additions & 0 deletions Sources/MediaMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,89 @@

import UIKit

open class PlayButtonView: UIView {

override open var frame: CGRect {
didSet {
commonSetup()
}
}

let triangleView: UIView = {
let triangleView = UIView()
triangleView.layer.masksToBounds = true
triangleView.clipsToBounds = true
return triangleView
}()

func triangleMask(for frame: CGRect) -> CAShapeLayer {
let shapeLayer = CAShapeLayer()
let trianglePath = UIBezierPath()
let point1 = CGPoint(x: frame.minX, y: frame.minY)
let point2 = CGPoint(x: frame.maxX, y: frame.maxY/2)
let point3 = CGPoint(x: frame.minX, y: frame.maxY)
trianglePath .move(to: point1)
trianglePath .addLine(to: point2)
trianglePath .addLine(to: point3)
trianglePath .close()
shapeLayer.path = trianglePath.cgPath
return shapeLayer
}

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

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

func setupSubviews() {
addSubview(triangleView)
}

func commonSetup() {

let size = CGSize(width: frame.width/2, height: frame.height/2)
triangleView.bounds.size = size
triangleView.layer.mask = triangleMask(for: triangleView.bounds)

let centerOffset = triangleView.bounds.width/8
triangleView.center = CGPoint(x: center.x + centerOffset, y: center.y)

layer.cornerRadius = frame.width / 2
layer.masksToBounds = true
}

}

open class MediaMessageCell: MessageCollectionViewCell<UIImageView> {

open lazy var playButtonView: PlayButtonView = {
let playButtonView = PlayButtonView()
playButtonView.triangleView.backgroundColor = .black
playButtonView.backgroundColor = .playButtonLightGray
playButtonView.frame = CGRect(origin: .zero, size: CGSize(width: 35, height: 35))
return playButtonView
}()

override func setupSubviews() {
super.setupSubviews()
messageContentView.addSubview(playButtonView)
}

override open func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
super.configure(with: message, at: indexPath, and: messagesCollectionView)
switch message.data {
case .photo(let image):
messageContentView.image = image
playButtonView.isHidden = true
case .video(_, let image):
messageContentView.image = image
playButtonView.isHidden = false
playButtonView.center = messageContentView.center
default:
break
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell

// MARK: - Methods

private func setupSubviews() {
internal func setupSubviews() {

contentView.addSubview(cellTopLabel)
contentView.addSubview(messageContainerView)
Expand Down
6 changes: 6 additions & 0 deletions Sources/MessageContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ open class MessageContainerView: UIImageView {

private let imageMask = UIImageView()

open override var frame: CGRect {
didSet {
applyMessageStyle()
}
}

open var style: MessageStyle = .none {
didSet {
applyMessageStyle()
Expand Down
2 changes: 1 addition & 1 deletion Sources/MessageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum MessageData {
case text(String)
case attributedText(NSAttributedString)
case photo(UIImage)
// case video(file: NSURL, thumbnail: UIImage)
case video(file: URL, thumbnail: UIImage)

// MARK: - Not supported yet

Expand Down
2 changes: 1 addition & 1 deletion Sources/MessagesCollectionViewFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ extension MessagesCollectionViewFlowLayout {
messageContainerSize = labelSize(for: text, considering: maxWidth, and: messageLabelFont)
case .attributedText(let text):
messageContainerSize = labelSize(for: text, considering: maxWidth)
case .photo(let image):
case .photo(let image), .video(_, let image):
let boundingRect = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: .greatestFiniteMagnitude))
messageContainerSize = AVMakeRect(aspectRatio: image.size, insideRect: boundingRect).size
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension MessagesViewController: UICollectionViewDataSource {
}
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
case .photo:
case .photo, .video:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MediaMessageCell", for: indexPath) as? MediaMessageCell else {
fatalError("Unable to dequeue MediaMessageCell")
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/UIColor+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extension UIColor {

static let inputBarGray = UIColor(colorLiteralRed: 247/255, green: 247/255, blue: 247/255, alpha: 1.0)

static let playButtonLightGray = UIColor(colorLiteralRed: 230/255, green: 230/255, blue: 230/255, alpha: 1.0)

static let sendButtonBlue = UIColor(colorLiteralRed: 15/255, green: 135/255, blue: 255/255, alpha: 1.0)

}

0 comments on commit 4caea44

Please sign in to comment.