Skip to content

Commit

Permalink
Merge pull request #528 from badoo/lazy_initialization_of_builders
Browse files Browse the repository at this point in the history
Creation of a presenter factory is postponed until it's needed for it…
AntonPalich authored Nov 7, 2018

Verified

This commit was signed with the committer’s verified signature. The key has expired.
danielleadams Danielle Adams
2 parents aa67bac + 7cbac82 commit 6c33413
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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 ?? []
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 17 additions & 4 deletions Chatto/Source/ChatController/BaseChatViewController.swift
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@

import UIKit

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

public typealias ChatItemCompanionCollection = ReadOnlyOrderedDictionary<ChatItemCompanion>

@@ -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<UICollectionViewCell, AnyObject>(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

0 comments on commit 6c33413

Please sign in to comment.