Add MessageKitError to cleanup strings

This commit is contained in:
Steven Deutsch
2018-01-11 22:42:50 -06:00
parent 4a8d08e9e3
commit 810b58d88d
12 changed files with 106 additions and 43 deletions
@@ -27,29 +27,30 @@ import UIKit
extension MessagesViewController: UICollectionViewDataSource {
open func numberOfSections(in collectionView: UICollectionView) -> Int {
guard let collectionView = collectionView as? MessagesCollectionView else { return 0 }
guard let collectionView = collectionView as? MessagesCollectionView else {
fatalError(MessageKitError.notMessagesCollectionView)
}
// 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 }
guard let collectionView = collectionView as? MessagesCollectionView else {
fatalError(MessageKitError.notMessagesCollectionView)
}
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.")
fatalError(MessageKitError.notMessagesCollectionView)
}
guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
fatalError("MessagesDataSource has not been set.")
fatalError(MessageKitError.nilMessagesDataSource)
}
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
@@ -73,15 +74,15 @@ extension MessagesViewController: UICollectionViewDataSource {
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.")
fatalError(MessageKitError.notMessagesCollectionView)
}
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError("MessagesDataSource has not been set.")
fatalError(MessageKitError.nilMessagesDataSource)
}
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError("MessagesDisplayDelegate has not been set.")
fatalError(MessageKitError.nilMessagesDisplayDelegate)
}
let message = dataSource.messageForItem(at: indexPath, in: messagesCollectionView)
@@ -92,7 +93,7 @@ extension MessagesViewController: UICollectionViewDataSource {
case UICollectionElementKindSectionFooter:
return displayDelegate.messageFooterView(for: message, at: indexPath, in: messagesCollectionView)
default:
fatalError("Unrecognized element of kind: \(kind)")
fatalError(MessageKitError.unrecognizedSectionKind)
}
}
}
@@ -32,23 +32,36 @@ extension MessagesViewController: UICollectionViewDelegateFlowLayout {
}
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 }
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
fatalError(MessageKitError.notMessagesCollectionView)
}
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
guard let layoutDelegate = messagesCollectionView.messagesLayoutDelegate else {
fatalError(MessageKitError.nilMessagesLayoutDeleagte)
}
// 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)
let message = dataSource.messageForItem(at: indexPath, in: messagesCollectionView)
return layoutDelegate.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 }
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
fatalError(MessageKitError.notMessagesCollectionView)
}
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
guard let layoutDelegate = messagesCollectionView.messagesLayoutDelegate else {
fatalError(MessageKitError.nilMessagesLayoutDeleagte)
}
// 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)
let message = dataSource.messageForItem(at: indexPath, in: messagesCollectionView)
return layoutDelegate.footerViewSize(for: message, at: indexPath, in: messagesCollectionView)
}
open func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
@@ -69,7 +82,9 @@ extension MessagesViewController: UICollectionViewDelegateFlowLayout {
}
open func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { fatalError("Please set messagesDataSource") }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
let pasteBoard = UIPasteboard.general
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
+2 -2
View File
@@ -30,11 +30,11 @@ extension Bundle {
let podBundle = Bundle(for: MessagesViewController.self)
guard let resourceBundleUrl = podBundle.url(forResource: "MessageKitAssets", withExtension: "bundle") else {
fatalError("MessageKit: Could not create path to the assets bundle")
fatalError(MessageKitError.couldNotCreateAssetsPath)
}
guard let resourceBundle = Bundle(url: resourceBundleUrl) else {
fatalError("MessageKit: Could not load the assets bundle")
fatalError(MessageKitError.couldNotLoadAssetsBundle)
}
return resourceBundle
@@ -52,7 +52,7 @@ final class MessageIntermediateLayoutAttributes {
case .cellTrailing:
origin.x = cellFrame.width - avatarSize.width
case .natural:
fatalError("AvatarPosition Horizontal.natural needs to be resolved.")
fatalError(MessageKitError.avatarPositionUnresolved)
}
switch avatarPosition.vertical {
@@ -91,7 +91,7 @@ final class MessageIntermediateLayoutAttributes {
case .cellTrailing:
origin.x = cellFrame.width - avatarSize.width - messageContainerSize.width - messageContainerPadding.right
case .natural:
fatalError("AvatarPosition Horizontal.natural needs to be resolved.")
fatalError(MessageKitError.avatarPositionUnresolved)
}
return CGRect(origin: origin, size: messageContainerSize)
@@ -66,7 +66,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
/// Convenience property for accessing the layout object's `MessagesCollectionView`.
fileprivate var messagesCollectionView: MessagesCollectionView {
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
fatalError("MessagesCollectionViewFlowLayout is being used on a foreign type.")
fatalError(MessageKitError.layoutUsedOnForeignType)
}
return messagesCollectionView
}
@@ -74,7 +74,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
/// Convenience property for unwrapping the `MessagesCollectionView`'s `MessagesDataSource`.
fileprivate var messagesDataSource: MessagesDataSource {
guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
fatalError("MessagesDataSource has not been set.")
fatalError(MessageKitError.nilMessagesDataSource)
}
return messagesDataSource
}
@@ -82,7 +82,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
/// Convenience property for unwrapping the `MessagesCollectionView`'s `MessagesLayoutDelegate`.
fileprivate var messagesLayoutDelegate: MessagesLayoutDelegate {
guard let messagesLayoutDelegate = messagesCollectionView.messagesLayoutDelegate else {
fatalError("MessagesLayoutDeleagte has not been set.")
fatalError(MessageKitError.nilMessagesLayoutDeleagte)
}
return messagesLayoutDelegate
}
@@ -502,7 +502,7 @@ private extension MessagesCollectionViewFlowLayout {
return itemWidth - avatarWidth - attributes.messageContainerPadding.right - attributes.bottomLabelHorizontalPadding
case (_, .natural):
fatalError("AvatarPosition Horizontal.natural needs to be resolved.")
fatalError(MessageKitError.avatarPositionUnresolved)
}
}
@@ -575,7 +575,7 @@ private extension MessagesCollectionViewFlowLayout {
return itemWidth - avatarWidth - attributes.messageContainerPadding.right - attributes.topLabelHorizontalPadding
case (_, .natural):
fatalError("AvatarPosition Horizontal.natural needs to be resolved.")
fatalError(MessageKitError.avatarPositionUnresolved)
}
}
+36
View File
@@ -0,0 +1,36 @@
/*
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.
*/
enum MessageKitError {
static let avatarPositionUnresolved = "AvatarPosition Horizontal.natural needs to be resolved."
static let nilMessagesDataSource = "MessagesDataSource has not been set."
static let nilMessagesDisplayDelegate = "MessagesDisplayDelegate has not been set."
static let nilMessagesLayoutDeleagte = "MessagesLayoutDelegate has not been set."
static let notMessagesCollectionView = "The collectionView is not a MessagesCollectionView."
static let layoutUsedOnForeignType = "MessagesCollectionViewFlowLayout is being used on a foreign type."
static let unrecognizedSectionKind = "Received unrecognized element kind:"
static let unrecognizedCheckingResult = "Received an unrecognized NSTextCheckingResult.CheckingType"
static let couldNotLoadAssetsBundle = "MessageKit: Could not load the assets bundle"
static let couldNotCreateAssetsPath = "MessageKit: Could not create path to the assets bundle."
}
@@ -212,7 +212,9 @@ public extension MessagesDisplayDelegate {
// MARK: - Text Messages Defaults
func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
guard let dataSource = messagesCollectionView.messagesDataSource else { return .darkText }
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
return dataSource.isFromCurrentSender(message: message) ? .white : .darkText
}
+15 -6
View File
@@ -192,7 +192,9 @@ public extension MessagesLayoutDelegate {
// MARK: - All Messages Defaults
func messagePadding(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets {
guard let dataSource = messagesCollectionView.messagesDataSource else { return .zero }
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
if dataSource.isFromCurrentSender(message: message) {
return UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 4)
} else {
@@ -201,12 +203,16 @@ public extension MessagesLayoutDelegate {
}
func cellTopLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment {
guard let dataSource = messagesCollectionView.messagesDataSource else { return .cellCenter(.zero) }
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
return dataSource.isFromCurrentSender(message: message) ? .messageTrailing(.zero) : .messageLeading(.zero)
}
func cellBottomLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment {
guard let dataSource = messagesCollectionView.messagesDataSource else { return .cellCenter(.zero) }
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
return dataSource.isFromCurrentSender(message: message) ? .messageLeading(.zero) : .messageTrailing(.zero)
}
@@ -219,7 +225,9 @@ public extension MessagesLayoutDelegate {
}
func headerViewSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else { return .zero }
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError(MessageKitError.nilMessagesDisplayDelegate)
}
let shouldDisplay = displayDelegate.shouldDisplayHeader(for: message, at: indexPath, in: messagesCollectionView)
return shouldDisplay ? CGSize(width: messagesCollectionView.bounds.width, height: 12) : .zero
}
@@ -235,7 +243,9 @@ public extension MessagesLayoutDelegate {
// MARK: - Text Messages Defaults
func messageLabelInset(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets {
guard let dataSource = messagesCollectionView.messagesDataSource else { return .zero }
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError(MessageKitError.nilMessagesDataSource)
}
if dataSource.isFromCurrentSender(message: message) {
return UIEdgeInsets(top: 7, left: 14, bottom: 7, right: 18)
} else {
@@ -264,5 +274,4 @@ public extension MessagesLayoutDelegate {
func widthForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
return maxWidth
}
}
@@ -51,7 +51,7 @@ open class LocationMessageCell: MessageCollectionViewCell {
open override func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
super.configure(with: message, at: indexPath, and: messagesCollectionView)
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError("MessagesDisplayDelegate is not set.")
fatalError(MessageKitError.nilMessagesDisplayDelegate)
}
let options = displayDelegate.snapshotOptionsForLocation(message: message, at: indexPath, in: messagesCollectionView)
let annotationView = displayDelegate.annotationViewForLocation(message: message, at: indexPath, in: messagesCollectionView)
@@ -91,10 +91,10 @@ open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusab
open func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
guard let dataSource = messagesCollectionView.messagesDataSource else {
fatalError("MessagesDataSource is not set.")
fatalError(MessageKitError.nilMessagesDataSource)
}
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError("MessagesDisplayDelegate is not set.")
fatalError(MessageKitError.nilMessagesDisplayDelegate)
}
delegate = messagesCollectionView.messageCellDelegate
+1 -1
View File
@@ -64,7 +64,7 @@ open class TextMessageCell: MessageCollectionViewCell {
super.configure(with: message, at: indexPath, and: messagesCollectionView)
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError("MessagesDisplayDelegate not set.")
fatalError(MessageKitError.nilMessagesDisplayDelegate)
}
let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView)
+1 -1
View File
@@ -283,7 +283,7 @@ open class MessageLabel: UILabel {
case .link:
return urlAttributes
default:
fatalError("Received an unrecognized NSTextCheckingResult.CheckingType")
fatalError(MessageKitError.unrecognizedCheckingResult)
}
}