From 735639776bf25268c7216025e31f6ba375d9b464 Mon Sep 17 00:00:00 2001 From: alwx Date: Fri, 21 Apr 2017 10:37:44 +0300 Subject: [PATCH] text-change optimisations (#971) --- src/status_im/chat/handlers.cljs | 11 +++--- src/status_im/chat/handlers/input.cljs | 22 ++++++------ src/status_im/chat/handlers/send_message.cljs | 4 +-- src/status_im/chat/screen.cljs | 6 ++-- src/status_im/chat/views/actions.cljs | 4 +-- src/status_im/chat/views/bottom_info.cljs | 2 +- src/status_im/chat/views/input/input.cljs | 35 +++++++++++-------- .../chat/views/input/result_box.cljs | 2 +- src/status_im/chat/views/input/web_view.cljs | 2 +- src/status_im/commands/handlers/jail.cljs | 2 +- src/status_im/protocol/web3/delivery.cljs | 2 +- src/status_im/transactions/handlers.cljs | 4 +-- 12 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index dbfe174f88..4afb2b1b18 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -47,9 +47,8 @@ (assoc db :layout-height height))) (register-handler :set-chat-ui-props - (fn [{:keys [current-chat-id] :as db} [_ ui-element value chat-id]] - (let [chat-id (or chat-id current-chat-id)] - (assoc-in db [:chat-ui-props chat-id ui-element] value)))) + (fn [{:keys [current-chat-id] :as db} [_ kvs]] + (update-in db [:chat-ui-props current-chat-id] merge kvs))) (register-handler :toggle-chat-ui-props (fn [{:keys [current-chat-id chat-ui-props] :as db} [_ ui-element chat-id]] @@ -59,9 +58,9 @@ (register-handler :show-message-details (u/side-effect! (fn [_ [_ details]] - (dispatch [:set-chat-ui-props :show-bottom-info? true]) - (dispatch [:set-chat-ui-props :show-emoji? false]) - (dispatch [:set-chat-ui-props :bottom-info details])))) + (dispatch [:set-chat-ui-props {:show-bottom-info? true + :show-emoji? false + :bottom-info details}])))) (register-handler :load-more-messages (fn [{:keys [current-chat-id loading-allowed] :as db} _] diff --git a/src/status_im/chat/handlers/input.cljs b/src/status_im/chat/handlers/input.cljs index 08663f7956..1c682eb950 100644 --- a/src/status_im/chat/handlers/input.cljs +++ b/src/status_im/chat/handlers/input.cljs @@ -45,9 +45,9 @@ (when-not sequential-params (input-model/join-command-args prefill)))]) (dispatch [:set-chat-input-metadata metadata]) - (dispatch [:set-chat-ui-props :show-suggestions? false]) - (dispatch [:set-chat-ui-props :result-box nil]) - (dispatch [:set-chat-ui-props :validation-messages nil]) + (dispatch [:set-chat-ui-props {:show-suggestions? false + :result-box nil + :validation-messages nil}]) (dispatch [:load-chat-parameter-box command 0]) (if sequential-params (js/setTimeout @@ -145,7 +145,7 @@ :address current-account-id}] (dispatch [:set-chat-input-text nil chat-id]) (dispatch [:set-chat-input-metadata nil chat-id]) - (dispatch [:set-chat-ui-props :sending-in-progress? false]) + (dispatch [:set-chat-ui-props {:sending-in-progress? false}]) (cond command-message (dispatch [:check-commands-handlers! data]) @@ -173,15 +173,15 @@ ::proceed-validation-messages (handlers/side-effect! (fn [db [_ command chat-id {:keys [markup validationHandler parameters] :as errors} proceed-fn]] - (let [set-errors #(do (dispatch [:set-chat-ui-props :validation-messages %]) - (dispatch [:set-chat-ui-props :sending-in-progress? false]))] + (let [set-errors #(do (dispatch [:set-chat-ui-props {:validation-messages % + :sending-in-progress? false}]))] (cond markup (set-errors markup) validationHandler (do (dispatch [::execute-validation-handler validationHandler parameters set-errors proceed-fn]) - (dispatch [:set-chat-ui-props :sending-in-progress? false])) + (dispatch [:set-chat-ui-props {:sending-in-progress? false}])) :default (proceed-fn)))))) @@ -201,8 +201,8 @@ (do (when fullscreen (dispatch [:choose-predefined-expandable-height :result-box :max])) - (dispatch [:set-chat-ui-props :result-box on-send]) - (dispatch [:set-chat-ui-props :sending-in-progress? false]) + (dispatch [:set-chat-ui-props {:result-box on-send + :sending-in-progress? false}]) (react-comp/dismiss-keyboard!)) (dispatch [::request-command-data {:command command @@ -241,7 +241,7 @@ :send-current-message (handlers/side-effect! (fn [{:keys [current-chat-id] :as db} [_ chat-id]] - (dispatch [:set-chat-ui-props :sending-in-progress? true]) + (dispatch [:set-chat-ui-props {:sending-in-progress? true}]) (let [chat-id (or chat-id current-chat-id) chat-command (input-model/selected-chat-command db chat-id) seq-command? (get-in chat-command [:command :sequential-params]) @@ -255,7 +255,7 @@ (dispatch [:proceed-command chat-command chat-id]) (dispatch [:clear-seq-arguments chat-id])) (let [text (get-in db [:chats chat-id :input-text])] - (dispatch [:set-chat-ui-props :sending-in-progress? false]) + (dispatch [:set-chat-ui-props {:sending-in-progress? false}]) (when-not (input-model/text-ends-with-space? text) (dispatch [:set-chat-input-text (str text const/spacing-char)])))) (dispatch [::send-message nil chat-id])))))) diff --git a/src/status_im/chat/handlers/send_message.cljs b/src/status_im/chat/handlers/send_message.cljs index bf4ce4e80f..337fbb4f30 100644 --- a/src/status_im/chat/handlers/send_message.cljs +++ b/src/status_im/chat/handlers/send_message.cljs @@ -62,7 +62,7 @@ :else (dispatch [:prepare-command! chat-id params'])))) - (dispatch [:set-chat-ui-props :sending-in-progress? false])))) + (dispatch [:set-chat-ui-props {:sending-in-progress? false}])))) (register-handler :prepare-command! (u/side-effect! @@ -78,7 +78,7 @@ (cu/check-author-direction db chat-id))] (log/debug "Handler data: " request handler-data (dissoc params :commands :command-message)) (dispatch [:update-message-overhead! chat-id network-status]) - (dispatch [:set-chat-ui-props :sending-in-progress? false]) + (dispatch [:set-chat-ui-props {:sending-in-progress? false}]) (dispatch [::send-command! add-to-chat-id (assoc params :command command') hidden-params]) (when (cu/console? chat-id) (dispatch [:console-respond-command params])) diff --git a/src/status_im/chat/screen.cljs b/src/status_im/chat/screen.cljs index 53f9386180..cab32f3b20 100644 --- a/src/status_im/chat/screen.cljs +++ b/src/status_im/chat/screen.cljs @@ -90,11 +90,11 @@ (fn [] (if @show-actions [touchable-highlight - {:on-press #(dispatch [:set-chat-ui-props :show-actions? false])} + {:on-press #(dispatch [:set-chat-ui-props {:show-actions? false}])} [view st/action [icon :up st/up-icon]]] [touchable-highlight - {:on-press #(dispatch [:set-chat-ui-props :show-actions? true])} + {:on-press #(dispatch [:set-chat-ui-props {:show-actions? true}])} [view st/action [chat-icon]]])))) @@ -184,7 +184,7 @@ layout-height [:get :layout-height] input-text [:chat :input-text]] {:component-did-mount #(dispatch [:check-autorun]) - :component-will-unmount #(dispatch [:set-chat-ui-props :show-emoji? false])} + :component-will-unmount #(dispatch [:set-chat-ui-props {:show-emoji? false}])} [view {:style st/chat-view :on-layout (fn [event] (let [height (.. event -nativeEvent -layout -height)] diff --git a/src/status_im/chat/views/actions.cljs b/src/status_im/chat/views/actions.cljs index ca6b7218bd..f7fc7705b4 100644 --- a/src/status_im/chat/views/actions.cljs +++ b/src/status_im/chat/views/actions.cljs @@ -95,7 +95,7 @@ subtitle] icon-name :icon}] [touchable-highlight {:on-press (fn [] - (dispatch [:set-chat-ui-props :show-actions? false]) + (dispatch [:set-chat-ui-props {:show-actions? false}]) (when handler (handler)))} [view st/action-icon-row @@ -132,5 +132,5 @@ ^{:key action} [action-view action]))]])))) (defn actions-view [] - [overlay {:on-click-outside #(dispatch [:set-chat-ui-props :show-actions? false])} + [overlay {:on-click-outside #(dispatch [:set-chat-ui-props {:show-actions? false}])} [actions-list-view]]) diff --git a/src/status_im/chat/views/bottom_info.cljs b/src/status_im/chat/views/bottom_info.cljs index 72aec7eaf4..59fd92c936 100644 --- a/src/status_im/chat/views/bottom_info.cljs +++ b/src/status_im/chat/views/bottom_info.cljs @@ -78,7 +78,7 @@ :status message-status}])) (into {})) statuses (vals (merge participants user-statuses))] - [overlay {:on-click-outside #(dispatch [:set-chat-ui-props :show-bottom-info? false])} + [overlay {:on-click-outside #(dispatch [:set-chat-ui-props {:show-bottom-info? false}])} [container (* st/item-height (count statuses)) [list-view {:dataSource (lw/to-datasource statuses) :enableEmptySections true diff --git a/src/status_im/chat/views/input/input.cljs b/src/status_im/chat/views/input/input.cljs index d8223d034e..c0f3b82a34 100644 --- a/src/status_im/chat/views/input/input.cljs +++ b/src/status_im/chat/views/input/input.cljs @@ -41,7 +41,7 @@ [view style/commands-root [view style/command-list-icon-container [touchable-highlight {:on-press #(do (dispatch [:toggle-chat-ui-props :show-suggestions?]) - (dispatch [:set-chat-ui-props :validation-messages nil]) + (dispatch [:set-chat-ui-props {:validation-messages nil}]) (dispatch [:update-suggestions]))} [view style/commands-list-icon (if show-suggestions? @@ -65,12 +65,13 @@ input-focused? (subscribe [:chat-ui-props :input-focused?])] (fn [{:keys [set-layout-height height]}] [text-input - {:ref #(dispatch [:set-chat-ui-props :input-ref %]) + {:ref #(when % + (dispatch [:set-chat-ui-props {:input-ref %}])) :accessibility-label id/chat-message-input :multiline true :default-value (or @input-text "") :editable (not @sending-in-progress?) - :on-blur #(do (dispatch [:set-chat-ui-props :input-focused? false]) + :on-blur #(do (dispatch [:set-chat-ui-props {:input-focused? false}]) (set-layout-height 0)) :on-content-size-change (when-not @input-focused? #(let [h (-> (.-nativeEvent %) @@ -82,18 +83,21 @@ (.-height))] (set-layout-height h)) :on-change-text #(do (dispatch [:set-chat-input-text %]) - (dispatch [:load-chat-parameter-box (:command @command)]) - (when (not @command) - (dispatch [:set-chat-input-metadata nil]) - (dispatch [:set-chat-ui-props :result-box nil])) - (dispatch [:set-chat-ui-props :validation-messages nil])) + (if @command + (do + (dispatch [:load-chat-parameter-box (:command @command)]) + (dispatch [:set-chat-ui-props {:validation-messages nil}])) + (do + (dispatch [:set-chat-input-metadata nil]) + (dispatch [:set-chat-ui-props {:result-box nil + :validation-messages nil}])))) :on-selection-change #(let [s (-> (.-nativeEvent %) (.-selection))] (when (and (= (.-end s) (+ 2 (count (get-in @command [:command :name])))) (get-in @command [:command :sequential-params])) (dispatch [:chat-input-focus :seq-input-ref]))) - :on-focus #(do (dispatch [:set-chat-ui-props :input-focused? true]) - (dispatch [:set-chat-ui-props :show-emoji? false])) + :on-focus #(dispatch [:set-chat-ui-props {:input-focused? true + :show-emoji? false}]) :style (style/input-view height) :placeholder-text-color style/color-input-helper-placeholder :auto-capitalize :sentences}]))) @@ -141,11 +145,11 @@ (fn [{:keys [command-width]}] (when (get-in @command [:command :sequential-params]) (let [{:keys [placeholder hidden type]} (get-in @command [:command :params @arg-pos])] - [text-input (merge {:ref #(dispatch [:set-chat-ui-props :seq-input-ref %]) + [text-input (merge {:ref #(dispatch [:set-chat-ui-props {:seq-input-ref %}]) :style (style/seq-input-text command-width) :default-value (or @seq-arg-input-text "") :on-change-text #(do (dispatch [:set-chat-seq-arg-input-text %]) - (dispatch [:set-chat-ui-props :validation-messages nil])) + (dispatch [:set-chat-ui-props {:validation-messages nil}])) :secure-text-entry hidden :placeholder placeholder :blur-on-submit false @@ -184,8 +188,8 @@ [touchable-highlight {:on-press #(do (dispatch [:set-chat-input-text nil]) (dispatch [:set-chat-input-metadata nil]) - (dispatch [:set-chat-ui-props :result-box nil]) - (dispatch [:set-chat-ui-props :validation-messages nil]) + (dispatch [:set-chat-ui-props {:result-box nil + :validation-messages nil}]) (dispatch [:clear-seq-arguments]))} [view style/input-clear-container [icon :close_gray style/input-clear-icon]]])]))}))) @@ -252,7 +256,8 @@ :on-layout #(let [h (-> (.-nativeEvent %) (.-layout) (.-height))] - (dispatch [:set-chat-ui-props :input-height h]))} + (when (> h 0) + (dispatch [:set-chat-ui-props {:input-height h}])))} [animated-view {:style (style/container container-anim-margin bottom-anim-margin)} (when (str/blank? @input-text) [commands-view]) diff --git a/src/status_im/chat/views/input/result_box.cljs b/src/status_im/chat/views/input/result_box.cljs index 17ba855990..e7f8b8e19e 100644 --- a/src/status_im/chat/views/input/result_box.cljs +++ b/src/status_im/chat/views/input/result_box.cljs @@ -20,7 +20,7 @@ :number-of-lines 1 :font :medium} title] - [touchable-highlight {:on-press #(dispatch [:set-chat-ui-props :result-box nil])} + [touchable-highlight {:on-press #(dispatch [:set-chat-ui-props {:result-box nil}])} [view style/header-close-container [icon :close_light_gray style/header-close-icon]]]]]) diff --git a/src/status_im/chat/views/input/web_view.cljs b/src/status_im/chat/views/input/web_view.cljs index cc1fb1e156..a60bbe4dba 100644 --- a/src/status_im/chat/views/input/web_view.cljs +++ b/src/status_im/chat/views/input/web_view.cljs @@ -16,7 +16,7 @@ (when-not loading (dispatch [:set-command-argument [0 url]])) (when (and dynamicTitle (not (str/blank? title))) - (dispatch [:set-chat-ui-props :result-box (assoc result-box :title title)]))))) + (dispatch [:set-chat-ui-props {:result-box (assoc result-box :title title)}]))))) (defn web-view-error [] (r/as-element diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 5535a7f59c..9f68fd6f8c 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -21,7 +21,7 @@ (cond handler-error (when-let [markup (:markup handler-error)] - (dispatch [:set-chat-ui-props :validation-messages (cu/generate-hiccup markup)])) + (dispatch [:set-chat-ui-props {:validation-messages (cu/generate-hiccup markup)}])) result (let [command' (assoc command-message :handler-data returned) diff --git a/src/status_im/protocol/web3/delivery.cljs b/src/status_im/protocol/web3/delivery.cljs index d5c4b7dd4e..98c4df176e 100644 --- a/src/status_im/protocol/web3/delivery.cljs +++ b/src/status_im/protocol/web3/delivery.cljs @@ -77,7 +77,7 @@ [web3 {:keys [message-id to] :as pending-message}] {:pre [(valid? :delivery/pending-message pending-message)]} (debug :add-prepeared-pending-message!) - (let [message (select-keys pending-message [:from :to :topics :payload]) + (let [message (select-keys pending-message [:from :to :topics :payload]) pending-message' (assoc pending-message :message message :id message-id)] (swap! messages assoc-in [web3 message-id to] pending-message') diff --git a/src/status_im/transactions/handlers.cljs b/src/status_im/transactions/handlers.cljs index 6b673c2081..388c0e457b 100644 --- a/src/status_im/transactions/handlers.cljs +++ b/src/status_im/transactions/handlers.cljs @@ -205,10 +205,10 @@ (dispatch [::remove-pending-message message_id])) (dispatch [:clear-selected-transaction]) (dispatch [::remove-transaction id]) - (dispatch [:set-chat-ui-props :validation-messages error_message])) + (dispatch [:set-chat-ui-props {:validation-messages error_message}])) :else - (dispatch [:set-chat-ui-props :validation-messages nil]))))) + (dispatch [:set-chat-ui-props {:validation-messages nil}]))))) (register-handler :clear-selected-transaction (fn [db _]