Skip to content

Commit 698e9d2

Browse files
committed
Changed ChatLayoutDelegate and improved performance
1 parent e982336 commit 698e9d2

File tree

97 files changed

+4248
-4045
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+4248
-4045
lines changed

ChatLayout.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ChatLayout'
3-
s.version = '0.3.0'
3+
s.version = '0.4.0'
44
s.summary = 'An alternative solution to MessageKit. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
55
s.swift_version = '5.2'
66

ChatLayout/Classes/Core/ChatLayout.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public final class ChatLayout: UICollectionViewLayout {
135135
static let shouldInvalidateOnBoundsChange = InvalidationActions(rawValue: 1 << 0)
136136
}
137137

138-
private lazy var controller = StateController(collectionLayout: self)
138+
private lazy var controller = StateController(layoutRepresentation: self)
139139

140140
private var state: ModelState = .beforeUpdate
141141

@@ -250,7 +250,7 @@ public final class ChatLayout: UICollectionViewLayout {
250250
for sectionIndex in 0..<collectionView.numberOfSections {
251251
// Header
252252
let header: ItemModel?
253-
if delegate?.shouldPresentHeader(at: sectionIndex) == true {
253+
if delegate?.shouldPresentHeader(self, at: sectionIndex) == true {
254254
let headerIndexPath = IndexPath(item: 0, section: sectionIndex)
255255
header = ItemModel(with: configuration(for: .header, at: headerIndexPath))
256256
} else {
@@ -266,7 +266,7 @@ public final class ChatLayout: UICollectionViewLayout {
266266

267267
// Footer
268268
let footer: ItemModel?
269-
if delegate?.shouldPresentFooter(at: sectionIndex) == true {
269+
if delegate?.shouldPresentFooter(self, at: sectionIndex) == true {
270270
let footerIndexPath = IndexPath(item: 0, section: sectionIndex)
271271
footer = ItemModel(with: configuration(for: .footer, at: footerIndexPath))
272272
} else {
@@ -288,7 +288,7 @@ public final class ChatLayout: UICollectionViewLayout {
288288
var section = controller.section(at: sectionIndex, at: state)
289289

290290
// Header
291-
if delegate?.shouldPresentHeader(at: sectionIndex) == true {
291+
if delegate?.shouldPresentHeader(self, at: sectionIndex) == true {
292292
var header = section.header
293293
header?.resetSize()
294294
let headerIndexPath = IndexPath(item: 0, section: sectionIndex)
@@ -312,7 +312,7 @@ public final class ChatLayout: UICollectionViewLayout {
312312
section.set(items: items)
313313

314314
// Footer
315-
if delegate?.shouldPresentFooter(at: sectionIndex) == true {
315+
if delegate?.shouldPresentFooter(self, at: sectionIndex) == true {
316316
var footer = section.footer
317317
let footerIndexPath = IndexPath(item: 0, section: sectionIndex)
318318
footer?.alignment = alignment(for: .footer, at: footerIndexPath)
@@ -745,7 +745,7 @@ extension ChatLayout {
745745
return (estimated: estimatedItemSize, exact: nil)
746746
}
747747

748-
let itemSize = delegate.sizeForItem(of: element, at: indexPath)
748+
let itemSize = delegate.sizeForItem(self, of: element, at: indexPath)
749749

750750
switch itemSize {
751751
case .auto:
@@ -761,7 +761,7 @@ extension ChatLayout {
761761
guard let delegate = delegate else {
762762
return .full
763763
}
764-
return delegate.alignmentForItem(of: element, at: indexPath)
764+
return delegate.alignmentForItem(self, of: element, at: indexPath)
765765
}
766766

767767
private var estimatedItemSize: CGSize {
@@ -798,6 +798,14 @@ extension ChatLayout: ChatLayoutRepresentation {
798798
return collectionView.numberOfItems(inSection: section)
799799
}
800800

801+
func shouldPresentHeader(at sectionIndex: Int) -> Bool {
802+
return delegate?.shouldPresentHeader(self, at: sectionIndex) ?? false
803+
}
804+
805+
func shouldPresentFooter(at sectionIndex: Int) -> Bool {
806+
return delegate?.shouldPresentFooter(self, at: sectionIndex) ?? false
807+
}
808+
801809
}
802810

803811
extension ChatLayout {

ChatLayout/Classes/Core/ChatLayoutDelegate.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,57 @@ import UIKit
1414
public protocol ChatLayoutDelegate: AnyObject {
1515

1616
/// `ChatLayout` will call this method to ask if it should present the header in the current layout.
17-
/// - Parameter sectionIndex: Index of the section.
18-
func shouldPresentHeader(at sectionIndex: Int) -> Bool
17+
/// - Parameters:
18+
/// - chatLayout: ChatLayout reference.
19+
/// - sectionIndex: Index of the section.
20+
/// - Returns: `Bool`.
21+
func shouldPresentHeader(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool
1922

2023
/// `ChatLayout` will call this method to ask if it should present the footer in the current layout.
21-
/// - Parameter sectionIndex: Index of the section.
22-
func shouldPresentFooter(at sectionIndex: Int) -> Bool
24+
/// - Parameters:
25+
/// - chatLayout: ChatLayout reference.
26+
/// - sectionIndex: Index of the section.
27+
/// - Returns: `Bool`.
28+
func shouldPresentFooter(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool
2329

24-
/// `ChatLayout` will call this method to ask what type of alignment the item should have.
30+
/// `ChatLayout` will call this method to ask what size the item should have.
2531
/// - Parameters:
32+
/// - chatLayout: ChatLayout reference.
2633
/// - kind: Type of element represented by `ItemKind`.
2734
/// - indexPath: Index path of the item.
28-
func alignmentForItem(of kind: ItemKind, at indexPath: IndexPath) -> ChatItemAlignment
35+
/// - Returns: `ItemSize`.
36+
func sizeForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ItemSize
2937

30-
/// `ChatLayout` will call this method to ask what size the item should have.
38+
/// `ChatLayout` will call this method to ask what type of alignment the item should have.
3139
/// - Parameters:
40+
/// - chatLayout: ChatLayout reference.
3241
/// - kind: Type of element represented by `ItemKind`.
3342
/// - indexPath: Index path of the item.
34-
func sizeForItem(of kind: ItemKind, at indexPath: IndexPath) -> ItemSize
43+
/// - Returns: `ChatItemAlignment`.
44+
func alignmentForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ChatItemAlignment
3545

3646
}
3747

3848
/// Default extension.
3949
public extension ChatLayoutDelegate {
4050

41-
func shouldPresentHeader(at sectionIndex: Int) -> Bool {
51+
/// Default implementation returns: `false`.
52+
func shouldPresentHeader(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool {
4253
return false
4354
}
4455

45-
func shouldPresentFooter(at sectionIndex: Int) -> Bool {
56+
/// Default implementation returns: `false`.
57+
func shouldPresentFooter(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool {
4658
return false
4759
}
4860

49-
func sizeForItem(of kind: ItemKind, at indexPath: IndexPath) -> ItemSize {
61+
/// Default implementation returns: `ItemSize.auto`.
62+
func sizeForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ItemSize {
5063
return .auto
5164
}
5265

53-
func alignmentForItem(of kind: ItemKind, at indexPath: IndexPath) -> ChatItemAlignment {
66+
/// Default implementation returns: `ChatItemAlignment.full`.
67+
func alignmentForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ChatItemAlignment {
5468
return .full
5569
}
5670

ChatLayout/Classes/Core/Model/LayoutModel.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,7 @@ struct LayoutModel {
3232

3333
// MARK: To use when its is important to make the correct insertion
3434

35-
mutating func setAndAssemble(section: SectionModel, sectionIndex: Int) {
36-
guard sectionIndex < sections.count else {
37-
assertionFailure("Internal inconsistency")
38-
return
39-
}
40-
let oldSection = sections[sectionIndex]
41-
sections[sectionIndex] = section
42-
let heightDiff = sections[sectionIndex].height - oldSection.height
43-
offsetEverything(below: sectionIndex, by: heightDiff)
44-
}
45-
46-
mutating func setAndAssemble(header: ItemModel?, sectionIndex: Int) {
35+
mutating func setAndAssemble(header: ItemModel, sectionIndex: Int) {
4736
guard sectionIndex < sections.count else {
4837
assertionFailure("Internal inconsistency")
4938
return
@@ -66,7 +55,7 @@ struct LayoutModel {
6655
offsetEverything(below: sectionIndex, by: heightDiff)
6756
}
6857

69-
mutating func setAndAssemble(footer: ItemModel?, sectionIndex: Int) {
58+
mutating func setAndAssemble(footer: ItemModel, sectionIndex: Int) {
7059
guard sectionIndex < sections.count else {
7160
assertionFailure("Internal inconsistency")
7261
return

ChatLayout/Classes/Core/Model/SectionModel.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,14 @@ struct SectionModel {
7979

8080
// MARK: To use when its is important to make the correct insertion
8181

82-
mutating func setAndAssemble(header: ItemModel?) {
83-
guard let oldHeader = self.header,
84-
let newHeader = header else {
85-
if let header = header, self.header == nil {
86-
offsetEverything(below: -1, by: header.height)
87-
} else if let oldHeader = self.header, header == nil {
88-
offsetEverything(below: -1, by: -oldHeader.height)
89-
}
82+
mutating func setAndAssemble(header: ItemModel) {
83+
guard let oldHeader = self.header else {
9084
self.header = header
85+
offsetEverything(below: -1, by: header.height)
9186
return
9287
}
93-
self.header = newHeader
94-
let heightDiff = newHeader.height - oldHeader.height
88+
self.header = header
89+
let heightDiff = header.height - oldHeader.height
9590
offsetEverything(below: -1, by: heightDiff)
9691
}
9792

@@ -107,11 +102,7 @@ struct SectionModel {
107102
offsetEverything(below: index, by: heightDiff)
108103
}
109104

110-
mutating func setAndAssemble(footer: ItemModel?) {
111-
guard let _ = self.footer, let _ = footer else {
112-
self.footer = footer
113-
return
114-
}
105+
mutating func setAndAssemble(footer: ItemModel) {
115106
self.footer = footer
116107
}
117108

0 commit comments

Comments
 (0)