mirror of
https://github.com/status-im/status-mobile.git
synced 2025-02-28 16:20:43 +00:00
Chat WebView bridge refactoring (#2535)
This commit is contained in:
parent
16930cf4e1
commit
3795cd9888
@ -20,7 +20,8 @@
|
|||||||
status-im.chat.events.animation
|
status-im.chat.events.animation
|
||||||
status-im.chat.events.receive-message
|
status-im.chat.events.receive-message
|
||||||
status-im.chat.events.sign-up
|
status-im.chat.events.sign-up
|
||||||
status-im.chat.events.console))
|
status-im.chat.events.console
|
||||||
|
status-im.chat.events.webview-bridge))
|
||||||
|
|
||||||
;;;; Coeffects
|
;;;; Coeffects
|
||||||
|
|
||||||
|
56
src/status_im/chat/events/webview_bridge.cljs
Normal file
56
src/status_im/chat/events/webview_bridge.cljs
Normal file
@ -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]}))
|
@ -16,8 +16,7 @@
|
|||||||
[status-im.utils.random :as random]
|
[status-im.utils.random :as random]
|
||||||
[status-im.utils.handlers :refer [register-handler register-handler-fx] :as u]
|
[status-im.utils.handlers :refer [register-handler register-handler-fx] :as u]
|
||||||
status-im.chat.events
|
status-im.chat.events
|
||||||
status-im.chat.handlers.send-message
|
status-im.chat.handlers.send-message))
|
||||||
status-im.chat.handlers.webview-bridge))
|
|
||||||
|
|
||||||
(defn remove-chat
|
(defn remove-chat
|
||||||
[db [_ chat-id]]
|
[db [_ chat-id]]
|
||||||
|
@ -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)))))
|
|
@ -1,70 +1,63 @@
|
|||||||
(ns status-im.chat.views.input.input-actions
|
(ns status-im.chat.views.input.input-actions
|
||||||
(:require-macros [status-im.utils.views :refer [defview]])
|
(:require [re-frame.core :as re-frame]
|
||||||
(:require [clojure.string :as str]
|
|
||||||
[reagent.core :as r]
|
|
||||||
[re-frame.core :refer [subscribe dispatch]]
|
|
||||||
[taoensso.timbre :as log]
|
[taoensso.timbre :as log]
|
||||||
[status-im.ui.components.react :refer [view
|
[status-im.ui.components.react :as react]
|
||||||
text
|
|
||||||
icon
|
|
||||||
touchable-highlight]]
|
|
||||||
[status-im.chat.styles.input.input-actions :as style]))
|
[status-im.chat.styles.input.input-actions :as style]))
|
||||||
|
|
||||||
(defmulti action-view (fn [{:keys [type]}] (keyword type)))
|
(defmulti action-view (fn [{:keys [type]}] (keyword type)))
|
||||||
|
|
||||||
(defmethod action-view :fullscreen
|
(defmethod action-view :fullscreen
|
||||||
[_]
|
[_]
|
||||||
(let [fullscreen? (subscribe [:chat-ui-props :fullscreen?])]
|
(let [fullscreen? (re-frame/subscribe [:chat-ui-props :fullscreen?])]
|
||||||
(fn []
|
(fn []
|
||||||
[touchable-highlight
|
[react/touchable-highlight
|
||||||
{:on-press #(dispatch [:set-chat-ui-props {:fullscreen? (not @fullscreen?)}])}
|
{:on-press #(re-frame/dispatch [:set-chat-ui-props {:fullscreen? (not @fullscreen?)}])}
|
||||||
(if @fullscreen?
|
(if @fullscreen?
|
||||||
[view (style/action-view true)
|
[react/view (style/action-view true)
|
||||||
[icon :action_fullscreen_collapse style/action-view-icon]]
|
[react/icon :action_fullscreen_collapse style/action-view-icon]]
|
||||||
[view (style/action-view true)
|
[react/view (style/action-view true)
|
||||||
[icon :action_fullscreen_expand style/action-view-fullscreen-expand-icon]])])))
|
[react/icon :action_fullscreen_expand style/action-view-fullscreen-expand-icon]])])))
|
||||||
|
|
||||||
(defmethod action-view :web-view-back
|
(defmethod action-view :web-view-back
|
||||||
[_]
|
[_]
|
||||||
(let [result-box (subscribe [:chat-ui-props :result-box])
|
(let [result-box (re-frame/subscribe [:chat-ui-props :result-box])
|
||||||
webview (subscribe [:get :webview-bridge])]
|
webview (re-frame/subscribe [:get :webview-bridge])]
|
||||||
(fn []
|
(fn []
|
||||||
[touchable-highlight
|
[react/touchable-highlight
|
||||||
{:on-press #(.goBack @webview)}
|
{:on-press #(.goBack @webview)}
|
||||||
[view (style/action-view true)
|
[react/view (style/action-view true)
|
||||||
[icon :action_back style/action-view-icon-tinted]]]
|
[react/icon :action_back style/action-view-icon-tinted]]]
|
||||||
#_(if (:can-go-back? @result-box)
|
#_(if (:can-go-back? @result-box)
|
||||||
|
[react/view (style/action-view false)
|
||||||
[view (style/action-view false)
|
[react/icon :action_back style/action-view-icon]]))))
|
||||||
[icon :action_back style/action-view-icon]]))))
|
|
||||||
|
|
||||||
(defmethod action-view :web-view-forward
|
(defmethod action-view :web-view-forward
|
||||||
[_]
|
[_]
|
||||||
(let [result-box (subscribe [:chat-ui-props :result-box])
|
(let [result-box (re-frame/subscribe [:chat-ui-props :result-box])
|
||||||
webview (subscribe [:get :webview-bridge])]
|
webview (re-frame/subscribe [:get :webview-bridge])]
|
||||||
(fn []
|
(fn []
|
||||||
[touchable-highlight
|
[react/touchable-highlight
|
||||||
{:on-press #(.goForward @webview)}
|
{:on-press #(.goForward @webview)}
|
||||||
[view (style/action-view true)
|
[react/view (style/action-view true)
|
||||||
[icon :action_forward style/action-view-icon-tinted]]]
|
[react/icon :action_forward style/action-view-icon-tinted]]]
|
||||||
#_(if (:can-go-forward? @result-box)
|
#_(if (:can-go-forward? @result-box)
|
||||||
|
[react/view (style/action-view false)
|
||||||
[view (style/action-view false)
|
[react/icon :action_forward style/action-view-icon]]))))
|
||||||
[icon :action_forward style/action-view-icon]]))))
|
|
||||||
|
|
||||||
(defmethod action-view :default
|
(defmethod action-view :default
|
||||||
[{:keys [image executeJs]}]
|
[{:keys [image executeJs]}]
|
||||||
[touchable-highlight
|
[react/touchable-highlight
|
||||||
{:on-press #(dispatch [:send-to-webview-bridge {:event "actions-execute-js"
|
{:on-press #(re-frame/dispatch [:chat-webview-bridge/send-to-bridge
|
||||||
:js executeJs}])}
|
{:event "actions-execute-js"
|
||||||
[view (style/action-view true)
|
:js executeJs}])}
|
||||||
[icon (str "action_" image) style/action-view-icon]]])
|
[react/view (style/action-view true)
|
||||||
|
[react/icon (str "action_" image) style/action-view-icon]]])
|
||||||
|
|
||||||
(defn input-actions-view []
|
(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 []
|
(fn []
|
||||||
(let [{:keys [actions]} @result-box]
|
(let [{:keys [actions]} @result-box]
|
||||||
[view style/actions-container
|
[react/view style/actions-container
|
||||||
(for [{:keys [type] :as action} actions]
|
(for [{:keys [type] :as action} actions]
|
||||||
^{:key type}
|
^{:key type}
|
||||||
[action-view action])]))))
|
[action-view action])]))))
|
||||||
|
@ -35,8 +35,8 @@
|
|||||||
result-box [:chat-ui-props :result-box]]
|
result-box [:chat-ui-props :result-box]]
|
||||||
(when url
|
(when url
|
||||||
[webview-bridge
|
[webview-bridge
|
||||||
{:ref #(dispatch [:set-webview-bridge %])
|
{:ref #(dispatch [:chat-webview-bridge/set-ref %])
|
||||||
:on-bridge-message #(dispatch [:webview-bridge-message %])
|
:on-bridge-message #(dispatch [:chat-webview-bridge/process-message %])
|
||||||
:source {:uri url}
|
:source {:uri url}
|
||||||
:render-error web-view-error
|
:render-error web-view-error
|
||||||
:java-script-enabled true
|
:java-script-enabled true
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
:icon :icons/address
|
:icon :icons/address
|
||||||
:icon-opts {:color :blue}
|
:icon-opts {:color :blue}
|
||||||
:on-press #(do
|
:on-press #(do
|
||||||
(dispatch [:send-to-webview-bridge
|
(dispatch [:chat-webview-bridge/send-to-bridge
|
||||||
{:event (name :webview-send-transaction)}])
|
{:event (name :webview-send-transaction)}])
|
||||||
(dispatch [:navigate-back]))}]
|
(dispatch [:navigate-back]))}]
|
||||||
[action-separator]
|
[action-separator]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user