From f830972d325915a4a7abc1593eacd276cd326092 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Tue, 21 Dec 2021 17:56:56 +0400 Subject: [PATCH] Reaction improvements --- .../Sources/ReactionButtonListComponent.swift | 8 ++--- .../Sources/DefaultPresentationStrings.swift | 28 ++++++++++++++++ .../TelegramUI/Sources/ChatController.swift | 32 +++++++++++-------- .../Sources/ChatMessageTransitionNode.swift | 11 ++++--- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/submodules/Components/ReactionButtonListComponent/Sources/ReactionButtonListComponent.swift b/submodules/Components/ReactionButtonListComponent/Sources/ReactionButtonListComponent.swift index 437824c587..a98a6f642c 100644 --- a/submodules/Components/ReactionButtonListComponent/Sources/ReactionButtonListComponent.swift +++ b/submodules/Components/ReactionButtonListComponent/Sources/ReactionButtonListComponent.swift @@ -153,7 +153,7 @@ public final class ReactionButtonAsyncNode: ContextControllerSourceNode { } var counterComponents: [String] = [] - for character in "\(spec.component.count)" { + for character in countString(Int64(spec.component.count)) { counterComponents.append(String(character)) } @@ -164,12 +164,12 @@ public final class ReactionButtonAsyncNode: ContextControllerSourceNode { var previousDisplayCounter: String? if let currentLayout = currentLayout { if currentLayout.spec.component.avatarPeers.isEmpty { - previousDisplayCounter = "\(spec.component.count)" + previousDisplayCounter = countString(Int64(spec.component.count)) } } var currentDisplayCounter: String? if spec.component.avatarPeers.isEmpty { - currentDisplayCounter = "\(spec.component.count)" + currentDisplayCounter = countString(Int64(spec.component.count)) } let backgroundImage: UIImage @@ -669,7 +669,7 @@ public final class ReactionButtonComponent: Component { self.iconView.frame = CGRect(origin: CGPoint(x: sideInsets, y: floorToScreenPixels((height - imageSize.height) / 2.0)), size: imageSize) - let text = "\(component.count)" + let text = countString(Int64(component.count)) var measureText = "" for _ in 0 ..< text.count { measureText.append("0") diff --git a/submodules/TelegramPresentationData/Sources/DefaultPresentationStrings.swift b/submodules/TelegramPresentationData/Sources/DefaultPresentationStrings.swift index 22191fc8e1..53da3d2101 100644 --- a/submodules/TelegramPresentationData/Sources/DefaultPresentationStrings.swift +++ b/submodules/TelegramPresentationData/Sources/DefaultPresentationStrings.swift @@ -95,3 +95,31 @@ public func dataSizeString(_ size: Int64, forceDecimal: Bool = false, formatting return formatting.byte("\(size)").string } } + +public func countString(_ count: Int64, forceDecimal: Bool = false) -> String { + let decimalSeparator = "." + if count >= 1000 * 1000 * 1000 { + let remainder = Int64((Double(count % (1000 * 1000 * 1000)) / (1000 * 1000 * 100.0)).rounded(.down)) + if remainder != 0 || forceDecimal { + return "\(count / (1000 * 1000 * 1000))\(decimalSeparator)\(remainder)T" + } else { + return "\(count / (1000 * 1000 * 1000))T" + } + } else if count >= 1000 * 1000 { + let remainder = Int64((Double(count % (1000 * 1000)) / (1000.0 * 100.0)).rounded(.down)) + if remainder != 0 || forceDecimal { + return "\(count / (1000 * 1000))\(decimalSeparator)\(remainder)M" + } else { + return "\(count / (1000 * 1000))M" + } + } else if count >= 1000 { + let remainder = (count % (1000)) / (102) + if remainder != 0 || forceDecimal { + return "\(count / 1000)\(decimalSeparator)\(remainder)K" + } else { + return "\(count / 1000)K" + } + } else { + return "\(count)" + } +} diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index 90cdd71d0a..98ca92e802 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -1265,23 +1265,27 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G } } }) - } else if let removedReaction = removedReaction, let targetView = itemNode.targetReactionView(value: removedReaction), shouldDisplayInlineDateReactions(message: message) { - var hideRemovedReaction: Bool = false - if let reactions = mergedMessageReactions(attributes: message.attributes) { - for reaction in reactions.reactions { - if reaction.value == removedReaction { - hideRemovedReaction = reaction.count == 1 - break + } else { + strongSelf.chatDisplayNode.messageTransitionNode.dismissMessageReactionContexts(itemNode: itemNode) + + if let removedReaction = removedReaction, let targetView = itemNode.targetReactionView(value: removedReaction), shouldDisplayInlineDateReactions(message: message) { + var hideRemovedReaction: Bool = false + if let reactions = mergedMessageReactions(attributes: message.attributes) { + for reaction in reactions.reactions { + if reaction.value == removedReaction { + hideRemovedReaction = reaction.count == 1 + break + } } } + + let standaloneDismissAnimation = StandaloneDismissReactionAnimation() + standaloneDismissAnimation.frame = strongSelf.chatDisplayNode.bounds + strongSelf.chatDisplayNode.addSubnode(standaloneDismissAnimation) + standaloneDismissAnimation.animateReactionDismiss(sourceView: targetView, hideNode: hideRemovedReaction, completion: { [weak standaloneDismissAnimation] in + standaloneDismissAnimation?.removeFromSupernode() + }) } - - let standaloneDismissAnimation = StandaloneDismissReactionAnimation() - standaloneDismissAnimation.frame = strongSelf.chatDisplayNode.bounds - strongSelf.chatDisplayNode.addSubnode(standaloneDismissAnimation) - standaloneDismissAnimation.animateReactionDismiss(sourceView: targetView, hideNode: hideRemovedReaction, completion: { [weak standaloneDismissAnimation] in - standaloneDismissAnimation?.removeFromSupernode() - }) } let _ = updateMessageReactionsInteractively(account: strongSelf.context.account, messageId: message.id, reaction: updatedReaction).start() diff --git a/submodules/TelegramUI/Sources/ChatMessageTransitionNode.swift b/submodules/TelegramUI/Sources/ChatMessageTransitionNode.swift index 92493c11a6..62e47ed088 100644 --- a/submodules/TelegramUI/Sources/ChatMessageTransitionNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageTransitionNode.swift @@ -775,11 +775,14 @@ public final class ChatMessageTransitionNode: ASDisplayNode { } } - func dismissMessageReactionContexts() { - for messageReactionContext in self.messageReactionContexts { - messageReactionContext.dismiss() + func dismissMessageReactionContexts(itemNode: ListViewItemNode? = nil) { + for i in (0 ..< self.messageReactionContexts.count).reversed() { + let messageReactionContext = self.messageReactionContexts[i] + if itemNode == nil || messageReactionContext.itemNode === itemNode { + self.messageReactionContexts.remove(at: i) + messageReactionContext.dismiss() + } } - self.messageReactionContexts.removeAll() } func addMessageContextController(messageId: MessageId, contextController: ContextController) {