// // CustomMessageFlowLayout.swift // Messenger // // Created by Nathan Tannar on 2018-07-14. // Copyright © 2018 Nathan Tannar. All rights reserved. // import Foundation import MessageKit open class CustomMessagesFlowLayout: MessagesCollectionViewFlowLayout { open lazy var customMessageSizeCalculator = CustomMessageSizeCalculator(layout: self) open override func cellSizeCalculatorForItem(at indexPath: IndexPath) -> CellSizeCalculator { // if isSectionReservedForTypingBubble(indexPath.section) { // return typingMessageSizeCalculator // } let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView) if case .custom = message.kind { return customMessageSizeCalculator } return super.cellSizeCalculatorForItem(at: indexPath) } open override func messageSizeCalculators() -> [MessageSizeCalculator] { var superCalculators = super.messageSizeCalculators() // Append any of your custom `MessageSizeCalculator` if you wish for the convenience // functions to work such as `setMessageIncoming...` or `setMessageOutgoing...` superCalculators.append(customMessageSizeCalculator) return superCalculators } } open class CustomMessageSizeCalculator: 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 return CGSize(width: collectionViewWidth - inset, height: 44) } }