Files
MessageKit/Sources/Controllers/MessagesViewController.swift
T
2017-11-15 03:14:49 +05:00

333 lines
15 KiB
Swift

/*
MIT License
Copyright (c) 2017 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
open class MessagesViewController: UIViewController {
// MARK: - Properties [Public]
/// The `MessagesCollectionView` managed by the messages view controller object.
open var messagesCollectionView = MessagesCollectionView()
/// The `MessageInputBar` used as the `inputAccessoryView` in the view controller.
open var messageInputBar = MessageInputBar()
/// A Boolean value that determines whether the `MessagesCollectionView` scrolls to the
/// bottom on the view's first layout.
///
/// The default value of this property is `false`.
open var scrollsToBottomOnFirstLayout: Bool = false
/// A Boolean value that determines whether the `MessagesCollectionView` scrolls to the
/// bottom whenever the `InputTextView` begins editing.
///
/// The default value of this property is `false`.
open var scrollsToBottomOnKeybordBeginsEditing: Bool = false
open override var canBecomeFirstResponder: Bool {
return true
}
open override var inputAccessoryView: UIView? {
return messageInputBar
}
open override var shouldAutorotate: Bool {
return false
}
/// A Boolean value used to determine if `viewDidLayoutSubviews()` has been called.
private var isFirstLayout: Bool = true
// MARK: - View Life Cycle
open override func viewDidLoad() {
super.viewDidLoad()
extendedLayoutIncludesOpaqueBars = true
automaticallyAdjustsScrollViewInsets = false
view.backgroundColor = .white
messagesCollectionView.keyboardDismissMode = .interactive
messagesCollectionView.alwaysBounceVertical = true
setupSubviews()
setupConstraints()
registerReusableViews()
setupDelegates()
}
open override func viewDidLayoutSubviews() {
// Hack to prevent animation of the contentInset after viewDidAppear
if isFirstLayout {
defer { isFirstLayout = false }
addKeyboardObservers()
messagesCollectionView.contentInset.bottom = keyboardOffsetFrame.height
messagesCollectionView.scrollIndicatorInsets.bottom = keyboardOffsetFrame.height
//Scroll to bottom at first load
if scrollsToBottomOnFirstLayout {
messagesCollectionView.scrollToBottom(animated: false)
}
}
}
// MARK: - Initializers
deinit {
removeKeyboardObservers()
}
// MARK: - Methods [Private]
/// Sets the delegate and dataSource of the messagesCollectionView property.
private func setupDelegates() {
messagesCollectionView.delegate = self
messagesCollectionView.dataSource = self
}
/// Registers all cells and supplementary views of the messagesCollectionView property.
private func registerReusableViews() {
messagesCollectionView.register(TextMessageCell.self)
messagesCollectionView.register(MediaMessageCell.self)
messagesCollectionView.register(LocationMessageCell.self)
messagesCollectionView.register(MessageFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter)
messagesCollectionView.register(MessageHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader)
messagesCollectionView.register(MessageDateHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader)
}
/// Adds the messagesCollectionView to the controllers root view.
private func setupSubviews() {
view.addSubview(messagesCollectionView)
}
/// Sets the constraints of the `MessagesCollectionView`.
private func setupConstraints() {
messagesCollectionView.translatesAutoresizingMaskIntoConstraints = false
let top = messagesCollectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: topLayoutGuide.length)
let bottom = messagesCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
if #available(iOS 11.0, *) {
let leading = messagesCollectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)
let trailing = messagesCollectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
NSLayoutConstraint.activate([top, bottom, trailing, leading])
} else {
let leading = messagesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailing = messagesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
NSLayoutConstraint.activate([top, bottom, trailing, leading])
}
adjustScrollViewInset()
}
@objc
private func adjustScrollViewInset() {
if #available(iOS 11.0, *) {
// No need to add to the top contentInset
} else {
let navigationBarInset = navigationController?.navigationBar.frame.height ?? 0
let statusBarInset: CGFloat = UIApplication.shared.isStatusBarHidden ? 0 : 20
let topInset = navigationBarInset + statusBarInset
messagesCollectionView.contentInset.top = topInset
messagesCollectionView.scrollIndicatorInsets.top = topInset
}
}
}
// MARK: - UICollectionViewDelegate & UICollectionViewDelegateFlowLayout Conformance
extension MessagesViewController: UICollectionViewDelegateFlowLayout {
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let messagesFlowLayout = collectionViewLayout as? MessagesCollectionViewFlowLayout else { return .zero }
return messagesFlowLayout.sizeForItem(at: indexPath)
}
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
guard let messagesCollectionView = collectionView as? MessagesCollectionView else { return .zero }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return .zero }
guard let messagesLayoutDelegate = messagesCollectionView.messagesLayoutDelegate else { return .zero }
// Could pose a problem if subclass behaviors allows more than one item per section
let indexPath = IndexPath(item: 0, section: section)
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
return messagesLayoutDelegate.headerViewSize(for: message, at: indexPath, in: messagesCollectionView)
}
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
guard let messagesCollectionView = collectionView as? MessagesCollectionView else { return .zero }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return .zero }
guard let messagesLayoutDelegate = messagesCollectionView.messagesLayoutDelegate else { return .zero }
// Could pose a problem if subclass behaviors allows more than one item per section
let indexPath = IndexPath(item: 0, section: section)
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
return messagesLayoutDelegate.footerViewSize(for: message, at: indexPath, in: messagesCollectionView)
}
}
// MARK: - UICollectionViewDataSource Conformance
extension MessagesViewController: UICollectionViewDataSource {
open func numberOfSections(in collectionView: UICollectionView) -> Int {
guard let collectionView = collectionView as? MessagesCollectionView else { return 0 }
// Each message is its own section
return collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
}
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let collectionView = collectionView as? MessagesCollectionView else { return 0 }
let messageCount = collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
// There will only ever be 1 message per section
return messageCount > 0 ? 1 : 0
}
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
fatalError("Managed collectionView: \(collectionView.debugDescription) is not a MessagesCollectionView.")
}
guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
fatalError("MessagesDataSource has not been set.")
}
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
switch message.data {
case .text, .attributedText, .emoji:
let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
case .photo, .video:
let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
case .location:
let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
}
}
open func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
fatalError("Managed collectionView: \(collectionView.debugDescription) is not a MessagesCollectionView.")
}
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError("MessagesDataSource has not been set.")
}
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError("MessagesDisplayDelegate has not been set.")
}
let message = dataSource.messageForItem(at: indexPath, in: messagesCollectionView)
switch kind {
case UICollectionElementKindSectionHeader:
return displayDelegate.messageHeaderView(for: message, at: indexPath, in: messagesCollectionView)
case UICollectionElementKindSectionFooter:
return displayDelegate.messageFooterView(for: message, at: indexPath, in: messagesCollectionView)
default:
fatalError("Unrecognized element of kind: \(kind)")
}
}
}
// MARK: - Keyboard Handling
fileprivate extension MessagesViewController {
func addKeyboardObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidChangeState), name: .UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleTextViewDidBeginEditing), name: .UITextViewTextDidBeginEditing, object: messageInputBar.inputTextView)
NotificationCenter.default.addObserver(self, selector: #selector(adjustScrollViewInset), name: .UIDeviceOrientationDidChange, object: nil)
}
func removeKeyboardObservers() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.removeObserver(self, name: .UITextViewTextDidBeginEditing, object: messageInputBar.inputTextView)
NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil)
}
@objc
func handleTextViewDidBeginEditing(_ notification: Notification) {
if scrollsToBottomOnKeybordBeginsEditing {
messagesCollectionView.scrollToBottom(animated: true)
}
}
@objc
func handleKeyboardDidChangeState(_ notification: Notification) {
guard let keyboardEndFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return }
if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > UIScreen.main.bounds.height {
// Hardware keyboard is found
let bottomInset = view.frame.size.height - keyboardEndFrame.origin.y - iPhoneXBottomInset
messagesCollectionView.contentInset.bottom = bottomInset
messagesCollectionView.scrollIndicatorInsets.bottom = bottomInset
} else {
//Software keyboard is found
let bottomInset = keyboardEndFrame.height > keyboardOffsetFrame.height ? (keyboardEndFrame.height - iPhoneXBottomInset) : keyboardOffsetFrame.height
messagesCollectionView.contentInset.bottom = bottomInset
messagesCollectionView.scrollIndicatorInsets.bottom = bottomInset
}
}
fileprivate var keyboardOffsetFrame: CGRect {
guard let inputFrame = inputAccessoryView?.frame else { return .zero }
return CGRect(origin: inputFrame.origin, size: CGSize(width: inputFrame.width, height: inputFrame.height - iPhoneXBottomInset))
}
/// On the iPhone X the inputAccessoryView is anchored to the layoutMarginesGuide.bottom anchor so the frame of the inputAccessoryView
/// is larger than the required offset for the MessagesCollectionView
///
/// - Returns: The safeAreaInsets.bottom if its an iPhoneX, else 0
fileprivate var iPhoneXBottomInset: CGFloat {
if #available(iOS 11.0, *) {
guard UIScreen.main.nativeBounds.height == 2436 else { return 0 }
return view.safeAreaInsets.bottom
}
return 0
}
}