do not dispatch unecessary events

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
yenda 2018-10-01 12:53:24 +02:00 committed by Andrey Shovkoplyas
parent a1d0dcc0ec
commit df6432240a
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
4 changed files with 104 additions and 75 deletions

View File

@ -249,7 +249,7 @@
(let [{:dapps/keys [permissions]} db]
(if (and (#{"eth_accounts" "eth_coinbase" "eth_sendTransaction" "eth_sign"
"eth_signTypedData" "personal_sign" "personal_ecRecover"} method)
(not (some #{"WEB3"} (get-in permissions [dapp-name :permissions]))))
(not (some #{constants/dapp-permission-web3} (get-in permissions [dapp-name :permissions]))))
(send-to-bridge cofx
{:type constants/web3-send-async-callback
:messageId message-id

View File

@ -19,8 +19,9 @@
constants/dapp-permission-web3 (ethereum/normalized-address (:address account))}
allowed-permission)))
(fx/defn send-permission-data-to-bridge
"If there is granted permission, return the data to the bridge"
(fx/defn send-response-to-bridge
"Send response to the bridge. If the permission is allowed, send data associated
with the permission"
[{:keys [db] :as cofx} permission message-id allowed?]
{:browser/send-to-bridge {:message (cond-> {:type constants/api-response
:isAllowed allowed?
@ -64,14 +65,16 @@
(let [{:keys [requested-permission message-id dapp-name]} (get-in db [:browser/options :show-permission])]
(fx/merge (assoc-in cofx [:db :browser/options :show-permission] nil)
(update-dapp-permissions dapp-name requested-permission true)
(send-permission-data-to-bridge requested-permission message-id true))))
(send-response-to-bridge requested-permission message-id true)
(process-next-permission dapp-name))))
(fx/defn deny-permission
"Add permission to set of allowed permission and process next permission"
[{:keys [db] :as cofx}]
(let [{:keys [requested-permission message-id dapp-name]} (get-in db [:browser/options :show-permission])]
(fx/merge (assoc-in cofx [:db :browser/options :show-permission] nil)
(send-permission-data-to-bridge requested-permission message-id false))))
(send-response-to-bridge requested-permission message-id false)
(process-next-permission dapp-name))))
(fx/defn process-permission
"Process the permission requested by a dapp
@ -82,8 +85,8 @@
permission-allowed? (boolean (allowed-permissions permission))
permission-supported? ((set (keys supported-permissions)) permission)]
(if (or permission-allowed? (not permission-supported?))
(send-permission-data-to-bridge cofx permission message-id permission-allowed?)
(send-response-to-bridge cofx permission message-id permission-allowed?)
(process-next-permission (update-in cofx [:db :browser/options :pending-permissions]
conj {:permission permission
:message-id message-id})
dapp-name))))
dapp-name))))

View File

@ -11,71 +11,97 @@
[status-im.ui.screens.browser.styles :as styles])
(:require-macros [status-im.utils.views :as views]))
(views/defview permissions-panel [{:keys [dapp? dapp]} {:keys [requested-permission dapp-name]}]
(views/letsubs [bottom-anim-value (anim/create-value -354)
alpha-value (anim/create-value 0)
hide-panel (fn []
(js/setTimeout #(re-frame/dispatch
[:browser.permissions.ui/permission-animation-finished
dapp-name])
600)
(anim/start
(anim/parallel
[(anim/spring bottom-anim-value {:toValue -354})
(anim/timing alpha-value {:toValue 0
:duration 500})])))]
{:component-did-mount #(anim/start
(anim/parallel
[(anim/spring bottom-anim-value {:toValue -20})
(anim/timing alpha-value {:toValue 0.6
:duration 500})]))}
(let [_ (when-not requested-permission (js/setTimeout hide-panel 10))
{:keys [title description icon]} (get browser.permissions/supported-permissions requested-permission)]
[react/view styles/permissions-panel-container
[react/animated-view {:style (styles/permissions-panel-background alpha-value)}]
[react/animated-view {:style (styles/permissions-panel bottom-anim-value)}
[react/view styles/permissions-panel-icons-container
(if dapp?
[chat-icon.screen/dapp-icon-permission dapp 48]
[react/view styles/permissions-panel-dapp-icon-container
[react/text {:style styles/permissions-panel-d-label} "Ð"]])
[react/view {:margin-left 3 :margin-right 3}
[react/view styles/dot]]
[react/view {:margin-right 3}
[react/view styles/dot]]
[react/view styles/permissions-panel-ok-icon-container
[icons/icon :icons/ok styles/permissions-panel-ok-ico]]
[react/view {:margin-left 3 :margin-right 3}
[react/view styles/dot]]
[react/view {:margin-right 3}
[react/view styles/dot]]
[react/view styles/permissions-panel-wallet-icon-container
(when icon
[icons/icon icon {:color :white}])]]
[react/text {:style styles/permissions-panel-title-label}
(str "\"" dapp-name "\" " title)]
[react/text {:style styles/permissions-panel-description-label}
description]
[react/view {:flex-direction :row :margin-top 14}
[components.common/button
{:on-press #(re-frame/dispatch [:browser.permissions.ui/dapp-permission-denied])
:label (i18n/label :t/deny)}]
[react/view {:width 16}]
[components.common/button
{:on-press #(re-frame/dispatch [:browser.permissions.ui/dapp-permission-allowed])
:label (i18n/label :t/allow)}]]]])))
(defn hide-panel-anim
[bottom-anim-value alpha-value]
(anim/start
(anim/parallel
[(anim/spring bottom-anim-value {:toValue -354})
(anim/timing alpha-value {:toValue 0
:duration 500})])))
;; NOTE (andrey) we need this complex function, to show animation before component will be unmounted
(defn permissions-anim-panel [browser show-permission]
(let [timeout (atom nil)
render? (reagent/atom false)]
(fn [browser show-permission]
(if show-permission
(do
(when @timeout
(js/clearTimeout @timeout)
(reset! timeout nil))
(when-not @render? (reset! render? true)))
(reset! timeout (js/setTimeout #(reset! render? false) 600)))
(when @render?
[permissions-panel browser show-permission]))))
(defn show-panel-anim
[bottom-anim-value alpha-value]
(anim/start
(anim/parallel
[(anim/spring bottom-anim-value {:toValue -20})
(anim/timing alpha-value {:toValue 0.6
:duration 500})])))
(defn permission-details [requested-permission]
(get browser.permissions/supported-permissions requested-permission))
(views/defview permissions-panel [{:keys [dapp? dapp]} {:keys [dapp-name]}]
(views/letsubs [bottom-anim-value (anim/create-value -354)
alpha-value (anim/create-value 0)
current-permission (reagent/atom nil)
update? (reagent/atom nil)]
{:component-will-update (fn [_ [_ _ {:keys [requested-permission]}]]
(cond
@update?
;; the component has been updated with a new permission, we show the panel
(do (reset! update? false)
(show-panel-anim bottom-anim-value alpha-value))
(and @current-permission requested-permission)
;; a permission has been accepted/denied by the user, and there is
;; another permission that needs to be processed by the user
;; we hide the processed permission with an animation and update
;; `current-permission` with a delay so that the information is still
;; available during the animation
(do (reset! update? true)
(js/setTimeout #(reset! current-permission
(permission-details requested-permission))
600)
(hide-panel-anim bottom-anim-value alpha-value))
requested-permission
;; the dapp is asking for a permission, we put it in current-permission
;; and start the show-animation
(do (reset! current-permission
(get browser.permissions/supported-permissions
requested-permission))
(show-panel-anim bottom-anim-value alpha-value))
:else
;; a permission has been accepted/denied by the user, and there is
;; no other permission that needs to be processed by the user
;; we hide the processed permission with an animation and update
;; `current-permission` with a delay so that the information is still
;; available during the animation
(do (js/setTimeout #(reset! current-permission nil) 500)
(hide-panel-anim bottom-anim-value alpha-value))))}
(when @current-permission
(let [{:keys [title description icon]} @current-permission]
[react/view styles/permissions-panel-container
[react/animated-view {:style (styles/permissions-panel-background alpha-value)}]
[react/animated-view {:style (styles/permissions-panel bottom-anim-value)}
[react/view styles/permissions-panel-icons-container
(if dapp?
[chat-icon.screen/dapp-icon-permission dapp 48]
[react/view styles/permissions-panel-dapp-icon-container
[react/text {:style styles/permissions-panel-d-label} "Ð"]])
[react/view {:margin-left 3 :margin-right 3}
[react/view styles/dot]]
[react/view {:margin-right 3}
[react/view styles/dot]]
[react/view styles/permissions-panel-ok-icon-container
[icons/icon :icons/ok styles/permissions-panel-ok-ico]]
[react/view {:margin-left 3 :margin-right 3}
[react/view styles/dot]]
[react/view {:margin-right 3}
[react/view styles/dot]]
[react/view styles/permissions-panel-wallet-icon-container
(when icon
[icons/icon icon {:color :white}])]]
[react/text {:style styles/permissions-panel-title-label}
(str "\"" dapp-name "\" " title)]
[react/text {:style styles/permissions-panel-description-label}
description]
[react/view {:flex-direction :row :margin-top 14}
[components.common/button
{:on-press #(re-frame/dispatch [:browser.permissions.ui/dapp-permission-denied])
:label (i18n/label :t/deny)}]
[react/view {:width 16}]
[components.common/button
{:on-press #(re-frame/dispatch [:browser.permissions.ui/dapp-permission-allowed])
:label (i18n/label :t/allow)}]]]]))))

View File

@ -147,7 +147,7 @@
[react/view styles/web-view-loading
[react/activity-indicator {:animating true}]])]
[navigation url webview can-go-back? can-go-forward?]
[permissions.views/permissions-anim-panel browser show-permission]
[permissions.views/permissions-panel browser show-permission]
(when show-tooltip
[tooltip/bottom-tooltip-info
(if (= show-tooltip :secure)