From 0a8df7b7a929318023263e8a020c10bd7b381282 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 3 Oct 2017 22:05:59 -0500 Subject: [PATCH 1/4] [Deprecate] dequeueMessageHeaderView & dequeueMessageFooterView --- Sources/MessageKit+Availability.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/MessageKit+Availability.swift b/Sources/MessageKit+Availability.swift index 292cc4a7..ef9a9aac 100644 --- a/Sources/MessageKit+Availability.swift +++ b/Sources/MessageKit+Availability.swift @@ -38,6 +38,16 @@ public extension MessagesCollectionView { return nil } + @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use dequeueReusableHeaderView") + public func dequeueMessageHeaderView(withReuseIdentifier identifier: String = "MessageHeaderView", for indexPath: IndexPath) -> MessageHeaderView { + return dequeueReusableHeaderView(MessageHeaderView.self, for: indexPath) + } + + @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use dequeueReusableFooterView") + public func dequeueMessageFooterView(withReuseIdentifier identifier: String = "MessageFooterView", for indexPath: IndexPath) -> MessageFooterView { + return dequeueReusableFooterView(MessageFooterView.self, for: indexPath) + } + } // MARK: - MessagesCollectionViewFlowLayout From ef3e2c02974e49724cfa6c5347ee070edbf7f97f Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 3 Oct 2017 22:06:50 -0500 Subject: [PATCH 2/4] Add generic method for header/footer dequeueing and change extension to MessagesCollectionView --- Sources/UICollectionView+Extensions.swift | 52 +++++++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/Sources/UICollectionView+Extensions.swift b/Sources/UICollectionView+Extensions.swift index 3d2b7c3d..dc5d1ff1 100644 --- a/Sources/UICollectionView+Extensions.swift +++ b/Sources/UICollectionView+Extensions.swift @@ -1,19 +1,35 @@ -// -// UICollectionView+Extensions.swift -// MessageKit -// -// Created by Frederic Barthelemy on 10/3/17. -// Copyright © 2017 MessageKit. All rights reserved. -// +/* + 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 Foundation /// Optional Cell Protocol to Simplify registration/cell type loading in a generic way -protocol CollectionViewReusable: class { +public protocol CollectionViewReusable: class { static func reuseIdentifier() -> String } -extension UICollectionView { +public extension MessagesCollectionView { /// Registers a particular cell using its reuse-identifier func register(_ cellClass: CellType.Type) { register(cellClass, forCellWithReuseIdentifier: CellType.reuseIdentifier()) @@ -33,4 +49,22 @@ extension UICollectionView { } return cell } + + /// Generically dequeues a header of the correct type allowing you to avoid scattering your code with guard-let-else-fatal + func dequeueReusableHeaderView(_ viewClass: ViewType.Type, for indexPath: IndexPath) -> ViewType { + let view = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: viewClass.reuseIdentifier(), for: indexPath) + guard let viewType = view as? ViewType else { + fatalError("Unable to dequeue \(String(describing: viewClass)) with reuseId of \(viewClass.reuseIdentifier())") + } + return viewType + } + + /// Generically dequeues a footer of the correct type allowing you to avoid scattering your code with guard-let-else-fatal + func dequeueReusableFooterView(_ viewClass: ViewType.Type, for indexPath: IndexPath) -> ViewType { + let view = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: viewClass.reuseIdentifier(), for: indexPath) + guard let viewType = view as? ViewType else { + fatalError("Unable to dequeue \(String(describing: viewClass)) with reuseId of \(viewClass.reuseIdentifier())") + } + return viewType + } } From 20f5f662375edd7b984b9a544e4db17e64c72a22 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 3 Oct 2017 22:08:09 -0500 Subject: [PATCH 3/4] [Changed] MessagesDisplayDelegate footer & header signatures to return non-optionals --- Example/Sources/ConversationViewController.swift | 4 ---- Sources/MessagesCollectionView.swift | 10 ---------- Sources/MessagesDisplayDelegate.swift | 14 +++++++------- Sources/MessagesViewController.swift | 10 +++++----- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index 391dc3c8..7806f6b3 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -235,10 +235,6 @@ extension ConversationViewController: MessagesDisplayDelegate { // return .custom(configurationClosure) } - func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView? { - return messagesCollectionView.dequeueMessageFooterView(for: indexPath) - } - } // MARK: - MessagesLayoutDelegate diff --git a/Sources/MessagesCollectionView.swift b/Sources/MessagesCollectionView.swift index e3e8e45a..811eb8e7 100644 --- a/Sources/MessagesCollectionView.swift +++ b/Sources/MessagesCollectionView.swift @@ -64,14 +64,4 @@ open class MessagesCollectionView: UICollectionView { scrollToItem(at: indexPath, at: .bottom, animated: animated) } - open func dequeueMessageHeaderView(withReuseIdentifier identifier: String = "MessageHeaderView", for indexPath: IndexPath) -> MessageHeaderView { - let header = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: identifier, for: indexPath) - return header as? MessageHeaderView ?? MessageHeaderView() - } - - open func dequeueMessageFooterView(withReuseIdentifier identifier: String = "MessageFooterView", for indexPath: IndexPath) -> MessageFooterView { - let footer = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: identifier, for: indexPath) - return footer as? MessageFooterView ?? MessageFooterView() - } - } diff --git a/Sources/MessagesDisplayDelegate.swift b/Sources/MessagesDisplayDelegate.swift index 3b8d3b2d..b1d17099 100644 --- a/Sources/MessagesDisplayDelegate.swift +++ b/Sources/MessagesDisplayDelegate.swift @@ -32,11 +32,11 @@ public protocol MessagesDisplayDelegate: class { func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor - func messageHeaderView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageHeaderView? + func messageHeaderView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageHeaderView func shouldDisplayHeader(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> Bool - func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView? + func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView } @@ -56,9 +56,9 @@ public extension MessagesDisplayDelegate { return dataSource.isFromCurrentSender(message: message) ? .outgoingGreen : .incomingGray } - func messageHeaderView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageHeaderView? { - let header = messagesCollectionView.dequeueMessageHeaderView(withReuseIdentifier: "MessageDateHeaderView", for: indexPath) as? MessageDateHeaderView - header?.dateLabel.text = MessageKitDateFormatter.shared.string(from: message.sentDate) + func messageHeaderView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageHeaderView { + let header = messagesCollectionView.dequeueReusableHeaderView(MessageDateHeaderView.self, for: indexPath) + header.dateLabel.text = MessageKitDateFormatter.shared.string(from: message.sentDate) return header } @@ -72,8 +72,8 @@ public extension MessagesDisplayDelegate { return timeIntervalSinceLastMessage >= messagesCollectionView.showsDateHeaderAfterTimeInterval } - func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView? { - return nil + func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView { + return messagesCollectionView.dequeueReusableFooterView(MessageFooterView.self, for: indexPath) } } diff --git a/Sources/MessagesViewController.swift b/Sources/MessagesViewController.swift index 72051a67..5327a7d4 100644 --- a/Sources/MessagesViewController.swift +++ b/Sources/MessagesViewController.swift @@ -172,15 +172,15 @@ extension MessagesViewController: UICollectionViewDataSource { switch message.data { case .text, .attributedText: - let cell = collectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath) + let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath) cell.configure(with: message, at: indexPath, and: messagesCollectionView) return cell case .photo, .video: - let cell = collectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath) + let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath) cell.configure(with: message, at: indexPath, and: messagesCollectionView) return cell case .location: - let cell = collectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath) + let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath) cell.configure(with: message, at: indexPath, and: messagesCollectionView) return cell } @@ -197,9 +197,9 @@ extension MessagesViewController: UICollectionViewDataSource { switch kind { case UICollectionElementKindSectionHeader: - return displayDelegate.messageHeaderView(for: message, at: indexPath, in: messagesCollectionView) ?? MessageHeaderView() + return displayDelegate.messageHeaderView(for: message, at: indexPath, in: messagesCollectionView) case UICollectionElementKindSectionFooter: - return displayDelegate.messageFooterView(for: message, at: indexPath, in: messagesCollectionView) ?? MessageFooterView() + return displayDelegate.messageFooterView(for: message, at: indexPath, in: messagesCollectionView) default: fatalError("Unrecognized element of kind: \(kind)") } From 94a288de6da3436da1cfa1328bf8674ae2953688 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 3 Oct 2017 22:23:57 -0500 Subject: [PATCH 4/4] [Changelog] Add changelog entry for #229 --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe6a9244..5e48b833 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,6 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa - `additionalTopContentInset` property to `MessagesColectionViewController` to allow users to account for extra subviews. [#218](https://github.com/MessageKit/MessageKit/pull/218) by [@SD10](https://github.com/SD10). - ### Fixed - `MessageInputBar` now correctly sizes itself when breaking its max height or pasting in large amounts of text @@ -65,6 +64,13 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa - **Breaking Change** `InputTextView`'s `UITextViewDelegate` is now set to `self` [#173](https://github.com/MessageKit/MessageKit/pull/173) by [@nathantannar4](https://github.com/nathantannar4). +- **Breaking Change** `MessagesDisplayDelegate` `messageHeaderView(for:at:in)` and `messageFooterView(for:at:in)` to return non-optionals. +[#229](https://github.com/MessageKit/MessageKit/pull/229) by [@SD10](https://github.com/SD10). + +- **Breaking Change** `MessagesCollectionView` `dequeueMessageHeaderView(withIdentifier:for:)` & `dequeueMessageFooterView(widthIdentifier:for:)` +have been renamed to `dequeueReusableHeaderView(CollectionViewReusable.Type,for:)` & `dequeueReusableFooterView(CollectionViewReusable.Type,for:)`. +[#229](https://github.com/MessageKit/MessageKit/pull/229) by [@SD10](https://github.com/SD10). + - `configure` method of all `MessageCollectionViewCell` types to be marked as `open`. [#200](https://github.com/MessageKit/MessageKit/pull/200) by [@SD10](https://github.com/sd10).