Browse Source

use deeplink to handle shared content

llcc 3 years ago
parent
commit
58ebc5b4d7

+ 3 - 33
ios/App/App/AppDelegate.swift

@@ -1,19 +1,14 @@
 import UIKit
 import Capacitor
-import SendIntent
 
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {
 
     var window: UIWindow?
-    let store = ShareStore.store
     
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         // Override point for customization after application launch.
-        DispatchQueue.global().asyncAfter(deadline: .now() + 3) {
-            NotificationCenter.default
-                .post(name: Notification.Name("triggerSendIntent"), object: nil )
-        }
+        
         return true
     }
     
@@ -33,6 +28,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 
     func applicationDidBecomeActive(_ application: UIApplication) {
         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
+        
+        
     }
 
     func applicationWillTerminate(_ application: UIApplication) {
@@ -45,33 +42,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
             if CAPBridge.handleOpenUrl(url, options) {
                 success = ApplicationDelegateProxy.shared.application(app, open: url, options: options)
             }
-            
-            guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
-                  let params = components.queryItems else {
-                      return false
-                  }
-            let titles = params.filter { $0.name == "title" }
-            let descriptions = params.filter { $0.name == "description" }
-            let types = params.filter { $0.name == "type" }
-            let urls = params.filter { $0.name == "url" }
-            
-            store.shareItems.removeAll()
-        
-            if(titles.count > 0){
-                for index in 0...titles.count-1 {
-                    var shareItem: JSObject = JSObject()
-                    shareItem["title"] = titles[index].value!
-                    shareItem["description"] = descriptions[index].value!
-                    shareItem["type"] = types[index].value!
-                    shareItem["url"] = urls[index].value!
-                    store.shareItems.append(shareItem)
-                }
-            }
-            
-            store.processed = false
-            
-            NotificationCenter.default.post(name: Notification.Name("triggerSendIntent"), object: nil )
-            
             return success
         }
 

+ 1 - 1
ios/App/ShareViewController/ShareViewController.swift

@@ -44,7 +44,7 @@ class ShareViewController: UIViewController {
                     value: $0.url?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""),
             ]
         }.flatMap({ $0 })
-        var urlComps = URLComponents(string: "logseq://")!
+        var urlComps = URLComponents(string: "logseq://shared?")!
         urlComps.queryItems = queryItems
         openURL(urlComps.url!)
     }

+ 65 - 49
src/main/frontend/mobile/core.cljs

@@ -9,69 +9,85 @@
             [frontend.state :as state]
             [frontend.util :as util]))
 
+(def *url (atom nil))
+
 (defn- ios-init
+  "Initialize iOS-specified event listeners"
   []
   (let [path (fs/iOS-ensure-documents!)]
     (println "iOS container path: " path))
 
-  ;; Keyboard watcher
-  ;; (.addListener Keyboard "keyboardWillShow"
-  ;;               #(state/pub-event! [:mobile/keyboard-will-show]))
-  ;; (.addListener Keyboard "keyboardDidShow"
-  ;;               #(state/pub-event! [:mobile/keyboard-did-show]))
-  )
+  (.addEventListener js/window
+                     "load"
+                     (fn [_event]
+                       (when @*url
+                         (js/setTimeout #(deeplink/deeplink @*url)
+                                        1000))))
+
+  (.removeAllListeners mobile-util/file-sync)
+
+  (.addListener mobile-util/file-sync "debug"
+                (fn [event]
+                  (js/console.log "🔄" event))))
 
-(defn init!
+(defn- android-init
+  "Initialize Android-specified event listeners"
   []
   ;; patch back navigation
-  (when (mobile-util/native-android?)
-    (.addListener App "backButton"
-                  #(let [href js/window.location.href]
-                     (when (true? (cond
-                                    (state/get-left-sidebar-open?)
-                                    (state/set-left-sidebar-open! false)
+  (.addListener App "backButton"
+                #(let [href js/window.location.href]
+                   (when (true? (cond
+                                  (state/get-left-sidebar-open?)
+                                  (state/set-left-sidebar-open! false)
 
-                                    (state/settings-open?)
-                                    (state/close-settings!)
+                                  (state/settings-open?)
+                                  (state/close-settings!)
 
-                                    (state/modal-opened?)
-                                    (state/close-modal!)
+                                  (state/modal-opened?)
+                                  (state/close-modal!)
 
-                                    :else true))
+                                  :else true))
 
-                       (if (or (string/ends-with? href "#/")
-                               (string/ends-with? href "/")
-                               (not (string/includes? href "#/")))
-                         (.exitApp App)
-                         (js/window.history.back))))))
+                     (if (or (string/ends-with? href "#/")
+                             (string/ends-with? href "/")
+                             (not (string/includes? href "#/")))
+                       (.exitApp App)
+                       (js/window.history.back)))))
 
-  (when (mobile-util/native-ios?)
-    (ios-init)
-    (.removeAllListeners mobile-util/file-sync)
+  (.addEventListener js/window "sendIntentReceived"
+                       #(intent/handle-received)))
 
-    (.addListener App "appUrlOpen"
-                  (fn [^js data]
-                    (when-let [url (.-url data)]
-                      (deeplink/deeplink url))))
+(defn- general-init
+  "Initialize event listeners used by both iOS and Android"
+  []
+  (.addListener App "appUrlOpen"
+                (fn [^js data]
+                  (prn :data data)
+                  (when-let [url (.-url data)]
+                    (if-not (= (.-readyState js/document) "complete")
+                      (reset! *url url)
+                      (deeplink/deeplink url)))))
+    
+  (.addListener mobile-util/fs-watcher "watcher"
+                (fn [event]
+                  (state/pub-event! [:file-watcher/changed event])))
 
-    (.addListener mobile-util/file-sync "debug"
-                  (fn [event]
-                    (js/console.log "🔄" event))))
+  (.addEventListener js/window "statusTap"
+                     #(util/scroll-to-top true))
+
+  (.addListener App "appStateChange"
+                (fn [^js state]
+                  (when (state/get-current-repo)
+                    (let [is-active? (.-isActive state)]
+                      (when-not is-active?
+                        (editor-handler/save-current-block!)))))))
+
+(defn init! []
+  (when (mobile-util/native-android?)
+    (android-init))
+
+  (when (mobile-util/native-ios?)
+    (ios-init))
 
   (when (mobile-util/is-native-platform?)
-    (.addListener mobile-util/fs-watcher "watcher"
-                  (fn [event]
-                    (state/pub-event! [:file-watcher/changed event])))
-
-    (.addEventListener js/window "statusTap"
-                       #(util/scroll-to-top true))
-
-    (.addListener App "appStateChange"
-                  (fn [^js state]
-                    (when (state/get-current-repo)
-                      (let [is-active? (.-isActive state)]
-                        (when is-active?
-                          (editor-handler/save-current-block!))))))
-
-    (.addEventListener js/window "sendIntentReceived"
-                       #(intent/handle-received))))
+    (general-init)))

+ 8 - 1
src/main/frontend/mobile/deeplink.cljs

@@ -6,6 +6,7 @@
    [frontend.handler.notification :as notification]
    [frontend.handler.route :as route-handler]
    [frontend.handler.user :as user-handler]
+   [frontend.mobile.intent :as intent]
    [frontend.state :as state]
    [frontend.text :as text]))
 
@@ -48,5 +49,11 @@
               (notification/show! (str "Opening File link is not supported on mobile.") :error false))
             (notification/show! (str "The SCHEME across graphs has not been supported yet.") :error false))))
 
+      (= hostname "shared")
+      (let [result (into {} (map (fn [key]
+                                   [(keyword key) (.get search-params key)])
+                                 ["title" "url" "type"]))]
+        (intent/handle-result result))
+
       :else
-      (notification/show! (str "The url  has not been supported yet.") :error false))))
+      nil)))

+ 13 - 10
src/main/frontend/mobile/intent.cljs

@@ -144,15 +144,9 @@
                         (js/decodeURIComponent v)
                         v))])))
 
-(defn handle-received []
-  (p/let [received (p/catch
-                    (.checkSendIntentReceived SendIntent)
-                    (fn [error]
-                      (log/error :intent-received-error {:error error})))]
-    (when received
-      (let [result (-> (js->clj received :keywordize-keys true)
-                       decode-received-result)]
-        (when-let [type (:type result)]
+(defn handle-result [result]
+  (let [result (decode-received-result result)]
+    (when-let [type (:type result)]
           (cond
             (string/starts-with? type "text/")
             (handle-received-text result)
@@ -172,4 +166,13 @@
               [:a {:href "https://github.com/logseq/logseq/issues/new?labels=from:in-app&template=bug_report.yaml"
                    :target "_blank"} "Github"]
               ". We will look into it soon."
-              [:pre.code (with-out-str (pprint/pprint result))]] :warning false)))))))
+              [:pre.code (with-out-str (pprint/pprint result))]] :warning false)))))
+
+(defn handle-received []
+  (p/let [received (p/catch
+                       (.checkSendIntentReceived SendIntent)
+                       (fn [error]
+                         (log/error :intent-received-error {:error error})))]
+    (when received
+      (let [result (js->clj received :keywordize-keys true)]
+        (handle-result result)))))