Clean up last utils/handlers

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
Eric Dvorsak 2018-03-04 01:18:02 +01:00 committed by Andrey Shovkoplyas
parent e85823056c
commit b3ef1ed53a
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
7 changed files with 52 additions and 85 deletions

View File

@ -45,7 +45,7 @@
:params jail-params :params jail-params
:callback-events-creator (fn [jail-response] :callback-events-creator (fn [jail-response]
[[::jail-command-data-response [[::jail-command-data-response
jail-response message opts]])}}) jail-response message opts]])}})
{:db (update-in db [:contacts/contacts jail-id :jail-loaded-events] {:db (update-in db [:contacts/contacts jail-id :jail-loaded-events]
conj [:request-command-message-data message opts])})))) conj [:request-command-message-data message opts])}))))
@ -70,7 +70,6 @@
(fn [_ [{command-name :name}]] (fn [_ [{command-name :name}]]
(case (keyword command-name) (case (keyword command-name)
:grant-permissions :grant-permissions
{:dispatch [:request-permissions {:dispatch [:request-permissions {:permissions [:read-external-storage]
[:read-external-storage] :on-allowed #(re-frame/dispatch [:initialize-geth])}]}
#(re-frame/dispatch [:initialize-geth])]}
(log/debug "ignoring command: " command-name)))) (log/debug "ignoring command: " command-name))))

View File

@ -16,16 +16,18 @@
(and (= (count permission-vals) 1) (and (= (count permission-vals) 1)
(not= (first permission-vals) "denied")))) (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? (if platform/android?
(letfn [(else-fn [] (when else (else)))] (let [permissions (mapv #(get permissions-map %) permissions)]
(let [permissions (mapv #(get permissions-map %) permissions)] (-> (.requestMultiple permissions-class (clj->js permissions))
(-> (.requestMultiple permissions-class (clj->js permissions)) (.then #(if (all-granted? (js->clj %))
(.then #(if (all-granted? (js->clj %)) (on-allowed)
(then) (on-denied)))
(else-fn))) (.catch on-denied)))
(.catch else-fn))))
(if ((set permissions) :camera) (if ((set permissions) :camera)
(camera/request-access-ios then else) (camera/request-access-ios on-allowed on-denied)
(then)))) (on-allowed))))

View File

@ -162,10 +162,8 @@
(status/should-move-to-internal-storage? (status/should-move-to-internal-storage?
(fn [should-move?] (fn [should-move?]
(if should-move? (if should-move?
(re-frame/dispatch [:request-permissions (re-frame/dispatch [:request-permissions {:permissions [:read-external-storage]
[:read-external-storage] :on-allowed #(move-to-internal-storage config)}])
#(move-to-internal-storage config)
#()])
(status/start-node config)))))) (status/start-node config))))))
(re-frame/reg-fx (re-frame/reg-fx
@ -174,9 +172,9 @@
(status/module-initialized!))) (status/module-initialized!)))
(re-frame/reg-fx (re-frame/reg-fx
::request-permissions-fx :request-permissions-fx
(fn [[permissions then else]] (fn [options]
(permissions/request-permissions permissions then else))) (permissions/request-permissions options)))
(re-frame/reg-fx (re-frame/reg-fx
::testfairy-alert ::testfairy-alert
@ -417,8 +415,8 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:request-permissions :request-permissions
(fn [_ [_ permissions then else]] (fn [_ [_ options]]
{::request-permissions-fx [permissions then else]})) {:request-permissions-fx options}))
(handlers/register-handler-db (handlers/register-handler-db
:set-swipe-position :set-swipe-position

View File

@ -49,11 +49,10 @@
:action #(re-frame/dispatch [:my-profile/update-picture])} :action #(re-frame/dispatch [:my-profile/update-picture])}
{:label (i18n/label :t/image-source-make-photo) {:label (i18n/label :t/image-source-make-photo)
:action (fn [] :action (fn []
(re-frame/dispatch [:request-permissions (re-frame/dispatch [:request-permissions {:permissions [:camera :write-external-storage]
[:camera :write-external-storage] :on-allowed #(re-frame/dispatch [:navigate-to :profile-photo-capture])
#(re-frame/dispatch [:navigate-to :profile-photo-capture]) :on-denied #(utils/show-popup (i18n/label :t/error)
#(utils/show-popup (i18n/label :t/error) (i18n/label :t/camera-access-error))}]))}])
(i18n/label :t/camera-access-error))]))}])
(defn qr-viewer-toolbar [label value] (defn qr-viewer-toolbar [label value]
[toolbar/toolbar {} [toolbar/toolbar {}

View File

@ -1,46 +1,32 @@
(ns status-im.ui.screens.qr-scanner.events (ns status-im.ui.screens.qr-scanner.events
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[status-im.ui.components.camera :as camera] [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.utils.utils :as utils]
[status-im.i18n :as i18n])) [status-im.i18n :as i18n]))
(defn set-current-identifier [db [_ identifier handler]] (handlers/register-handler-fx
(assoc-in db [:qr-codes identifier] handler)) :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 (handlers/register-handler-fx
[_ [_ identifier]] :clear-qr-code
(re-frame/dispatch [:request-permissions (fn [{:keys [db]} [_ identifier]]
[:camera] {:db (update db :qr-codes dissoc identifier)}))
#(re-frame/dispatch [:navigate-to :qr-scanner {:current-qr-context identifier}])
#(utils/show-popup (i18n/label :t/error)
(i18n/label :t/camera-access-error))]))
(register-handler :scan-qr-code (handlers/register-handler-fx
(re-frame/after navigate-to-scanner) :set-qr-code
set-current-identifier) (fn [{:keys [db]} [_ context data]]
(let [handler-event (when-let [handler (get-in db [:qr-codes context])]
(register-handler :clear-qr-code [handler context data])
(fn [db [_ identifier]] navigate-back-event (when (= :qr-scanner (:view-id db))
(update db :qr-codes dissoc identifier))) [:navigate-back])]
{:dispatch-n [handler-event navigate-back-event]
(defn- handle-qr-request :db (-> db
[db [_ context data]] (update :qr-codes dissoc context)
(when-let [handler (get-in db [:qr-codes context])] (dissoc :current-qr-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!))

View File

@ -157,8 +157,8 @@
[choose-recipient/choose-recipient]) [choose-recipient/choose-recipient])
(defn- request-camera-permissions [] (defn- request-camera-permissions []
(re-frame/dispatch [:request-permissions [:camera] (re-frame/dispatch [:request-permissions {:permissions [:camera]
#(re-frame/dispatch [:navigate-to :recipient-qr-code])])) :on-allowed #(re-frame/dispatch [:navigate-to :recipient-qr-code])}]))
(defn- on-choose-recipient [contact-only?] (defn- on-choose-recipient [contact-only?]
(list-selection/show {:title (i18n/label :t/wallet-choose-recipient) (list-selection/show {:title (i18n/label :t/wallet-choose-recipient)

View File

@ -1,22 +1,5 @@
(ns status-im.utils.handlers) (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* (defmacro merge-fx*
"This macro is called recursively from merge-fx "This macro is called recursively from merge-fx
It wraps each form in a let binding that captures It wraps each form in a let binding that captures