Skip to content

Decorator

Diego Sánchez edited this page Jul 4, 2016 · 1 revision

Decorator

It was originally conceived as a help for the layout to provide different margins between the messages (depending on how close in time they were sent, and if the sender changes).

Then it was also used to mark what messages should display a tail or not (the last one in a batch from the same sender).

public protocol ChatItemsDecoratorProtocol {
    func decorateItems(chatItems: [ChatItemProtocol]) -> [DecoratedChatItem]
}

public struct DecoratedChatItem: UniqueIdentificable {
    public let uid: String
    public let chatItem: ChatItemProtocol
    public let decorationAttributes: ChatItemDecorationAttributesProtocol?
}

public protocol ChatItemDecorationAttributesProtocol {
    var bottomMargin: CGFloat { get }
}

You can think of this entity as a second data source where you can refine your messages and provide context for them. It is possible to add new ChatItems (for instance time separators).

This collaborator is kind of optional. There's a default one that will decorate the ChatItems with nil decorationAttributes. If you use cells from ChattoAdditions, you must create your own decorator, as ChattoAdditions's presenters expect some metadata along with the messages:

public struct ChatItemDecorationAttributes: ChatItemDecorationAttributesProtocol {
    public let bottomMargin: CGFloat
    public let showsTail: Bool
    public init(bottomMargin: CGFloat, showsTail: Bool) {
        self.bottomMargin = bottomMargin
        self.showsTail = showsTail
    }
}

TL;DR

  • Helps you decouple ui-logic from your data source
  • Provides ui-context to your messages (showsTail, bottomMargin)
  • Can insert new items (time separators, status for messages -sending, delivered, failed-)
  • Decoration happens in a background thread as part of the Update flow
Clone this wiki locally