Revision 052fc0b6

View differences:

ChatSecure/Classes/View Controllers/OTRMessagesCollectionViewFlowLayout.swift
14 14
    func hasBubbleSizeForCellAtIndexPath(_ indexPath: IndexPath) -> Bool
15 15
}
16 16

  
17
@objc public class OTRMessagesCollectionSupplementaryViewInfo: NSObject {
18
    public var kind:String
19
    public var height:CGFloat
20

  
21
    @objc public init(kind:String, height:CGFloat) {
22
        self.kind = kind
23
        self.height = height
24
        super.init()
25
    }
26
}
27

  
28
@objc public protocol OTRMessagesCollectionViewFlowLayoutSupplementaryViewProtocol {
29
    func supplementaryViewsForCellAtIndexPath(_ indexPath: IndexPath) -> [OTRMessagesCollectionSupplementaryViewInfo]?
30
}
31

  
17 32
@objc open class OTRMessagesCollectionViewFlowLayout:JSQMessagesCollectionViewFlowLayout {
18 33
    
19 34
    @objc open weak var sizeDelegate:OTRMessagesCollectionViewFlowLayoutSizeProtocol?
20
    
35
    @objc open weak var supplementaryViewDelegate:OTRMessagesCollectionViewFlowLayoutSupplementaryViewProtocol?
36

  
21 37
    @objc override open func messageBubbleSizeForItem(at indexPath: IndexPath!) -> CGSize {
22 38
        guard let delegate = self.sizeDelegate, !delegate.hasBubbleSizeForCellAtIndexPath(indexPath) else {
23 39
            return super.messageBubbleSizeForItem(at: indexPath)
......
27 43
        return CGSize(width: 1, height: 0)
28 44
    }
29 45
    
46
    open override var collectionViewContentSize: CGSize {
47
        var size = super.collectionViewContentSize
48
        if let delegate = self.supplementaryViewDelegate {
49
            for section in 0..<self.collectionView.numberOfSections {
50
                for item in 0..<self.collectionView.numberOfItems(inSection: section) {
51
                    if let views = delegate.supplementaryViewsForCellAtIndexPath(IndexPath(item: item, section: section)) {
52
                        for view in views {
53
                            size.height += view.height
54
                        }
55
                    }
56
                }
57
            }
58
        }
59
        return size
60
    }
61
    
62
    override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
63
        guard var attributes = super.layoutAttributesForElements(in: rect) else { return nil }
64
        
65
        var offset:CGFloat = 0
66
        for attribute in attributes {
67
            if (attribute.representedElementCategory == UICollectionElementCategory.cell) {
68
                attribute.frame = attribute.frame.offsetBy(dx: 0, dy: offset)
69
                if let delegate = self.supplementaryViewDelegate {
70
                    if let views = delegate.supplementaryViewsForCellAtIndexPath(attribute.indexPath) {
71
                        for view in views {
72
                            offset += view.height
73
                            let suppViewAttrs = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: view.kind, with: attribute.indexPath)
74
                            suppViewAttrs.frame = CGRect(x: attribute.frame.origin.x, y: attribute.frame.origin.y+attribute.frame.size.height, width: attribute.frame.size.width, height: view.height)
75
                            attributes.append(suppViewAttrs)
76
                        }
77
                    }
78
                }
79
            }
80
        }
81
        return attributes
82
    }
30 83
}

Also available in: Unified diff