Swiftgram/submodules/Postbox/Sources/AllChatListHolesView.swift
2021-09-22 01:56:45 +03:00

64 lines
2.1 KiB
Swift

import Foundation
final class MutableAllChatListHolesView: MutablePostboxView {
fileprivate let groupId: PeerGroupId
private var holes = Set<ChatListHole>()
fileprivate var latestHole: ChatListHole?
init(postbox: PostboxImpl, groupId: PeerGroupId) {
self.groupId = groupId
self.holes = Set(postbox.chatListTable.allHoles(groupId: groupId))
self.latestHole = self.holes.max(by: { $0.index < $1.index })
}
func replay(postbox: PostboxImpl, transaction: PostboxTransaction) -> Bool {
if let operations = transaction.chatListOperations[self.groupId] {
var updated = false
for operation in operations {
switch operation {
case let .InsertHole(hole):
if !self.holes.contains(hole) {
self.holes.insert(hole)
updated = true
}
case let .RemoveHoles(indices):
for index in indices {
if self.holes.contains(ChatListHole(index: index.messageIndex)) {
self.holes.remove(ChatListHole(index: index.messageIndex))
updated = true
}
}
default:
break
}
}
if updated {
let updatedLatestHole = self.holes.max(by: { $0.index < $1.index })
if updatedLatestHole != self.latestHole {
self.latestHole = updatedLatestHole
return true
} else {
return false
}
} else {
return false
}
} else {
return false
}
}
func immutableView() -> PostboxView {
return AllChatListHolesView(self)
}
}
public final class AllChatListHolesView: PostboxView {
public let latestHole: ChatListHole?
init(_ view: MutableAllChatListHolesView) {
self.latestHole = view.latestHole
}
}