diff --git a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift index 292d47dab7..5004e5bb9d 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift @@ -1424,6 +1424,38 @@ private func finalStateWithUpdatesAndServerTime(postbox: Postbox, network: Netwo updatedState.addUpdateChatListFilterOrder(order: order) case let .updateDialogFilter(_, id, filter): updatedState.addUpdateChatListFilter(id: id, filter: filter) + case let .updateBotCommands(peer, botId, apiCommands): + let botPeerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt32Value(botId)) + let commands: [BotCommand] = apiCommands.map { command in + switch command { + case let .botCommand(command, description): + return BotCommand(text: command, description: description) + } + } + updatedState.updateCachedPeerData(peer.peerId, { current in + if peer.peerId.namespace == Namespaces.Peer.CloudUser, let previous = current as? CachedUserData { + if let botInfo = previous.botInfo { + return previous.withUpdatedBotInfo(BotInfo(description: botInfo.description, commands: commands)) + } + } else if peer.peerId.namespace == Namespaces.Peer.CloudGroup, let previous = current as? CachedGroupData { + if let index = previous.botInfos.firstIndex(where: { $0.peerId == botPeerId }) { + var updatedBotInfos = previous.botInfos + let previousBotInfo = updatedBotInfos[index] + updatedBotInfos.remove(at: index) + updatedBotInfos.insert(CachedPeerBotInfo(peerId: botPeerId, botInfo: BotInfo(description: previousBotInfo.botInfo.description, commands: commands)), at: index) + return previous.withUpdatedBotInfos(updatedBotInfos) + } + } else if peer.peerId.namespace == Namespaces.Peer.CloudChannel, let previous = current as? CachedChannelData { + if let index = previous.botInfos.firstIndex(where: { $0.peerId == botPeerId }) { + var updatedBotInfos = previous.botInfos + let previousBotInfo = updatedBotInfos[index] + updatedBotInfos.remove(at: index) + updatedBotInfos.insert(CachedPeerBotInfo(peerId: botPeerId, botInfo: BotInfo(description: previousBotInfo.botInfo.description, commands: commands)), at: index) + return previous.withUpdatedBotInfos(updatedBotInfos) + } + } + return current + }) default: break } diff --git a/submodules/TelegramUI/Sources/ChatMediaInputPeerSpecificItem.swift b/submodules/TelegramUI/Sources/ChatMediaInputPeerSpecificItem.swift index 8d4d0181fc..c2945daf59 100644 --- a/submodules/TelegramUI/Sources/ChatMediaInputPeerSpecificItem.swift +++ b/submodules/TelegramUI/Sources/ChatMediaInputPeerSpecificItem.swift @@ -36,13 +36,13 @@ final class ChatMediaInputPeerSpecificItem: ListViewItem { func nodeConfiguredForParams(async: @escaping (@escaping () -> Void) -> Void, params: ListViewItemLayoutParams, synchronousLoads: Bool, previousItem: ListViewItem?, nextItem: ListViewItem?, completion: @escaping (ListViewItemNode, @escaping () -> (Signal?, (ListViewItemApply) -> Void)) -> Void) { async { let node = ChatMediaInputPeerSpecificItemNode() - node.contentSize = boundingSize + node.contentSize = self.expanded ? expandedBoundingSize : boundingSize node.insets = ChatMediaInputNode.setupPanelIconInsets(item: self, previousItem: previousItem, nextItem: nextItem) node.inputNodeInteraction = self.inputNodeInteraction Queue.mainQueue().async { completion(node, { return (nil, { _ in - node.updateItem(context: self.context, peer: self.peer, collectionId: self.collectionId, theme: self.theme) + node.updateItem(context: self.context, peer: self.peer, collectionId: self.collectionId, theme: self.theme, expanded: self.expanded) node.updateAppearanceTransition(transition: .immediate) }) }) @@ -52,8 +52,8 @@ final class ChatMediaInputPeerSpecificItem: ListViewItem { public func updateNode(async: @escaping (@escaping () -> Void) -> Void, node: @escaping () -> ListViewItemNode, params: ListViewItemLayoutParams, previousItem: ListViewItem?, nextItem: ListViewItem?, animation: ListViewItemUpdateAnimation, completion: @escaping (ListViewItemNodeLayout, @escaping (ListViewItemApply) -> Void) -> Void) { Queue.mainQueue().async { - completion(ListViewItemNodeLayout(contentSize: node().contentSize, insets: ChatMediaInputNode.setupPanelIconInsets(item: self, previousItem: previousItem, nextItem: nextItem)), { _ in - (node() as? ChatMediaInputPeerSpecificItemNode)?.updateItem(context: self.context, peer: self.peer, collectionId: self.collectionId, theme: self.theme) + completion(ListViewItemNodeLayout(contentSize: self.expanded ? expandedBoundingSize : boundingSize, insets: ChatMediaInputNode.setupPanelIconInsets(item: self, previousItem: previousItem, nextItem: nextItem)), { _ in + (node() as? ChatMediaInputPeerSpecificItemNode)?.updateItem(context: self.context, peer: self.peer, collectionId: self.collectionId, theme: self.theme, expanded: self.expanded) }) } } @@ -63,55 +63,94 @@ final class ChatMediaInputPeerSpecificItem: ListViewItem { } } -private let avatarFont = avatarPlaceholderFont(size: 12.0) -private let boundingSize = CGSize(width: 41.0, height: 41.0) -private let boundingImageSize = CGSize(width: 28.0, height: 28.0) -private let highlightSize = CGSize(width: 35.0, height: 35.0) +private let avatarFont = avatarPlaceholderFont(size: 19.0) +private let boundingSize = CGSize(width: 72.0, height: 41.0) +private let expandedBoundingSize = CGSize(width: 72.0, height: 72.0) +private let boundingImageSize = CGSize(width: 45.0, height: 45.0) +private let boundingImageScale: CGFloat = 0.625 +private let highlightSize = CGSize(width: 56.0, height: 56.0) private let verticalOffset: CGFloat = 3.0 final class ChatMediaInputPeerSpecificItemNode: ListViewItemNode { + private let containerNode: ASDisplayNode + private let scalingNode: ASDisplayNode private let avatarNode: AvatarNode private let highlightNode: ASImageNode + private let titleNode: ImmediateTextNode var inputNodeInteraction: ChatMediaInputNodeInteraction? var currentCollectionId: ItemCollectionId? + private var currentExpanded = false private var theme: PresentationTheme? private let stickerFetchedDisposable = MetaDisposable() - init() { + init() { + self.containerNode = ASDisplayNode() + self.containerNode.transform = CATransform3DMakeRotation(CGFloat.pi / 2.0, 0.0, 0.0, 1.0) + + self.scalingNode = ASDisplayNode() + self.highlightNode = ASImageNode() self.highlightNode.isLayerBacked = true self.highlightNode.isHidden = true self.avatarNode = AvatarNode(font: avatarFont) self.avatarNode.isLayerBacked = !smartInvertColorsEnabled() - self.avatarNode.transform = CATransform3DMakeRotation(CGFloat.pi / 2.0, 0.0, 0.0, 1.0) - - let imageSize = CGSize(width: 26.0, height: 26.0) - self.avatarNode.frame = CGRect(origin: CGPoint(x: floor((boundingSize.width - imageSize.width) / 2.0) + verticalOffset, y: floor((boundingSize.height - imageSize.height) / 2.0)), size: imageSize) - - self.highlightNode.frame = CGRect(origin: CGPoint(x: floor((boundingSize.width - highlightSize.width) / 2.0) + verticalOffset, y: floor((boundingSize.height - highlightSize.height) / 2.0)), size: highlightSize) + + self.titleNode = ImmediateTextNode() super.init(layerBacked: false, dynamicBounce: false) - self.addSubnode(self.highlightNode) - self.addSubnode(self.avatarNode) + self.addSubnode(self.containerNode) + self.containerNode.addSubnode(self.scalingNode) + + self.scalingNode.addSubnode(self.highlightNode) + self.scalingNode.addSubnode(self.titleNode) + self.scalingNode.addSubnode(self.avatarNode) } deinit { self.stickerFetchedDisposable.dispose() } - func updateItem(context: AccountContext, peer: Peer, collectionId: ItemCollectionId, theme: PresentationTheme) { + func updateItem(context: AccountContext, peer: Peer, collectionId: ItemCollectionId, theme: PresentationTheme, expanded: Bool) { self.currentCollectionId = collectionId if self.theme !== theme { self.theme = theme self.highlightNode.image = PresentationResourcesChat.chatMediaInputPanelHighlightedIconImage(theme) + + self.titleNode.attributedText = NSAttributedString(string: peer.compactDisplayTitle, font: Font.regular(11.0), textColor: theme.chat.inputPanel.primaryTextColor) } + self.containerNode.frame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: expandedBoundingSize) + self.scalingNode.bounds = CGRect(origin: CGPoint(), size: expandedBoundingSize) + + let boundsSize = expanded ? expandedBoundingSize : CGSize(width: boundingSize.height, height: boundingSize.height) + let imageSize = CGSize(width: 26.0 * 1.6, height: 26.0 * 1.6) + + let expandScale: CGFloat = expanded ? 1.0 : boundingImageScale + let expandTransition: ContainedViewLayoutTransition = self.currentExpanded != expanded ? .animated(duration: 0.3, curve: .spring) : .immediate + expandTransition.updateTransformScale(node: self.scalingNode, scale: expandScale) + expandTransition.updatePosition(node: self.scalingNode, position: CGPoint(x: boundsSize.width / 2.0, y: boundsSize.height / 2.0 + (expanded ? -2.0 : 3.0))) + + expandTransition.updateAlpha(node: self.titleNode, alpha: expanded ? 1.0 : 0.0) + let titleSize = self.titleNode.updateLayout(CGSize(width: expandedBoundingSize.width - 8.0, height: expandedBoundingSize.height)) + + let titleFrame = CGRect(origin: CGPoint(x: floorToScreenPixels((expandedBoundingSize.width - titleSize.width) / 2.0), y: expandedBoundingSize.height - titleSize.height + 2.0), size: titleSize) + let displayTitleFrame = expanded ? titleFrame : CGRect(origin: CGPoint(x: titleFrame.minX, y: self.avatarNode.position.y - titleFrame.size.height), size: titleFrame.size) + expandTransition.updateFrameAsPositionAndBounds(node: self.titleNode, frame: displayTitleFrame) + expandTransition.updateTransformScale(node: self.titleNode, scale: expanded ? 1.0 : 0.001) + + self.currentExpanded = expanded + + self.avatarNode.bounds = CGRect(origin: CGPoint(), size: imageSize) + self.avatarNode.position = CGPoint(x: expandedBoundingSize.height / 2.0, y: expandedBoundingSize.width / 2.0) + self.avatarNode.frame = self.avatarNode.frame + expandTransition.updateFrame(node: self.highlightNode, frame: expanded ? titleFrame.insetBy(dx: -7.0, dy: -2.0) : CGRect(origin: CGPoint(x: self.avatarNode.position.x - highlightSize.width / 2.0, y: self.avatarNode.position.y - highlightSize.height / 2.0), size: highlightSize)) + self.avatarNode.setPeer(context: context, theme: theme, peer: peer) }