diff --git a/src/status_im/chat/events.cljs b/src/status_im/chat/events.cljs index 18e382f4b4..01326e1e22 100644 --- a/src/status_im/chat/events.cljs +++ b/src/status_im/chat/events.cljs @@ -20,7 +20,8 @@ status-im.chat.events.animation status-im.chat.events.receive-message status-im.chat.events.sign-up - status-im.chat.events.console)) + status-im.chat.events.console + status-im.chat.events.webview-bridge)) ;;;; Coeffects diff --git a/src/status_im/chat/events/webview_bridge.cljs b/src/status_im/chat/events/webview_bridge.cljs new file mode 100644 index 0000000000..2672951e7d --- /dev/null +++ b/src/status_im/chat/events/webview_bridge.cljs @@ -0,0 +1,56 @@ +(ns status-im.chat.events.webview-bridge + (:require [re-frame.core :as re-frame] + [status-im.ui.components.nfc :as nfc] + [status-im.utils.handlers :as handlers] + [status-im.utils.types :as types] + [taoensso.timbre :as log])) + +;;;; Effects + +(re-frame/reg-fx + ::webview-nfc + (fn [{:keys [event params]}] + (let [callback #(re-frame/dispatch [:chat-webview-bridge/send-to-bridge + {:params % :event "nfc"}])] + (case (keyword event) + :get-card-id (nfc/get-card-id #(callback {:event :get-card-id :card %}) + #(callback {:event :get-card-id :error %})) + :read-tag (let [{:keys [sectors]} params] + (nfc/read-tag sectors + #(callback {:event :read-tag :card %}) + #(callback {:event :read-tag :error %}))) + :write-tag (let [{:keys [sectors id]} params] + (nfc/write-tag sectors + id + #(callback {:event :write-tag :card %}) + #(callback {:event :write-tag :error %}))) + :default)))) + +(re-frame/reg-fx + ::send-to-bridge + (fn [[webview-bridge data]] + (when webview-bridge + (.sendToBridge webview-bridge data)))) + +;;;; Handlers + +(handlers/register-handler-db + :chat-webview-bridge/set-ref + (fn [db [_ ref]] + (assoc db :webview-bridge ref))) + +(handlers/register-handler-fx + :chat-webview-bridge/process-message + (fn [{:keys [db]} [_ 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' + :nfc {::webview-nfc params} + nil)))) + +(handlers/register-handler-fx + :chat-webview-bridge/send-to-bridge + (fn [{{:keys [webview-bridge]} :db} [_ data]] + {::send-to-bridge [webview-bridge data]})) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index d06c26e7a8..5445919c51 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -16,8 +16,7 @@ [status-im.utils.random :as random] [status-im.utils.handlers :refer [register-handler register-handler-fx] :as u] status-im.chat.events - status-im.chat.handlers.send-message - status-im.chat.handlers.webview-bridge)) + status-im.chat.handlers.send-message)) (defn remove-chat [db [_ chat-id]] diff --git a/src/status_im/chat/handlers/webview_bridge.cljs b/src/status_im/chat/handlers/webview_bridge.cljs deleted file mode 100644 index ed1419f169..0000000000 --- a/src/status_im/chat/handlers/webview_bridge.cljs +++ /dev/null @@ -1,45 +0,0 @@ -(ns status-im.chat.handlers.webview-bridge - (:require [re-frame.core :refer [dispatch]] - [status-im.utils.handlers :as handlers] - [status-im.utils.types :as types] - [taoensso.timbre :as log] - [status-im.ui.components.nfc :as nfc])) - -(handlers/register-handler :set-webview-bridge - (fn [db [_ bridge]] - (assoc db :webview-bridge bridge))) - -(handlers/register-handler :webview-bridge-message - (handlers/side-effect! - (fn [_ [_ 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' - :nfc (dispatch [:webview-nfc params]) - (log/error (str "Unknown event: " event'))))))) - -(handlers/register-handler :send-to-webview-bridge - (handlers/side-effect! - (fn [{:keys [webview-bridge]} [_ data]] - (when webview-bridge - (.sendToBridge webview-bridge (types/clj->json data)))))) - -(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) - :get-card-id (nfc/get-card-id #(callback {:event :get-card-id :card %}) - #(callback {:event :get-card-id :error %})) - :read-tag (let [{:keys [sectors]} params] - (nfc/read-tag sectors - #(callback {:event :read-tag :card %}) - #(callback {:event :read-tag :error %}))) - :write-tag (let [{:keys [sectors id]} params] - (nfc/write-tag sectors - id - #(callback {:event :write-tag :card %}) - #(callback {:event :write-tag :error %}))) - :default))))) diff --git a/src/status_im/chat/views/input/input_actions.cljs b/src/status_im/chat/views/input/input_actions.cljs index c1aa3e45a8..d4de5412f7 100644 --- a/src/status_im/chat/views/input/input_actions.cljs +++ b/src/status_im/chat/views/input/input_actions.cljs @@ -1,70 +1,63 @@ (ns status-im.chat.views.input.input-actions - (:require-macros [status-im.utils.views :refer [defview]]) - (:require [clojure.string :as str] - [reagent.core :as r] - [re-frame.core :refer [subscribe dispatch]] + (:require [re-frame.core :as re-frame] [taoensso.timbre :as log] - [status-im.ui.components.react :refer [view - text - icon - touchable-highlight]] + [status-im.ui.components.react :as react] [status-im.chat.styles.input.input-actions :as style])) (defmulti action-view (fn [{:keys [type]}] (keyword type))) (defmethod action-view :fullscreen [_] - (let [fullscreen? (subscribe [:chat-ui-props :fullscreen?])] + (let [fullscreen? (re-frame/subscribe [:chat-ui-props :fullscreen?])] (fn [] - [touchable-highlight - {:on-press #(dispatch [:set-chat-ui-props {:fullscreen? (not @fullscreen?)}])} + [react/touchable-highlight + {:on-press #(re-frame/dispatch [:set-chat-ui-props {:fullscreen? (not @fullscreen?)}])} (if @fullscreen? - [view (style/action-view true) - [icon :action_fullscreen_collapse style/action-view-icon]] - [view (style/action-view true) - [icon :action_fullscreen_expand style/action-view-fullscreen-expand-icon]])]))) + [react/view (style/action-view true) + [react/icon :action_fullscreen_collapse style/action-view-icon]] + [react/view (style/action-view true) + [react/icon :action_fullscreen_expand style/action-view-fullscreen-expand-icon]])]))) (defmethod action-view :web-view-back [_] - (let [result-box (subscribe [:chat-ui-props :result-box]) - webview (subscribe [:get :webview-bridge])] + (let [result-box (re-frame/subscribe [:chat-ui-props :result-box]) + webview (re-frame/subscribe [:get :webview-bridge])] (fn [] - [touchable-highlight + [react/touchable-highlight {:on-press #(.goBack @webview)} - [view (style/action-view true) - [icon :action_back style/action-view-icon-tinted]]] + [react/view (style/action-view true) + [react/icon :action_back style/action-view-icon-tinted]]] #_(if (:can-go-back? @result-box) - - [view (style/action-view false) - [icon :action_back style/action-view-icon]])))) + [react/view (style/action-view false) + [react/icon :action_back style/action-view-icon]])))) (defmethod action-view :web-view-forward [_] - (let [result-box (subscribe [:chat-ui-props :result-box]) - webview (subscribe [:get :webview-bridge])] + (let [result-box (re-frame/subscribe [:chat-ui-props :result-box]) + webview (re-frame/subscribe [:get :webview-bridge])] (fn [] - [touchable-highlight + [react/touchable-highlight {:on-press #(.goForward @webview)} - [view (style/action-view true) - [icon :action_forward style/action-view-icon-tinted]]] + [react/view (style/action-view true) + [react/icon :action_forward style/action-view-icon-tinted]]] #_(if (:can-go-forward? @result-box) - - [view (style/action-view false) - [icon :action_forward style/action-view-icon]])))) + [react/view (style/action-view false) + [react/icon :action_forward style/action-view-icon]])))) (defmethod action-view :default [{:keys [image executeJs]}] - [touchable-highlight - {:on-press #(dispatch [:send-to-webview-bridge {:event "actions-execute-js" - :js executeJs}])} - [view (style/action-view true) - [icon (str "action_" image) style/action-view-icon]]]) + [react/touchable-highlight + {:on-press #(re-frame/dispatch [:chat-webview-bridge/send-to-bridge + {:event "actions-execute-js" + :js executeJs}])} + [react/view (style/action-view true) + [react/icon (str "action_" image) style/action-view-icon]]]) (defn input-actions-view [] - (let [result-box (subscribe [:chat-ui-props :result-box])] + (let [result-box (re-frame/subscribe [:chat-ui-props :result-box])] (fn [] (let [{:keys [actions]} @result-box] - [view style/actions-container + [react/view style/actions-container (for [{:keys [type] :as action} actions] ^{:key type} [action-view action])])))) diff --git a/src/status_im/chat/views/input/web_view.cljs b/src/status_im/chat/views/input/web_view.cljs index b550d5dbe4..b939c96df1 100644 --- a/src/status_im/chat/views/input/web_view.cljs +++ b/src/status_im/chat/views/input/web_view.cljs @@ -35,8 +35,8 @@ result-box [:chat-ui-props :result-box]] (when url [webview-bridge - {:ref #(dispatch [:set-webview-bridge %]) - :on-bridge-message #(dispatch [:webview-bridge-message %]) + {:ref #(dispatch [:chat-webview-bridge/set-ref %]) + :on-bridge-message #(dispatch [:chat-webview-bridge/process-message %]) :source {:uri url} :render-error web-view-error :java-script-enabled true diff --git a/src/status_im/ui/screens/contacts/contact_list_modal/views.cljs b/src/status_im/ui/screens/contacts/contact_list_modal/views.cljs index 8fa63b658e..5161527332 100644 --- a/src/status_im/ui/screens/contacts/contact_list_modal/views.cljs +++ b/src/status_im/ui/screens/contacts/contact_list_modal/views.cljs @@ -33,7 +33,7 @@ :icon :icons/address :icon-opts {:color :blue} :on-press #(do - (dispatch [:send-to-webview-bridge + (dispatch [:chat-webview-bridge/send-to-bridge {:event (name :webview-send-transaction)}]) (dispatch [:navigate-back]))}] [action-separator]