Skip to content

Commit

Permalink
Added mandatory reading of the size of the cell from delegate on reload.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekazaev committed Oct 4, 2022
1 parent 62bec2e commit 3f1a1b8
Show file tree
Hide file tree
Showing 77 changed files with 2,922 additions and 2,810 deletions.
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/CollectionViewChatLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ extension CollectionViewChatLayout {
return ItemModel.Configuration(alignment: alignment(for: element, at: indexPath), preferredSize: itemSize.estimated, calculatedSize: itemSize.exact)
}

func alignment(for element: ItemKind, at itemPath: ItemPath) -> ChatItemAlignment {
private func alignment(for element: ItemKind, at itemPath: ItemPath) -> ChatItemAlignment {
let indexPath = itemPath.indexPath
return alignment(for: element, at: indexPath)
}
Expand Down
38 changes: 22 additions & 16 deletions ChatLayout/Classes/Core/Model/StateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ protocol ChatLayoutRepresentation: AnyObject {

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

func alignment(for element: ItemKind, at itemPath: ItemPath) -> ChatItemAlignment

func shouldPresentHeader(at sectionIndex: Int) -> Bool

func shouldPresentFooter(at sectionIndex: Int) -> Bool
Expand Down Expand Up @@ -401,6 +399,16 @@ final class StateController {
}

func process(changeItems: [ChangeItem]) {
func applyConfiguration(_ configuration: ItemModel.Configuration, to item: inout ItemModel) {
item.alignment = configuration.alignment
if let calculatedSize = configuration.calculatedSize {
item.calculatedSize = calculatedSize
item.calculatedOnce = true
} else {
item.resetSize()
}
}

batchUpdateCompensatingOffset = 0
proposedCompensatingOffset = 0
let changeItems = changeItems.sorted()
Expand Down Expand Up @@ -451,11 +459,10 @@ final class StateController {
var header: ItemModel?
if layoutRepresentation.shouldPresentHeader(at: sectionIndex) == true {
let headerIndexPath = IndexPath(item: 0, section: sectionIndex)
header = section.header ?? ItemModel(with: layoutRepresentation.configuration(for: .header, at: headerIndexPath.itemPath))
header?.resetSize()
if section.header != nil {
header?.alignment = layoutRepresentation.alignment(for: .cell, at: headerIndexPath.itemPath)
}
var newHeader = section.header ?? ItemModel(with: layoutRepresentation.configuration(for: .header, at: headerIndexPath.itemPath))
let configuration = layoutRepresentation.configuration(for: .header, at: headerIndexPath.itemPath)
applyConfiguration(configuration, to: &newHeader)
header = newHeader
} else {
header = nil
}
Expand All @@ -464,11 +471,10 @@ final class StateController {
var footer: ItemModel?
if layoutRepresentation.shouldPresentFooter(at: sectionIndex) == true {
let footerIndexPath = IndexPath(item: 0, section: sectionIndex)
footer = section.footer ?? ItemModel(with: layoutRepresentation.configuration(for: .footer, at: footerIndexPath.itemPath))
footer?.resetSize()
if section.footer != nil {
footer?.alignment = layoutRepresentation.alignment(for: .cell, at: footerIndexPath.itemPath)
}
var newFooter = section.footer ?? ItemModel(with: layoutRepresentation.configuration(for: .footer, at: footerIndexPath.itemPath))
let configuration = layoutRepresentation.configuration(for: .footer, at: footerIndexPath.itemPath)
applyConfiguration(configuration, to: &newFooter)
footer = newFooter
} else {
footer = nil
}
Expand All @@ -480,11 +486,11 @@ final class StateController {
let itemIndexPath = IndexPath(item: index, section: sectionIndex)
if index < oldItems.count {
newItem = oldItems[index]
newItem.alignment = layoutRepresentation.alignment(for: .cell, at: itemIndexPath.itemPath)
let configuration = layoutRepresentation.configuration(for: .cell, at: itemIndexPath.itemPath)
applyConfiguration(configuration, to: &newItem)
} else {
newItem = ItemModel(with: layoutRepresentation.configuration(for: .cell, at: itemIndexPath.itemPath))
}
newItem.resetSize()
return newItem
}
section.set(items: ContiguousArray(items))
Expand All @@ -495,8 +501,8 @@ final class StateController {
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
return
}
item.resetSize()
item.alignment = layoutRepresentation.alignment(for: .cell, at: indexPath.itemPath)
let configuration = layoutRepresentation.configuration(for: .cell, at: indexPath.itemPath)
applyConfiguration(configuration, to: &item)
afterUpdateModel.replaceItem(item, at: indexPath)
reloadedIndexes.insert(indexPath)
case let .sectionMove(initialSectionIndex: initialSectionIndex, finalSectionIndex: finalSectionIndex):
Expand Down
11 changes: 6 additions & 5 deletions Example/Tests/StateControllerProcessUpdatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ class StateControllerProcessUpdatesTests: XCTestCase {

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

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

XCTAssertEqual(layout.controller.contentHeight(at: .beforeUpdate), layout.controller.contentHeight(at: .afterUpdate))
XCTAssertEqual(layout.controller.contentHeight(at: .beforeUpdate) + CGFloat(10 * 4), layout.controller.contentHeight(at: .afterUpdate))
layout.controller.commitUpdates()
}

Expand Down Expand Up @@ -113,10 +113,11 @@ class StateControllerProcessUpdatesTests: XCTestCase {
// Frames
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)?.origin, .zero)
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .afterUpdate)?.origin, .zero)
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)
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 99, section: 0), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 40))
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)?.size, CGSize(width: 300, height: 40))
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 0, section: 0), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 50))
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 99, section: 0), kind: .cell, at: .beforeUpdate)?.size, CGSize(width: 300, height: 40))
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 100, section: 0), kind: .cell, at: .afterUpdate)?.size, layout.settings.estimatedItemSize)
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 49, section: 1), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 40))
XCTAssertEqual(layout.controller.itemFrame(for: ItemPath(item: 49, section: 1), kind: .cell, at: .afterUpdate)?.size, CGSize(width: 300, height: 50))

layout.controller.commitUpdates()
}
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/CellLayoutContainerView.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -457,7 +457,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/ChatLayoutAttributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -416,7 +416,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/ChatLayoutInvalidationContext.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -201,7 +201,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/CollectionViewChatLayout.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -1308,7 +1308,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/ContainerCollectionReusableView.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -377,7 +377,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/ContainerCollectionViewCell.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -377,7 +377,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/EdgeAligningView.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -361,7 +361,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/EdgeAligningView/Edge.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -281,7 +281,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/ImageMaskedView.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -401,7 +401,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/MessageContainerView.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -347,7 +347,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Classes/RoundedCornersContainerView.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -347,7 +347,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
4 changes: 2 additions & 2 deletions docs/Core.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="index.html">
ChatLayout 1.2.10 Docs
ChatLayout 1.2.11 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -435,7 +435,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-03)</p>
<p>&copy; 2022 <a class="link" href="https:/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2022-10-04)</p>
<p>Generated by <a class="link" href="https:/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>
</section>
</body>
Expand Down
Loading

0 comments on commit 3f1a1b8

Please sign in to comment.