Files
MessageKit/Example/Sources/View Controllers/ChatViewController.swift
T
2018-10-03 12:19:33 +03:00

358 lines
13 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 MessageInputBar
import AVFoundation
/// A base class for the example controllers
class ChatViewController: MessagesViewController, MessagesDataSource {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
var audioPlayer: AVAudioPlayer?
var audioTimer: Timer?
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()
self.stopCurrentAudioPlayer()
}
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)
let frame = messagesCollectionView.layoutAttributesForItem(at: lastIndexPath)?.frame ?? .zero
var rect = messagesCollectionView.convert(frame, to: view)
// substract 100 to make the "visible" area of a cell bigger
rect.origin.y -= 100
var visibleRect = CGRect(x: messagesCollectionView.bounds.origin.x, y: messagesCollectionView.bounds.origin.y, width:
messagesCollectionView.bounds.size.width, height:
messagesCollectionView.bounds.size.height - messagesCollectionView.contentInset.bottom)
visibleRect = messagesCollectionView.convert(visibleRect, to: view)
return visibleRect.contains(rect)
}
// 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) {
print("Did tap on play button")
// guard let indexPath = messagesCollectionView.indexPath(for: cell),
// let message = messagesCollectionView.messagesDataSource?.messageForItem(at: indexPath, in: messagesCollectionView) else {
// return
// }
// switch message.kind {
// case .audio(let item):
// self.stopPreviousAudioIfNeeded(item)
// if cell.isPlaying() == true {
// audioPlayer?.pause()
// self.audioTimer?.invalidate()
// cell.pasue()
// } else {
// if audioPlayer == nil {
// audioPlayer = try? AVAudioPlayer.init(contentsOf: item.url)
// audioPlayer?.delegate = self
// }
// cell.play()
// audioPlayer?.play()
// // start timer to update audio pregress
// self.audioTimer?.invalidate()
// self.audioTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ChatViewController.didFireAudioTimer(_:)), userInfo: cell, repeats: true)
// }
// default: break
// }
}
// MARK: - Audio Helpers
@objc internal func didFireAudioTimer(_ timer: Timer) {
guard let audioCell = timer.userInfo as? AudioMessageCell else {
return
}
let totalDuration = self.audioPlayer?.duration ?? 0.0
let currentTime = self.audioPlayer?.currentTime ?? 0.0
let percent = (totalDuration != 0) ? (Float(currentTime/totalDuration)) : 0.0
audioCell.updateProgress(percent: percent, duration: currentTime)
}
private func stopPreviousAudioIfNeeded(_ currentAudioItem: AudioItem) {
// stop a previous audio if current received item url is diffrent than audio player url
guard let playingAudioURL = self.audioPlayer?.url else {
return // there is no audio player playing at this moment
}
if playingAudioURL.absoluteString != currentAudioItem.url.absoluteString {
self.stopCurrentAudioPlayer()
}
}
fileprivate func stopCurrentAudioPlayer() {
guard let player = audioPlayer else {
return // there is no audio player to stop
}
player.stop() // by calling stop will not audioPlayerDidFinishPlaying(player:flag:) delegate method - it should be called manualy
self.audioPlayerDidFinishPlaying(player, successfully: true)
}
}
// 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)
}
}
// MARK: - AVAudioPlayerDelegate
extension ChatViewController: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
guard let playingURL = player.url, let currentPlayingMessage = self.getMessageWithAudioURL(playingURL) else {
return
}
// update cell that is current playing
if let sectionIndex = messageList.index(where: { $0.messageId == currentPlayingMessage.messageId }) {
let indexPath = IndexPath.init(row: 0, section: sectionIndex)
if let audioCell = messagesCollectionView.cellForItem(at: indexPath) as? AudioMessageCell {
audioCell.stop(with: currentPlayingMessage.kind)
}
}
self.audioTimer?.invalidate()
self.audioTimer = nil
audioPlayer = nil
}
private func getMessageWithAudioURL(_ url: URL) -> MockMessage? {
let message = messageList.filter { (message) -> Bool in
switch message.kind {
case .audio(let audioItem):
if audioItem.url.absoluteString == url.absoluteString {
return true
}
default: break
}
return false
}.first
return message
}
}