Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for System Messages #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Example/Sources/Data Generation/SampleData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ final internal class SampleData {
case Phone
case Custom
case ShareContact
case System
}

static let shared = SampleData()
Expand Down Expand Up @@ -234,6 +235,11 @@ final internal class SampleData {
return MockMessage(custom: "Someone left the conversation", user: system, messageId: uniqueID, date: date)
case .ShareContact:
return MockMessage(contact: contactsToShare.random()!, user: user, messageId: uniqueID, date: date)
case .System:
let message = NSAttributedString(string: "John Doe entered the conversation", attributes: [
.font: UIFont.preferredFont(forTextStyle: .footnote)
])
return MockMessage(system: message, user: system, messageId: uniqueID, date: date)
}
}

Expand Down
4 changes: 4 additions & 0 deletions Example/Sources/Models/MockMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ internal struct MockMessage: MessageType {
self.init(kind: .text(text), user: user, messageId: messageId, date: date)
}

init(system: NSAttributedString, user: MockUser, messageId: String, date: Date) {
self.init(kind: .system(system), user: user, messageId: messageId, date: date)
}

init(attributedText: NSAttributedString, user: MockUser, messageId: String, date: Date) {
self.init(kind: .attributedText(attributedText), user: user, messageId: messageId, date: date)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ final internal class SettingsViewController: UITableViewController {
"Url Messages",
"Phone Messages",
"ShareContact Messages",
"System Messages"
]

var messagesPicker = UIPickerView()
Expand Down
4 changes: 4 additions & 0 deletions Sources/Controllers/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ open class MessagesViewController: UIViewController, UICollectionViewDelegateFlo
return cell
case .custom:
return messagesDataSource.customCell(for: message, at: indexPath, in: messagesCollectionView)
case .system:
let cell = messagesCollectionView.dequeueReusableCell(SystemMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Layout/MessagesCollectionViewFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
lazy open var contactMessageSizeCalculator = ContactMessageSizeCalculator(layout: self)
lazy open var typingIndicatorSizeCalculator = TypingCellSizeCalculator(layout: self)
lazy open var linkPreviewMessageSizeCalculator = LinkPreviewMessageSizeCalculator(layout: self)
lazy open var systemMessageSizeCalculator = SystemMessageSizeCalculator(layout: self)

/// A method that by default checks if the section is the last in the
/// `messagesCollectionView` and that `isTypingIndicatorViewHidden`
Expand Down Expand Up @@ -161,6 +162,8 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
return linkPreviewMessageSizeCalculator
case .custom:
return messagesLayoutDelegate.customCellSizeCalculator(for: message, at: indexPath, in: messagesCollectionView)
case .system:
return systemMessageSizeCalculator
}
}

Expand All @@ -181,6 +184,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
audioMessageSizeCalculator,
contactMessageSizeCalculator,
linkPreviewMessageSizeCalculator,
systemMessageSizeCalculator
]
}

Expand Down
53 changes: 53 additions & 0 deletions Sources/Layout/SystemMessageSizeCalculator.swift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
MIT License

Copyright (c) 2017-2019 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

open class SystemMessageSizeCalculator: MessageSizeCalculator {

public override init(layout: MessagesCollectionViewFlowLayout? = nil) {
super.init()
self.layout = layout
}

open override func sizeForItem(at indexPath: IndexPath) -> CGSize {
guard let layout = layout else { return .zero }

let collectionViewWidth = layout.collectionView?.bounds.width ?? 0
let contentInset = layout.collectionView?.contentInset ?? .zero
let inset = layout.sectionInset.left + layout.sectionInset.right + contentInset.left + contentInset.right

guard let messagesCollectionViewFlowLayout = layout as? MessagesCollectionViewFlowLayout else { return .zero }
guard let messagesCollectionView = layout.collectionView as? MessagesCollectionView else { return .zero }

let message = messagesCollectionViewFlowLayout.messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
guard case .system = message.kind else { return .zero }

return CGSize(width: collectionViewWidth - inset, height: 44)
}

open override func cellContentHeight(for message: MessageType, at indexPath: IndexPath) -> CGFloat {
return 44
}
}
3 changes: 3 additions & 0 deletions Sources/Models/MessageKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public enum MessageKind {
/// - MessagesLayoutDelegate: customCellSizeCalculator(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CellSizeCalculator
case custom(Any?)

/// A system message.
case system(NSAttributedString)

// MARK: - Not supported yet

// case system(String)
Expand Down
86 changes: 86 additions & 0 deletions Sources/Views/Cells/SystemMessageCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
MIT License

Copyright (c) 2017-2019 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

/// A subclass of `MessageContentCell` used to display system messages.
open class SystemMessageCell: MessageCollectionViewCell {

/// The label used to display the message's text.
open var label = UILabel()

/// The `MessageCellDelegate` for the cell.
open weak var delegate: MessageCellDelegate?

// MARK: - Lifecycle

public override init(frame: CGRect) {
super.init(frame: frame)
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
setupSubviews()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
setupSubviews()
}

// MARK: - Methods

open func setupSubviews() {
contentView.addSubview(label)
label.textColor = .gray
label.textAlignment = .center
label.numberOfLines = 0
if #available(iOS 10.0, *) {
label.adjustsFontForContentSizeCategory = true
}
}

open override func layoutSubviews() {
super.layoutSubviews()
label.frame = contentView.bounds
}

open override func prepareForReuse() {
super.prepareForReuse()
label.attributedText = nil
label.text = nil
}

open func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
guard case let .system(systemMessage) = message.kind else {
fatalError("Message must be of kind .system to be displayed in SystemMessageCell")
}

delegate = messagesCollectionView.messageCellDelegate

label.attributedText = systemMessage
}

open override func handleTapGesture(_: UIGestureRecognizer) {
delegate?.didTapMessage(in: self)
}
}
1 change: 1 addition & 0 deletions Sources/Views/MessagesCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ open class MessagesCollectionView: UICollectionView {
register(ContactMessageCell.self)
register(TypingIndicatorCell.self)
register(LinkPreviewMessageCell.self)
register(SystemMessageCell.self)
register(MessageReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader)
register(MessageReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter)
}
Expand Down