Просмотр исходного кода

implement webxdc update observer

cyberta 3 лет назад
Родитель
Сommit
7ecb436382
2 измененных файлов с 58 добавлено и 2 удалено
  1. 18 0
      DcCore/DcCore/DC/events.swift
  2. 40 2
      deltachat-ios/Controller/WebxdcViewController.swift

+ 18 - 0
DcCore/DcCore/DC/events.swift

@@ -11,6 +11,7 @@ public let dcNotificationChatModified = Notification.Name(rawValue: "dcNotificat
 public let dcEphemeralTimerModified =  Notification.Name(rawValue: "dcEphemeralTimerModified")
 public let dcMsgsNoticed = Notification.Name(rawValue: "dcMsgsNoticed")
 public let dcNotificationConnectivityChanged = Notification.Name(rawValue: "dcNotificationConnectivityChanged")
+public let dcNotificationWebxdcUpdate = Notification.Name(rawValue: "dcNotificationWebxdcUpdate")
 
 public class DcEventHandler {
     let dcAccounts: DcAccounts
@@ -222,6 +223,23 @@ public class DcEventHandler {
                 )
             }
 
+        case DC_EVENT_WEBXDC_STATUS_UPDATE:
+            if dcContext.id != dcAccounts.getSelected().id {
+                return
+            }
+            dcContext.logger?.info("webxdc: update!")
+            DispatchQueue.main.async {
+                let nc = NotificationCenter.default
+                nc.post(
+                    name: dcNotificationWebxdcUpdate,
+                    object: nil,
+                    userInfo: [
+                        "message_id": Int(data1),
+                        "status_id": Int(data2),
+                    ]
+                )
+            }
+
         default:
             dcContext.logger?.warning("unknown event: \(id)")
         }

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

@@ -13,7 +13,8 @@ class WebxdcViewController: WebViewViewController {
 
     var messageId: Int
     var dcContext: DcContext
-    private var loadContentOnce = false
+    var webxdcUpdateObserver: NSObjectProtocol?
+
 
     // Block just everything :)
     let blockRules = """
@@ -36,7 +37,7 @@ class WebxdcViewController: WebViewViewController {
 
           // instead of calling .getStatusUpdatesHandler (-> async),
           // we're passing the updates directly to this js function
-          window.__webxdcUpdateiOS = (updateString) => {
+          window.__webxdcUpdate = (updateString) => {
             var updates = JSON.parse(updateString);
             if (updates.length === 1) {
               update_listener(updates[0]);
@@ -115,6 +116,38 @@ class WebxdcViewController: WebViewViewController {
         self.title = getTitleFromWebxdcInfoJson()
     }
 
+    override func willMove(toParent parent: UIViewController?) {
+        super.willMove(toParent: parent)
+        if parent == nil {
+            // remove observer
+            let nc = NotificationCenter.default
+            if let webxdcUpdateObserver = webxdcUpdateObserver {
+                nc.removeObserver(webxdcUpdateObserver)
+            }
+        } else {
+            addObserver()
+        }
+    }
+
+    private func addObserver() {
+        let nc = NotificationCenter.default
+        webxdcUpdateObserver = nc.addObserver(
+            forName: dcNotificationWebxdcUpdate,
+            object: nil,
+            queue: OperationQueue.main
+        ) { [weak self] notification in
+            guard let self = self else { return }
+            guard let ui = notification.userInfo,
+                  let messageId = ui["message_id"] as? Int,
+                  let statusId = ui["status_id"] as? Int,
+                      messageId == self.messageId else {
+                          logger.error("failed to handle dcNotificationWebxdcUpdate")
+                          return
+                      }
+            self.updateWebxdc(statusId: statusId)
+        }
+    }
+
     override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
         // TODO: what about tel:// and mailto://
         if let url = navigationAction.request.url,
@@ -167,6 +200,11 @@ class WebxdcViewController: WebViewViewController {
             }
         }
     }
+
+    private func updateWebxdc(statusId: Int) {
+        let statusUpdates = self.dcContext.getWebxdcStatusUpdates(msgId: messageId, statusUpdateId: statusId)
+        logger.debug("status updates: \(statusUpdates)")
+    }
 }
 
 extension WebxdcViewController: WKScriptMessageHandler {