Skip to content

Commit 0b96991

Browse files
committed
[trello.com/c/jKqpxiCa/12] update scroll to bottom reading
added a method that guarantees reading messages from the current position to the bottom
1 parent 83919a5 commit 0b96991

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Adamant/Modules/Chat/View/ChatViewController.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,6 @@ extension ChatViewController {
811811
viewModel.animationType = MessageAnimationType.none
812812
let isFirstMessagesInChat = viewModel.unreadMessagesIds?.count == viewModel.messages.count
813813
scrollToPosition(.messageId(unreadMessage), setExtraOffset: !isFirstMessagesInChat, scrollAt: .top)
814-
815814
}
816815
}
817816
}
@@ -881,6 +880,22 @@ extension ChatViewController {
881880
break
882881
}
883882
}
883+
884+
fileprivate func markMessageFromCurrentToBottomAsRead() {
885+
guard let unreadIndexes = viewModel.unreadMesaggesIndexes, !unreadIndexes.isEmpty else { return }
886+
887+
let visibleSections = messagesCollectionView.indexPathsForVisibleItems.map { $0.section }
888+
guard let minSection = visibleSections.min() else { return }
889+
890+
let totalSections = messagesCollectionView.numberOfSections
891+
guard totalSections > 0 else { return }
892+
893+
for section in minSection..<totalSections {
894+
if unreadIndexes.contains(section) {
895+
viewModel.markMessageAsRead(index: section)
896+
}
897+
}
898+
}
884899
}
885900

886901
// MARK: Making entities
@@ -892,6 +907,7 @@ extension ChatViewController {
892907
guard let self else { return }
893908
if viewModel.shouldScrollToBottom {
894909
state.isScrollingToBottom = true
910+
markMessageFromCurrentToBottomAsRead()
895911
self.messagesCollectionView.scrollToBottom(animated: true)
896912
} else if let id = viewModel.unreadMessagesIds?.first {
897913
viewModel.animationType = MessageAnimationType.none

0 commit comments

Comments
 (0)