mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
943ed2d899
1. Set .swiftlint.yml for ChatExample 2. Fix Access Control for Explicit Top Level ACL Violation 3. Fix SwiftLint Warning: Colon Violation, Function Body Length 4. Fix Forced Unwrapping style
447 lines
19 KiB
Swift
447 lines
19 KiB
Swift
/*
|
|
MIT License
|
|
|
|
Copyright (c) 2017-2018 MessageKit
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
import UIKit
|
|
import MessageKit
|
|
import MapKit
|
|
|
|
internal class ConversationViewController: MessagesViewController {
|
|
|
|
let refreshControl = UIRefreshControl()
|
|
|
|
var messageList: [MockMessage] = []
|
|
|
|
var isTyping = false
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
let messagesToFetch = UserDefaults.standard.mockMessagesCount()
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
|
SampleData.shared.getMessages(count: messagesToFetch) { messages in
|
|
DispatchQueue.main.async {
|
|
self.messageList = messages
|
|
self.messagesCollectionView.reloadData()
|
|
self.messagesCollectionView.scrollToBottom()
|
|
}
|
|
}
|
|
}
|
|
|
|
messagesCollectionView.messagesDataSource = self
|
|
messagesCollectionView.messagesLayoutDelegate = self
|
|
messagesCollectionView.messagesDisplayDelegate = self
|
|
messagesCollectionView.messageCellDelegate = self
|
|
messageInputBar.delegate = self
|
|
|
|
messageInputBar.sendButton.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
scrollsToBottomOnKeybordBeginsEditing = true // default false
|
|
maintainPositionOnKeyboardFrameChanged = true // default false
|
|
|
|
messagesCollectionView.addSubview(refreshControl)
|
|
refreshControl.addTarget(self, action: #selector(ConversationViewController.loadMoreMessages), for: .valueChanged)
|
|
|
|
navigationItem.rightBarButtonItems = [
|
|
UIBarButtonItem(image: UIImage(named: "ic_keyboard"),
|
|
style: .plain,
|
|
target: self,
|
|
action: #selector(ConversationViewController.handleKeyboardButton)),
|
|
UIBarButtonItem(image: UIImage(named: "ic_typing"),
|
|
style: .plain,
|
|
target: self,
|
|
action: #selector(ConversationViewController.handleTyping))
|
|
]
|
|
}
|
|
|
|
@objc func handleTyping() {
|
|
|
|
defer {
|
|
isTyping = !isTyping
|
|
}
|
|
|
|
if isTyping {
|
|
|
|
messageInputBar.topStackView.arrangedSubviews.first?.removeFromSuperview()
|
|
messageInputBar.topStackViewPadding = .zero
|
|
|
|
} else {
|
|
|
|
let label = UILabel()
|
|
label.text = "nathan.tannar is typing..."
|
|
label.font = UIFont.boldSystemFont(ofSize: 16)
|
|
messageInputBar.topStackView.addArrangedSubview(label)
|
|
messageInputBar.topStackViewPadding.top = 6
|
|
messageInputBar.topStackViewPadding.left = 12
|
|
|
|
// The backgroundView doesn't include the topStackView. This is so things in the topStackView can have transparent backgrounds if you need it that way or another color all together
|
|
messageInputBar.backgroundColor = messageInputBar.backgroundView.backgroundColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@objc func loadMoreMessages() {
|
|
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: DispatchTime.now() + 4) {
|
|
SampleData.shared.getMessages(count: 10) { messages in
|
|
DispatchQueue.main.async {
|
|
self.messageList.insert(contentsOf: messages, at: 0)
|
|
self.messagesCollectionView.reloadDataAndKeepOffset()
|
|
self.refreshControl.endRefreshing()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func handleKeyboardButton() {
|
|
|
|
messageInputBar.inputTextView.resignFirstResponder()
|
|
let actionSheetController = UIAlertController(title: "Change Keyboard Style", message: nil, preferredStyle: .actionSheet)
|
|
let actions = [
|
|
UIAlertAction(title: "Slack", style: .default, handler: { _ in
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
|
|
self.slack()
|
|
})
|
|
}),
|
|
UIAlertAction(title: "iMessage", style: .default, handler: { _ in
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
|
|
self.iMessage()
|
|
})
|
|
}),
|
|
UIAlertAction(title: "Default", style: .default, handler: { _ in
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
|
|
self.defaultStyle()
|
|
})
|
|
}),
|
|
UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
|
]
|
|
actions.forEach { actionSheetController.addAction($0) }
|
|
actionSheetController.view.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
present(actionSheetController, animated: true, completion: nil)
|
|
}
|
|
|
|
// MARK: - Keyboard Style
|
|
|
|
func slack() {
|
|
defaultStyle()
|
|
messageInputBar.backgroundView.backgroundColor = .white
|
|
messageInputBar.isTranslucent = false
|
|
messageInputBar.inputTextView.backgroundColor = .clear
|
|
messageInputBar.inputTextView.layer.borderWidth = 0
|
|
let items = [
|
|
makeButton(named: "ic_camera").onTextViewDidChange { button, textView in
|
|
button.isEnabled = textView.text.isEmpty
|
|
},
|
|
makeButton(named: "ic_at").onSelected {
|
|
$0.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
},
|
|
makeButton(named: "ic_hashtag").onSelected {
|
|
$0.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
},
|
|
.flexibleSpace,
|
|
makeButton(named: "ic_library").onTextViewDidChange { button, textView in
|
|
button.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
button.isEnabled = textView.text.isEmpty
|
|
},
|
|
messageInputBar.sendButton
|
|
.configure {
|
|
$0.layer.cornerRadius = 8
|
|
$0.layer.borderWidth = 1.5
|
|
$0.layer.borderColor = $0.titleColor(for: .disabled)?.cgColor
|
|
$0.setTitleColor(.white, for: .normal)
|
|
$0.setTitleColor(.white, for: .highlighted)
|
|
$0.setSize(CGSize(width: 52, height: 30), animated: true)
|
|
}.onDisabled {
|
|
$0.layer.borderColor = $0.titleColor(for: .disabled)?.cgColor
|
|
$0.backgroundColor = .white
|
|
}.onEnabled {
|
|
$0.backgroundColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
$0.layer.borderColor = UIColor.clear.cgColor
|
|
}.onSelected {
|
|
// We use a transform becuase changing the size would cause the other views to relayout
|
|
$0.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
|
|
}.onDeselected {
|
|
$0.transform = CGAffineTransform.identity
|
|
}
|
|
]
|
|
items.forEach { $0.tintColor = .lightGray }
|
|
|
|
// We can change the container insets if we want
|
|
messageInputBar.inputTextView.textContainerInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
|
|
messageInputBar.inputTextView.placeholderLabelInsets = UIEdgeInsets(top: 8, left: 5, bottom: 8, right: 5)
|
|
|
|
// Since we moved the send button to the bottom stack lets set the right stack width to 0
|
|
messageInputBar.setRightStackViewWidthConstant(to: 0, animated: true)
|
|
|
|
// Finally set the items
|
|
messageInputBar.setStackViewItems(items, forStack: .bottom, animated: true)
|
|
}
|
|
|
|
func iMessage() {
|
|
defaultStyle()
|
|
messageInputBar.isTranslucent = false
|
|
messageInputBar.backgroundView.backgroundColor = .white
|
|
messageInputBar.separatorLine.isHidden = true
|
|
messageInputBar.inputTextView.backgroundColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)
|
|
messageInputBar.inputTextView.placeholderTextColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
|
|
messageInputBar.inputTextView.textContainerInset = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 36)
|
|
messageInputBar.inputTextView.placeholderLabelInsets = UIEdgeInsets(top: 8, left: 20, bottom: 8, right: 36)
|
|
messageInputBar.inputTextView.layer.borderColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1).cgColor
|
|
messageInputBar.inputTextView.layer.borderWidth = 1.0
|
|
messageInputBar.inputTextView.layer.cornerRadius = 16.0
|
|
messageInputBar.inputTextView.layer.masksToBounds = true
|
|
messageInputBar.inputTextView.scrollIndicatorInsets = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
|
|
messageInputBar.setRightStackViewWidthConstant(to: 36, animated: true)
|
|
messageInputBar.setStackViewItems([messageInputBar.sendButton], forStack: .right, animated: true)
|
|
messageInputBar.sendButton.imageView?.backgroundColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
messageInputBar.sendButton.contentEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
|
|
messageInputBar.sendButton.setSize(CGSize(width: 36, height: 36), animated: true)
|
|
messageInputBar.sendButton.image = #imageLiteral(resourceName: "ic_up")
|
|
messageInputBar.sendButton.title = nil
|
|
messageInputBar.sendButton.imageView?.layer.cornerRadius = 16
|
|
messageInputBar.sendButton.backgroundColor = .clear
|
|
messageInputBar.textViewPadding.right = -38
|
|
}
|
|
|
|
func defaultStyle() {
|
|
let newMessageInputBar = MessageInputBar()
|
|
newMessageInputBar.sendButton.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
newMessageInputBar.delegate = self
|
|
messageInputBar = newMessageInputBar
|
|
reloadInputViews()
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
func makeButton(named: String) -> InputBarButtonItem {
|
|
return InputBarButtonItem()
|
|
.configure {
|
|
$0.spacing = .fixed(10)
|
|
$0.image = UIImage(named: named)?.withRenderingMode(.alwaysTemplate)
|
|
$0.setSize(CGSize(width: 30, height: 30), animated: true)
|
|
}.onSelected {
|
|
$0.tintColor = UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1)
|
|
}.onDeselected {
|
|
$0.tintColor = UIColor.lightGray
|
|
}.onTouchUpInside { _ in
|
|
print("Item Tapped")
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - MessagesDataSource
|
|
|
|
extension ConversationViewController: MessagesDataSource {
|
|
|
|
func currentSender() -> Sender {
|
|
return SampleData.shared.currentSender
|
|
}
|
|
|
|
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
|
|
return messageList.count
|
|
}
|
|
|
|
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
|
|
return messageList[indexPath.section]
|
|
}
|
|
|
|
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
|
|
let name = message.sender.displayName
|
|
return NSAttributedString(string: name, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1)])
|
|
}
|
|
|
|
func cellBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
|
|
|
|
let formatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateStyle = .medium
|
|
return formatter
|
|
}()
|
|
|
|
let dateString = formatter.string(from: message.sentDate)
|
|
return NSAttributedString(string: dateString, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption2)])
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - MessagesDisplayDelegate
|
|
|
|
extension ConversationViewController: MessagesDisplayDelegate {
|
|
|
|
// MARK: - Text Messages
|
|
|
|
func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
|
|
return isFromCurrentSender(message: message) ? .white : .darkText
|
|
}
|
|
|
|
func detectorAttributes(for detector: DetectorType, and message: MessageType, at indexPath: IndexPath) -> [NSAttributedStringKey: Any] {
|
|
return MessageLabel.defaultAttributes
|
|
}
|
|
|
|
func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] {
|
|
return [.url, .address, .phoneNumber, .date, .transitInformation]
|
|
}
|
|
|
|
// MARK: - All Messages
|
|
|
|
func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
|
|
return isFromCurrentSender(message: message) ? UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1) : UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
|
|
}
|
|
|
|
func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
|
|
let corner: MessageStyle.TailCorner = isFromCurrentSender(message: message) ? .bottomRight : .bottomLeft
|
|
return .bubbleTail(corner, .curved)
|
|
// let configurationClosure = { (view: MessageContainerView) in}
|
|
// return .custom(configurationClosure)
|
|
}
|
|
|
|
func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
|
|
let avatar = SampleData.shared.getAvatarFor(sender: message.sender)
|
|
avatarView.set(avatar: avatar)
|
|
}
|
|
|
|
// MARK: - Location Messages
|
|
|
|
func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? {
|
|
let annotationView = MKAnnotationView(annotation: nil, reuseIdentifier: nil)
|
|
let pinImage = #imageLiteral(resourceName: "pin")
|
|
annotationView.image = pinImage
|
|
annotationView.centerOffset = CGPoint(x: 0, y: -pinImage.size.height / 2)
|
|
return annotationView
|
|
}
|
|
|
|
func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? {
|
|
return { view in
|
|
view.layer.transform = CATransform3DMakeScale(0, 0, 0)
|
|
view.alpha = 0.0
|
|
UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0, options: [], animations: {
|
|
view.layer.transform = CATransform3DIdentity
|
|
view.alpha = 1.0
|
|
}, completion: nil)
|
|
}
|
|
}
|
|
|
|
func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions {
|
|
|
|
return LocationMessageSnapshotOptions()
|
|
}
|
|
}
|
|
|
|
// MARK: - MessagesLayoutDelegate
|
|
|
|
extension ConversationViewController: MessagesLayoutDelegate {
|
|
|
|
func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
|
|
return 16
|
|
}
|
|
|
|
func cellBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
|
|
return 16
|
|
}
|
|
|
|
private func footerViewSize(for indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
|
|
return CGSize(width: messagesCollectionView.bounds.width, height: 10)
|
|
}
|
|
}
|
|
|
|
// MARK: - MessageCellDelegate
|
|
|
|
extension ConversationViewController: MessageCellDelegate {
|
|
|
|
func didTapAvatar(in cell: MessageCollectionViewCell) {
|
|
print("Avatar tapped")
|
|
}
|
|
|
|
func didTapMessage(in cell: MessageCollectionViewCell) {
|
|
print("Message tapped")
|
|
}
|
|
|
|
func didTapTopLabel(in cell: MessageCollectionViewCell) {
|
|
print("Top label tapped")
|
|
}
|
|
|
|
func didTapBottomLabel(in cell: MessageCollectionViewCell) {
|
|
print("Bottom label tapped")
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - MessageLabelDelegate
|
|
|
|
extension ConversationViewController: MessageLabelDelegate {
|
|
|
|
func didSelectAddress(_ addressComponents: [String: String]) {
|
|
print("Address Selected: \(addressComponents)")
|
|
}
|
|
|
|
func didSelectDate(_ date: Date) {
|
|
print("Date Selected: \(date)")
|
|
}
|
|
|
|
func didSelectPhoneNumber(_ phoneNumber: String) {
|
|
print("Phone Number Selected: \(phoneNumber)")
|
|
}
|
|
|
|
func didSelectURL(_ url: URL) {
|
|
print("URL Selected: \(url)")
|
|
}
|
|
|
|
func didSelectTransitInformation(_ transitInformation: [String: String]) {
|
|
print("TransitInformation Selected: \(transitInformation)")
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - MessageInputBarDelegate
|
|
|
|
extension ConversationViewController: MessageInputBarDelegate {
|
|
|
|
func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
|
|
|
|
// Each NSTextAttachment that contains an image will count as one empty character in the text: String
|
|
|
|
for component in inputBar.inputTextView.components {
|
|
|
|
if let image = component as? UIImage {
|
|
|
|
let imageMessage = MockMessage(image: image, sender: currentSender(), messageId: UUID().uuidString, date: Date())
|
|
messageList.append(imageMessage)
|
|
messagesCollectionView.insertSections([messageList.count - 1])
|
|
|
|
} else if let text = component as? String {
|
|
|
|
let attributedText = NSAttributedString(string: text, attributes: [.font: UIFont.systemFont(ofSize: 15), .foregroundColor: UIColor.blue])
|
|
|
|
let message = MockMessage(attributedText: attributedText, sender: currentSender(), messageId: UUID().uuidString, date: Date())
|
|
messageList.append(message)
|
|
messagesCollectionView.insertSections([messageList.count - 1])
|
|
}
|
|
|
|
}
|
|
|
|
inputBar.inputTextView.text = String()
|
|
messagesCollectionView.scrollToBottom()
|
|
}
|
|
|
|
}
|