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

Creation of a presenter factory is postponed until it's needed for it… #528

Merged
merged 3 commits into from
Nov 7, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 17 additions & 4 deletions Chatto/Source/ChatController/BaseChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import UIKit

open class BaseChatViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
open class BaseChatViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, ChatDataSourceDelegateProtocol {

public typealias ChatItemCompanionCollection = ReadOnlyOrderedDictionary<ChatItemCompanion>

Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<UICollectionViewCell, AnyObject>(keyOptions: .weakMemory, valueOptions: .weakMemory)
var visibleCells: [IndexPath: UICollectionViewCell] = [:] // @see visibleCellsAreValid(changes:)

Expand Down Expand Up @@ -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
Expand Down