Skip to content

Commit

Permalink
Support of the message update if uid is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGasanov committed Aug 12, 2019
1 parent 8d6e119 commit 3f7daf5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,18 @@ open class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandlerT>

let isUpdateNeeded = !self.messageModel.hasSameContent(as: newMessageModel)
self.messageModel = newMessageModel
if isUpdateNeeded { self.updateContent() }

guard !isUpdateNeeded else {
self.updateContent()
return
}

let isUidUpdateNeeded = self.messageModel.uid != newMessageModel.uid

guard !isUidUpdateNeeded else {
self.updateContentPresenters(with: newMessageModel.uid)
return
}
}

private func updateContent() {
Expand All @@ -103,6 +114,12 @@ open class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandlerT>
self.menuPresenter = self.contentFactories.lazy.compactMap { $0.createMenuPresenter(forModel: self.messageModel) }.first
}

private func updateContentPresenters(with newMessage: Any) {
self.contentPresenters.forEach {
$0.updateMessage(newMessage)
}
}

open override func dequeueCell(collectionView: UICollectionView, indexPath: IndexPath) -> UICollectionViewCell {
let cellReuseIdentifier = self.compoundCellReuseId
collectionView.register(CompoundMessageCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ public final class DefaultMessageContentPresenter<MessageType, ViewType: UIView>
public func unbindFromView() {
self.onUnbinding?(self.view)
}

public func updateMessage(_ newMessage: Any) {
guard let message = newMessage as? MessageType else {
assertionFailure("Unexpected message type: \(type(of: newMessage))")
return
}
self.message = message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public protocol MessageContentPresenterProtocol {

func bindToView(with viewReference: ViewReference)
func unbindFromView()

func updateMessage(_ newMessage: Any)
}

0 comments on commit 3f7daf5

Please sign in to comment.