浏览代码

Merge pull request #1593 from deltachat/support_source_code_url

implement source code menu entry in webxdc views
cyBerta 3 年之前
父节点
当前提交
8090a15ca8
共有 1 个文件被更改,包括 39 次插入2 次删除
  1. 39 2
      deltachat-ios/Controller/WebxdcViewController.swift

+ 39 - 2
deltachat-ios/Controller/WebxdcViewController.swift

@@ -14,7 +14,21 @@ class WebxdcViewController: WebViewViewController {
     var messageId: Int
     var dcContext: DcContext
     var webxdcUpdateObserver: NSObjectProtocol?
-    
+    var webxdcName: String = ""
+    var sourceCodeUrl: String?
+
+    private lazy var moreButton: UIBarButtonItem = {
+        let image: UIImage?
+        if #available(iOS 13.0, *) {
+            image = UIImage(systemName: "ellipsis.circle")
+        } else {
+            image = UIImage(named: "ic_more")
+        }
+        return UIBarButtonItem(image: image,
+                               style: .plain,
+                               target: self,
+                               action: #selector(moreButtonPressed))
+    }()
     
     // Block just everything, except of webxdc urls
     let blockRules = """
@@ -141,10 +155,15 @@ class WebxdcViewController: WebViewViewController {
         let dict = msg.getWebxdcInfoDict()
 
         let document = dict["document"] as? String ?? ""
-        let webxdcName = dict["name"] as? String ?? "ErrName" // name should not be empty
+        webxdcName = dict["name"] as? String ?? "ErrName" // name should not be empty
         let chatName = dcContext.getChat(chatId: msg.chatId).name
 
         self.title = document.isEmpty ? "\(webxdcName) – \(chatName)" : "\(document) – \(chatName)"
+        if let sourceCode = dict["source_code_url"] as? String,
+           !sourceCode.isEmpty {
+            sourceCodeUrl = sourceCode
+            navigationItem.rightBarButtonItem = moreButton
+        }
     }
     
     override func willMove(toParent parent: UIViewController?) {
@@ -243,6 +262,24 @@ class WebxdcViewController: WebViewViewController {
             webView.evaluateJavaScript("window.__webxdcUpdate(atob(\"\(statusUpdates.toBase64())\"))", completionHandler: nil)
         }
     }
+
+    @objc private func moreButtonPressed() {
+        let alert = UIAlertController(title: webxdcName,
+                                      message: nil,
+                                      preferredStyle: .safeActionSheet)
+        let sourceCodeAction = UIAlertAction(title: String.localized("source_code"), style: .default, handler: openUrl(_:))
+        let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
+        alert.addAction(sourceCodeAction)
+        alert.addAction(cancelAction)
+        self.present(alert, animated: true, completion: nil)
+    }
+
+    private func openUrl(_ action: UIAlertAction) {
+        if let sourceCodeUrl = sourceCodeUrl,
+           let url = URL(string: sourceCodeUrl) {
+            UIApplication.shared.open(url)
+        }
+    }
 }
 
 extension WebxdcViewController: WKScriptMessageHandler {