浏览代码

refactor(ios): refine ios initial appOpenUrl handling using preinit-homepage

Andelf 2 年之前
父节点
当前提交
4dd753d265

+ 21 - 14
src/main/frontend/fs/watcher_handler.cljs

@@ -133,7 +133,7 @@
       nil)))
 
 (defn preload-graph-homepage-files!
-  "Preload the homepage file for the current graph.
+  "Preload the homepage file for the current graph. Return loaded file paths.
 
    Prerequisites:
    - current graph is set
@@ -157,7 +157,7 @@
                                               (config/get-journals-directory)
                                               (config/get-pages-directory))]
                              (str parent-dir "/" file-name "." ext)))]
-        (prn ::preload-homepage repo-dir file-rpath)
+        (prn ::preload-homepage file-rpath)
         (p/let [file-exists? (fs/file-exists? repo-dir file-rpath)
                 _ (when file-exists?
                     ;; BUG: avoid active-editing block content overwrites incoming fs changes
@@ -170,23 +170,29 @@
                 db-content (if-not db-empty?
                              (db/get-file repo file-rpath)
                              "")]
-          (cond
-            (and file-exists?
-                 db-empty?)
-            (handle-add-and-change! repo file-rpath file-content db-content file-mtime false)
+          (p/do!
+           (cond
+             (and file-exists?
+                  db-empty?)
+             (handle-add-and-change! repo file-rpath file-content db-content file-mtime false)
 
-            (and file-exists?
-                 (not db-empty?)
-                 (not= file-content db-content))
-            (handle-add-and-change! repo file-rpath file-content db-content file-mtime true))
-          (ui-handler/re-render-root!))))))
+             (and file-exists?
+                  (not db-empty?)
+                  (not= file-content db-content))
+             (handle-add-and-change! repo file-rpath file-content db-content file-mtime true))
+
+           (ui-handler/re-render-root!)
+
+           [file-rpath]))))))
 
 (defn load-graph-files!
-  [graph]
+  "This fn replaces the former initial fs watcher"
+  [graph exclude-files]
   (when graph
     (let [repo-dir (config/get-repo-dir graph)
           db-files (->> (db/get-files graph)
-                        (map first))]
+                        (map first))
+          exclude-files (set (or exclude-files []))]
       ;; read all files in the repo dir, notify if readdir error
       (p/let [[files deleted-files]
               (-> (fs/readdir repo-dir :path-only? true)
@@ -200,7 +206,8 @@
                                                     (string/lower-case f)]))))
                            (fn [files]
                              (let [deleted-files (set/difference (set db-files) (set files))]
-                               [files
+                               [(->> files
+                                     (remove #(contains? exclude-files %)))
                                 deleted-files])))
                   (p/catch (fn [error]
                              (when-not (config/demo-graph? graph)

+ 8 - 7
src/main/frontend/handler/events.cljs

@@ -380,12 +380,13 @@
       (when (and (not dir-exists?)
                  (not util/nfs?))
         (state/pub-event! [:graph/dir-gone dir]))))
-  (p/do!
-   (fs-watcher/preload-graph-homepage-files!)
-   ;; FIXME: an ugly implementation for redirecting to page on new window is restored
-   (repo-handler/graph-ready! repo)
-   ;; This replaces the former initial fs watcher
-   (fs-watcher/load-graph-files! repo))
+  (p/let [loaded-homepage-files (fs-watcher/preload-graph-homepage-files!)
+          ;; re-render-root is async and delegated to rum, so we need to wait for main ui to refresh
+          _ (js/setTimeout #(mobile/mobile-postinit) 1000)
+          ;; FIXME: an ugly implementation for redirecting to page on new window is restored
+          _ (repo-handler/graph-ready! repo)
+          _ (fs-watcher/load-graph-files! repo loaded-homepage-files)]
+
   ;; TODO(junyi): Notify user to update filename format when the UX is smooth enough
   ;; (when-not config/test?
   ;;   (js/setTimeout
@@ -397,7 +398,7 @@
   ;;                   (not= filename-format :triple-lowbar))
   ;;          (state/pub-event! [:ui/notify-outdated-filename-format []]))))
   ;;    3000))
-  )
+    ))
 
 (defmethod handle :notification/show [[_ {:keys [content status clear?]}]]
   (notification/show! content status clear?))

+ 2 - 1
src/main/frontend/handler/repo.cljs

@@ -530,4 +530,5 @@
 (defn graph-ready!
   ;; FIXME: Call electron that the graph is loaded, an ugly implementation for redirect to page when graph is restored
   [graph]
-  (ipc/ipc "graphReady" graph))
+  (when (util/electron?)
+    (ipc/ipc "graphReady" graph)))

+ 11 - 10
src/main/frontend/mobile/core.cljs

@@ -15,7 +15,7 @@
             [frontend.config :as config]
             [frontend.handler.repo :as repo-handler]))
 
-(def *url (atom nil))
+(def *init-url (atom nil))
 ;; FIXME: `appUrlOpen` are fired twice when receiving a same intent.
 ;; The following two variable atoms are used to compare whether
 ;; they are from the same intent share.
@@ -29,6 +29,15 @@
     ;; Caution: This must be called before any file accessing
     (capacitor-fs/ios-ensure-documents!)))
 
+
+(defn mobile-postinit
+  "postinit logic of mobile platforms: handle deeplink and intent"
+  []
+  (when (mobile-util/native-ios?)
+    (when @*init-url
+      (deeplink/deeplink @*init-url)
+      (reset! *init-url nil))))
+
 (defn- ios-init
   "Initialize iOS-specified event listeners"
   []
@@ -44,19 +53,11 @@
   (when (not (config/demo-graph?))
     (state/pub-event! [:validate-appId]))
 
-  (.addEventListener js/window
-                     "load"
-                     (fn [_event]
-                       (when @*url
-                         (js/setTimeout #(deeplink/deeplink @*url)
-                                        1000))))
-
   (mobile-util/check-ios-zoomed-display)
 
   ;; keep this the same logic as src/main/electron/listener.cljs
   (.addListener mobile-util/file-sync "debug"
                 (fn [event]
-                  (js/console.log "🔄" event)
                   (let [event (js->clj event :keywordize-keys true)
                         payload (:data event)]
                     (when (or (= (:event event) "download:progress")
@@ -115,7 +116,7 @@
                 (fn [^js data]
                   (when-let [url (.-url data)]
                     (if-not (= (.-readyState js/document) "complete")
-                      (reset! *url url)
+                      (reset! *init-url url)
                       (when-not (and (= @*last-shared-url url)
                                      (<= (- (.getSeconds (js/Date.)) @*last-shared-seconds) 1))
                         (reset! *last-shared-url url)

+ 0 - 2
src/main/frontend/search/agency.cljs

@@ -46,12 +46,10 @@
       (protocol/rebuild-blocks-indice! e1)))
 
   (transact-blocks! [_this data]
-    (println "D:Search > Transact blocks!:" repo)
     (doseq [e (get-flatten-registered-engines repo)]
       (protocol/transact-blocks! e data)))
 
   (transact-pages! [_this data]
-    (println "D:Search > Transact pages!:" repo)
     (doseq [e (get-flatten-registered-engines repo)]
       (protocol/transact-pages! e data)))