Skip to content

Commit

Permalink
Update FlowLayout to use delegate for media messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 19, 2017
1 parent 2eea060 commit 124189f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
30 changes: 15 additions & 15 deletions Sources/LocationMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ import MapKit

open class LocationMessageCell: MessageCollectionViewCell<UIImageView> {

lazy var mapSnapshotOptions: MKMapSnapshotOptions = {
let options = MKMapSnapshotOptions()
options.showsBuildings = true
options.showsPointsOfInterest = true
options.scale = UIScreen.main.scale
return options
}()

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 .location(let location):
let span = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: 0)
mapSnapshotOptions.region = MKCoordinateRegion(center: location.coordinate, span: span)
mapSnapshotOptions.size = messageContainerView.frame.size
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start(completionHandler: { (snapshot: MKMapSnapshot?, error: Error?) in
self.messageContentView.image = snapshot?.image
})
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else { return }

let options = displayDelegate.snapshotOptionsForLocation(message: message, at: indexPath, in: messagesCollectionView)
let snapshotOptions = MKMapSnapshotOptions()

snapshotOptions.scale = options.scale
snapshotOptions.region = MKCoordinateRegion(center: location.coordinate, span: options.span)
snapshotOptions.size = messageContainerView.frame.size
snapshotOptions.showsBuildings = options.showsBuildings
snapshotOptions.showsPointsOfInterest = options.showsPointsOfInterest

let snapShotter = MKMapSnapshotter(options: snapshotOptions)
snapShotter.start { (snapshot: MKMapSnapshot?, error: Error?) in
if error == nil { self.messageContentView.image = snapshot?.image }
}
default:
break
}
Expand Down
23 changes: 15 additions & 8 deletions Sources/MessagesCollectionViewFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,26 @@ extension MessagesCollectionViewFlowLayout {
switch message.data {
case .text(let text):
messageContainerSize = labelSize(for: text, considering: maxWidth, and: messageLabelFont)
messageContainerSize.width += messageHorizontalInsets
messageContainerSize.height += messageVerticalInsets
case .attributedText(let text):
messageContainerSize = labelSize(for: text, considering: maxWidth)
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
messageContainerSize.width += messageHorizontalInsets
messageContainerSize.height += messageVerticalInsets
case .photo, .video:
guard let messagesCollectionView = messagesCollectionView else { return .zero }
guard let layoutDelegate = messagesCollectionView.messagesLayoutDelegate as? MediaMessageLayoutDelegate else { return .zero }
let width = layoutDelegate.widthForMedia(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView)
let height = layoutDelegate.heightForMedia(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView)
messageContainerSize = CGSize(width: width, height: height)
case .location:
let size = CGSize(width: maxWidth, height: 300)
messageContainerSize = size
guard let messagesCollectionView = messagesCollectionView else { return .zero }
guard let layoutDelegate = messagesCollectionView.messagesLayoutDelegate as? LocationMessageLayoutDelegate else { return .zero }
let width = layoutDelegate.widthForLocation(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView)
let height = layoutDelegate.heightForLocation(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView)
messageContainerSize = CGSize(width: width, height: height)
}

messageContainerSize.width += messageHorizontalInsets
messageContainerSize.height += messageVerticalInsets

return messageContainerSize
}

Expand Down
8 changes: 7 additions & 1 deletion Sources/MessagesDisplayDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public protocol MessagesDisplayDelegate: class {

func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView?

func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions

}

public extension MessagesDisplayDelegate {
Expand Down Expand Up @@ -75,5 +77,9 @@ public extension MessagesDisplayDelegate {
func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView? {
return nil
}


func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions {
return LocationMessageSnapshotOptions()
}

}

0 comments on commit 124189f

Please sign in to comment.