Skip to content

Commit

Permalink
Only scroll when the unread marker was added in the same messages batch
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <[email protected]>
  • Loading branch information
SystemKeeper committed Oct 20, 2023
1 parent 2683b82 commit 0f21544
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion NextcloudTalk/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,16 @@ import UIKit
var insertSections: IndexSet = []
var reloadIndexPaths: Set<IndexPath> = []

var addedUnreadMessageSeparator = false

// Check if unread messages separator should be added (only if it's not already shown)
if firstNewMessagesAfterHistory, self.getLastRealMessage() != nil, self.indexPathForUnreadMessageSeparator() == nil, newMessagesContainVisibleMessages,
let lastDateSection = self.dateSections.last, var messagesBeforeUpdate = self.messages[lastDateSection] {

messagesBeforeUpdate.append(self.unreadMessagesSeparator)
self.messages[lastDateSection] = messagesBeforeUpdate
insertIndexPaths.insert(IndexPath(row: messagesBeforeUpdate.count - 1, section: self.dateSections.count - 1))
addedUnreadMessageSeparator = true
}

self.appendMessages(messages: messages)
Expand Down Expand Up @@ -925,7 +928,9 @@ import UIKit
self.removeUnreadMessagesSeparator()
}

if let indexPathUnreadMessageSeparator = self.indexPathForUnreadMessageSeparator() {
// Only scroll to unread message separator if we added it while processing the received messages
// Otherwise we would scroll whenever a unread message separator is available
if addedUnreadMessageSeparator, let indexPathUnreadMessageSeparator = self.indexPathForUnreadMessageSeparator() {
tableView.scrollToRow(at: indexPathUnreadMessageSeparator, at: .middle, animated: true)
} else if (shouldScrollOnNewMessages || messages.containsUserMessage()), let lastIndexPath = self.getLastRealMessage()?.indexPath {
tableView.scrollToRow(at: lastIndexPath, at: .none, animated: true)
Expand Down

0 comments on commit 0f21544

Please sign in to comment.