From 231a5ff79f4669f551e0c3d61cfda01fda3984a0 Mon Sep 17 00:00:00 2001 From: Miloslav Nenadal Date: Fri, 19 Jan 2024 17:18:37 +0100 Subject: [PATCH] Show info about pending update --- resources/private/css/styles.css | 4 ++++ shadow-cljs.edn | 2 +- src/app/core.cljs | 19 ++++++++++++++++++- src/app/events.cljs | 5 +++++ src/app/subs.cljs | 6 ++++++ src/app/views.cljs | 8 +++++++- .../clojure_utils/re_frame/autosave.cljs | 2 +- 7 files changed, 42 insertions(+), 4 deletions(-) diff --git a/resources/private/css/styles.css b/resources/private/css/styles.css index 5e9e5cf..6fe3bcb 100644 --- a/resources/private/css/styles.css +++ b/resources/private/css/styles.css @@ -13,6 +13,10 @@ body { user-select: none; } +.message { + font-size: 1.2rem; +} + .form-label { font-size: 1.2rem; margin-bottom: 2rem; diff --git a/shadow-cljs.edn b/shadow-cljs.edn index 622808c..0674012 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -9,7 +9,7 @@ :compiler-options {:source-map true} :dev {:closure-defines {"re_frame.trace.trace_enabled_QMARK_" true}} :release {:module-hash-names true} - :devtools {:preloads [day8.re-frame-10x.preload]} + :devtools {:preloads [day8.re-frame-10x.preload.react-18]} :build-hooks [(build.hook/hook {:public-dir "resources/public"})] :target :browser} :test {:target :node-test diff --git a/src/app/core.cljs b/src/app/core.cljs index fcf7aee..4760340 100644 --- a/src/app/core.cljs +++ b/src/app/core.cljs @@ -16,7 +16,24 @@ (defn register-worker [] (some-> js/navigator .-serviceWorker - (.register "worker.js"))) + (.register "worker.js") + (.then + (fn [registration] + (if (and (.-waiting registration) + js/navigator.serviceWorker.controller) + (re-frame/dispatch [::events/update-available]) + (.addEventListener + registration + "updatefound" + (fn [] + (when-let [installing (.-installing registration)] + (.addEventListener + installing + "statechange" + (fn [] + (when (and (.-waiting registration) + js/navigator.serviceWorker.controller) + (re-frame/dispatch [::events/update-available])))))))))))) (defn- dev-setup [] (when config/debug? diff --git a/src/app/events.cljs b/src/app/events.cljs index 658d9e5..1d2d247 100644 --- a/src/app/events.cljs +++ b/src/app/events.cljs @@ -273,6 +273,11 @@ (merge db data) (reset-game data))}))) +(re-frame/reg-event-fx + ::update-available + (fn [{:keys [db]} _] + {:db (assoc-in db [:app-info :update-available] true)})) + (re-frame/reg-event-db ::reset (fn [db _] diff --git a/src/app/subs.cljs b/src/app/subs.cljs index 942c34b..71b49b8 100644 --- a/src/app/subs.cljs +++ b/src/app/subs.cljs @@ -62,3 +62,9 @@ ::settings (fn [db _] (:settings db))) + +(re-frame/reg-sub + ::messages + (fn [db _] + (cond-> [] + (get-in db [:app-info :update-available]) (conj {:text "New version is available."})))) diff --git a/src/app/views.cljs b/src/app/views.cljs index 2c91137..5e5d5fb 100644 --- a/src/app/views.cljs +++ b/src/app/views.cljs @@ -145,12 +145,18 @@ (defn menu [] (let [settings @(re-frame/subscribe [::subs/settings]) - app-info @(re-frame/subscribe [::subs/app-info])] + app-info @(re-frame/subscribe [::subs/app-info]) + messages @(re-frame/subscribe [::subs/messages])] [:div.menu [:div.menu--header [:button.close {:on-click (fn [_] (re-frame/dispatch [::events/open-page :game]))} [i/close]]] + (when (seq messages) + [:div.messages + (for [message messages] + ^{:key (:text message)} [:div.message (:text message)]) + [:hr]]) [:form {:on-submit (fn [e] (.preventDefault e) diff --git a/src/nenadalm/clojure_utils/re_frame/autosave.cljs b/src/nenadalm/clojure_utils/re_frame/autosave.cljs index 34c64cb..d1d8fef 100644 --- a/src/nenadalm/clojure_utils/re_frame/autosave.cljs +++ b/src/nenadalm/clojure_utils/re_frame/autosave.cljs @@ -24,7 +24,7 @@ ::ls/remove [autosave-key]} {}))) - (re-frame/dispatch [::autosave-load]) + (re-frame/dispatch-sync [::autosave-load]) (js/document.addEventListener "visibilitychange" (fn []