diff --git a/src/status_im/chat/events/commands.cljs b/src/status_im/chat/events/commands.cljs index dc870c12ea..669c8e1123 100644 --- a/src/status_im/chat/events/commands.cljs +++ b/src/status_im/chat/events/commands.cljs @@ -45,7 +45,7 @@ :params jail-params :callback-events-creator (fn [jail-response] [[::jail-command-data-response - jail-response message opts]])}}) + jail-response message opts]])}}) {:db (update-in db [:contacts/contacts jail-id :jail-loaded-events] conj [:request-command-message-data message opts])})))) @@ -70,7 +70,6 @@ (fn [_ [{command-name :name}]] (case (keyword command-name) :grant-permissions - {:dispatch [:request-permissions - [:read-external-storage] - #(re-frame/dispatch [:initialize-geth])]} + {:dispatch [:request-permissions {:permissions [:read-external-storage] + :on-allowed #(re-frame/dispatch [:initialize-geth])}]} (log/debug "ignoring command: " command-name)))) diff --git a/src/status_im/ui/components/permissions.cljs b/src/status_im/ui/components/permissions.cljs index b037e0bfb0..cd06473166 100644 --- a/src/status_im/ui/components/permissions.cljs +++ b/src/status_im/ui/components/permissions.cljs @@ -16,16 +16,18 @@ (and (= (count permission-vals) 1) (not= (first permission-vals) "denied")))) -(defn request-permissions [permissions then else] +(defn request-permissions [{:keys [permissions on-allowed on-denied] + :or {on-allowed #() + on-denied #()} + :as options}] (if platform/android? - (letfn [(else-fn [] (when else (else)))] - (let [permissions (mapv #(get permissions-map %) permissions)] - (-> (.requestMultiple permissions-class (clj->js permissions)) - (.then #(if (all-granted? (js->clj %)) - (then) - (else-fn))) - (.catch else-fn)))) + (let [permissions (mapv #(get permissions-map %) permissions)] + (-> (.requestMultiple permissions-class (clj->js permissions)) + (.then #(if (all-granted? (js->clj %)) + (on-allowed) + (on-denied))) + (.catch on-denied))) (if ((set permissions) :camera) - (camera/request-access-ios then else) - (then)))) + (camera/request-access-ios on-allowed on-denied) + (on-allowed)))) diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 7127645ac5..368f02e2bb 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -162,10 +162,8 @@ (status/should-move-to-internal-storage? (fn [should-move?] (if should-move? - (re-frame/dispatch [:request-permissions - [:read-external-storage] - #(move-to-internal-storage config) - #()]) + (re-frame/dispatch [:request-permissions {:permissions [:read-external-storage] + :on-allowed #(move-to-internal-storage config)}]) (status/start-node config)))))) (re-frame/reg-fx @@ -174,9 +172,9 @@ (status/module-initialized!))) (re-frame/reg-fx - ::request-permissions-fx - (fn [[permissions then else]] - (permissions/request-permissions permissions then else))) + :request-permissions-fx + (fn [options] + (permissions/request-permissions options))) (re-frame/reg-fx ::testfairy-alert @@ -417,8 +415,8 @@ (handlers/register-handler-fx :request-permissions - (fn [_ [_ permissions then else]] - {::request-permissions-fx [permissions then else]})) + (fn [_ [_ options]] + {:request-permissions-fx options})) (handlers/register-handler-db :set-swipe-position diff --git a/src/status_im/ui/screens/profile/user/views.cljs b/src/status_im/ui/screens/profile/user/views.cljs index 80e40b4ebb..925c4965ca 100644 --- a/src/status_im/ui/screens/profile/user/views.cljs +++ b/src/status_im/ui/screens/profile/user/views.cljs @@ -49,11 +49,10 @@ :action #(re-frame/dispatch [:my-profile/update-picture])} {:label (i18n/label :t/image-source-make-photo) :action (fn [] - (re-frame/dispatch [:request-permissions - [:camera :write-external-storage] - #(re-frame/dispatch [:navigate-to :profile-photo-capture]) - #(utils/show-popup (i18n/label :t/error) - (i18n/label :t/camera-access-error))]))}]) + (re-frame/dispatch [:request-permissions {:permissions [:camera :write-external-storage] + :on-allowed #(re-frame/dispatch [:navigate-to :profile-photo-capture]) + :on-denied #(utils/show-popup (i18n/label :t/error) + (i18n/label :t/camera-access-error))}]))}]) (defn qr-viewer-toolbar [label value] [toolbar/toolbar {} diff --git a/src/status_im/ui/screens/qr_scanner/events.cljs b/src/status_im/ui/screens/qr_scanner/events.cljs index 95e29a357e..45c729b9ff 100644 --- a/src/status_im/ui/screens/qr_scanner/events.cljs +++ b/src/status_im/ui/screens/qr_scanner/events.cljs @@ -1,46 +1,32 @@ (ns status-im.ui.screens.qr-scanner.events (:require [re-frame.core :as re-frame] [status-im.ui.components.camera :as camera] - [status-im.utils.handlers :as u :refer [register-handler]] + [status-im.utils.handlers :as handlers] [status-im.utils.utils :as utils] [status-im.i18n :as i18n])) -(defn set-current-identifier [db [_ identifier handler]] - (assoc-in db [:qr-codes identifier] handler)) +(handlers/register-handler-fx + :scan-qr-code + (fn [{:keys [db]} [_ identifier handler]] + {:db (assoc-in db [:qr-codes identifier] handler) + :request-permissions-fx {:permissions [:camera] + :on-allowed #(re-frame/dispatch [:navigate-to :qr-scanner {:current-qr-context identifier}]) + :on-denied #(utils/show-popup (i18n/label :t/error) + (i18n/label :t/camera-access-error))}})) -(defn navigate-to-scanner - [_ [_ identifier]] - (re-frame/dispatch [:request-permissions - [:camera] - #(re-frame/dispatch [:navigate-to :qr-scanner {:current-qr-context identifier}]) - #(utils/show-popup (i18n/label :t/error) - (i18n/label :t/camera-access-error))])) +(handlers/register-handler-fx + :clear-qr-code + (fn [{:keys [db]} [_ identifier]] + {:db (update db :qr-codes dissoc identifier)})) -(register-handler :scan-qr-code - (re-frame/after navigate-to-scanner) - set-current-identifier) - -(register-handler :clear-qr-code - (fn [db [_ identifier]] - (update db :qr-codes dissoc identifier))) - -(defn- handle-qr-request - [db [_ context data]] - (when-let [handler (get-in db [:qr-codes context])] - (re-frame/dispatch [handler context data]))) - -(defn clear-qr-request [db [_ context]] - (-> db - (update :qr-codes dissoc context) - (dissoc :current-qr-context))) - -(defn navigate-back! - [{:keys [view-id]} _] - (when (= :qr-scanner view-id) - (re-frame/dispatch [:navigate-back]))) - -(register-handler :set-qr-code - (u/handlers-> - handle-qr-request - clear-qr-request - navigate-back!)) +(handlers/register-handler-fx + :set-qr-code + (fn [{:keys [db]} [_ context data]] + (let [handler-event (when-let [handler (get-in db [:qr-codes context])] + [handler context data]) + navigate-back-event (when (= :qr-scanner (:view-id db)) + [:navigate-back])] + {:dispatch-n [handler-event navigate-back-event] + :db (-> db + (update :qr-codes dissoc context) + (dissoc :current-qr-context))}))) diff --git a/src/status_im/ui/screens/wallet/components/views.cljs b/src/status_im/ui/screens/wallet/components/views.cljs index 06a490db76..b15b95fdaf 100644 --- a/src/status_im/ui/screens/wallet/components/views.cljs +++ b/src/status_im/ui/screens/wallet/components/views.cljs @@ -157,8 +157,8 @@ [choose-recipient/choose-recipient]) (defn- request-camera-permissions [] - (re-frame/dispatch [:request-permissions [:camera] - #(re-frame/dispatch [:navigate-to :recipient-qr-code])])) + (re-frame/dispatch [:request-permissions {:permissions [:camera] + :on-allowed #(re-frame/dispatch [:navigate-to :recipient-qr-code])}])) (defn- on-choose-recipient [contact-only?] (list-selection/show {:title (i18n/label :t/wallet-choose-recipient) diff --git a/src/status_im/utils/handlers.clj b/src/status_im/utils/handlers.clj index 79b0b109a9..956e94ef1c 100644 --- a/src/status_im/utils/handlers.clj +++ b/src/status_im/utils/handlers.clj @@ -1,22 +1,5 @@ (ns status-im.utils.handlers) -(defmacro handlers-> - "Help thread multiple handler functions. - All functions are expected to accept [db event] as parameters. - If one handler returns a modified db it will be used as parameters for subsequent handlers." - [& forms] - (let [db (gensym "db") - event (gensym "event") - new-db (gensym "new-db")] - `(fn [~db ~event] - (let [~@(interleave (repeat db) - (map (fn [form] - `(let [~new-db (~form ~db ~event)] - (if (map? ~new-db) - ~new-db - ~db))) forms))] - ~db)))) - (defmacro merge-fx* "This macro is called recursively from merge-fx It wraps each form in a let binding that captures