From ea086d7e4c09c767d98344904db34d93347d4fb3 Mon Sep 17 00:00:00 2001 From: Edward Warrender Date: Mon, 20 Dec 2021 15:27:38 +0000 Subject: [PATCH 1/2] Improvement in logic for scrollToLastItem --- Sources/Views/MessagesCollectionView.swift | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Sources/Views/MessagesCollectionView.swift b/Sources/Views/MessagesCollectionView.swift index bbf5de97..bf051529 100644 --- a/Sources/Views/MessagesCollectionView.swift +++ b/Sources/Views/MessagesCollectionView.swift @@ -46,9 +46,14 @@ 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 +113,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) } From 88604e94418eb7897359ebbbb2f2dfe3ce7d0acc Mon Sep 17 00:00:00 2001 From: Edward Warrender Date: Tue, 4 Jan 2022 08:52:16 +0000 Subject: [PATCH 2/2] Put return on a newline within indexPathForLastItem --- Sources/Views/MessagesCollectionView.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/Views/MessagesCollectionView.swift b/Sources/Views/MessagesCollectionView.swift index bf051529..547526e0 100644 --- a/Sources/Views/MessagesCollectionView.swift +++ b/Sources/Views/MessagesCollectionView.swift @@ -51,7 +51,9 @@ open class MessagesCollectionView: UICollectionView { for offset in 1...numberOfSections { let section = numberOfSections - offset let lastItem = numberOfItems(inSection: section) - 1 - if lastItem >= 0 { return IndexPath(item: lastItem, section: section) } + if lastItem >= 0 { + return IndexPath(item: lastItem, section: section) + } } return nil }