Various improvements [skip ci]

This commit is contained in:
Ilya Laktyushin
2025-02-12 22:15:02 +04:00
parent 8c523ec741
commit 2f2a27f0d2
130 changed files with 5126 additions and 1700 deletions

View File

@@ -37,6 +37,7 @@ import DeviceAccess
import DeviceLocationManager
import LegacyMediaPickerUI
import GenerateStickerPlaceholderImage
import PassKit
private let durgerKingBotIds: [Int64] = [5104055776, 2200339955]
@@ -147,7 +148,7 @@ public final class WebAppController: ViewController, AttachmentContainable {
static var activeDownloads: [FileDownload] = []
fileprivate class Node: ViewControllerTracingNode, WKNavigationDelegate, WKUIDelegate, ASScrollViewDelegate {
fileprivate class Node: ViewControllerTracingNode, WKNavigationDelegate, WKUIDelegate, WKDownloadDelegate, ASScrollViewDelegate {
private weak var controller: WebAppController?
private let backgroundNode: ASDisplayNode
@@ -549,6 +550,60 @@ public final class WebAppController: ViewController, AttachmentContainable {
return placeholderSize
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping @MainActor (WKNavigationResponsePolicy) -> Void) {
if #available(iOS 14.5, *), navigationResponse.response.suggestedFilename?.lowercased().hasSuffix(".pkpass") == true {
decisionHandler(.download)
} else {
decisionHandler(.allow)
}
}
private var downloadArguments: (String, String)?
@available(iOS 14.5, *)
func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {
download.delegate = self
}
@available(iOS 14.5, *)
func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
download.delegate = self
}
@available(iOS 14.5, *)
func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {
let path = NSTemporaryDirectory() + NSUUID().uuidString
self.downloadArguments = (path, suggestedFilename)
completionHandler(URL(fileURLWithPath: path))
}
@available(iOS 14.5, *)
func downloadDidFinish(_ download: WKDownload) {
if let (path, fileName) = self.downloadArguments {
let tempFile = TempBox.shared.file(path: path, fileName: fileName)
let url = URL(fileURLWithPath: tempFile.path)
if fileName.hasSuffix(".pkpass") {
if let data = try? Data(contentsOf: url), let pass = try? PKPass(data: data) {
let passLibrary = PKPassLibrary()
if passLibrary.containsPass(pass) {
let alertController = textAlertController(context: self.context, updatedPresentationData: nil, title: nil, text: self.presentationData.strings.WebBrowser_PassExistsError, actions: [TextAlertAction(type: .genericAction, title: self.presentationData.strings.Common_OK, action: {})])
self.controller?.present(alertController, in: .window(.root))
} else if let controller = PKAddPassesViewController(pass: pass) {
self.controller?.view.window?.rootViewController?.present(controller, animated: true)
}
}
}
self.downloadArguments = nil
}
}
@available(iOS 14.5, *)
func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) {
self.downloadArguments = nil
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url?.absoluteString {
if isTelegramMeLink(url) || isTelegraPhLink(url) {