Browse Source

implement source code menu entry in webxdcs

cyberta 3 years ago
parent
commit
cb92528cce
1 changed files with 35 additions and 0 deletions
  1. 35 0
      deltachat-ios/Controller/WebxdcViewController.swift

+ 35 - 0
deltachat-ios/Controller/WebxdcViewController.swift

@@ -15,6 +15,19 @@ class WebxdcViewController: WebViewViewController {
     var dcContext: DcContext
     var webxdcUpdateObserver: NSObjectProtocol?
     
+    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 = """
@@ -145,6 +158,10 @@ class WebxdcViewController: WebViewViewController {
         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 {
+            sourceCodeUrl = sourceCode
+            navigationItem.rightBarButtonItem = moreButton
+        }
     }
     
     override func willMove(toParent parent: UIViewController?) {
@@ -243,6 +260,24 @@ class WebxdcViewController: WebViewViewController {
             webView.evaluateJavaScript("window.__webxdcUpdate(atob(\"\(statusUpdates.toBase64())\"))", completionHandler: nil)
         }
     }
+
+    @objc private func moreButtonPressed() {
+        let alert = UIAlertController(title: nil,
+                                      message: nil,
+                                      preferredStyle: .safeActionSheet)
+        let sourceCodeAction = UIAlertAction(title: String.localized("source_code"), style: .default, handler: openUrl(_:))
+        let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .destructive, 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 {