Stars improvements

This commit is contained in:
Isaac 2024-08-15 00:05:37 +08:00
parent d8d7264772
commit 8a041e61f7
5 changed files with 43 additions and 14 deletions

View File

@ -172,7 +172,7 @@ public func updateMessageReactionsInteractively(account: Account, messageIds: [M
|> ignoreValues
}
public func sendStarsReactionsInteractively(account: Account, messageId: MessageId, count: Int, isAnonymous: Bool) -> Signal<Never, NoError> {
public func sendStarsReactionsInteractively(account: Account, messageId: MessageId, count: Int, isAnonymous: Bool?) -> Signal<Never, NoError> {
return account.postbox.transaction { transaction -> Void in
transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: SendStarsReactionsAction(randomId: Int64.random(in: Int64.min ... Int64.max)))
transaction.updateMessage(messageId, update: { currentMessage in
@ -182,15 +182,28 @@ public func sendStarsReactionsInteractively(account: Account, messageId: Message
}
var mappedCount = Int32(count)
var attributes = currentMessage.attributes
var resolvedIsAnonymous = false
for attribute in attributes {
if let attribute = attribute as? ReactionsMessageAttribute {
if let myReaction = attribute.topPeers.first(where: { $0.isMy }) {
resolvedIsAnonymous = myReaction.isAnonymous
}
}
}
loop: for j in 0 ..< attributes.count {
if let current = attributes[j] as? PendingStarsReactionsMessageAttribute {
mappedCount += current.count
resolvedIsAnonymous = current.isAnonymous
attributes.remove(at: j)
break loop
}
}
if let isAnonymous {
resolvedIsAnonymous = isAnonymous
}
attributes.append(PendingStarsReactionsMessageAttribute(accountPeerId: account.peerId, count: mappedCount, isAnonymous: isAnonymous))
attributes.append(PendingStarsReactionsMessageAttribute(accountPeerId: account.peerId, count: mappedCount, isAnonymous: resolvedIsAnonymous))
return .update(StoreMessage(id: currentMessage.id, globallyUniqueId: currentMessage.globallyUniqueId, groupingKey: currentMessage.groupingKey, threadId: currentMessage.threadId, timestamp: currentMessage.timestamp, flags: StoreMessageFlags(currentMessage.flags), tags: currentMessage.tags, globalTags: currentMessage.globalTags, localTags: currentMessage.localTags, forwardInfo: storeForwardInfo, authorId: currentMessage.author?.id, text: currentMessage.text, attributes: attributes, media: currentMessage.media))
})
@ -395,7 +408,6 @@ private func requestSendStarsReaction(postbox: Postbox, network: Network, stateM
}
|> mapToSignal { result -> Signal<Never, RequestUpdateMessageReactionError> in
stateManager.starsContext?.add(balance: Int64(-count), addTransaction: false)
//stateManager.starsContext?.load(force: true)
return postbox.transaction { transaction -> Void in
transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: UpdateMessageReactionsAction())

View File

@ -334,7 +334,7 @@ public extension TelegramEngine {
).startStandalone()
}
public func sendStarsReaction(id: EngineMessage.Id, count: Int, isAnonymous: Bool) {
public func sendStarsReaction(id: EngineMessage.Id, count: Int, isAnonymous: Bool?) {
let _ = sendStarsReactionsInteractively(account: self.account, messageId: id, count: count, isAnonymous: isAnonymous).startStandalone()
}

View File

@ -1652,11 +1652,14 @@ private final class ChatSendStarsScreenComponent: Component {
myCountAddition = Int(self.amount.realValue)
}
myCount += myCountAddition
mappedTopPeers.append(ChatSendStarsScreen.TopPeer(
peer: self.isAnonymous ? nil : component.myPeer,
isMy: true,
count: myCount
))
if myCount != 0 {
mappedTopPeers.append(ChatSendStarsScreen.TopPeer(
randomIndex: -1,
peer: self.isAnonymous ? nil : component.myPeer,
isMy: true,
count: myCount
))
}
mappedTopPeers.sort(by: { $0.count > $1.count })
if mappedTopPeers.count > 3 {
mappedTopPeers = Array(mappedTopPeers.prefix(3))
@ -2064,7 +2067,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
fileprivate final class TopPeer: Equatable {
enum Id: Hashable {
case anonymous
case anonymous(Int)
case my
case peer(EnginePeer.Id)
}
@ -2075,7 +2078,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
} else if let peer = self.peer {
return .peer(peer.id)
} else {
return .anonymous
return .anonymous(self.randomIndex)
}
}
@ -2083,17 +2086,22 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
return self.peer == nil
}
let randomIndex: Int
let peer: EnginePeer?
let isMy: Bool
let count: Int
init(peer: EnginePeer?, isMy: Bool, count: Int) {
init(randomIndex: Int, peer: EnginePeer?, isMy: Bool, count: Int) {
self.randomIndex = randomIndex
self.peer = peer
self.isMy = isMy
self.count = count
}
static func ==(lhs: TopPeer, rhs: TopPeer) -> Bool {
if lhs.randomIndex != rhs.randomIndex {
return false
}
if lhs.peer != rhs.peer {
return false
}
@ -2210,6 +2218,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
return nil
}
var nextRandomIndex = 0
return InitialData(
peer: peer,
myPeer: myPeer,
@ -2218,7 +2227,10 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
currentSentAmount: currentSentAmount,
topPeers: topPeers.compactMap { topPeer -> ChatSendStarsScreen.TopPeer? in
guard let topPeerId = topPeer.peerId else {
let randomIndex = nextRandomIndex
nextRandomIndex += 1
return ChatSendStarsScreen.TopPeer(
randomIndex: randomIndex,
peer: nil,
isMy: topPeer.isMy,
count: Int(topPeer.count)
@ -2230,7 +2242,10 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
guard let topPeerValue else {
return nil
}
let randomIndex = nextRandomIndex
nextRandomIndex += 1
return ChatSendStarsScreen.TopPeer(
randomIndex: randomIndex,
peer: topPeer.isAnonymous ? nil : topPeerValue,
isMy: topPeer.isMy,
count: Int(topPeer.count)
@ -2239,6 +2254,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
myTopPeer: myTopPeer.flatMap { topPeer -> ChatSendStarsScreen.TopPeer? in
guard let topPeerId = topPeer.peerId else {
return ChatSendStarsScreen.TopPeer(
randomIndex: -1,
peer: nil,
isMy: topPeer.isMy,
count: Int(topPeer.count)
@ -2251,6 +2267,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer {
return nil
}
return ChatSendStarsScreen.TopPeer(
randomIndex: -1,
peer: topPeer.isAnonymous ? nil : topPeerValue,
isMy: topPeer.isMy,
count: Int(topPeer.count)

View File

@ -424,7 +424,7 @@ extension ChatControllerImpl {
return
}
strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: false)
strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: nil)
strongSelf.displayOrUpdateSendStarsUndo(messageId: message.id, count: 1)
})
} else {

View File

@ -1729,7 +1729,7 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G
return
}
strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: false)
strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: nil)
strongSelf.displayOrUpdateSendStarsUndo(messageId: message.id, count: 1)
})
} else {