From 1cec1b71c6498f481f2707455c3ec6eaee6036eb Mon Sep 17 00:00:00 2001 From: Ali <> Date: Fri, 10 Apr 2020 13:34:27 +0400 Subject: [PATCH] Debug ChatListViewState --- .../Postbox/Sources/ChatListViewState.swift | 68 +++++++++++++++++-- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/submodules/Postbox/Sources/ChatListViewState.swift b/submodules/Postbox/Sources/ChatListViewState.swift index 64f1deb0aa..a2907c3244 100644 --- a/submodules/Postbox/Sources/ChatListViewState.swift +++ b/submodules/Postbox/Sources/ChatListViewState.swift @@ -138,6 +138,8 @@ private final class ChatListViewSpaceState { self.halfLimit = halfLimit self.orderedEntries = OrderedChatListViewEntries(anchorIndex: anchorIndex.index, lowerOrAtAnchor: [], higherThanAnchor: []) self.fillSpace(postbox: postbox) + + self.checkEntries(postbox: postbox) } private func fillSpace(postbox: Postbox) { @@ -215,7 +217,7 @@ private final class ChatListViewSpaceState { if let lastMessage = lowerOrAtAnchorMessages.min(by: { $0.entryIndex < $1.entryIndex }) { nextLowerIndex = lastMessage.entryIndex } else { - nextLowerIndex = resolvedAnchorIndex.predecessor + nextLowerIndex = resolvedAnchorIndex.successor } let loadedLowerMessages = postbox.chatListTable.entries(groupId: groupId, from: (nextLowerIndex.index, nextLowerIndex.isMessage), to: (lowerBound.index, lowerBound.isMessage), peerChatInterfaceStateTable: postbox.peerChatInterfaceStateTable, count: self.halfLimit - lowerOrAtAnchorMessages.count, predicate: filterPredicate.flatMap { mappedChatListFilterPredicate(postbox: postbox, groupId: groupId, predicate: $0) }).map(mapEntry) lowerOrAtAnchorMessages.append(contentsOf: loadedLowerMessages) @@ -334,6 +336,9 @@ private final class ChatListViewSpaceState { self.orderedEntries = entries } } + + precondition(self.orderedEntries.lowerOrAtAnchor.count <= self.halfLimit) + precondition(self.orderedEntries.higherThanAnchor.count <= self.halfLimit) } func replay(postbox: Postbox, transaction: PostboxTransaction) -> Bool { @@ -379,6 +384,9 @@ private final class ChatListViewSpaceState { } if self.add(entry: .IntermediateMessageEntry(index: updatedIndex, messageIndex: messageIndex)) { hasUpdates = true + } else { + hasUpdates = true + hadRemovals = true } case let .peers(peerIds, asPinned): if let peerIndex = peerIds.firstIndex(of: index.messageIndex.id.peerId) { @@ -388,6 +396,9 @@ private final class ChatListViewSpaceState { } if self.add(entry: .IntermediateMessageEntry(index: updatedIndex, messageIndex: messageIndex)) { hasUpdates = true + } else { + hasUpdates = true + hadRemovals = true } } else { continue inner @@ -399,6 +410,9 @@ private final class ChatListViewSpaceState { if spaceGroupId == groupId && !pinned.include { if self.add(entry: .HoleEntry(hole)) { hasUpdates = true + } else { + hasUpdates = true + hadRemovals = true } } case .peers: @@ -540,6 +554,9 @@ private final class ChatListViewSpaceState { case let .message(index, messageIndex): if self.add(entry: .IntermediateMessageEntry(index: index, messageIndex: messageIndex)) { hasUpdates = true + } else { + hasUpdates = true + hadRemovals = true } default: break @@ -738,6 +755,9 @@ private final class ChatListViewSpaceState { case let .message(index, messageIndex): if self.add(entry: .IntermediateMessageEntry(index: index, messageIndex: messageIndex)) { hasUpdates = true + } else { + hasUpdates = true + hadRemovals = true } default: break @@ -787,12 +807,48 @@ private final class ChatListViewSpaceState { } } - if hadRemovals { + if true || hadRemovals { self.fillSpace(postbox: postbox) } + + self.checkEntries(postbox: postbox) + self.checkReplayEntries(postbox: postbox) + return hasUpdates } + private func checkEntries(postbox: Postbox) { + #if DEBUG + if case .group(.root, .notPinned, nil) = self.space { + let allEntries = self.orderedEntries.lowerOrAtAnchor + self.orderedEntries.higherThanAnchor + if !allEntries.isEmpty { + assert(allEntries.sorted(by: { $0.index < $1.index }) == allEntries) + + func mapEntry(_ entry: ChatListIntermediateEntry) -> MutableChatListEntry { + switch entry { + case let .message(index, messageIndex): + return .IntermediateMessageEntry(index: index, messageIndex: messageIndex) + case let .hole(hole): + return .HoleEntry(hole) + } + } + + let loadedEntries = postbox.chatListTable.entries(groupId: .root, from: (allEntries[0].index.predecessor, true), to: (allEntries[allEntries.count - 1].index.successor, true), peerChatInterfaceStateTable: postbox.peerChatInterfaceStateTable, count: 1000, predicate: nil).map(mapEntry) + + assert(loadedEntries.map({ $0.index }) == allEntries.map({ $0.index })) + } + } + #endif + } + + private func checkReplayEntries(postbox: Postbox) { + #if DEBUG + let cleanState = ChatListViewSpaceState(postbox: postbox, space: self.space, anchorIndex: self.anchorIndex, summaryComponents: self.summaryComponents, halfLimit: self.halfLimit) + assert(self.orderedEntries.lowerOrAtAnchor.map { $0.index } == cleanState.orderedEntries.lowerOrAtAnchor.map { $0.index }) + assert(self.orderedEntries.higherThanAnchor.map { $0.index } == cleanState.orderedEntries.higherThanAnchor.map { $0.index }) + #endif + } + private func add(entry: MutableChatListEntry) -> Bool { if self.anchorIndex >= entry.entryIndex { let insertionIndex = binaryInsertionIndex(self.orderedEntries.lowerOrAtAnchor, extract: { $0.entryIndex }, searchItem: entry.entryIndex) @@ -805,7 +861,7 @@ private final class ChatListViewSpaceState { } } - if insertionIndex == 0 && self.orderedEntries.lowerOrAtAnchor.count >= self.halfLimit { + if insertionIndex == 0 { return false } self.orderedEntries.insertLowerOrAtAnchorAtArrayIndex(insertionIndex, value: entry) @@ -824,7 +880,7 @@ private final class ChatListViewSpaceState { } } - if insertionIndex == self.orderedEntries.higherThanAnchor.count && self.orderedEntries.higherThanAnchor.count >= self.halfLimit { + if insertionIndex == self.orderedEntries.higherThanAnchor.count { return false } self.orderedEntries.insertHigherThanAnchorAtArrayIndex(insertionIndex, value: entry) @@ -841,11 +897,11 @@ private struct MutableChatListEntryIndex: Hashable, Comparable { var isMessage: Bool var predecessor: MutableChatListEntryIndex { - return MutableChatListEntryIndex(index: self.index.predecessor, isMessage: true) + return MutableChatListEntryIndex(index: self.index.predecessor, isMessage: self.isMessage) } var successor: MutableChatListEntryIndex { - return MutableChatListEntryIndex(index: self.index.successor, isMessage: true) + return MutableChatListEntryIndex(index: self.index.successor, isMessage: self.isMessage) } static let absoluteLowerBound = MutableChatListEntryIndex(index: .absoluteLowerBound, isMessage: true)