mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
Merge pull request #1658 from PortablePixels/master
Improvement in logic for scrollToLastItem
This commit is contained in:
@@ -46,9 +46,16 @@ open class MessagesCollectionView: UICollectionView {
|
||||
internal var showMessageTimestampOnSwipeLeft: Bool = false
|
||||
|
||||
private var indexPathForLastItem: IndexPath? {
|
||||
let lastSection = numberOfSections - 1
|
||||
guard lastSection >= 0, numberOfItems(inSection: lastSection) > 0 else { return nil }
|
||||
return IndexPath(item: numberOfItems(inSection: lastSection) - 1, section: lastSection)
|
||||
guard numberOfSections > 0 else { return nil }
|
||||
|
||||
for offset in 1...numberOfSections {
|
||||
let section = numberOfSections - offset
|
||||
let lastItem = numberOfItems(inSection: section) - 1
|
||||
if lastItem >= 0 {
|
||||
return IndexPath(item: lastItem, section: section)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
open var messagesCollectionViewFlowLayout: MessagesCollectionViewFlowLayout {
|
||||
@@ -108,14 +115,8 @@ open class MessagesCollectionView: UICollectionView {
|
||||
|
||||
// NOTE: It's possible for small content size this wouldn't work - https://github.com/MessageKit/MessageKit/issues/725
|
||||
public func scrollToLastItem(at pos: UICollectionView.ScrollPosition = .bottom, animated: Bool = true) {
|
||||
guard numberOfSections > 0 else { return }
|
||||
guard let indexPath = indexPathForLastItem else { return }
|
||||
|
||||
let lastSection = numberOfSections - 1
|
||||
let lastItemIndex = numberOfItems(inSection: lastSection) - 1
|
||||
|
||||
guard lastItemIndex >= 0 else { return }
|
||||
|
||||
let indexPath = IndexPath(row: lastItemIndex, section: lastSection)
|
||||
scrollToItem(at: indexPath, at: pos, animated: animated)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user