Browse Source

enhance(capacitor): switch graphs

charlie 7 months ago
parent
commit
785c682ce7
2 changed files with 62 additions and 1 deletions
  1. 18 1
      src/main/capacitor/app.cljs
  2. 44 0
      src/main/capacitor/events.cljs

+ 18 - 1
src/main/capacitor/app.cljs

@@ -115,7 +115,24 @@
                                            (-> (repo-handler/new-db! db-name)
                                              (p/then #(set-reload! (inc reload)))))))}
           [:span {:slot "icon-only"} (ionic/tabler-icon "plus" {:size 22})])]
-       [:h2.py-1.text-lg (fstate/get-current-repo)]
+
+       [:h2.py-1.text-lg
+        (let [graphs (fstate/get-repos)]
+          (ionic/ion-button
+            {:fill "clear" :mode "ios"
+             :class "border w-full rounded-lg"
+             :on-click (fn []
+                         (ui/open-modal! "Switch graph"
+                           {:type :action-sheet
+                            :buttons (for [repo graphs]
+                                       {:text (some-> (:url repo) (string/replace #"^logseq_db_" ""))
+                                        :role (:url repo)})
+                            :inputs []
+                            :on-action (fn [e]
+                                         (when-let [url (:role e)]
+                                           (when (string/starts-with? url "logseq_db_")
+                                             (fstate/pub-event! [:graph/switch url]))))}))}
+            (fstate/get-current-repo)))]
 
        [:div.flex.justify-between.items-center.mt-4
         [:h1.text-3xl.font-mono.font-bold.py-2 "Journals"]

+ 44 - 0
src/main/capacitor/events.cljs

@@ -1,5 +1,8 @@
 (ns capacitor.events
   (:require [cljs.core.async :as async]
+            [frontend.db.transact :as db-transact]
+            [frontend.handler.notification :as notification]
+            [frontend.handler.repo :as repo-handler]
             [frontend.modules.outliner.pipeline :as pipeline]
             [frontend.state :as state]
             [logseq.db :as ldb]
@@ -18,6 +21,47 @@
 
     nil))
 
+;(defn- graph-switch
+;  ([graph]
+;   (graph-switch graph false))
+;  ([graph skip-ios-check?]
+;   (let [db-based? (config/db-based-graph? graph)]
+;     (if (and (mobile-util/native-ios?) (not skip-ios-check?))
+;       (state/pub-event! [:validate-appId graph-switch graph])
+;       (do
+;         (state/set-current-repo! graph)
+;         (page-handler/init-commands!)
+;         ;; load config
+;         (repo-config-handler/restore-repo-config! graph)
+;         (when-not (= :draw (state/get-current-route))
+;           (route-handler/redirect-to-home!))
+;         (when-not db-based?
+;           ;; graph-switch will trigger a rtc-start automatically
+;           ;; (rtc-handler/<rtc-start! graph)
+;           (file-sync-restart!))
+;         (when-let [dir-name (and (not db-based?) (config/get-repo-dir graph))]
+;           (fs/watch-dir! dir-name))
+;         (graph-handler/settle-metadata-to-local! {:last-seen-at (js/Date.now)}))))))
+
+(defn- graph-switch-on-persisted
+  "graph: the target graph to switch to"
+  [graph opts]
+  (p/do!
+    (repo-handler/restore-and-setup-repo! graph)
+    ;(graph-switch graph)
+    (state/set-current-repo! graph)
+    (state/set-state! :sync-graph/init? false)))
+
+(defmethod handle :graph/switch [[_ graph opts]]
+  (state/set-state! :db/async-queries {})
+
+  (p/let [writes-finished? (state/<invoke-db-worker :thread-api/file-writes-finished? (state/get-current-repo))]
+    (if (not writes-finished?)
+      (notification/show!
+        "Please wait seconds until all changes are saved for the current graph."
+        :warning)
+      (graph-switch-on-persisted graph opts))))
+
 (defmethod handle :default [[k]]
   (prn "[skip handle] " k))