From 592c8ee142c17e5cdd1f4d698aeae5707cc549bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20S=C3=A1nchez?= Date: Wed, 1 Jun 2016 12:30:53 +0100 Subject: [PATCH] Allows the decorator to override item ids (#143) --- Chatto/Source/Chat Items/ChatItemCompanion.swift | 13 +++++++++---- .../BaseChatViewController+Changes.swift | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Chatto/Source/Chat Items/ChatItemCompanion.swift b/Chatto/Source/Chat Items/ChatItemCompanion.swift index c5c903c..c47c341 100644 --- a/Chatto/Source/Chat Items/ChatItemCompanion.swift +++ b/Chatto/Source/Chat Items/ChatItemCompanion.swift @@ -28,20 +28,25 @@ public protocol ChatItemsDecoratorProtocol { func decorateItems(chatItems: [ChatItemProtocol]) -> [DecoratedChatItem] } -public struct DecoratedChatItem { +public struct DecoratedChatItem: UniqueIdentificable { + public let uid: String public let chatItem: ChatItemProtocol public let decorationAttributes: ChatItemDecorationAttributesProtocol? + public init(chatItem: ChatItemProtocol, decorationAttributes: ChatItemDecorationAttributesProtocol?) { + self.init(uid: chatItem.uid, chatItem: chatItem, decorationAttributes: decorationAttributes) + } + + public init(uid: String, chatItem: ChatItemProtocol, decorationAttributes: ChatItemDecorationAttributesProtocol?) { + self.uid = uid self.chatItem = chatItem self.decorationAttributes = decorationAttributes } } public struct ChatItemCompanion: UniqueIdentificable { + public let uid: String public let chatItem: ChatItemProtocol public let presenter: ChatItemPresenterProtocol public var decorationAttributes: ChatItemDecorationAttributesProtocol? - public var uid: String { - return self.chatItem.uid - } } diff --git a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift index c8666c2..457d166 100644 --- a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift +++ b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift @@ -201,7 +201,7 @@ extension BaseChatViewController: ChatDataSourceDelegateProtocol { private func createModelUpdates(newItems newItems: [ChatItemProtocol], oldItems: ChatItemCompanionCollection, collectionViewWidth: CGFloat) -> (changes: CollectionChanges, updateModelClosure: () -> Void) { let newDecoratedItems = self.chatItemsDecorator?.decorateItems(newItems) ?? newItems.map { DecoratedChatItem(chatItem: $0, decorationAttributes: nil) } - let changes = Chatto.generateChanges(oldCollection: oldItems.map { $0.chatItem }, newCollection: newDecoratedItems.map { $0.chatItem }) + let changes = Chatto.generateChanges(oldCollection: oldItems.lazy.map { $0 }, newCollection: newDecoratedItems.lazy.map { $0 }) let itemCompanionCollection = self.createCompanionCollection(fromChatItems: newDecoratedItems, previousCompanionCollection: oldItems) let layoutModel = self.createLayoutModel(itemCompanionCollection, collectionViewWidth: collectionViewWidth) let updateModelClosure : () -> Void = { [weak self] in @@ -220,12 +220,12 @@ extension BaseChatViewController: ChatDataSourceDelegateProtocol { // Oherwise updateVisibleCells may try to update existing cell with a new presenter which is working with a different type of cell // Optimization: reuse presenter if it's the same instance. - if let oldChatItemCompanion = oldItems[chatItem.uid] where oldChatItemCompanion.chatItem === chatItem { + if let oldChatItemCompanion = oldItems[decoratedChatItem.uid] where oldChatItemCompanion.chatItem === chatItem { presenter = oldChatItemCompanion.presenter } else { presenter = self.createPresenterForChatItem(decoratedChatItem.chatItem) } - return ChatItemCompanion(chatItem: decoratedChatItem.chatItem, presenter: presenter, decorationAttributes: decoratedChatItem.decorationAttributes) + return ChatItemCompanion(uid: decoratedChatItem.uid, chatItem: decoratedChatItem.chatItem, presenter: presenter, decorationAttributes: decoratedChatItem.decorationAttributes) }) }