diff --git a/Example/Sources/MockMessage.swift b/Example/Sources/MockMessage.swift index b60a9ac8..6ba213aa 100644 --- a/Example/Sources/MockMessage.swift +++ b/Example/Sources/MockMessage.swift @@ -24,6 +24,9 @@ import Foundation import MessageKit +import CoreLocation + + struct MockMessage: MessageType { @@ -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) + } + } diff --git a/Example/Sources/SampleData.swift b/Example/Sources/SampleData.swift index 84d9e5fd..377eede0 100644 --- a/Example/Sources/SampleData.swift +++ b/Example/Sources/SampleData.swift @@ -23,6 +23,7 @@ */ import MessageKit +import CoreLocation struct SampleData { let Dan = Sender(id: "123456", displayName: "Dan Leonard") @@ -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)! @@ -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 diff --git a/Sources/LocationMessageCell.swift b/Sources/LocationMessageCell.swift index 28190575..f89e2fa1 100644 --- a/Sources/LocationMessageCell.swift +++ b/Sources/LocationMessageCell.swift @@ -25,6 +25,30 @@ import UIKit import MapKit -open class LocationMessageCell: MessageCollectionViewCell { +open class LocationMessageCell: MessageCollectionViewCell { + + 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 + } + } } diff --git a/Sources/MessageData.swift b/Sources/MessageData.swift index 5362cc71..3e677278 100644 --- a/Sources/MessageData.swift +++ b/Sources/MessageData.swift @@ -23,7 +23,7 @@ */ import Foundation -//import class CoreLocation.CLLocation +import class CoreLocation.CLLocation public enum MessageData { @@ -31,14 +31,12 @@ public enum MessageData { 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) diff --git a/Sources/MessagesCollectionViewFlowLayout.swift b/Sources/MessagesCollectionViewFlowLayout.swift index 38d198b7..c580fc73 100644 --- a/Sources/MessagesCollectionViewFlowLayout.swift +++ b/Sources/MessagesCollectionViewFlowLayout.swift @@ -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 diff --git a/Sources/MessagesViewController.swift b/Sources/MessagesViewController.swift index 03f3c411..43c0816f 100644 --- a/Sources/MessagesViewController.swift +++ b/Sources/MessagesViewController.swift @@ -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") @@ -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 } }