Skip to content

Commit

Permalink
feat: upload files when the app switching to background
Browse files Browse the repository at this point in the history
Notice: Android is not supported yet.
  • Loading branch information
tiensonqin committed Nov 11, 2022
1 parent 6ef87a5 commit c228bba
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"@axe-core/playwright": "^4.4.4",
"@capacitor/cli": "^4.0.0",
"@playwright/test": "^1.24.2",
"@tailwindcss/forms": "0.5.3",
"@tailwindcss/typography": "0.5.7",
"@tailwindcss/aspect-ratio": "0.4.2",
"@tailwindcss/forms": "0.5.3",
"@tailwindcss/line-clamp": "0.4.2",
"@tailwindcss/typography": "0.5.7",
"@types/gulp": "^4.0.7",
"cross-env": "^7.0.3",
"cssnano": "^5.1.13",
Expand Down Expand Up @@ -87,6 +87,7 @@
"@capacitor/share": "^4.0.0",
"@capacitor/splash-screen": "^4.0.0",
"@capacitor/status-bar": "^4.0.0",
"@capawesome/capacitor-background-task": "^2.0.0",
"@excalidraw/excalidraw": "0.12.0",
"@kanru/rage-wasm": "^0.3.0",
"@logseq/capacitor-file-sync": "0.0.13",
Expand Down Expand Up @@ -119,6 +120,7 @@
"jszip": "3.7.0",
"mldoc": "^1.5.0",
"path": "0.12.7",
"path-complete-extname": "1.0.0",
"pixi-graph-fork": "0.2.0",
"pixi.js": "6.2.0",
"posthog-js": "1.10.2",
Expand All @@ -134,12 +136,11 @@
"react-transition-group": "4.3.0",
"reakit": "1.3.11",
"remove-accents": "0.4.2",
"sanitize-filename": "1.6.3",
"send-intent": "3.0.11",
"threads": "1.6.5",
"url": "^0.11.0",
"yargs-parser": "20.2.4",
"path-complete-extname": "1.0.0",
"sanitize-filename": "1.6.3"
"yargs-parser": "20.2.4"
},
"resolutions": {
"pixi-graph-fork/@pixi/app": "6.2.0",
Expand Down
43 changes: 36 additions & 7 deletions src/main/frontend/fs/sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
[rum.core :as rum]
[promesa.core :as p]
[lambdaisland.glogi :as log]
[frontend.fs.capacitor-fs :as capacitor-fs]))
[frontend.fs.capacitor-fs :as capacitor-fs]
["@capawesome/capacitor-background-task" :refer [BackgroundTask]]))

;;; ### Commentary
;; file-sync related local files/dirs:
Expand All @@ -54,7 +55,7 @@
;; then we need a remote->local-full-sync,
;; which compare local-files with remote-files, sync diff-remote-files to local
;; - local->remote-full-sync will be triggered after 20mins of idle
;; - every 20s, flush local changes, and sync to remote
;; - every 10s, flush local changes, and sync to remote

;; TODO: use access-token instead of id-token
;; TODO: a remote delete-diff cause local related-file deleted, then trigger a `FileChangeEvent`,
Expand Down Expand Up @@ -1985,11 +1986,6 @@
see also `*resume-state`"
(chan 1))
(def pause-resume-mult (async/mult pause-resume-chan))
(def app-state-changed-cursor (rum/cursor state/state :mobile/app-state-change))
(add-watch app-state-changed-cursor "sync"
(fn [_ _ _ {:keys [is-active?]}]
(prn "is-active? " is-active?)
(offer! pause-resume-chan is-active?)))

(def recent-edited-chan
"Triggered when there is content editing"
Expand Down Expand Up @@ -2980,11 +2976,44 @@

(reset! current-sm-graph-uuid nil)))

(defn <sync-local->remote []
(go
(when-let [_sm ^SyncManager (state/get-file-sync-manager (state/get-current-file-sync-graph-uuid))]
(offer! immediately-local->remote-chan true))))

(defn sync-need-password!
[]
(when-let [sm ^SyncManager (state/get-file-sync-manager (state/get-current-file-sync-graph-uuid))]
(.need-password sm)))

(def app-state-changed-cursor (rum/cursor state/state :mobile/app-state-change))

(def finished-local->remote-chan (chan 1))
(add-watch app-state-changed-cursor "sync"
(fn [_ _ _ {:keys [is-active?]}]
(prn "is-active? " is-active?)
(cond
(mobile-util/native-android?)
;; TODO: support background task on Android
(offer! pause-resume-chan is-active?)

(mobile-util/native-ios?)
(let [*task-id (atom nil)]
(when (and (not is-active?) (state/get-current-file-sync-graph-uuid))
(go
;; Wait for file watcher events
(<! (timeout 2000))
(<! (<sync-local->remote))

(p/let [task-id (.beforeExit ^js BackgroundTask
(fn []
(<! finished-local->remote-chan)
(.finish ^js BackgroundTask @*task-id)))]
(reset! *task-id task-id)))))

:else
nil)))

(defn check-graph-belong-to-current-user
[current-user-uuid graph-user-uuid]
(cond
Expand Down
4 changes: 3 additions & 1 deletion src/main/frontend/handler/file_sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@
(list :finished-local->remote :finished-remote->local)
(when-let [current-uuid (state/get-current-file-sync-graph-uuid)]
(state/clear-file-sync-progress! current-uuid)
(state/set-state! [:file-sync/graph-state current-uuid :file-sync/last-synced-at] (:epoch data)))
(state/set-state! [:file-sync/graph-state current-uuid :file-sync/last-synced-at] (:epoch data))
(when (= event :finished-local->remote)
(async/offer! sync/finished-local->remote-chan true)))

:start
(when-let [current-uuid (state/get-current-file-sync-graph-uuid)]
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/mobile/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
(println :debug :app-state-change-handler state (js/Date.))
(when (state/get-current-repo)
(let [is-active? (.-isActive state)]
(state/set-mobile-app-state-change is-active?)
(when-not is-active?
(editor-handler/save-current-block!)))))
(editor-handler/save-current-block!))
(state/set-mobile-app-state-change is-active?))))

(defn- general-init
"Initialize event listeners used by both iOS and Android"
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@
resolved "https://registry.yarnpkg.com/@capacitor/status-bar/-/status-bar-4.0.1.tgz#d1320add5f4ef383394b94beae45a0c5af7250e1"
integrity sha512-BmEyOf3m/hAf8bO4hCX0m5gpQPSgd05mGYl+9E841WUZeJjcYlmiG/EBshAUb2uGCVtyNaG36yPXB0r0Ypg+rw==

"@capawesome/capacitor-background-task@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@capawesome/capacitor-background-task/-/capacitor-background-task-2.0.0.tgz#d95029423ba23adfa1681b54516154465a40e759"
integrity sha512-rDr2uZIzWW5j66SbahQd+uqJvX/K/joEVLzf4e5ouOrz/WUS9Opi6d6it8rrMpILt5RhYRYSMX6kvTmscv1LoA==

"@electron/get@^1.14.1":
version "1.14.1"
resolved "https://registry.yarnpkg.com/@electron/get/-/get-1.14.1.tgz#16ba75f02dffb74c23965e72d617adc721d27f40"
Expand Down

0 comments on commit c228bba

Please sign in to comment.