From 8d7566afec9337a36359ea98567e421460bec039 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sun, 9 Apr 2023 04:50:07 +0400 Subject: [PATCH] Bot app improvements --- .../TelegramUI/Sources/ChatController.swift | 22 ++++++- .../Sources/ChatTextInputPanelNode.swift | 58 ++++++++++--------- .../Sources/NavigateToChatController.swift | 4 +- .../WebUI/Sources/WebAppController.swift | 2 +- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index 301c4bee33..5887e0432b 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -12797,7 +12797,7 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G self.presentAttachmentMenu(subject: .bot(id: botId, payload: payload, justInstalled: justInstalled)) } - public func presentBotApp(botApp: BotApp, payload: String?) { + public func presentBotApp(botApp: BotApp, botPeer: EnginePeer, payload: String?) { guard let peerId = self.chatLocation.peerId else { return } @@ -12845,6 +12845,8 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G } } + let botAddress = botPeer.addressName ?? "" + self.messageActionCallbackDisposable.set(((self.context.engine.messages.requestAppWebView(peerId: peerId, appReference: .id(id: botApp.id, accessHash: botApp.accessHash), payload: payload, themeParams: generateWebAppThemeParams(self.presentationData.theme), allowWrite: false) |> afterDisposed { updateProgress() @@ -12856,6 +12858,22 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G let params = WebAppParameters(peerId: peerId, botId: peerId, botName: botApp.title, url: url, queryId: 0, payload: payload, buttonText: "", keepAliveSignal: nil, fromMenu: false, fromAttachMenu: false, isInline: false, isSimple: false) let controller = standaloneWebAppController(context: strongSelf.context, updatedPresentationData: strongSelf.updatedPresentationData, params: params, threadId: strongSelf.chatLocation.threadId, openUrl: { [weak self] url in self?.openUrl(url, concealed: true, forceExternal: true) + }, requestSwitchInline: { [weak self] query, chatTypes, completion in + if let strongSelf = self { + if let chatTypes { + let controller = strongSelf.context.sharedContext.makePeerSelectionController(PeerSelectionControllerParams(context: strongSelf.context, filter: [.excludeRecent, .doNotSearchMessages], requestPeerType: chatTypes, hasContactSelector: false, hasCreation: false)) + controller.peerSelected = { [weak self, weak controller] peer, _ in + if let strongSelf = self { + completion() + controller?.dismiss() + strongSelf.controllerInteraction?.activateSwitchInline(peer.id, "@\(botAddress) \(query)") + } + } + strongSelf.push(controller) + } else { + strongSelf.controllerInteraction?.activateSwitchInline(peerId, "@\(botAddress) \(query)") + } + } }, completion: { [weak self] in self?.chatDisplayNode.historyNode.scrollToEndOfHistory() }, getNavigationController: { [weak self] in @@ -17267,7 +17285,7 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G if let strongSelf = self, let peer { let openBotApp = { [weak self] in if let strongSelf = self { - strongSelf.presentBotApp(botApp: botAppStart.botApp, payload: botAppStart.payload) + strongSelf.presentBotApp(botApp: botAppStart.botApp, botPeer: peerId, payload: botAppStart.payload) } } if concealed { diff --git a/submodules/TelegramUI/Sources/ChatTextInputPanelNode.swift b/submodules/TelegramUI/Sources/ChatTextInputPanelNode.swift index aa7835459d..e92874376b 100644 --- a/submodules/TelegramUI/Sources/ChatTextInputPanelNode.swift +++ b/submodules/TelegramUI/Sources/ChatTextInputPanelNode.swift @@ -1472,43 +1472,47 @@ class ChatTextInputPanelNode: ChatInputPanelNode, ASEditableTextNodeDelegate { } else { transition.animatePosition(layer: self.startButton.layer, from: CGPoint(x: 0.0, y: 80.0), to: CGPoint(), additive: true) } - } - - if let context = self.context { - let absoluteFrame = self.startButton.view.convert(self.startButton.bounds, to: nil) - let location = CGRect(origin: CGPoint(x: absoluteFrame.midX, y: absoluteFrame.minY - 1.0), size: CGSize()) - - if let tooltipController = self.tooltipController { - if self.view.window != nil { - tooltipController.location = .point(location, .bottom) - } - } else { - let controller = TooltipScreen(account: context.account, sharedContext: context.sharedContext, text: interfaceState.strings.Bot_TapToUse, icon: .downArrows, location: .point(location, .bottom), displayDuration: .infinite, shouldDismissOnTouch: { _ in - return .ignore - }) - controller.alwaysVisible = true - self.tooltipController = controller + if let context = self.context { + let absoluteFrame = self.startButton.view.convert(self.startButton.bounds, to: nil) + let location = CGRect(origin: CGPoint(x: absoluteFrame.midX, y: absoluteFrame.minY - 1.0), size: CGSize()) - let delay: Double - if case .regular = metrics.widthClass { - delay = 0.1 + if let tooltipController = self.tooltipController { + if self.view.window != nil { + tooltipController.location = .point(location, .bottom) + } } else { - delay = 0.35 + let controller = TooltipScreen(account: context.account, sharedContext: context.sharedContext, text: interfaceState.strings.Bot_TapToUse, icon: .downArrows, location: .point(location, .bottom), displayDuration: .infinite, shouldDismissOnTouch: { _ in + return .ignore + }) + controller.alwaysVisible = true + self.tooltipController = controller + + let delay: Double + if case .regular = metrics.widthClass { + delay = 0.1 + } else { + delay = 0.35 + } + Queue.mainQueue().after(delay, { + let absoluteFrame = self.startButton.view.convert(self.startButton.bounds, to: nil) + let location = CGRect(origin: CGPoint(x: absoluteFrame.midX, y: absoluteFrame.minY - 1.0), size: CGSize()) + controller.location = .point(location, .bottom) + self.interfaceInteraction?.presentControllerInCurrent(controller, nil) + }) } - Queue.mainQueue().after(delay, { - let absoluteFrame = self.startButton.view.convert(self.startButton.bounds, to: nil) - let location = CGRect(origin: CGPoint(x: absoluteFrame.midX, y: absoluteFrame.minY - 1.0), size: CGSize()) - controller.location = .point(location, .bottom) - self.interfaceInteraction?.presentControllerInCurrent(controller, nil) - }) + } + } else { + if hasMenuButton && !self.animatingTransition { + self.menuButton.isHidden = true } } } else if !self.startButton.isHidden { if hasMenuButton { self.animateBotButtonOutToMenu(transition: transition) } else { - transition.animatePosition(node: self.startButton, to: CGPoint(x: 0.0, y: 80.0), additive: true, completion: { _ in + transition.animatePosition(node: self.startButton, to: CGPoint(x: 0.0, y: 80.0), removeOnCompletion: false, additive: true, completion: { _ in self.startButton.isHidden = true + self.startButton.layer.removeAllAnimations() }) } } diff --git a/submodules/TelegramUI/Sources/NavigateToChatController.swift b/submodules/TelegramUI/Sources/NavigateToChatController.swift index 396aba0eab..4776b1c213 100644 --- a/submodules/TelegramUI/Sources/NavigateToChatController.swift +++ b/submodules/TelegramUI/Sources/NavigateToChatController.swift @@ -132,8 +132,8 @@ public func navigateToChatControllerImpl(_ params: NavigateToChatControllerParam if let attachBotStart = params.attachBotStart { controller.presentAttachmentBot(botId: attachBotStart.botId, payload: attachBotStart.payload, justInstalled: attachBotStart.justInstalled) } - if let botAppStart = params.botAppStart { - controller.presentBotApp(botApp: botAppStart.botApp, payload: botAppStart.payload) + if let botAppStart = params.botAppStart, case let .peer(peer) = params.chatLocation { + controller.presentBotApp(botApp: botAppStart.botApp, botPeer: peer, payload: botAppStart.payload) } } else { controller = ChatControllerImpl(context: params.context, chatLocation: params.chatLocation.asChatLocation, chatLocationContextHolder: params.chatLocationContextHolder, subject: params.subject, botStart: params.botStart, attachBotStart: params.attachBotStart, botAppStart: params.botAppStart, peekData: params.peekData, peerNearbyData: params.peerNearbyData, chatListFilter: params.chatListFilter, chatNavigationStack: params.chatNavigationStack) diff --git a/submodules/WebUI/Sources/WebAppController.swift b/submodules/WebUI/Sources/WebAppController.swift index e1b7eb7816..26c91b9f62 100644 --- a/submodules/WebUI/Sources/WebAppController.swift +++ b/submodules/WebUI/Sources/WebAppController.swift @@ -629,7 +629,7 @@ public final class WebAppController: ViewController, AttachmentContainable { case "web_app_ready": self.animateTransitionIn() case "web_app_switch_inline_query": - if controller.isInline, let json, let query = json["query"] as? String { + if let json, let query = json["query"] as? String { if let chatTypes = json["chat_types"] as? [String], !chatTypes.isEmpty { var requestPeerTypes: [ReplyMarkupButtonRequestPeerType] = [] for type in chatTypes {