chatsecureios / ChatSecure / Classes / View Controllers / OTRMessagesCollectionViewFlowLayout.swift @ 052fc0b6
History | View | Annotate | Download (3.4 KB)
1 |
// |
---|---|
2 |
// OTRMessagesCollectionViewFlowLayout.swift |
3 |
// ChatSecure |
4 |
// |
5 |
// Created by David Chiles on 3/4/16. |
6 |
// Copyright © 2016 Chris Ballinger. All rights reserved. |
7 |
// |
8 |
|
9 |
import Foundation |
10 |
|
11 |
import JSQMessagesViewController |
12 |
|
13 |
@objc public protocol OTRMessagesCollectionViewFlowLayoutSizeProtocol { |
14 |
func hasBubbleSizeForCellAtIndexPath(_ indexPath: IndexPath) -> Bool |
15 |
} |
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 |
|
32 |
@objc open class OTRMessagesCollectionViewFlowLayout:JSQMessagesCollectionViewFlowLayout { |
33 |
|
34 |
@objc open weak var sizeDelegate:OTRMessagesCollectionViewFlowLayoutSizeProtocol? |
35 |
@objc open weak var supplementaryViewDelegate:OTRMessagesCollectionViewFlowLayoutSupplementaryViewProtocol? |
36 |
|
37 |
@objc override open func messageBubbleSizeForItem(at indexPath: IndexPath!) -> CGSize { |
38 |
guard let delegate = self.sizeDelegate, !delegate.hasBubbleSizeForCellAtIndexPath(indexPath) else { |
39 |
return super.messageBubbleSizeForItem(at: indexPath) |
40 |
} |
41 |
|
42 |
//Set width to one because of an Assert inside of JSQMessagesViewController |
43 |
return CGSize(width: 1, height: 0) |
44 |
} |
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 |
} |
83 |
} |