Skip to content

Commit

Permalink
Create location snapshot for LocationMessageCell
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 18, 2017
1 parent dfad049 commit c15a5f8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Example/Sources/MockMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import Foundation
import MessageKit
import CoreLocation



struct MockMessage: MessageType {

Expand Down Expand Up @@ -56,4 +59,8 @@ struct MockMessage: MessageType {
self.init(data: .video(file: url, thumbnail: thumbnail), sender: sender, messageId: messageId)
}

init(location: CLLocation, sender: Sender, messageId: String) {
self.init(data: .location(location), sender: sender, messageId: messageId)
}

}
6 changes: 5 additions & 1 deletion Example/Sources/SampleData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import MessageKit
import CoreLocation

struct SampleData {
let Dan = Sender(id: "123456", displayName: "Dan Leonard")
Expand Down Expand Up @@ -58,6 +59,9 @@ struct SampleData {
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)

let location = CLLocation(latitude: 37.3318, longitude: -122.0312) // Apple HQ
let msg15 = MockMessage(location: location, 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)!
msg4.sentDate = Calendar.current.date(byAdding: .minute, value: 3, to: msg3.sentDate)!
Expand All @@ -69,7 +73,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, msg14].map { msg -> MockMessage in
return [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13, msg14, msg15].map { msg -> MockMessage in
var msg = msg
msg.sender = msg.sender == Dan ? Steven : Dan
return msg
Expand Down
26 changes: 25 additions & 1 deletion Sources/LocationMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
import UIKit
import MapKit

open class LocationMessageCell: MessageCollectionViewCell<MKMapView> {
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
})
default:
break
}
}

}
6 changes: 2 additions & 4 deletions Sources/MessageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@
*/

import Foundation
//import class CoreLocation.CLLocation
import class CoreLocation.CLLocation

public enum MessageData {

case text(String)
case attributedText(NSAttributedString)
case photo(UIImage)
case video(file: URL, thumbnail: UIImage)
case location(CLLocation)

// MARK: - Not supported yet

// case audio(Data)
//
// case location(CLLocation)
//
//
// case system(String)
//
// case custom(Any)
Expand Down
3 changes: 3 additions & 0 deletions Sources/MessagesCollectionViewFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ extension MessagesCollectionViewFlowLayout {
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
case .location:
let size = CGSize(width: maxWidth, height: 300)
messageContainerSize = size
}

messageContainerSize.width += messageHorizontalInsets
Expand Down
9 changes: 9 additions & 0 deletions Sources/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ open class MessagesViewController: UIViewController {
messagesCollectionView.register(MediaMessageCell.self,
forCellWithReuseIdentifier: "MediaMessageCell")

messagesCollectionView.register(LocationMessageCell.self,
forCellWithReuseIdentifier: "LocationMessageCell")

messagesCollectionView.register(MessageFooterView.self,
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter,
withReuseIdentifier: "MessageFooterView")
Expand Down Expand Up @@ -206,6 +209,12 @@ extension MessagesViewController: UICollectionViewDataSource {
}
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
case .location:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LocationMessageCell", for: indexPath) as? LocationMessageCell else {
fatalError("Unable to dequeue LocationMessageCell")
}
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
}

}
Expand Down

0 comments on commit c15a5f8

Please sign in to comment.