diff --git a/src/status_im/chat/events/input.cljs b/src/status_im/chat/events/input.cljs index ff7a6f2a9a..d4fce35ab7 100644 --- a/src/status_im/chat/events/input.cljs +++ b/src/status_im/chat/events/input.cljs @@ -404,12 +404,6 @@ (fn [{:keys [db]} [command]] (load-chat-parameter-box db command))) -(handlers/register-handler-fx - :proceed-command - [re-frame/trim-v (re-frame/inject-cofx :random-id)] - (fn [{:keys [db random-id now]} [content]] - (proceed-command db content random-id now))) - (handlers/register-handler-fx ::proceed-validation [re-frame/trim-v] diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index e559b070f2..0a319c894a 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -65,27 +65,27 @@ commands-loaded? (get-in db [:contacts/contacts chat-id :commands-loaded?]) bot-url (get-in db [:contacts/contacts chat-id :bot-url]) was-opened? (get-in db [:chats chat-id :was-opened?]) - call-init-command #(when (and (not was-opened?) bot-url) - (status/call-function! - {:chat-id chat-id - :function :init - :context {:from current-account-id}}))] + call-init-command #(do + (dispatch [:invoke-chat-loaded-callbacks chat-id]) + (when (and (not was-opened?) bot-url) + (status/call-function! + {:chat-id chat-id + :function :init + :context {:from current-account-id}})))] ; Reset validation messages, if any (dispatch [:set-chat-ui-props {:validation-messages nil}]) (dispatch [:load-requests! chat-id]) ;; todo rewrite this. temporary fix for https://github.com/status-im/status-react/issues/607 #_(dispatch [:load-commands! chat-id]) - (if-not commands-loaded? - (dispatch [:load-commands! chat-id call-init-command]) - (do - (call-init-command) - (dispatch [:invoke-chat-loaded-callbacks chat-id]))) - (if (and (seq messages) - (not= (count messages) 1)) - db' - (-> db' - load-messages! - init-chat)))) + (if-not commands-loaded? + (dispatch [:load-commands! chat-id call-init-command]) + (call-init-command)) + (if (and (seq messages) + (not= (count messages) 1)) + db' + (-> db' + load-messages! + init-chat)))) (register-handler :add-chat-loaded-callback (fn [db [_ chat-id callback]] diff --git a/src/status_im/chat/handlers/webview_bridge.cljs b/src/status_im/chat/handlers/webview_bridge.cljs index c0f356ecb7..2af3ead964 100644 --- a/src/status_im/chat/handlers/webview_bridge.cljs +++ b/src/status_im/chat/handlers/webview_bridge.cljs @@ -1,129 +1,33 @@ (ns status-im.chat.handlers.webview-bridge - (:require [re-frame.core :refer [after dispatch enrich]] - [status-im.utils.handlers :refer [register-handler]] - [status-im.utils.handlers :as u] - [status-im.utils.types :as t] - [status-im.i18n :refer [label]] + (:require [re-frame.core :refer [dispatch]] + [status-im.utils.handlers :as handlers] + [status-im.utils.types :as types] [taoensso.timbre :as log] - [status-im.commands.utils :as cu] - [status-im.native-module.core :as s] - [status-im.components.nfc :as nfc] - [status-im.constants :as c] - [cljs.reader :refer [read-string]] - [status-im.ui.screens.navigation :as nav] - [cljs.spec.alpha :as spec])) + [status-im.components.nfc :as nfc])) -(defn by-public-key [public-key contacts] - (when-let [{:keys [address]} (contacts public-key)] - (when address {:address address}))) - -(defn wrap-hex [s] - (if (js/isNaN (.parseInt js/Number s)) - s - (str "\"" s "\""))) - -(defn scan-qr-handler - [{:contacts/keys [contacts]} [_ _ data]] - (let [data' (try (read-string (wrap-hex data)) - (catch :default e data)) - data'' (cond - (map? data') data' - (spec/valid? :global/address data') {:address data'} - (string? data') (by-public-key data' contacts) - :else nil)] - (when data'' - (dispatch [:send-to-webview-bridge - {:params data'' - :event (name :webview-send-transaction)}])) - (dispatch [:navigate-back]))) - -(register-handler :webview-address-from-qr - (u/side-effect! scan-qr-handler)) - -(register-handler :set-webview-bridge +(handlers/register-handler :set-webview-bridge (fn [db [_ bridge]] (assoc db :webview-bridge bridge))) -(defn contacts-click-handler - [{:keys [whisper-identity] :as contact} action params] - (dispatch [:navigate-back]) - (when action - (if (= contact :qr-scan) - (if (= action :send) - (dispatch [:show-scan-qr :webview-address-from-qr]) - (dispatch [:navigate-to-modal :qr-code-view {:qr-source :whisper-identity - :amount? true}])) - (dispatch [:chat-with-command whisper-identity action - (assoc params :contact contact)])))) - - -(register-handler ::send-command - (u/side-effect! - (fn [{:keys [current-chat-id] - :contacts/keys [contacts]} - [_ command-key {:keys [contact amount]}]] - (let [command (get-in contacts [current-chat-id :commands command-key])] - (dispatch [:set-in [:bot-db current-chat-id :public :recipient] contact]) - (dispatch [:proceed-command - {:command command, - :metadata nil, - :args [(get contact :name) amount]}]))))) - -(defn chat-with-command - [_ [_ whisper-identity command-key params]] - (dispatch [:remove-contacts-click-handler]) - (dispatch [:add-chat-loaded-callback whisper-identity - #(dispatch [::send-command command-key params])]) - (dispatch [:start-chat whisper-identity])) - -(register-handler :chat-with-command - (u/side-effect! chat-with-command)) - -(register-handler :webview-bridge-message - (u/side-effect! +(handlers/register-handler :webview-bridge-message + (handlers/side-effect! (fn [_ [_ message-string]] - (let [{:keys [event options] :as message} (t/json->clj message-string) + (let [{:keys [event options] :as message} (types/json->clj message-string) event' (keyword event) params (:data options)] (log/debug (str "message from webview: " message)) (case event' - :webview-send-transaction - (dispatch [:navigate-to-modal - :contact-list-modal - {:handler contacts-click-handler - :action :send - :params params}]) - :webview-receive-transaction - (dispatch [:navigate-to-modal - :contact-list-modal - {:handler contacts-click-handler - :action :request - :params params}]) - :webview-scan-qr (dispatch [:show-scan-qr :webview-address-from-qr]) :nfc (dispatch [:webview-nfc params]) (log/error (str "Unknown event: " event'))))))) -(defmethod nav/preload-data! :contact-list-modal - [db [_ _ {:keys [handler action params]}]] - (assoc db :contacts/click-handler handler - :contacts/click-action action - :contacts/click-params params)) - -(def qr-context {:toolbar-title (label :t/address)}) - -(register-handler :show-scan-qr - (after #(dispatch [:navigate-to-modal :qr-scanner qr-context])) - (fn [db [_ click-handler]] - (assoc-in db [:qr-codes qr-context] click-handler))) - -(register-handler :send-to-webview-bridge - (u/side-effect! +(handlers/register-handler :send-to-webview-bridge + (handlers/side-effect! (fn [{:keys [webview-bridge]} [_ data]] (when webview-bridge - (.sendToBridge webview-bridge (t/clj->json data)))))) + (.sendToBridge webview-bridge (types/clj->json data)))))) -(register-handler :webview-nfc - (u/side-effect! +(handlers/register-handler :webview-nfc + (handlers/side-effect! (fn [_ [_ {:keys [event params]}]] (let [callback #(dispatch [:send-to-webview-bridge {:params % :event "nfc"}])] (case (keyword event) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 794e78d497..9dff2ab653 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -44,11 +44,13 @@ dapp-url bot-url dapp?]} :contact + callback :callback :as params}]] - (when bot-url + (if bot-url (if-let [resource (js-res/get-resource bot-url)] (dispatch [::validate-hash params resource]) - (http-get-commands params bot-url)))) + (http-get-commands params bot-url)) + (when callback (callback)))) (defn dispatch-loaded! [db [{{:keys [whisper-identity]} :contact diff --git a/src/status_im/ui/screens/contacts/navigation.cljs b/src/status_im/ui/screens/contacts/navigation.cljs index 47d9c833f1..d184d61469 100644 --- a/src/status_im/ui/screens/contacts/navigation.cljs +++ b/src/status_im/ui/screens/contacts/navigation.cljs @@ -7,4 +7,10 @@ (assoc-in [:toolbar-search :show] nil) (assoc-in [:contacts/list-ui-props :edit?] false) (assoc-in [:contacts/ui-props :edit?] false) - (assoc :contacts/click-handler click-handler))) \ No newline at end of file + (assoc :contacts/click-handler click-handler))) + +(defmethod nav/preload-data! :contact-list-modal + [db [_ _ {:keys [handler action params]}]] + (assoc db :contacts/click-handler handler + :contacts/click-action action + :contacts/click-params params)) \ No newline at end of file diff --git a/src/status_im/ui/screens/wallet/choose_recipient/views.cljs b/src/status_im/ui/screens/wallet/choose_recipient/views.cljs index 863be88a8b..b69494d55d 100644 --- a/src/status_im/ui/screens/wallet/choose_recipient/views.cljs +++ b/src/status_im/ui/screens/wallet/choose_recipient/views.cljs @@ -84,10 +84,11 @@ {:width (.-width layout) :height (.-height layout)}]))} (when (or platform/android? - camera-permitted?)[camera/camera {:style styles/preview - :aspect :fill - :captureAudio false - :torchMode (camera/set-torch camera-flashlight) - :onBarCodeRead #(re-frame/dispatch [:choose-recipient (camera/get-qr-code-data %) nil])}]) + camera-permitted?) + [camera/camera {:style styles/preview + :aspect :fill + :captureAudio false + :torchMode (camera/set-torch camera-flashlight) + :onBarCodeRead #(re-frame/dispatch [:choose-recipient (camera/get-qr-code-data %) nil])}]) [viewfinder camera-dimensions]] [recipient-buttons]])) diff --git a/src/status_im/ui/screens/wallet/request/events.cljs b/src/status_im/ui/screens/wallet/request/events.cljs index d831db718f..ff2f15231b 100644 --- a/src/status_im/ui/screens/wallet/request/events.cljs +++ b/src/status_im/ui/screens/wallet/request/events.cljs @@ -1,16 +1,22 @@ (ns status-im.ui.screens.wallet.request.events (:require [status-im.utils.handlers :as handlers] - [status-im.ui.screens.wallet.db :as wallet.db])) + [status-im.ui.screens.wallet.db :as wallet.db] + [re-frame.core :as re-frame])) + +(defn chat-loaded-callback [amount] + (fn [] + (re-frame/dispatch [:select-chat-input-command {:name "request" :prefill [amount]}]) + ;;TODO get rid of timeout + (js/setTimeout #(re-frame/dispatch [:send-current-message]) 100))) (handlers/register-handler-fx :wallet-send-request - (fn [{{:wallet/keys [request-transaction]} :db} [_ {:keys [whisper-identity] :as contact}]] + (fn [{{:wallet/keys [request-transaction]} :db} [_ {:keys [whisper-identity]}]] {:dispatch-n [[:navigate-back] [:navigate-to-clean :chat-list] - [:chat-with-command whisper-identity :request - {:contact contact - :amount (:amount request-transaction)}]]})) + [:add-chat-loaded-callback whisper-identity (chat-loaded-callback (:amount request-transaction))] + [:start-chat whisper-identity]]})) (handlers/register-handler-fx :wallet-validate-request-amount