Skip to content

Commit

Permalink
Don't update cell if model didn't change
Browse files Browse the repository at this point in the history
  • Loading branch information
wiruzx committed Feb 5, 2019
1 parent a4839c9 commit 80dd088
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Chatto
public final class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandlerT>
: BaseMessagePresenter<CompoundBubbleView, ViewModelBuilderT, InteractionHandlerT> where
ViewModelBuilderT: ViewModelBuilderProtocol,
ViewModelBuilderT.ModelT: Equatable,
InteractionHandlerT: BaseMessageInteractionHandlerProtocol,
InteractionHandlerT.ViewModelT == ViewModelBuilderT.ViewModelT {

Expand All @@ -43,7 +44,7 @@ public final class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandle
viewModelBuilder: ViewModelBuilderT,
interactionHandler: InteractionHandlerT?,
contentFactories: [AnyMessageContentFactory<ModelT>],
sizingCell: CompoundMessageCollectionViewCell,
sizingCell: CompoundMessageCollectionViewCell<ModelT>,
baseCellStyle: BaseMessageCollectionViewCellStyleProtocol,
compoundCellStyle: CompoundBubbleViewStyleProtocol,
cache: Cache<CompoundBubbleLayoutProvider.Configuration, CompoundBubbleLayoutProvider>
Expand All @@ -65,7 +66,7 @@ public final class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandle
}

public override class func registerCells(_ collectionView: UICollectionView) {
collectionView.register(CompoundMessageCollectionViewCell.self,
collectionView.register(CompoundMessageCollectionViewCell<ModelT>.self,
forCellWithReuseIdentifier: .compoundCellReuseId)
}

Expand All @@ -85,12 +86,14 @@ public final class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandle
decorationAttributes: ChatItemDecorationAttributes,
animated: Bool,
additionalConfiguration: (() -> Void)?) {
guard let compoundCell = cell as? CompoundMessageCollectionViewCell else {
assertionFailure("\(cell) is not CompoundMessageCollectionViewCell")
guard let compoundCell = cell as? CompoundMessageCollectionViewCell<ModelT> else {
assertionFailure("\(cell) is not CompoundMessageCollectionViewCell<\(ModelT.self)>")
return
}

super.configureCell(cell, decorationAttributes: decorationAttributes, animated: animated) {
super.configureCell(compoundCell, decorationAttributes: decorationAttributes, animated: animated) {
guard compoundCell.lastDisplayedModel != self.messageModel else { return }
compoundCell.lastDisplayedModel = self.messageModel
let modules = self.contentFactories.map { $0.createMessageModule(forModel: self.messageModel) }
let bubbleView = compoundCell.bubbleView!
let borderedViewIndexes = modules.enumerated().compactMap { index, module in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Chatto
@available(iOS 11, *)
public final class CompoundMessagePresenterBuilder<ViewModelBuilderT, InteractionHandlerT>: ChatItemPresenterBuilderProtocol where
ViewModelBuilderT: ViewModelBuilderProtocol,
ViewModelBuilderT.ModelT: Equatable,
InteractionHandlerT: BaseMessageInteractionHandlerProtocol,
InteractionHandlerT.ViewModelT == ViewModelBuilderT.ViewModelT {
public typealias ModelT = ViewModelBuilderT.ModelT
Expand All @@ -43,7 +44,7 @@ public final class CompoundMessagePresenterBuilder<ViewModelBuilderT, Interactio
public let viewModelBuilder: ViewModelBuilderT
public let interactionHandler: InteractionHandlerT?
private let contentFactories: [AnyMessageContentFactory<ModelT>]
public let sizingCell: CompoundMessageCollectionViewCell = CompoundMessageCollectionViewCell()
public let sizingCell: CompoundMessageCollectionViewCell = CompoundMessageCollectionViewCell<ModelT>()
public lazy var compoundCellStyle: CompoundBubbleViewStyleProtocol = DefaultCompoundBubbleViewStyle()
public lazy var baseCellStyle: BaseMessageCollectionViewCellStyleProtocol = BaseMessageCollectionViewCellDefaultStyle()
private let cache = Cache<CompoundBubbleLayoutProvider.Configuration, CompoundBubbleLayoutProvider>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
// THE SOFTWARE.

@available(iOS 11, *)
public final class CompoundMessageCollectionViewCell: BaseMessageCollectionViewCell<CompoundBubbleView> {
public final class CompoundMessageCollectionViewCell<Model: Equatable>: BaseMessageCollectionViewCell<CompoundBubbleView> {
var lastDisplayedModel: Model?
public override func createBubbleView() -> CompoundBubbleView! {
return CompoundBubbleView()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import ChattoAdditions

final class DemoCompoundMessageModel: DecoratedMessageModelProtocol, DemoMessageModelProtocol {
final class DemoCompoundMessageModel: Equatable, DecoratedMessageModelProtocol, DemoMessageModelProtocol {

// MARK: - Instantiation

Expand All @@ -47,4 +47,12 @@ final class DemoCompoundMessageModel: DecoratedMessageModelProtocol, DemoMessage

var status: MessageStatus

// MARK: - Equatable

static func == (lhs: DemoCompoundMessageModel, rhs: DemoCompoundMessageModel) -> Bool {
return lhs.text == rhs.text
&& lhs.image == rhs.image
&& lhs.messageModel.uid == rhs.messageModel.uid
&& lhs.status == rhs.status
}
}

0 comments on commit 80dd088

Please sign in to comment.