From 3cb9b21c72835a8e1d3ab03a301035ad143ef2fe Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Mon, 19 Jun 2023 23:08:37 +0400 Subject: [PATCH] Fix story reactions --- .../StoryItemSetContainerComponent.swift | 97 +++++++++---------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift b/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift index cef36cd0ae..ba4b1f1230 100644 --- a/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift +++ b/submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift @@ -1965,18 +1965,14 @@ public final class StoryItemSetContainerComponent: Component { return } - var selectedReaction: AvailableReactions.Reaction? + var animation: TelegramMediaFile? for reaction in availableReactions.reactions { if reaction.value == updateReaction.reaction { - selectedReaction = reaction + animation = reaction.centerAnimation break } } - - guard let reaction = selectedReaction else { - return - } - + let targetView = UIView(frame: CGRect(origin: CGPoint(x: floor((self.bounds.width - 100.0) * 0.5), y: floor((self.bounds.height - 100.0) * 0.5)), size: CGSize(width: 100.0, height: 100.0))) targetView.isUserInteractionEnabled = false self.addSubview(targetView) @@ -1997,71 +1993,70 @@ public final class StoryItemSetContainerComponent: Component { }) } }) + if hasFirstResponder(self) { self.endEditing(true) } self.state?.updated(transition: Transition(animation: .curve(duration: 0.25, curve: .easeInOut))) - - if let centerAnimation = reaction.centerAnimation { - let file = centerAnimation - - var text = "" - var messageAttributes: [MessageAttribute] = [] - var inlineStickers: [MediaId : Media] = [:] - switch reaction.value { - case let .builtin(textValue): - text = textValue - case .custom: + + var text = "" + var messageAttributes: [MessageAttribute] = [] + var inlineStickers: [MediaId : Media] = [:] + switch updateReaction { + case let .builtin(textValue): + text = textValue + case let .custom(fileId, file): + if let file { + animation = file loop: for attribute in file.attributes { switch attribute { case let .CustomEmoji(_, _, displayText, _): text = displayText - messageAttributes = [ - TextEntitiesMessageAttribute(entities: [MessageTextEntity(range: 0 ..< 1, type: .CustomEmoji(stickerPack: nil, fileId: centerAnimation.fileId.id))]) - ] - inlineStickers = [centerAnimation.fileId: centerAnimation] + let length = (text as NSString).length + messageAttributes = [TextEntitiesMessageAttribute(entities: [MessageTextEntity(range: 0 ..< length, type: .CustomEmoji(stickerPack: nil, fileId: fileId))])] + inlineStickers = [file.fileId: file] break loop default: break } } } - - - let message: EnqueueMessage = .message( - text: text, - attributes: messageAttributes, - inlineStickers: inlineStickers, - mediaReference: nil, - replyToMessageId: nil, - replyToStoryId: StoryId(peerId: component.slice.peer.id, id: component.slice.item.storyItem.id), - localGroupingKey: nil, - correlationId: nil, - bubbleUpEmojiOrStickersets: [] - ) - - let context = component.context - let presentationData = component.context.sharedContext.currentPresentationData.with({ $0 }).withUpdated(theme: component.theme) - let presentController = component.presentController - let controller = component.controller - let peer = component.slice.peer - - let _ = (enqueueMessages(account: context.account, peerId: peer.id, messages: [message]) - |> deliverOnMainQueue).start(next: { messageIds in + } + + let message: EnqueueMessage = .message( + text: text, + attributes: messageAttributes, + inlineStickers: inlineStickers, + mediaReference: nil, + replyToMessageId: nil, + replyToStoryId: StoryId(peerId: component.slice.peer.id, id: component.slice.item.storyItem.id), + localGroupingKey: nil, + correlationId: nil, + bubbleUpEmojiOrStickersets: [] + ) + + let context = component.context + let presentationData = component.context.sharedContext.currentPresentationData.with({ $0 }).withUpdated(theme: component.theme) + let presentController = component.presentController + let peer = component.slice.peer + + let _ = (enqueueMessages(account: context.account, peerId: peer.id, messages: [message]) + |> deliverOnMainQueue).start(next: { [weak self] messageIds in + if let animation { presentController(UndoOverlayController( presentationData: presentationData, - content: .sticker(context: context, file: centerAnimation, loop: false, title: nil, text: "Reaction Sent.", undoText: "View in Chat", customAction: { - if let messageId = messageIds.first, let messageId, let navigationController = controller()?.navigationController as? NavigationController { - context.sharedContext.navigateToChatController(NavigateToChatControllerParams(navigationController: navigationController, context: context, chatLocation: .peer(peer), subject: .message(id: .id(messageId), highlight: false, timecode: nil))) + content: .sticker(context: context, file: animation, loop: false, title: nil, text: "Reaction Sent.", undoText: "View in Chat", customAction: { [weak self] in + if let messageId = messageIds.first, let self { + self.navigateToPeer(peer: peer, messageId: messageId) } }), elevatedLayout: false, animateInAsReplacement: false, action: { _ in return false } ), nil) - }) - } + } + }) }) } } @@ -2236,7 +2231,7 @@ public final class StoryItemSetContainerComponent: Component { }) } - private func navigateToPeer(peer: EnginePeer) { + private func navigateToPeer(peer: EnginePeer, messageId: EngineMessage.Id? = nil) { guard let component = self.component else { return } @@ -2247,7 +2242,7 @@ public final class StoryItemSetContainerComponent: Component { return } - component.context.sharedContext.navigateToChatController(NavigateToChatControllerParams(navigationController: navigationController, context: component.context, chatLocation: .peer(peer), keepStack: .always, animated: true, pushController: { [weak controller, weak navigationController] chatController, animated, completion in + component.context.sharedContext.navigateToChatController(NavigateToChatControllerParams(navigationController: navigationController, context: component.context, chatLocation: .peer(peer), subject: messageId.flatMap { .message(id: .id($0), highlight: false, timecode: nil) }, keepStack: .always, animated: true, pushController: { [weak controller, weak navigationController] chatController, animated, completion in guard let controller, let navigationController else { return }