Update API [skip ci]

This commit is contained in:
Ilya Laktyushin
2022-12-13 17:42:32 +04:00
parent 21121e1924
commit 47ffcd2e4d
41 changed files with 867 additions and 302 deletions

View File

@@ -21,6 +21,8 @@ import MoreButtonNode
import BotPaymentsUI
import PromptUI
import PhoneNumberFormat
import QrCodeUI
import InstantPageUI
private let durgerKingBotIds: [Int64] = [5104055776, 2200339955]
@@ -680,10 +682,27 @@ public final class WebAppController: ViewController, AttachmentContainable {
}
case "web_app_open_link":
if let json = json, let url = json["url"] as? String {
let tryInstantView = json["try_instant_view"] as? Bool ?? false
let currentTimestamp = CACurrentMediaTime()
if let lastTouchTimestamp = self.webView?.lastTouchTimestamp, currentTimestamp < lastTouchTimestamp + 10.0 {
self.webView?.lastTouchTimestamp = nil
self.context.sharedContext.openExternalUrl(context: self.context, urlContext: .generic, url: url, forceExternal: true, presentationData: self.context.sharedContext.currentPresentationData.with { $0 }, navigationController: nil, dismissInput: {})
if tryInstantView {
let _ = (resolveInstantViewUrl(account: self.context.account, url: url)
|> deliverOnMainQueue).start(next: { [weak self] result in
guard let strongSelf = self else {
return
}
switch result {
case let .instantView(webPage, anchor):
let controller = InstantPageController(context: strongSelf.context, webPage: webPage, sourcePeerType: .otherPrivate, anchor: anchor)
strongSelf.controller?.getNavigationController()?.pushViewController(controller)
default:
strongSelf.context.sharedContext.openExternalUrl(context: strongSelf.context, urlContext: .generic, url: url, forceExternal: true, presentationData: strongSelf.context.sharedContext.currentPresentationData.with { $0 }, navigationController: nil, dismissInput: {})
}
})
} else {
self.context.sharedContext.openExternalUrl(context: self.context, urlContext: .generic, url: url, forceExternal: true, presentationData: self.context.sharedContext.currentPresentationData.with { $0 }, navigationController: nil, dismissInput: {})
}
}
}
case "web_app_setup_back_button":
@@ -799,8 +818,28 @@ public final class WebAppController: ViewController, AttachmentContainable {
if let json = json, let needConfirmation = json["need_confirmation"] as? Bool {
self.needDismissConfirmation = needConfirmation
}
case "web_app_request_phone":
break
case "web_app_open_scan_qr_popup":
var info: String = ""
if let json = json, let text = json["text"] as? String {
info = text
}
let controller = QrCodeScanScreen(context: self.context, subject: .custom(info: info))
controller.completion = { [weak self] result in
if let strongSelf = self {
strongSelf.sendQrCodeScannedEvent(data: result)
}
}
self.controller?.present(controller, in: .window(.root))
case "web_app_read_text_from_clipboard":
if let json = json, let requestId = json["req_id"] as? String {
let currentTimestamp = CACurrentMediaTime()
var fillData = false
if let lastTouchTimestamp = self.webView?.lastTouchTimestamp, currentTimestamp < lastTouchTimestamp + 10.0, self.controller?.url == nil {
self.webView?.lastTouchTimestamp = nil
fillData = true
}
self.sendClipboardTextEvent(requestId: requestId, fillData: fillData)
}
default:
break
}
@@ -930,6 +969,22 @@ public final class WebAppController: ViewController, AttachmentContainable {
}
self.webView?.sendEvent(name: "phone_requested", data: paramsString)
}
fileprivate func sendQrCodeScannedEvent(data: String?) {
let paramsString = data.flatMap { "{data: \"\($0)\"}" } ?? "{}"
self.webView?.sendEvent(name: "scan_qr_popup_closed", data: paramsString)
}
fileprivate func sendClipboardTextEvent(requestId: String, fillData: Bool) {
var paramsString: String
if fillData {
let data = UIPasteboard.general.string ?? ""
paramsString = "{req_id: \"\(requestId)\", data: \"\(data)\"}"
} else {
paramsString = "{req_id: \"\(requestId)\"}"
}
self.webView?.sendEvent(name: "clipboard_text_received", data: paramsString)
}
}
fileprivate var controllerNode: Node {
@@ -1067,7 +1122,7 @@ public final class WebAppController: ViewController, AttachmentContainable {
let attachMenuBot = attachMenuBots.first(where: { $0.peer.id == botId})
if self?.url == nil, let attachMenuBot = attachMenuBot, attachMenuBot.hasSettings {
if self?.url == nil, let attachMenuBot = attachMenuBot, attachMenuBot.flags.contains(.hasSettings) {
items.append(.action(ContextMenuActionItem(text: presentationData.strings.WebApp_Settings, icon: { theme in
return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/Settings"), color: theme.contextMenu.primaryColor)
}, action: { [weak self] c, _ in