From 1c697c4a311e7f4eeb2f2f03c86e482697052539 Mon Sep 17 00:00:00 2001 From: Diogo Date: Wed, 6 Nov 2024 16:21:10 +0000 Subject: [PATCH] fixed scroll to unread when not visible --- apps/ios/Shared/Views/Chat/ChatView.swift | 6 ++++-- apps/ios/Shared/Views/Chat/ReverseList.swift | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/ChatView.swift b/apps/ios/Shared/Views/Chat/ChatView.swift index c3a492aa14..8cfb53ea86 100644 --- a/apps/ios/Shared/Views/Chat/ChatView.swift +++ b/apps/ios/Shared/Views/Chat/ChatView.swift @@ -363,8 +363,10 @@ struct ChatView: View { } ChatView.FloatingButtonModel.shared.totalUnread = chat.chatStats.unreadCount Task { - if let firstunreadItem = self.getFirstUnreadItem() { - scrollModel.scrollToUnread(id: firstunreadItem.id) + DispatchQueue.main.async { + if let firstunreadItem = self.getFirstUnreadItem() { + scrollModel.scrollToUnread(id: firstunreadItem.id) + } } } } diff --git a/apps/ios/Shared/Views/Chat/ReverseList.swift b/apps/ios/Shared/Views/Chat/ReverseList.swift index e7e55e8cf4..42b8df0d58 100644 --- a/apps/ios/Shared/Views/Chat/ReverseList.swift +++ b/apps/ios/Shared/Views/Chat/ReverseList.swift @@ -38,6 +38,7 @@ struct ReverseList: UIViewControllerRepresentable { case .bottom: controller.scroll(to: 0, position: .top) case let .unread(id): + logger.error("[scrolling] to unread id: (\(id)) size: \(items.count)") controller.scrollToUnread(to: items.first(where: { $0.id == id })) } } else { @@ -177,20 +178,23 @@ struct ReverseList: UIViewControllerRepresentable { func scrollToUnread(to cItem: ChatItem?) { if let it = cItem, let indexPath = dataSource.indexPath(for: it) { if isVisible(indexPath: indexPath) { - self.scroll(to: 0, position: .top) + logger.error("[scrolling] unread visible") + self.scroll(to: 0, position: .bottom) } else { - self.scroll(to: indexPath.row, position: .bottom) + logger.error("[scrolling] unread not visible and at line index: \(indexPath.row)") + self.scrollToItem(to: cItem, position: .bottom) } } else { - self.scroll(to: nil, position: .bottom) + logger.error("[scrolling] unread not found") + self.scroll(to: nil, position: .top) } } - func scrollToItem(to cItem: ChatItem?, position: UITableView.ScrollPosition) { + func scrollToItem(to cItem: ChatItem?, position: UITableView.ScrollPosition) { if let it = cItem, let indexPath = dataSource.indexPath(for: it) { self.scroll(to: indexPath.row, position: position) } else { - scroll(to: nil, position: position) + self.scroll(to: nil, position: position) } } @@ -201,7 +205,10 @@ struct ReverseList: UIViewControllerRepresentable { if #available(iOS 16.0, *) { animated = true } - if let index, tableView.numberOfRows(inSection: 0) != 0, !isVisible(indexPath: IndexPath(row: index, section: 0)) { + logger.error("[scrolling] requesting scroll") + + if let index, tableView.numberOfRows(inSection: 0) != 0 { + logger.error("[scrolling] requesting scroll to index: \(index) position: \(position.rawValue)") tableView.scrollToRow( at: IndexPath(row: index, section: 0), at: position,