Skip to content

Commit 3f1a1b8

Browse files
committed
Added mandatory reading of the size of the cell from delegate on reload.
1 parent 62bec2e commit 3f1a1b8

File tree

77 files changed

+2922
-2810
lines changed

Some content is hidden

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

77 files changed

+2922
-2810
lines changed

ChatLayout/Classes/Core/CollectionViewChatLayout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ extension CollectionViewChatLayout {
874874
return ItemModel.Configuration(alignment: alignment(for: element, at: indexPath), preferredSize: itemSize.estimated, calculatedSize: itemSize.exact)
875875
}
876876

877-
func alignment(for element: ItemKind, at itemPath: ItemPath) -> ChatItemAlignment {
877+
private func alignment(for element: ItemKind, at itemPath: ItemPath) -> ChatItemAlignment {
878878
let indexPath = itemPath.indexPath
879879
return alignment(for: element, at: indexPath)
880880
}

ChatLayout/Classes/Core/Model/StateController.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ protocol ChatLayoutRepresentation: AnyObject {
3232

3333
func configuration(for element: ItemKind, at itemPath: ItemPath) -> ItemModel.Configuration
3434

35-
func alignment(for element: ItemKind, at itemPath: ItemPath) -> ChatItemAlignment
36-
3735
func shouldPresentHeader(at sectionIndex: Int) -> Bool
3836

3937
func shouldPresentFooter(at sectionIndex: Int) -> Bool
@@ -401,6 +399,16 @@ final class StateController {
401399
}
402400

403401
func process(changeItems: [ChangeItem]) {
402+
func applyConfiguration(_ configuration: ItemModel.Configuration, to item: inout ItemModel) {
403+
item.alignment = configuration.alignment
404+
if let calculatedSize = configuration.calculatedSize {
405+
item.calculatedSize = calculatedSize
406+
item.calculatedOnce = true
407+
} else {
408+
item.resetSize()
409+
}
410+
}
411+
404412
batchUpdateCompensatingOffset = 0
405413
proposedCompensatingOffset = 0
406414
let changeItems = changeItems.sorted()
@@ -451,11 +459,10 @@ final class StateController {
451459
var header: ItemModel?
452460
if layoutRepresentation.shouldPresentHeader(at: sectionIndex) == true {
453461
let headerIndexPath = IndexPath(item: 0, section: sectionIndex)
454-
header = section.header ?? ItemModel(with: layoutRepresentation.configuration(for: .header, at: headerIndexPath.itemPath))
455-
header?.resetSize()
456-
if section.header != nil {
457-
header?.alignment = layoutRepresentation.alignment(for: .cell, at: headerIndexPath.itemPath)
458-
}
462+
var newHeader = section.header ?? ItemModel(with: layoutRepresentation.configuration(for: .header, at: headerIndexPath.itemPath))
463+
let configuration = layoutRepresentation.configuration(for: .header, at: headerIndexPath.itemPath)
464+
applyConfiguration(configuration, to: &newHeader)
465+
header = newHeader
459466
} else {
460467
header = nil
461468
}
@@ -464,11 +471,10 @@ final class StateController {
464471
var footer: ItemModel?
465472
if layoutRepresentation.shouldPresentFooter(at: sectionIndex) == true {
466473
let footerIndexPath = IndexPath(item: 0, section: sectionIndex)
467-
footer = section.footer ?? ItemModel(with: layoutRepresentation.configuration(for: .footer, at: footerIndexPath.itemPath))
468-
footer?.resetSize()
469-
if section.footer != nil {
470-
footer?.alignment = layoutRepresentation.alignment(for: .cell, at: footerIndexPath.itemPath)
471-
}
474+
var newFooter = section.footer ?? ItemModel(with: layoutRepresentation.configuration(for: .footer, at: footerIndexPath.itemPath))
475+
let configuration = layoutRepresentation.configuration(for: .footer, at: footerIndexPath.itemPath)
476+
applyConfiguration(configuration, to: &newFooter)
477+
footer = newFooter
472478
} else {
473479
footer = nil
474480
}
@@ -480,11 +486,11 @@ final class StateController {
480486
let itemIndexPath = IndexPath(item: index, section: sectionIndex)
481487
if index < oldItems.count {
482488
newItem = oldItems[index]
483-
newItem.alignment = layoutRepresentation.alignment(for: .cell, at: itemIndexPath.itemPath)
489+
let configuration = layoutRepresentation.configuration(for: .cell, at: itemIndexPath.itemPath)
490+
applyConfiguration(configuration, to: &newItem)
484491
} else {
485492
newItem = ItemModel(with: layoutRepresentation.configuration(for: .cell, at: itemIndexPath.itemPath))
486493
}
487-
newItem.resetSize()
488494
return newItem
489495
}
490496
section.set(items: ContiguousArray(items))
@@ -495,8 +501,8 @@ final class StateController {
495501
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
496502
return
497503
}
498-
item.resetSize()
499-
item.alignment = layoutRepresentation.alignment(for: .cell, at: indexPath.itemPath)
504+
let configuration = layoutRepresentation.configuration(for: .cell, at: indexPath.itemPath)
505+
applyConfiguration(configuration, to: &item)
500506
afterUpdateModel.replaceItem(item, at: indexPath)
501507
reloadedIndexes.insert(indexPath)
502508
case let .sectionMove(initialSectionIndex: initialSectionIndex, finalSectionIndex: finalSectionIndex):

Example/Tests/StateControllerProcessUpdatesTests.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ class StateControllerProcessUpdatesTests: XCTestCase {
6565

6666
layout.settings.estimatedItemSize = .init(width: 300, height: 50)
6767
layout.controller.process(changeItems: changeItems)
68-
XCTAssertEqual(layout.controller.contentHeight(at: .beforeUpdate), layout.controller.contentHeight(at: .afterUpdate))
68+
XCTAssertEqual(layout.controller.contentHeight(at: .beforeUpdate) + CGFloat(changeItems.count * 10), layout.controller.contentHeight(at: .afterUpdate))
6969
layout.controller.commitUpdates()
7070

7171
layout.controller.process(changeItems: [.sectionReload(sectionIndex: 0),
7272
.sectionReload(sectionIndex: 1)])
7373

74-
XCTAssertEqual(layout.controller.contentHeight(at: .beforeUpdate), layout.controller.contentHeight(at: .afterUpdate))
74+
XCTAssertEqual(layout.controller.contentHeight(at: .beforeUpdate) + CGFloat(10 * 4), layout.controller.contentHeight(at: .afterUpdate))
7575
layout.controller.commitUpdates()
7676
}
7777

@@ -113,10 +113,11 @@ class StateControllerProcessUpdatesTests: XCTestCase {
113113
// Frames
114114
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)?.origin, .zero)
115115
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .afterUpdate)?.origin, .zero)
116-
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)?.size, layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .afterUpdate)?.size)
117-
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 99, section: 0), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 40))
116+
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)?.size, CGSize(width: 300, height: 40))
117+
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 50))
118+
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 99, section: 0), kind: .cell, at: .beforeUpdate)?.size, CGSize(width: 300, height: 40))
118119
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 100, section: 0), kind: .cell, at: .afterUpdate)?.size, layout.settings.estimatedItemSize)
119-
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 49, section: 1), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 40))
120+
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 49, section: 1), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 50))
120121

121122
layout.controller.commitUpdates()
122123
}

docs/Classes/CellLayoutContainerView.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -457,7 +457,7 @@ <h4>Parameters</h4>
457457
</article>
458458
</div>
459459
<section class="footer">
460-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
460+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
461461
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
462462
</section>
463463
</body>

docs/Classes/ChatLayoutAttributes.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -416,7 +416,7 @@ <h4>Declaration</h4>
416416
</article>
417417
</div>
418418
<section class="footer">
419-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
419+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
420420
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
421421
</section>
422422
</body>

docs/Classes/ChatLayoutInvalidationContext.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -201,7 +201,7 @@ <h4>Declaration</h4>
201201
</article>
202202
</div>
203203
<section class="footer">
204-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
204+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
205205
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
206206
</section>
207207
</body>

docs/Classes/CollectionViewChatLayout.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -1308,7 +1308,7 @@ <h4>Declaration</h4>
13081308
</article>
13091309
</div>
13101310
<section class="footer">
1311-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
1311+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
13121312
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
13131313
</section>
13141314
</body>

docs/Classes/ContainerCollectionReusableView.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -377,7 +377,7 @@ <h4>Parameters</h4>
377377
</article>
378378
</div>
379379
<section class="footer">
380-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
380+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
381381
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
382382
</section>
383383
</body>

docs/Classes/ContainerCollectionViewCell.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -377,7 +377,7 @@ <h4>Parameters</h4>
377377
</article>
378378
</div>
379379
<section class="footer">
380-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
380+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
381381
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
382382
</section>
383383
</body>

docs/Classes/EdgeAligningView.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<header class="header">
2222
<p class="header-col header-col--primary">
2323
<a class="header-link" href="../index.html">
24-
ChatLayout 1.2.10 Docs
24+
ChatLayout 1.2.11 Docs
2525
</a>
2626
(100% documented)
2727
</p>
@@ -361,7 +361,7 @@ <h4>Parameters</h4>
361361
</article>
362362
</div>
363363
<section class="footer">
364-
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
364+
<p>&copy; 2022 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
365365
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.3</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
366366
</section>
367367
</body>

0 commit comments

Comments
 (0)