Loading state

This commit is contained in:
Ali
2022-10-14 17:41:19 +04:00
parent 0128f6af0a
commit 8838fe095c
4 changed files with 90 additions and 53 deletions

View File

@@ -30,6 +30,7 @@ final class MutableMessageHistoryThreadIndexView: MutablePostboxView {
fileprivate let summaryComponents: ChatListEntrySummaryComponents
fileprivate var peer: Peer?
fileprivate var items: [Item] = []
fileprivate var isLoading: Bool = false
init(postbox: PostboxImpl, peerId: PeerId, summaryComponents: ChatListEntrySummaryComponents) {
self.peerId = peerId
@@ -43,68 +44,73 @@ final class MutableMessageHistoryThreadIndexView: MutablePostboxView {
self.peer = postbox.peerTable.get(self.peerId)
let pinnedThreadIds = postbox.messageHistoryThreadPinnedTable.get(peerId: self.peerId)
var nextPinnedIndex = 0
let validIndexBoundary = postbox.peerThreadCombinedStateTable.get(peerId: peerId)?.validIndexBoundary
self.isLoading = validIndexBoundary == nil
for item in postbox.messageHistoryThreadIndexTable.getAll(peerId: self.peerId) {
var pinnedIndex: Int?
if pinnedThreadIds.contains(item.threadId) {
pinnedIndex = nextPinnedIndex
nextPinnedIndex += 1
if !self.isLoading {
let pinnedThreadIds = postbox.messageHistoryThreadPinnedTable.get(peerId: self.peerId)
var nextPinnedIndex = 0
for item in postbox.messageHistoryThreadIndexTable.getAll(peerId: self.peerId) {
var pinnedIndex: Int?
if pinnedThreadIds.contains(item.threadId) {
pinnedIndex = nextPinnedIndex
nextPinnedIndex += 1
}
var tagSummaryInfo: [ChatListEntryMessageTagSummaryKey: ChatListMessageTagSummaryInfo] = [:]
for (key, component) in self.summaryComponents.components {
var tagSummaryCount: Int32?
var actionsSummaryCount: Int32?
if let tagSummary = component.tagSummary {
let key = MessageHistoryTagsSummaryKey(tag: key.tag, peerId: self.peerId, threadId: item.threadId, namespace: tagSummary.namespace)
if let summary = postbox.messageHistoryTagsSummaryTable.get(key) {
tagSummaryCount = summary.count
}
}
if let actionsSummary = component.actionsSummary {
let key = PendingMessageActionsSummaryKey(type: key.actionType, peerId: self.peerId, namespace: actionsSummary.namespace)
actionsSummaryCount = postbox.pendingMessageActionsMetadataTable.getCount(.peerNamespaceAction(key.peerId, key.namespace, key.type))
}
tagSummaryInfo[key] = ChatListMessageTagSummaryInfo(
tagSummaryCount: tagSummaryCount,
actionsSummaryCount: actionsSummaryCount
)
}
self.items.append(Item(
id: item.threadId,
pinnedIndex: pinnedIndex,
index: item.index,
info: item.info.data,
tagSummaryInfo: tagSummaryInfo,
topMessage: postbox.getMessage(item.index.id)
))
}
var tagSummaryInfo: [ChatListEntryMessageTagSummaryKey: ChatListMessageTagSummaryInfo] = [:]
for (key, component) in self.summaryComponents.components {
var tagSummaryCount: Int32?
var actionsSummaryCount: Int32?
if let tagSummary = component.tagSummary {
let key = MessageHistoryTagsSummaryKey(tag: key.tag, peerId: self.peerId, threadId: item.threadId, namespace: tagSummary.namespace)
if let summary = postbox.messageHistoryTagsSummaryTable.get(key) {
tagSummaryCount = summary.count
self.items.sort(by: { lhs, rhs in
if let lhsPinnedIndex = lhs.pinnedIndex, let rhsPinnedIndex = rhs.pinnedIndex {
return lhsPinnedIndex < rhsPinnedIndex
} else if (lhs.pinnedIndex == nil) != (rhs.pinnedIndex == nil) {
if lhs.pinnedIndex != nil {
return true
} else {
return false
}
}
if let actionsSummary = component.actionsSummary {
let key = PendingMessageActionsSummaryKey(type: key.actionType, peerId: self.peerId, namespace: actionsSummary.namespace)
actionsSummaryCount = postbox.pendingMessageActionsMetadataTable.getCount(.peerNamespaceAction(key.peerId, key.namespace, key.type))
}
tagSummaryInfo[key] = ChatListMessageTagSummaryInfo(
tagSummaryCount: tagSummaryCount,
actionsSummaryCount: actionsSummaryCount
)
}
self.items.append(Item(
id: item.threadId,
pinnedIndex: pinnedIndex,
index: item.index,
info: item.info.data,
tagSummaryInfo: tagSummaryInfo,
topMessage: postbox.getMessage(item.index.id)
))
return lhs.index > rhs.index
})
}
self.items.sort(by: { lhs, rhs in
if let lhsPinnedIndex = lhs.pinnedIndex, let rhsPinnedIndex = rhs.pinnedIndex {
return lhsPinnedIndex < rhsPinnedIndex
} else if (lhs.pinnedIndex == nil) != (rhs.pinnedIndex == nil) {
if lhs.pinnedIndex != nil {
return true
} else {
return false
}
}
return lhs.index > rhs.index
})
}
func replay(postbox: PostboxImpl, transaction: PostboxTransaction) -> Bool {
var updated = false
if transaction.updatedMessageThreadPeerIds.contains(self.peerId) || transaction.updatedPinnedThreads.contains(self.peerId) || transaction.currentUpdatedMessageTagSummaries.contains(where: { $0.key.peerId == self.peerId }) || transaction.currentUpdatedMessageActionsSummaries.contains(where: { $0.key.peerId == self.peerId }) {
if transaction.updatedMessageThreadPeerIds.contains(self.peerId) || transaction.updatedPinnedThreads.contains(self.peerId) || transaction.updatedPeerThreadCombinedStates.contains(self.peerId) || transaction.currentUpdatedMessageTagSummaries.contains(where: { $0.key.peerId == self.peerId }) || transaction.currentUpdatedMessageActionsSummaries.contains(where: { $0.key.peerId == self.peerId }) {
self.reload(postbox: postbox)
updated = true
}
@@ -181,6 +187,7 @@ public final class EngineMessageHistoryThread {
public final class MessageHistoryThreadIndexView: PostboxView {
public let peer: Peer?
public let items: [EngineMessageHistoryThread.Item]
public let isLoading: Bool
init(_ view: MutableMessageHistoryThreadIndexView) {
self.peer = view.peer
@@ -197,6 +204,8 @@ public final class MessageHistoryThreadIndexView: PostboxView {
))
}
self.items = items
self.isLoading = view.isLoading
}
}