fozu
on
Apr 5, 20222022-04-05T00:00:00+08:00
Updated
Apr 10, 20232023-04-10T14:35:37+08:00
2 min read
MutableUnreadMessageCountsView
enum
UnreadMessageCountsItem
1
2
3
| case total(ValueBoxKey?)
case totalInGroup(PeerGroupId)
case peer(PeerId)
|
enum
MutableUnreadMessageCountsItemEntry
1
2
3
| case total((ValueBoxKey, PreferencesEntry?)?, ChatListTotalUnreadState)
case totalInGroup(PeerGroupId, ChatListTotalUnreadState)
case peer(PeerId, CombinedPeerReadState?)
|
enum
UnreadMessageCountsItemEntry
1
2
3
| case total(PreferencesEntry?, ChatListTotalUnreadState)
case totalInGroup(PeerGroupId, ChatListTotalUnreadState)
case peer(PeerId, CombinedPeerReadState?)
|
class
MutableUnreadMessageCountsView:
MutablePostboxView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| private let items: [UnreadMessageCountsItem]
fileprivate var entries: [MutableUnreadMessageCountsItemEntry]
init(postbox: PostboxImpl, items: [UnreadMessageCountsItem]) {
self.items = items
self.entries = items.map { item in
switch item {
case let .total(preferencesKey):
// read from message history metadatatable
return .total(preferencesKey.flatMap({ ($0, postbox.preferencesTable.get(key: $0)) }), postbox.messageHistoryMetadataTable.getTotalUnreadState(groupId: .root))
case let .totalInGroup(groupId):
// read from message history metadata table
return .totalInGroup(groupId, postbox.messageHistoryMetadataTable.getTotalUnreadState(groupId: groupId))
case let .peer(peerId):
// read from read state table
return .peer(peerId, postbox.readStateTable.getCombinedState(peerId))
}
}
}
|
Replay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| func replay(postbox: PostboxImpl, transaction: PostboxTransaction) -> Bool {
var updated = false
var updatedPreferencesEntry: PreferencesEntry?
if !transaction.currentPreferencesOperations.isEmpty {
for i in 0 ..< self.entries.count {
if case let .total(maybeKeyAndValue, _) = self.entries[i], let (key, _) = maybeKeyAndValue {
for operation in transaction.currentPreferencesOperations {
if case let .update(updateKey, value) = operation {
if key == updateKey {
updatedPreferencesEntry = value
}
}
}
}
}
}
if !transaction.currentUpdatedTotalUnreadStates.isEmpty || !transaction.alteredInitialPeerCombinedReadStates.isEmpty || updatedPreferencesEntry != nil {
for i in 0 ..< self.entries.count {
switch self.entries[i] {
case let .total(keyAndEntry, state):
if let updatedState = transaction.currentUpdatedTotalUnreadStates[.root] {
if updatedState != state {
self.entries[i] = .total(keyAndEntry.flatMap({ ($0.0, updatedPreferencesEntry ?? $0.1) }), updatedState)
updated = true
}
}
case let .totalInGroup(groupId, state):
if let updatedState = transaction.currentUpdatedTotalUnreadStates[groupId] {
if updatedState != state {
self.entries[i] = .totalInGroup(groupId, updatedState)
updated = true
}
}
case let .peer(peerId, _):
if transaction.alteredInitialPeerCombinedReadStates[peerId] != nil {
self.entries[i] = .peer(peerId, postbox.readStateTable.getCombinedState(peerId))
updated = true
}
}
}
}
return updated
}
func refreshDueToExternalTransaction(postbox: PostboxImpl) -> Bool {
let entries: [MutableUnreadMessageCountsItemEntry] = self.items.map { item -> MutableUnreadMessageCountsItemEntry in
switch item {
case let .total(preferencesKey):
return .total(preferencesKey.flatMap({ ($0, postbox.preferencesTable.get(key: $0)) }), postbox.messageHistoryMetadataTable.getTotalUnreadState(groupId: .root))
case let .totalInGroup(groupId):
return .totalInGroup(groupId, postbox.messageHistoryMetadataTable.getTotalUnreadState(groupId: groupId))
case let .peer(peerId):
return .peer(peerId, postbox.readStateTable.getCombinedState(peerId))
}
}
if self.entries != entries {
self.entries = entries
return true
} else {
return false
}
}
func immutableView() -> PostboxView {
return UnreadMessageCountsView(self)
}
|