Browse Source

refactor(ios): refine handling of app id change(upgrading)

Andelf 2 years ago
parent
commit
8274ff836f
3 changed files with 36 additions and 22 deletions
  1. 11 0
      src/main/frontend/db/persist.cljs
  2. 17 22
      src/main/frontend/handler/events.cljs
  3. 8 0
      src/main/frontend/idb.cljs

+ 11 - 0
src/main/frontend/db/persist.cljs

@@ -44,3 +44,14 @@
         (ipc/ipc "deleteGraph" key)
         (idb/remove-item! key))
      (idb/remove-item! key))))
+
+(defn rename-graph!
+  [old-repo new-repo]
+  (let [old-key (db-conn/datascript-db old-repo)
+        new-key (db-conn/datascript-db new-repo)]
+    (if (util/electron?)
+      (do
+        (js/console.error "rename-graph! is not supported in electron")
+        (idb/rename-item! old-key new-key))
+      (idb/rename-item! old-key new-key))))
+

+ 17 - 22
src/main/frontend/handler/events.cljs

@@ -10,7 +10,6 @@
             [clojure.core.async.interop :refer [p->c]]
             [clojure.set :as set]
             [clojure.string :as string]
-            [datascript.core :as d]
             [frontend.commands :as commands]
             [frontend.components.command-palette :as command-palette]
             [frontend.components.conversion :as conversion-component]
@@ -507,22 +506,7 @@
       (when-let [toolbar (.querySelector main-node "#mobile-editor-toolbar")]
         (set! (.. toolbar -style -bottom) 0)))))
 
-(defn update-file-path [deprecated-repo current-repo deprecated-app-id current-app-id]
-  (let [files (db-model/get-files-entity deprecated-repo)
-        conn (conn/get-db deprecated-repo false)
-        tx (mapv (fn [[id path]]
-                   (let [new-path (string/replace path deprecated-app-id current-app-id)]
-                     {:db/id id
-                      :file/path new-path}))
-                 files)]
-    (d/transact! conn tx)
-    (reset! conn/conns
-            (update-keys @conn/conns
-                         (fn [key] (if (string/includes? key deprecated-repo)
-                                     (string/replace key deprecated-repo current-repo)
-                                     key))))))
-
-(defn get-ios-app-id
+(defn- get-ios-app-id
   [repo-url]
   (when repo-url
     (let [app-id (-> (first (string/split repo-url "/Documents"))
@@ -532,11 +516,12 @@
 
 (defmethod handle :validate-appId [[_ graph-switch-f graph]]
   (when-let [deprecated-repo (or graph (state/get-current-repo))]
-    ;; Installation is not changed for iCloud
     (if (mobile-util/in-iCloud-container-path? deprecated-repo)
+      ;; Installation is not changed for iCloud
       (when graph-switch-f
         (graph-switch-f graph true)
         (state/pub-event! [:graph/ready (state/get-current-repo)]))
+      ;; Installation is changed for App Documents directory
       (p/let [deprecated-app-id (get-ios-app-id deprecated-repo)
               current-document-url (.getUri Filesystem #js {:path ""
                                                             :directory (.-Documents Directory)})
@@ -545,24 +530,34 @@
         (if (= deprecated-app-id current-app-id)
           (when graph-switch-f (graph-switch-f graph true))
           (do
+            (notification/show! [:div "Migrating from previous App installation..."]
+                                :warning
+                                true)
+            (prn ::migrate-app-id :from deprecated-app-id :to current-app-id)
             (file-sync-stop!)
             (.unwatch mobile-util/fs-watcher)
             (let [current-repo (string/replace deprecated-repo deprecated-app-id current-app-id)
                   current-repo-dir (config/get-repo-dir current-repo)]
               (try
-                (update-file-path deprecated-repo current-repo deprecated-app-id current-app-id)
-                (db-persist/delete-graph! deprecated-repo)
+                ;; replace app-id part of repo url
+                (reset! conn/conns
+                        (update-keys @conn/conns
+                                     (fn [key]
+                                       (if (string/includes? key deprecated-app-id)
+                                         (string/replace key deprecated-app-id current-app-id)
+                                         key))))
+                (db-persist/rename-graph! deprecated-repo current-repo)
                 (search/remove-db! deprecated-repo)
-                (state/delete-repo! {:url deprecated-repo})
                 (state/add-repo! {:url current-repo :nfs? true})
+                (state/delete-repo! {:url deprecated-repo})
                 (catch :default e
                   (js/console.error e)))
               (state/set-current-repo! current-repo)
               (db/listen-and-persist! current-repo)
               (db/persist-if-idle! current-repo)
               (repo-config-handler/restore-repo-config! current-repo)
-              (.watch mobile-util/fs-watcher #js {:path current-repo-dir})
               (when graph-switch-f (graph-switch-f current-repo true))
+              (.watch mobile-util/fs-watcher #js {:path current-repo-dir})
               (file-sync-restart!))))
         (state/pub-event! [:graph/ready (state/get-current-repo)])))))
 

+ 8 - 0
src/main/frontend/idb.cljs

@@ -40,6 +40,14 @@
   (when (and key @store)
     (idb-keyval/set key value @store)))
 
+(defn rename-item!
+  [old-key new-key]
+  (when (and old-key new-key @store)
+    (p/let [value (idb-keyval/get old-key @store)]
+      (when value
+        (idb-keyval/set new-key value @store)
+        (idb-keyval/del old-key @store)))))
+
 (defn set-batch!
   [items]
   (when (and (seq items) @store)