Skip to content

Commit

Permalink
Added keepContentAtBottomOfVisibleArea flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ekazaev committed Apr 30, 2024
1 parent c26d49c commit d26da55
Show file tree
Hide file tree
Showing 76 changed files with 221 additions and 144 deletions.
2 changes: 1 addition & 1 deletion ChatLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ChatLayout'
s.version = '2.0.4'
s.version = '2.0.5'
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
s.swift_version = '5.8'

Expand Down
7 changes: 6 additions & 1 deletion ChatLayout/Classes/Core/CollectionViewChatLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ open class CollectionViewChatLayout: UICollectionViewLayout {
}
}

/// Default `UIScrollView` behaviour is to keep content offset constant from the top edge. If this flag is set to `true`
/// The default `UIScrollView` behaviour is to keep content offset constant from the top edge. If this flag is set to `true`
/// `CollectionViewChatLayout` should try to compensate batch update changes to keep the current content at the bottom of the visible
/// part of `UICollectionView`.
///
Expand All @@ -61,6 +61,11 @@ open class CollectionViewChatLayout: UICollectionViewLayout {
/// the animation starts and wont be able to compensate that change too. It should be done manually.
public var keepContentOffsetAtBottomOnBatchUpdates: Bool = false

/// The default behavior of UICollectionView is to maintain UICollectionViewCells at the top of the visible rectangle
/// when the content size is smaller than the visible area. By setting the respective flag to true, this behavior can be
/// reversed to achieve the result like in Telegram..
public var keepContentAtBottomOfVisibleArea: Bool = false

/// Sometimes `UIScrollView` can behave weirdly if there are too many corrections in it's `contentOffset` during the animation. Especially when content size of the `UIScrollView`
// is getting smaller first and then expands again as the newly appearing cells sizes are being calculated. That is why `CollectionViewChatLayout`
/// tries to process only the elements that are currently visible on the screen. But often it is not needed. This flag allows you to have fine control over this behaviour.
Expand Down
7 changes: 7 additions & 0 deletions ChatLayout/Classes/Core/Model/StateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ protocol ChatLayoutRepresentation: AnyObject {

var keepContentOffsetAtBottomOnBatchUpdates: Bool { get }

var keepContentAtBottomOfVisibleArea: Bool { get }

var processOnlyVisibleItemsOnAnimatedBatchUpdates: Bool { get }

func numberOfItems(in section: Int) -> Int
Expand Down Expand Up @@ -359,6 +361,11 @@ final class StateController<Layout: ChatLayoutRepresentation> {
if isFinal {
offsetByCompensation(frame: &itemFrame, at: itemPath, for: state, backward: true)
}
if layoutRepresentation.keepContentAtBottomOfVisibleArea == true,
!(kind == .header && itemPath.section == 0),
!isLayoutBiggerThanVisibleBounds(at: state, withFullCompensation: false, visibleBounds: visibleBounds) {
itemFrame.offsettingBy(dx: 0, dy: visibleBounds.height.rounded() - contentSize(for: state).height.rounded())
}
return itemFrame
}

Expand Down
5 changes: 5 additions & 0 deletions Example/ChatLayout/Chat/View/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ final class ChatViewController: UIViewController {
chatLayout.settings.additionalInsets = UIEdgeInsets(top: 8, left: 5, bottom: 8, right: 5)
chatLayout.keepContentOffsetAtBottomOnBatchUpdates = true
chatLayout.processOnlyVisibleItemsOnAnimatedBatchUpdates = false
chatLayout.keepContentAtBottomOfVisibleArea = true

collectionView = UICollectionView(frame: view.frame, collectionViewLayout: chatLayout)
view.addSubview(collectionView)
Expand Down Expand Up @@ -453,6 +454,10 @@ extension ChatViewController: UICollectionViewDelegate {

extension ChatViewController: ChatControllerDelegate {
func update(with sections: [Section], requiresIsolatedProcess: Bool) {
// if `chatLayout.keepContentAtBottomOfVisibleArea` is enabled and content size is actually smaller than the visible size - it is better to process each batch update
// in isolation. Example: If you insert a cell animatingly and then reload some cell - the reload animation will appear on top of the insertion animation.
// Basically everytime you see any animation glitches - process batch updates in isolation.
let requiresIsolatedProcess = chatLayout.keepContentAtBottomOfVisibleArea == true && chatLayout.collectionViewContentSize.height < chatLayout.visibleBounds.height ? true : requiresIsolatedProcess
processUpdates(with: sections, animated: true, requiresIsolatedProcess: requiresIsolatedProcess)
}

Expand Down
12 changes: 6 additions & 6 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- ChatLayout (2.0.3):
- ChatLayout/Ultimate (= 2.0.3)
- ChatLayout/Core (2.0.3)
- ChatLayout/Extras (2.0.3):
- ChatLayout (2.0.5):
- ChatLayout/Ultimate (= 2.0.5)
- ChatLayout/Core (2.0.5)
- ChatLayout/Extras (2.0.5):
- ChatLayout/Core
- ChatLayout/Ultimate (2.0.3):
- ChatLayout/Ultimate (2.0.5):
- ChatLayout/Core
- ChatLayout/Extras
- DifferenceKit (1.3.0):
Expand Down Expand Up @@ -35,7 +35,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ChatLayout: 0e958d11b184b64116eb8b37009e4f1aa5b9385f
ChatLayout: 73e77fd1d5f3ea46dfee7045804d00116395bf04
DifferenceKit: ab185c4d7f9cef8af3fcf593e5b387fb81e999ca
FPSCounter: 884afec377de66637808c4f52ecc3b85a404732b
InputBarAccessoryView: 1d7b0a672b36e370f01f264b3907ef39d03328e3
Expand Down
2 changes: 2 additions & 0 deletions Example/Tests/MockCollectionLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class MockCollectionLayout: ChatLayoutRepresentation, ChatLayoutDelegate {

let keepContentOffsetAtBottomOnBatchUpdates: Bool = true

let keepContentAtBottomOfVisibleArea: Bool = false

let processOnlyVisibleItemsOnAnimatedBatchUpdates: Bool = true

func numberOfItems(in section: Int) -> Int {
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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -464,7 +464,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -450,7 +450,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -208,7 +208,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</body>
Expand Down
35 changes: 32 additions & 3 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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -269,7 +269,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Default <code>UIScrollView</code> behaviour is to keep content offset constant from the top edge. If this flag is set to <code>true</code>
<p>The default <code>UIScrollView</code> behaviour is to keep content offset constant from the top edge. If this flag is set to <code>true</code>
<code>CollectionViewChatLayout</code> should try to compensate batch update changes to keep the current content at the bottom of the visible
part of <code>UICollectionView</code>.</p>

Expand All @@ -289,6 +289,35 @@ <h4>Declaration</h4>
</section>
</div>
</li>
<li class="item">
<div>
<code>
<a name="/s:10ChatLayout014CollectionViewaB0C32keepContentAtBottomOfVisibleAreaSbvp"></a>
<a name="//apple_ref/swift/Property/keepContentAtBottomOfVisibleArea" class="dashAnchor"></a>
<a class="token" href="#/s:10ChatLayout014CollectionViewaB0C32keepContentAtBottomOfVisibleAreaSbvp">keepContentAtBottomOfVisibleArea</a>
</code>
</div>
<div class="height-container">
<div class="pointer-container"></div>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>The default behavior of UICollectionView is to maintain UICollectionViewCells at the top of the visible rectangle
when the content size is smaller than the visible area. By setting the respective flag to true, this behavior can be
reversed to achieve the result like in Telegram..</p>

</div>
<div class="declaration">
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight swift"><code><span class="kd">public</span> <span class="k">var</span> <span class="nv">keepContentAtBottomOfVisibleArea</span><span class="p">:</span> <span class="kt">Bool</span></code></pre>

</div>
</div>
</section>
</div>
</li>
<li class="item">
<div>
<code>
Expand Down Expand Up @@ -1421,7 +1450,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -384,7 +384,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -384,7 +384,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -536,7 +536,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -288,7 +288,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -408,7 +408,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -354,7 +354,7 @@ <h4>Parameters</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -354,7 +354,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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/SwappingContainerView.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 2.0.4 Docs
ChatLayout 2.0.5 Docs
</a>
(100% documented)
</p>
Expand Down Expand Up @@ -599,7 +599,7 @@ <h4>Declaration</h4>
</article>
</div>
<section class="footer">
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-03-11)</p>
<p>&copy; 2024 <a class="link" href="https://github.com/ekazaev" target="_blank" rel="external noopener">Evgeny Kazaev</a>. All rights reserved. (Last updated: 2024-04-30)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.4</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 d26da55

Please sign in to comment.