From 810b58d88d8b9d82ada648e7ebfa50fa4ef82aa2 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Thu, 11 Jan 2018 22:42:50 -0600 Subject: [PATCH] Add MessageKitError to cleanup strings --- .../MessagesViewController+DataSource.swift | 23 ++++++------ .../MessagesViewController+Delegate.swift | 37 +++++++++++++------ Sources/Extensions/Bundle+Extensions.swift | 4 +- .../MessageIntermediateLayoutAttributes.swift | 4 +- .../MessagesCollectionViewFlowLayout.swift | 10 ++--- Sources/Models/MessageKitError.swift | 36 ++++++++++++++++++ .../Protocols/MessagesDisplayDelegate.swift | 4 +- .../Protocols/MessagesLayoutDelegate.swift | 21 ++++++++--- Sources/Views/Cells/LocationMessageCell.swift | 2 +- .../Cells/MessageCollectionViewCell.swift | 4 +- Sources/Views/Cells/TextMessageCell.swift | 2 +- Sources/Views/MessageLabel.swift | 2 +- 12 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 Sources/Models/MessageKitError.swift diff --git a/Sources/Controllers/MessagesViewController+DataSource.swift b/Sources/Controllers/MessagesViewController+DataSource.swift index b14a6a5e..21c3c3ee 100644 --- a/Sources/Controllers/MessagesViewController+DataSource.swift +++ b/Sources/Controllers/MessagesViewController+DataSource.swift @@ -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) } } } diff --git a/Sources/Controllers/MessagesViewController+Delegate.swift b/Sources/Controllers/MessagesViewController+Delegate.swift index 5d5c7235..8d5224ab 100644 --- a/Sources/Controllers/MessagesViewController+Delegate.swift +++ b/Sources/Controllers/MessagesViewController+Delegate.swift @@ -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) diff --git a/Sources/Extensions/Bundle+Extensions.swift b/Sources/Extensions/Bundle+Extensions.swift index 8c8b88af..ff27d1b2 100644 --- a/Sources/Extensions/Bundle+Extensions.swift +++ b/Sources/Extensions/Bundle+Extensions.swift @@ -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 diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index 0164db19..d8ede2a0 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -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) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 0ce2aa66..698c2128 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -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) } } diff --git a/Sources/Models/MessageKitError.swift b/Sources/Models/MessageKitError.swift new file mode 100644 index 00000000..f26797c4 --- /dev/null +++ b/Sources/Models/MessageKitError.swift @@ -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." +} diff --git a/Sources/Protocols/MessagesDisplayDelegate.swift b/Sources/Protocols/MessagesDisplayDelegate.swift index d2bd79b4..d98d186a 100644 --- a/Sources/Protocols/MessagesDisplayDelegate.swift +++ b/Sources/Protocols/MessagesDisplayDelegate.swift @@ -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 } diff --git a/Sources/Protocols/MessagesLayoutDelegate.swift b/Sources/Protocols/MessagesLayoutDelegate.swift index 728f242e..30c207bb 100644 --- a/Sources/Protocols/MessagesLayoutDelegate.swift +++ b/Sources/Protocols/MessagesLayoutDelegate.swift @@ -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 } - } diff --git a/Sources/Views/Cells/LocationMessageCell.swift b/Sources/Views/Cells/LocationMessageCell.swift index 2368ad4e..d80ee9e4 100644 --- a/Sources/Views/Cells/LocationMessageCell.swift +++ b/Sources/Views/Cells/LocationMessageCell.swift @@ -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) diff --git a/Sources/Views/Cells/MessageCollectionViewCell.swift b/Sources/Views/Cells/MessageCollectionViewCell.swift index df5627f1..592313a6 100644 --- a/Sources/Views/Cells/MessageCollectionViewCell.swift +++ b/Sources/Views/Cells/MessageCollectionViewCell.swift @@ -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 diff --git a/Sources/Views/Cells/TextMessageCell.swift b/Sources/Views/Cells/TextMessageCell.swift index bd040ee4..4f63c048 100644 --- a/Sources/Views/Cells/TextMessageCell.swift +++ b/Sources/Views/Cells/TextMessageCell.swift @@ -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) diff --git a/Sources/Views/MessageLabel.swift b/Sources/Views/MessageLabel.swift index fc7a6b63..ff1f2382 100644 --- a/Sources/Views/MessageLabel.swift +++ b/Sources/Views/MessageLabel.swift @@ -283,7 +283,7 @@ open class MessageLabel: UILabel { case .link: return urlAttributes default: - fatalError("Received an unrecognized NSTextCheckingResult.CheckingType") + fatalError(MessageKitError.unrecognizedCheckingResult) } }