/* 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 MessageInputBar /// A base class for the example controllers class ChatViewController: MessagesViewController, MessagesDataSource { override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } var messageList: [MockMessage] = [] let refreshControl = UIRefreshControl() let formatter: DateFormatter = { let formatter = DateFormatter() formatter.dateStyle = .medium return formatter }() override func viewDidLoad() { super.viewDidLoad() configureMessageCollectionView() configureMessageInputBar() loadFirstMessages() title = "MessageKit" } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) MockSocket.shared.connect(with: [SampleData.shared.steven, SampleData.shared.wu]) .onNewMessage { [weak self] message in self?.insertMessage(message) } } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) MockSocket.shared.disconnect() audioController.stopAnyOngoingPlaying() } func loadFirstMessages() { DispatchQueue.global(qos: .userInitiated).async { let count = UserDefaults.standard.mockMessagesCount() SampleData.shared.getMessages(count: count) { messages in DispatchQueue.main.async { self.messageList = messages self.messagesCollectionView.reloadData() self.messagesCollectionView.scrollToBottom() } } } } @objc func loadMoreMessages() { DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 1) { SampleData.shared.getMessages(count: 20) { messages in DispatchQueue.main.async { self.messageList.insert(contentsOf: messages, at: 0) self.messagesCollectionView.reloadDataAndKeepOffset() self.refreshControl.endRefreshing() } } } } func configureMessageCollectionView() { messagesCollectionView.messagesDataSource = self messagesCollectionView.messageCellDelegate = self scrollsToBottomOnKeyboardBeginsEditing = true // default false maintainPositionOnKeyboardFrameChanged = true // default false messagesCollectionView.addSubview(refreshControl) refreshControl.addTarget(self, action: #selector(loadMoreMessages), for: .valueChanged) } func configureMessageInputBar() { messageInputBar.delegate = self messageInputBar.inputTextView.tintColor = .primaryColor messageInputBar.sendButton.tintColor = .primaryColor } // MARK: - Helpers func insertMessage(_ message: MockMessage) { messageList.append(message) // Reload last section to update header/footer labels and insert a new one messagesCollectionView.performBatchUpdates({ messagesCollectionView.insertSections([messageList.count - 1]) if messageList.count >= 2 { messagesCollectionView.reloadSections([messageList.count - 2]) } }, completion: { [weak self] _ in if self?.isLastSectionVisible() == true { self?.messagesCollectionView.scrollToBottom(animated: true) } }) } func isLastSectionVisible() -> Bool { guard !messageList.isEmpty else { return false } let lastIndexPath = IndexPath(item: 0, section: messageList.count - 1) return messagesCollectionView.indexPathsForVisibleItems.contains(lastIndexPath) } // MARK: - 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? { if indexPath.section % 3 == 0 { return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.darkGray]) } return nil } func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { let name = message.sender.displayName return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)]) } func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { let dateString = formatter.string(from: message.sentDate) return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]) } } // MARK: - MessageCellDelegate extension ChatViewController: MessageCellDelegate { func didTapAvatar(in cell: MessageCollectionViewCell) { print("Avatar tapped") } func didTapMessage(in cell: MessageCollectionViewCell) { print("Message tapped") } func didTapCellTopLabel(in cell: MessageCollectionViewCell) { print("Top cell label tapped") } func didTapMessageTopLabel(in cell: MessageCollectionViewCell) { print("Top message label tapped") } func didTapMessageBottomLabel(in cell: MessageCollectionViewCell) { print("Bottom label tapped") } func didTapPlayButton(in cell: AudioMessageCell) { guard let indexPath = messagesCollectionView.indexPath(for: cell), let message = messagesCollectionView.messagesDataSource?.messageForItem(at: indexPath, in: messagesCollectionView) else { print("Failed to identify message when audio cell receive tap gesture") return } guard audioController.state != .none else { // There is no audio sound playing - prepare to start playing for given audio message audioController.playSound(for: message, in: cell) return } if audioController.playingMessage?.messageId == message.messageId { // tap occur in the current cell that is playing audio sound if audioController.state == .playing { audioController.pauseSound(for: message, in: cell) } else { audioController.resumeSound() } } else { // tap occur in a difference cell that the one is currently playing sound. First stop currently playing and start the sound for given message audioController.stopAnyOngoingPlaying() audioController.playSound(for: message, in: cell) } } func didStartAudio(in cell: AudioMessageCell) { print("Did start playing audio sound") } func didPauseAudio(in cell: AudioMessageCell) { print("Did pause audio sound") } func didStopAudio(in cell: AudioMessageCell) { print("Did stop audio sound") } func didTapAccessoryView(in cell: MessageCollectionViewCell) { print("Accessory view tapped") } } // MARK: - MessageLabelDelegate extension ChatViewController: 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 ChatViewController: MessageInputBarDelegate { func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) { for component in inputBar.inputTextView.components { if let str = component as? String { let message = MockMessage(text: str, sender: currentSender(), messageId: UUID().uuidString, date: Date()) insertMessage(message) } else if let img = component as? UIImage { let message = MockMessage(image: img, sender: currentSender(), messageId: UUID().uuidString, date: Date()) insertMessage(message) } } inputBar.inputTextView.text = String() messagesCollectionView.scrollToBottom(animated: true) } }