Fix mark as read

This commit is contained in:
Peter 2019-07-26 17:46:10 +01:00
parent 897a76a0df
commit a91b0bade5
2 changed files with 38 additions and 20 deletions

View File

@ -282,6 +282,11 @@ public final class Transaction {
return self.postbox?.getTopPeerMessageIndex(peerId: peerId, namespace: namespace) return self.postbox?.getTopPeerMessageIndex(peerId: peerId, namespace: namespace)
} }
public func getTopPeerMessageIndex(peerId: PeerId) -> MessageIndex? {
assert(!self.disposed)
return self.postbox?.getTopPeerMessageIndex(peerId: peerId)
}
public func getPeerChatListIndex(_ peerId: PeerId) -> (PeerGroupId, ChatListIndex)? { public func getPeerChatListIndex(_ peerId: PeerId) -> (PeerGroupId, ChatListIndex)? {
assert(!self.disposed) assert(!self.disposed)
return self.postbox?.chatListTable.getPeerChatListIndex(peerId: peerId) return self.postbox?.chatListTable.getPeerChatListIndex(peerId: peerId)
@ -1781,6 +1786,15 @@ public final class Postbox {
} }
} }
fileprivate func getTopPeerMessageIndex(peerId: PeerId) -> MessageIndex? {
var indices: [MessageIndex] = []
for namespace in self.messageHistoryIndexTable.existingNamespaces(peerId: peerId) {
if let index = self.messageHistoryTable.topIndexEntry(peerId: peerId, namespace: namespace) {
indices.append(index)
}
}
return indices.max()
}
fileprivate func getPeerChatListInclusion(_ id: PeerId) -> PeerChatListInclusion { fileprivate func getPeerChatListInclusion(_ id: PeerId) -> PeerChatListInclusion {
if let inclusion = self.currentUpdatedChatListInclusions[id] { if let inclusion = self.currentUpdatedChatListInclusions[id] {
return inclusion return inclusion

View File

@ -123,32 +123,36 @@ public func togglePeerUnreadMarkInteractively(postbox: Postbox, viewTracker: Acc
} }
public func togglePeerUnreadMarkInteractively(transaction: Transaction, viewTracker: AccountViewTracker, peerId: PeerId, setToValue: Bool? = nil) { public func togglePeerUnreadMarkInteractively(transaction: Transaction, viewTracker: AccountViewTracker, peerId: PeerId, setToValue: Bool? = nil) {
let namespace: MessageId.Namespace let principalNamespace: MessageId.Namespace
if peerId.namespace == Namespaces.Peer.SecretChat { if peerId.namespace == Namespaces.Peer.SecretChat {
namespace = Namespaces.Message.SecretIncoming principalNamespace = Namespaces.Message.SecretIncoming
} else { } else {
namespace = Namespaces.Message.Cloud principalNamespace = Namespaces.Message.Cloud
} }
var hasUnread = false
if let states = transaction.getPeerReadStates(peerId) { if let states = transaction.getPeerReadStates(peerId) {
for i in 0 ..< states.count { for state in states {
if states[i].0 == namespace { if state.1.isUnread {
if states[i].1.isUnread { hasUnread = true
if setToValue == nil || !(setToValue!) { break
if let index = transaction.getTopPeerMessageIndex(peerId: peerId, namespace: namespace) {
let _ = transaction.applyInteractiveReadMaxIndex(index)
} else {
transaction.applyMarkUnread(peerId: peerId, namespace: namespace, value: false, interactive: true)
}
viewTracker.updateMarkAllMentionsSeen(peerId: peerId)
}
} else if namespace == Namespaces.Message.Cloud || namespace == Namespaces.Message.SecretIncoming {
if setToValue == nil || setToValue! {
transaction.applyMarkUnread(peerId: peerId, namespace: namespace, value: true, interactive: true)
}
}
} }
} }
} }
if hasUnread {
if setToValue == nil || !(setToValue!) {
if let index = transaction.getTopPeerMessageIndex(peerId: peerId) {
let _ = transaction.applyInteractiveReadMaxIndex(index)
} else {
transaction.applyMarkUnread(peerId: peerId, namespace: principalNamespace, value: false, interactive: true)
}
viewTracker.updateMarkAllMentionsSeen(peerId: peerId)
}
} else {
if setToValue == nil || setToValue! {
transaction.applyMarkUnread(peerId: peerId, namespace: principalNamespace, value: true, interactive: true)
}
}
} }
public func clearPeerUnseenPersonalMessagesInteractively(account: Account, peerId: PeerId) -> Signal<Never, NoError> { public func clearPeerUnseenPersonalMessagesInteractively(account: Account, peerId: PeerId) -> Signal<Never, NoError> {