diff --git a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift index 1a65e1664..2bcb4435f 100644 --- a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift +++ b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift @@ -24,15 +24,7 @@ import Foundation -extension BaseChatViewController: ChatDataSourceDelegateProtocol { - - public func chatDataSourceDidUpdate(_ chatDataSource: ChatDataSourceProtocol, updateType: UpdateType) { - self.enqueueModelUpdate(updateType: updateType) - } - - public func chatDataSourceDidUpdate(_ chatDataSource: ChatDataSourceProtocol) { - self.enqueueModelUpdate(updateType: .normal) - } +extension BaseChatViewController { public func enqueueModelUpdate(updateType: UpdateType) { let newItems = self.chatDataSource?.chatItems ?? [] diff --git a/Chatto/Source/ChatController/BaseChatViewController+Presenters.swift b/Chatto/Source/ChatController/BaseChatViewController+Presenters.swift index 08d07e07c..1107297e5 100644 --- a/Chatto/Source/ChatController/BaseChatViewController+Presenters.swift +++ b/Chatto/Source/ChatController/BaseChatViewController+Presenters.swift @@ -117,9 +117,16 @@ extension BaseChatViewController: ChatCollectionViewLayoutDelegate { } public func createPresenterForChatItem(_ chatItem: ChatItemProtocol) -> ChatItemPresenterProtocol { + assert(self.presenterFactory != nil, "Presenter factory is not initialized") return self.presenterFactory.createChatItemPresenter(chatItem) } + public func confugureCollectionViewWithPresenters() { + assert(self.presenterFactory == nil, "Presenter factory is already initialized") + self.presenterFactory = self.createPresenterFactory() + self.presenterFactory.configure(withCollectionView: self.collectionView) + } + public func decorationAttributesForIndexPath(_ indexPath: IndexPath) -> ChatItemDecorationAttributesProtocol? { return self.chatItemCompanionCollection[indexPath.item].decorationAttributes } diff --git a/Chatto/Source/ChatController/BaseChatViewController.swift b/Chatto/Source/ChatController/BaseChatViewController.swift index c20f2bbb8..cfc23ac85 100644 --- a/Chatto/Source/ChatController/BaseChatViewController.swift +++ b/Chatto/Source/ChatController/BaseChatViewController.swift @@ -24,7 +24,7 @@ import UIKit -open class BaseChatViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { +open class BaseChatViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, ChatDataSourceDelegateProtocol { public typealias ChatItemCompanionCollection = ReadOnlyOrderedDictionary @@ -50,6 +50,8 @@ open class BaseChatViewController: UIViewController, UICollectionViewDataSource, public var updatesConfig = UpdatesConfig() + open var customPresentersConfigurationPoint = false // If true then confugureCollectionViewWithPresenters() will not be called in viewDidLoad() method and has to be called manually + public private(set) var collectionView: UICollectionView! public final internal(set) var chatItemCompanionCollection: ChatItemCompanionCollection = ReadOnlyOrderedDictionary(items: []) private var _chatDataSource: ChatDataSourceProtocol? @@ -152,8 +154,9 @@ open class BaseChatViewController: UIViewController, UICollectionViewDataSource, self.accessoryViewRevealer = AccessoryViewRevealer(collectionView: self.collectionView) - self.presenterFactory = self.createPresenterFactory() - self.presenterFactory.configure(withCollectionView: self.collectionView) + if !self.customPresentersConfigurationPoint { + self.confugureCollectionViewWithPresenters() + } } var unfinishedBatchUpdatesCount: Int = 0 @@ -325,7 +328,7 @@ open class BaseChatViewController: UIViewController, UICollectionViewDataSource, var accessoryViewRevealer: AccessoryViewRevealer! public private(set) var inputContainer: UIView! public private(set) var bottomSpaceView: UIView! - var presenterFactory: ChatItemPresenterFactoryProtocol! + public internal(set) var presenterFactory: ChatItemPresenterFactoryProtocol! let presentersByCell = NSMapTable(keyOptions: .weakMemory, valueOptions: .weakMemory) var visibleCells: [IndexPath: UICollectionViewCell] = [:] // @see visibleCellsAreValid(changes:) @@ -372,6 +375,16 @@ open class BaseChatViewController: UIViewController, UICollectionViewDataSource, let firstItemMoved = changes.movedIndexPaths.first return (firstItemMoved?.indexPathOld as IndexPath?, firstItemMoved?.indexPathNew as IndexPath?) } + + // MARK: ChatDataSourceDelegateProtocol + + open func chatDataSourceDidUpdate(_ chatDataSource: ChatDataSourceProtocol, updateType: UpdateType) { + self.enqueueModelUpdate(updateType: updateType) + } + + open func chatDataSourceDidUpdate(_ chatDataSource: ChatDataSourceProtocol) { + self.enqueueModelUpdate(updateType: .normal) + } } extension BaseChatViewController { // Rotation