fix: use capacitor's api to get app's active state

pull/7379/head^2
Tienson Qin 2022-11-21 18:50:18 +08:00
parent 81f90cd34d
commit f87100dd10
2 changed files with 41 additions and 37 deletions

View File

@ -18,6 +18,7 @@
[frontend.handler.user :as user]
[frontend.state :as state]
[frontend.mobile.util :as mobile-util]
[frontend.mobile.core :as mobile-core]
[frontend.util :as util]
[frontend.util.persist-var :as persist-var]
[frontend.util.fs :as fs-util]
@ -3161,44 +3162,38 @@
(def finished-local->remote-chan (chan 1))
(let [*resumed? (atom false)
*into-background? (atom false)]
(add-watch app-state-changed-cursor "sync"
(fn [_ _ _ {:keys [is-active?]}]
(cond
(mobile-util/native-android?)
;; TODO: support background task on Android
(restart-if-stopped! is-active?)
(add-watch app-state-changed-cursor "sync"
(fn [_ _ _ {:keys [is-active?]}]
(cond
(mobile-util/native-android?)
;; TODO: support background task on Android
(restart-if-stopped! is-active?)
(mobile-util/native-ios?)
(let [*task-id (atom nil)]
(if is-active?
(do
(when @*into-background?
(reset! *into-background? false)
(reset! *resumed? true))
(restart-if-stopped! is-active?))
(when (state/get-current-file-sync-graph-uuid)
(p/let [task-id (.beforeExit ^js BackgroundTask
(fn []
(reset! *resumed? false)
(reset! *into-background? true)
(go
;; Wait for file watcher events
(<! (timeout 2000))
(util/drain-chan finished-local->remote-chan)
(<! (<sync-local->remote-now))
;; wait at most 20s
(async/alts! [finished-local->remote-chan (timeout 20000)])
(when-not @*resumed? (offer! pause-resume-chan is-active?))
(<! (timeout 5000))
(prn "finish task: " @*task-id)
(let [opt #js {:taskId @*task-id}]
(.finish ^js BackgroundTask opt)))))]
(reset! *task-id task-id)))))
(mobile-util/native-ios?)
(let [*task-id (atom nil)]
(if is-active?
(restart-if-stopped! is-active?)
(when (state/get-current-file-sync-graph-uuid)
(p/let [task-id (.beforeExit ^js BackgroundTask
(fn []
(go
;; Wait for file watcher events
(<! (timeout 2000))
(util/drain-chan finished-local->remote-chan)
(<! (<sync-local->remote-now))
;; wait at most 20s
(async/alts! [finished-local->remote-chan (timeout 20000)])
(p/let [active? (mobile-core/app-active?)]
(when-not active?
(offer! pause-resume-chan is-active?)))
(<! (timeout 5000))
(prn "finish task: " @*task-id)
(let [opt #js {:taskId @*task-id}]
(.finish ^js BackgroundTask opt)))))]
(reset! *task-id task-id)))))
:else
nil))))
:else
nil)))
;;; ### some add-watches

View File

@ -2,6 +2,7 @@
"Main ns for handling mobile start"
(:require ["@capacitor/app" :refer [^js App]]
["@capacitor/keyboard" :refer [^js Keyboard]]
["@capacitor/core" :refer [^js Plugins]]
[clojure.string :as string]
[promesa.core :as p]
[frontend.fs.capacitor-fs :as capacitor-fs]
@ -11,7 +12,8 @@
[frontend.mobile.util :as mobile-util]
[frontend.state :as state]
[frontend.util :as util]
[cljs-bean.core :as bean]))
[cljs-bean.core :as bean]
[goog.object :as gobj]))
(def *url (atom nil))
@ -97,6 +99,13 @@
(editor-handler/save-current-block!))
(state/set-mobile-app-state-change is-active?))))
(defn- app-active?
"Returns a promise"
[]
(let [app ^js (gobj/get Plugins "App")]
(p/let [state (.getState app)]
(gobj/get state "isActive"))))
(defn- general-init
"Initialize event listeners used by both iOS and Android"
[]