mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Initial web app implementation
This commit is contained in:
@@ -3,6 +3,7 @@ import UIKit
|
||||
import WebKit
|
||||
import Display
|
||||
import AsyncDisplayKit
|
||||
import Postbox
|
||||
import TelegramCore
|
||||
import SwiftSignalKit
|
||||
import TelegramPresentationData
|
||||
@@ -10,6 +11,8 @@ import AccountContext
|
||||
import AttachmentUI
|
||||
import CounterContollerTitleView
|
||||
import ContextUI
|
||||
import PresentationDataUtils
|
||||
import HexColor
|
||||
|
||||
private class WeakGameScriptMessageHandler: NSObject, WKScriptMessageHandler {
|
||||
private let f: (WKScriptMessage) -> ()
|
||||
@@ -25,6 +28,17 @@ private class WeakGameScriptMessageHandler: NSObject, WKScriptMessageHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private func generateThemeParams(_ presentationTheme: PresentationTheme) -> [String: Any] {
|
||||
return [
|
||||
"bg_color": Int32(bitPattern: presentationTheme.list.plainBackgroundColor.rgb),
|
||||
"text_color": Int32(bitPattern: presentationTheme.list.itemPrimaryTextColor.rgb),
|
||||
"hint_color": Int32(bitPattern: presentationTheme.list.blocksBackgroundColor.rgb),
|
||||
"link_color": Int32(bitPattern: presentationTheme.list.itemAccentColor.rgb),
|
||||
"button_color": Int32(bitPattern: presentationTheme.list.itemCheckColors.fillColor.rgb),
|
||||
"button_text_color": Int32(bitPattern: presentationTheme.list.itemCheckColors.foregroundColor.rgb)
|
||||
]
|
||||
}
|
||||
|
||||
public final class WebAppController: ViewController, AttachmentContainable {
|
||||
public var requestAttachmentMenuExpansion: () -> Void = { }
|
||||
public var updateNavigationStack: (@escaping ([AttachmentContainable]) -> ([AttachmentContainable], AttachmentMediaPickerContext?)) -> Void = { _ in }
|
||||
@@ -32,18 +46,20 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
public var cancelPanGesture: () -> Void = { }
|
||||
|
||||
private class Node: ViewControllerTracingNode {
|
||||
private weak var controller: WebAppController?
|
||||
|
||||
private var webView: WKWebView?
|
||||
|
||||
private let context: AccountContext
|
||||
var presentationData: PresentationData
|
||||
private let present: (ViewController, Any?) -> Void
|
||||
private let message: EngineMessage?
|
||||
private var queryId: Int64?
|
||||
|
||||
init(context: AccountContext, presentationData: PresentationData, url: String, present: @escaping (ViewController, Any?) -> Void, message: EngineMessage?) {
|
||||
init(context: AccountContext, controller: WebAppController, presentationData: PresentationData, peerId: PeerId, botId: PeerId, url: String?, present: @escaping (ViewController, Any?) -> Void) {
|
||||
self.context = context
|
||||
self.controller = controller
|
||||
self.presentationData = presentationData
|
||||
self.present = present
|
||||
self.message = message
|
||||
|
||||
super.init()
|
||||
|
||||
@@ -92,14 +108,26 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
self.view.addSubview(webView)
|
||||
self.webView = webView
|
||||
|
||||
if let parsedUrl = URL(string: url) {
|
||||
webView.load(URLRequest(url: parsedUrl))
|
||||
}
|
||||
let _ = (context.engine.messages.requestWebView(peerId: peerId, botId: botId, url: url, themeParams: generateThemeParams(presentationData.theme))
|
||||
|> deliverOnMainQueue).start(next: { [weak self] result in
|
||||
guard let strongSelf = self else {
|
||||
return
|
||||
}
|
||||
switch result {
|
||||
case let .webViewResult(queryId, url):
|
||||
if let parsedUrl = URL(string: url) {
|
||||
strongSelf.queryId = queryId
|
||||
strongSelf.webView?.load(URLRequest(url: parsedUrl))
|
||||
}
|
||||
case .requestConfirmation:
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func containerLayoutUpdated(_ layout: ContainerViewLayout, navigationBarHeight: CGFloat, transition: ContainedViewLayoutTransition) {
|
||||
if let webView = self.webView {
|
||||
webView.frame = CGRect(origin: CGPoint(x: 0.0, y: navigationBarHeight), size: CGSize(width: layout.size.width, height: max(1.0, layout.size.height - navigationBarHeight)))
|
||||
webView.frame = CGRect(origin: CGPoint(x: 0.0, y: navigationBarHeight), size: CGSize(width: layout.size.width, height: max(1.0, layout.size.height - navigationBarHeight - layout.intrinsicInsets.bottom)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,34 +141,6 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
})
|
||||
}
|
||||
|
||||
private func shareData() -> (EnginePeer, String)? {
|
||||
guard let message = self.message else {
|
||||
return nil
|
||||
}
|
||||
var botPeer: EnginePeer?
|
||||
var gameName: String?
|
||||
for media in message.media {
|
||||
if let game = media as? TelegramMediaGame {
|
||||
inner: for attribute in message.attributes {
|
||||
if let attribute = attribute as? InlineBotMessageAttribute, let peerId = attribute.peerId {
|
||||
botPeer = message.peers[peerId].flatMap(EnginePeer.init)
|
||||
break inner
|
||||
}
|
||||
}
|
||||
if botPeer == nil {
|
||||
botPeer = message.author
|
||||
}
|
||||
|
||||
gameName = game.name
|
||||
}
|
||||
}
|
||||
if let botPeer = botPeer, let gameName = gameName {
|
||||
return (botPeer, gameName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
private func handleScriptMessage(_ message: WKScriptMessage) {
|
||||
guard let body = message.body as? [String: Any] else {
|
||||
return
|
||||
@@ -150,15 +150,61 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
return
|
||||
}
|
||||
|
||||
if eventName == "share_game" || eventName == "share_score" {
|
||||
if let (botPeer, gameName) = self.shareData(), let addressName = botPeer.addressName, !addressName.isEmpty, !gameName.isEmpty {
|
||||
if eventName == "share_score" {
|
||||
|
||||
} else {
|
||||
|
||||
switch eventName {
|
||||
case "webview_send_result_message":
|
||||
self.handleSendResultMessage()
|
||||
case "webview_close":
|
||||
self.controller?.dismiss()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func sendEvent(name: String, data: String) {
|
||||
let script = "window.TelegramGameProxy.receiveEvent(\"\(name)\", \(data))"
|
||||
self.webView?.evaluateJavaScript(script, completionHandler: { _, _ in
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func updatePresentationData(_ presentationData: PresentationData) {
|
||||
self.presentationData = presentationData
|
||||
|
||||
let themeParams = generateThemeParams(presentationData.theme)
|
||||
var themeParamsString = "{"
|
||||
for (key, value) in themeParams {
|
||||
if let value = value as? Int32 {
|
||||
let color = UIColor(rgb: UInt32(bitPattern: value))
|
||||
|
||||
if themeParamsString.count > 1 {
|
||||
themeParamsString.append(", ")
|
||||
}
|
||||
themeParamsString.append("\"\(key)\": \"#\(color.hexString)\"")
|
||||
}
|
||||
}
|
||||
themeParamsString.append("}")
|
||||
self.sendEvent(name: "theme_changed", data: themeParamsString)
|
||||
}
|
||||
|
||||
private func handleSendResultMessage() {
|
||||
guard let controller = self.controller, let queryId = self.queryId else {
|
||||
return
|
||||
}
|
||||
|
||||
let _ = (self.context.engine.messages.getWebViewResult(peerId: controller.peerId, botId: controller.botId, queryId: queryId)
|
||||
|> deliverOnMainQueue).start(next: { [weak self] result in
|
||||
guard let strongSelf = self, let controller = strongSelf.controller else {
|
||||
return
|
||||
}
|
||||
|
||||
controller.present(textAlertController(context: strongSelf.context, updatedPresentationData: controller.updatedPresentationData, title: nil, text: "Send result?", actions: [TextAlertAction(type: .genericAction, title: strongSelf.presentationData.strings.Common_Cancel, action: {}), TextAlertAction(type: .defaultAction, title: strongSelf.presentationData.strings.Common_OK, action: { [weak self] in
|
||||
guard let strongSelf = self, let controller = strongSelf.controller else {
|
||||
return
|
||||
}
|
||||
let _ = strongSelf.context.engine.messages.enqueueOutgoingMessageWithChatContextResult(to: controller.peerId, botId: controller.botId, result: result)
|
||||
controller.dismiss()
|
||||
})]), in: .window(.root))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,19 +213,25 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
return self.displayNode as! Node
|
||||
}
|
||||
|
||||
private var titleView: CounterContollerTitleView?
|
||||
private let moreButtonNode: MoreButtonNode
|
||||
|
||||
private let context: AccountContext
|
||||
private let url: String
|
||||
private let message: EngineMessage?
|
||||
private let peerId: PeerId
|
||||
private let botId: PeerId
|
||||
private let url: String?
|
||||
|
||||
private var presentationData: PresentationData
|
||||
fileprivate let updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)?
|
||||
private var presentationDataDisposable: Disposable?
|
||||
|
||||
public init(context: AccountContext, updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)? = nil, url: String, message: EngineMessage?) {
|
||||
public init(context: AccountContext, updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)? = nil, peerId: PeerId, botId: PeerId, botName: String, url: String?) {
|
||||
self.context = context
|
||||
self.peerId = peerId
|
||||
self.botId = botId
|
||||
self.url = url
|
||||
self.message = message
|
||||
|
||||
self.updatedPresentationData = updatedPresentationData
|
||||
self.presentationData = updatedPresentationData?.initial ?? context.sharedContext.currentPresentationData.with { $0 }
|
||||
|
||||
var theme = NavigationBarTheme(rootControllerTheme: self.presentationData.theme)
|
||||
@@ -199,14 +251,30 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
self.navigationItem.rightBarButtonItem?.target = self
|
||||
|
||||
let titleView = CounterContollerTitleView(theme: self.presentationData.theme)
|
||||
titleView.title = CounterContollerTitle(title: "Web App", counter: self.presentationData.strings.Bot_GenericBotStatus)
|
||||
titleView.title = CounterContollerTitle(title: botName, counter: self.presentationData.strings.Bot_GenericBotStatus)
|
||||
self.navigationItem.titleView = titleView
|
||||
self.titleView = titleView
|
||||
|
||||
self.moreButtonNode.action = { [weak self] _, gesture in
|
||||
if let strongSelf = self {
|
||||
strongSelf.morePressed(node: strongSelf.moreButtonNode.contextSourceNode, gesture: gesture)
|
||||
}
|
||||
}
|
||||
|
||||
self.presentationDataDisposable = ((updatedPresentationData?.signal ?? context.sharedContext.presentationData)
|
||||
|> deliverOnMainQueue).start(next: { [weak self] presentationData in
|
||||
if let strongSelf = self {
|
||||
strongSelf.presentationData = presentationData
|
||||
|
||||
var theme = NavigationBarTheme(rootControllerTheme: presentationData.theme)
|
||||
theme = theme.withUpdatedBackgroundColor(presentationData.theme.list.plainBackgroundColor)
|
||||
let navigationBarPresentationData = NavigationBarPresentationData(theme: theme, strings: NavigationBarStrings(back: "", close: ""))
|
||||
strongSelf.navigationBar?.updatePresentationData(navigationBarPresentationData)
|
||||
strongSelf.titleView?.theme = presentationData.theme
|
||||
|
||||
strongSelf.controllerNode.updatePresentationData(presentationData)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
required public init(coder aDecoder: NSCoder) {
|
||||
@@ -215,6 +283,7 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
|
||||
deinit {
|
||||
assert(true)
|
||||
self.presentationDataDisposable?.dispose()
|
||||
}
|
||||
|
||||
@objc private func cancelPressed() {
|
||||
@@ -226,20 +295,22 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
}
|
||||
|
||||
@objc private func morePressed(node: ContextReferenceContentNode, gesture: ContextGesture?) {
|
||||
let context = self.context
|
||||
var items: [ContextMenuItem] = []
|
||||
items.append(.action(ContextMenuActionItem(text: "Open Bot", icon: { theme in
|
||||
return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/Bots"), color: theme.contextMenu.primaryColor)
|
||||
}, action: { [weak self] _, f in
|
||||
f(.default)
|
||||
|
||||
guard let strongSelf = self else {
|
||||
return
|
||||
}
|
||||
let controller = addWebAppToAttachmentController(sharedContext: strongSelf.context.sharedContext)
|
||||
strongSelf.present(controller, in: .window(.root))
|
||||
})))
|
||||
|
||||
if self.botId != self.peerId {
|
||||
items.append(.action(ContextMenuActionItem(text: self.presentationData.strings.WebApp_OpenBot, icon: { theme in
|
||||
return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/Bots"), color: theme.contextMenu.primaryColor)
|
||||
}, action: { _, f in
|
||||
f(.default)
|
||||
|
||||
// if let strongSelf = self {
|
||||
// strongSelf.context.sharedContext.navigateToChatController(NavigateToChatControllerParams(navigationController: strongSelf., context: strongSelf.context, chatLocation: .peer(id: strongSelf.peerId)))
|
||||
// }
|
||||
})))
|
||||
}
|
||||
|
||||
items.append(.action(ContextMenuActionItem(text: "Reload Page", icon: { theme in
|
||||
items.append(.action(ContextMenuActionItem(text: self.presentationData.strings.WebApp_ReloadPage, icon: { theme in
|
||||
return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/Share"), color: theme.contextMenu.primaryColor)
|
||||
}, action: { _, f in
|
||||
f(.default)
|
||||
@@ -247,11 +318,15 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
|
||||
})))
|
||||
|
||||
items.append(.action(ContextMenuActionItem(text: "Remove Bot", textColor: .destructive, icon: { theme in
|
||||
items.append(.action(ContextMenuActionItem(text: self.presentationData.strings.WebApp_RemoveBot, textColor: .destructive, icon: { theme in
|
||||
return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/Delete"), color: theme.contextMenu.destructiveColor)
|
||||
}, action: { _, f in
|
||||
}, action: { [weak self] _, f in
|
||||
f(.default)
|
||||
|
||||
if let strongSelf = self {
|
||||
let _ = context.engine.messages.removeBotFromAttachMenu(peerId: strongSelf.botId).start()
|
||||
strongSelf.dismiss()
|
||||
}
|
||||
})))
|
||||
|
||||
let contextController = ContextController(account: self.context.account, presentationData: self.presentationData, source: .reference(WebAppContextReferenceContentSource(controller: self, sourceNode: node)), items: .single(ContextController.Items(content: .list(items))), gesture: gesture)
|
||||
@@ -259,9 +334,9 @@ public final class WebAppController: ViewController, AttachmentContainable {
|
||||
}
|
||||
|
||||
override public func loadDisplayNode() {
|
||||
self.displayNode = Node(context: self.context, presentationData: self.presentationData, url: self.url, present: { [weak self] c, a in
|
||||
self.displayNode = Node(context: self.context, controller: self, presentationData: self.presentationData, peerId: self.peerId, botId: self.botId, url: self.url, present: { [weak self] c, a in
|
||||
self?.present(c, in: .window(.root), with: a)
|
||||
}, message: self.message)
|
||||
})
|
||||
}
|
||||
|
||||
override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
|
||||
|
||||
Reference in New Issue
Block a user