load more messages method
func loadMoreChatMessages() {
self.showLoadEarlierMessagesHeader = false
page += 1
let bottomOffset = self.collectionView.contentSize.height - self.collectionView.contentOffset.y
CATransaction.begin()
CATransaction.setDisableActions(true)
fetchChatData(false) {[unowned self] (_) in
if !self.historyMessages.isEmpty {
self.collectionView.performBatchUpdates({
self.collectionView.performBatchUpdates({
//let lastIdx = self.messagesCountIndexes
var indexPaths = [IndexPath]()
//create indexs
for i in 0..<self.historyMessages.count {
indexPaths.append(IndexPath(row: i, section: 0))
}
// insert messages and update data source.
self.incomingMessagesWithMedia.insert(contentsOf: self.historyMessages, at: 0)
self.collectionView.insertItems(at: indexPaths)
// invalidate layout
// self.collectionView.collectionViewLayout.invalidateLayout(with: JSQMessagesCollectionViewFlowLayoutInvalidationContext())
}, completion: { (finished) in
// self.finishReceivingMessage(animated: false)
self.collectionView.reloadData()
// self.collectionView.layoutIfNeeded()
//scroll back to current position
self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
// let lastIdx = IndexPath(row: self.historyMessages.count, section: 0)
// self.scroll(to: lastIdx, animated: true)
self.historyMessages.removeAll()
self.showLoadEarlierMessagesHeader = true
})
})
}
}
CATransaction.commit()
}