diff --git a/apps/ios/Shared/Views/Chat/ChatView.swift b/apps/ios/Shared/Views/Chat/ChatView.swift index 36d604602e..dc8d027b0f 100644 --- a/apps/ios/Shared/Views/Chat/ChatView.swift +++ b/apps/ios/Shared/Views/Chat/ChatView.swift @@ -23,7 +23,7 @@ struct ChatView: View { @Environment(\.scenePhase) var scenePhase @State @ObservedObject var chat: Chat @StateObject private var scrollModel = ReverseListScrollModel() - @StateObject private var sectionModel = ReverseListSectionModel.shared + @StateObject private var sectionModel = ReverseListSectionModel() @State private var showChatInfoSheet: Bool = false @State private var showAddMembersSheet: Bool = false @State private var composeState = ComposeState() @@ -914,6 +914,7 @@ struct ChatView: View { im.reversedChatItems.append(contentsOf: reversedPageToAppend) } } + self.sectionModel.manageActiveSection(scrollDirection: direction) loadingItems = false } } catch let error { diff --git a/apps/ios/Shared/Views/Chat/ReverseList.swift b/apps/ios/Shared/Views/Chat/ReverseList.swift index e4a68d1e55..7a86753fb7 100644 --- a/apps/ios/Shared/Views/Chat/ReverseList.swift +++ b/apps/ios/Shared/Views/Chat/ReverseList.swift @@ -101,13 +101,6 @@ struct ReverseList: UIViewControllerRepresentable { let firstItem = self.getFirstItemInItemSection(indexPath: indexPath) self.representer.loadPage(.toLatest, section, firstItem) } - - if (self.scrollDirection == .toOldest && indexPath.item - ReverseListSectionModel.IDEAL_SECTION_SIZE > 8) || - (self.scrollDirection == .toLatest && indexPath.item < ReverseListSectionModel.IDEAL_SECTION_SIZE) { - Task { - self.representer.sectionModel.manageActiveSection(scrollDirection: self.scrollDirection) - } - } } } let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseId, for: indexPath) @@ -427,8 +420,8 @@ struct SectionBoundaryReached { /// Manages ``ReverseList`` sections class ReverseListSectionModel: ObservableObject { static let shared = ReverseListSectionModel() - static let MAX_SECTION_SIZE = 50 - static let IDEAL_SECTION_SIZE = 35 + static let MAX_SECTION_SIZE = 500 + static let MIN_SECTION_SIZE = 200 @Published private(set) var activeSection: ChatSection = .bottom @Published var boundaries = SectionBoundaryReached(oldest: false, latest: false) @Published private var itemSection: [Int64: ChatSection] = [:] @@ -457,6 +450,21 @@ class ReverseListSectionModel: ObservableObject { return itemSection.values.filter { $0 == self.activeSection }.count } + private func splitSection(originalSection: ChatSection, newSection: ChatSection) { + var originalSectionCount = 0 + let im = ItemsModel.shared + + im.reversedChatItems.forEach { it in + if itemSection[it.id] == originalSection { + if originalSectionCount < ReverseListSectionModel.MIN_SECTION_SIZE { + originalSectionCount += 1 + } else { + itemSection[it.id] = newSection + } + } + } + } + private func trimSection(scrollDirection: ChatScrollDirection, section: ChatSection) { let im = ItemsModel.shared var i = scrollDirection == .toLatest ? 0 : im.reversedChatItems.count - 1 @@ -504,10 +512,15 @@ class ReverseListSectionModel: ObservableObject { switch self.activeSection { case .bottom: - if scrollDirection == .toLatest, activeSectionCount > ReverseListSectionModel.MAX_SECTION_SIZE { - // boundaries?, the visible item needs to not be there + if activeSectionCount > ReverseListSectionModel.MAX_SECTION_SIZE { + if scrollDirection == .toLatest { boundaries.oldest = false self.trimSection(scrollDirection: scrollDirection, section: self.activeSection) + } else { + splitSection(originalSection: .bottom, newSection: .current) + changeActiveSection(.current) + sections.insert(.current) + } } break; default: