From 6299e7d686a38c8bc30f1c13bcdeffeace3bef51 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 20 Jun 2016 21:15:05 +0300 Subject: [PATCH] some refactoring --- src/status_im/chat/subs.cljs | 18 ++++- src/status_im/chat/views/message_input.cljs | 90 +++++++++------------ src/status_im/chat/views/plain_message.cljs | 13 +-- 3 files changed, 53 insertions(+), 68 deletions(-) diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 9b11d29fd0..4486741df5 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -1,13 +1,15 @@ (ns status-im.chat.subs (:require-macros [reagent.ratom :refer [reaction]]) - (:require [re-frame.core :refer [register-sub dispatch]] + (:require [re-frame.core :refer [register-sub dispatch subscribe]] [status-im.db :as db] ;todo handlers in subs?... [status-im.chat.suggestions :refer [get-suggestions typing-command?]] [status-im.models.commands :as commands] [status-im.constants :refer [response-suggesstion-resize-duration]] - [status-im.handlers.content-suggestions :refer [get-content-suggestions]])) + [status-im.handlers.content-suggestions :refer [get-content-suggestions]] + [status-im.chat.views.plain-message :as plain-message] + [status-im.chat.views.command :as command])) (register-sub :chat-properties (fn [db [_ properties]] @@ -63,6 +65,18 @@ (get-in @db) (reaction)))) +(register-sub :valid-plain-message? + (fn [_ _] + (let [input-message (subscribe [:get-chat-input-text]) + staged-commands (subscribe [:get-chat-staged-commands])] + (reaction + (plain-message/message-valid? @staged-commands @input-message))))) + +(register-sub :valid-command? + (fn [_ [_ validator]] + (let [input (subscribe [:get-chat-command-content])] + (reaction (command/valid? @input validator))))) + (register-sub :get-chat-command (fn [db _] (reaction (commands/get-chat-command @db)))) diff --git a/src/status_im/chat/views/message_input.cljs b/src/status_im/chat/views/message_input.cljs index 3dc34d8e1a..67310f8a77 100644 --- a/src/status_im/chat/views/message_input.cljs +++ b/src/status_im/chat/views/message_input.cljs @@ -11,10 +11,8 @@ [status-im.components.animation :as anim] [status-im.chat.views.plain-message :as plain-message] [status-im.chat.views.command :as command] - [status-im.chat.views.response :as response] [status-im.chat.styles.message-input :as st] [status-im.chat.styles.plain-message :as st-message] - [status-im.chat.styles.input :as st-command] [status-im.chat.styles.response :as st-response] [status-im.constants :refer [response-input-hiding-duration]])) @@ -36,58 +34,42 @@ (defn message-input-container [input] [view st/message-input-container input]) -(defview message-input [input-options validator] - [input-message [:get-chat-input-text] - command [:get-chat-command] - command? [:animations :command?] - to-msg-id [:get-chat-command-to-msg-id] - input-command [:get-chat-command-content] - staged-commands [:get-chat-staged-commands] - typing-command? [:typing-command?]] - (let [dismiss-keyboard (not (or command typing-command?))] - [text-input (merge {:style (if command? - st-response/command-input - st-message/message-input) - :ref #(dispatch [:set-message-input %]) - :autoFocus false - :blurOnSubmit dismiss-keyboard - :onChangeText (if command? - command/set-input-message - plain-message/set-input-message) - :onSubmitEditing #(if command? - (command/try-send input-command validator) - (plain-message/try-send staged-commands - input-message - dismiss-keyboard))} - (when command? - {:accessibility-label :command-input}) - input-options) - (if command? - input-command - input-message)])) +(defview message-input [input-options] + [command? [:animations :command?] + input-message [:get-chat-input-text] + input-command [:get-chat-command-content]] + [text-input (merge {:style (if command? + st-response/command-input + st-message/message-input) + :ref #(dispatch [:set-message-input %]) + :autoFocus false + :blurOnSubmit false + :onChangeText (if command? + command/set-input-message + plain-message/set-input-message) + :onSubmitEditing (if command? + command/send-command + plain-message/send)} + (when command? + {:accessibility-label :command-input}) + input-options) + (if command? input-command input-message)]) (defview plain-message-input-view [{:keys [input-options validator]}] - [input-message [:get-chat-input-text] - command [:get-chat-command] - command? [:animations :command?] - to-msg-id [:get-chat-command-to-msg-id] + [command? [:animations :command?] input-command [:get-chat-command-content] - staged-commands [:get-chat-staged-commands] - typing-command? [:typing-command?]] - (let [dismiss-keyboard (not (or command typing-command?))] - [view st/input-container - [view st/input-view - [plain-message/commands-button] - [message-input-container - [message-input input-options validator]] - ;; TODO emoticons: not implemented - [plain-message/smile-button] - (if-not command? - (when (plain-message/message-valid? staged-commands input-message) - [send-button {:on-press #(plain-message/try-send staged-commands - input-message - dismiss-keyboard) - :accessibility-label :send-message}]) - (when (command/valid? input-command validator) - [send-button {:on-press command/send-command - :accessibility-label :stage-command}]))]])) + valid-plain-message? [:valid-plain-message?] + valid-command? [:valid-command? validator]] + [view st/input-container + [view st/input-view + [plain-message/commands-button] + [message-input-container + [message-input input-options validator]] + ;; TODO emoticons: not implemented + [plain-message/smile-button] + (when (if command? valid-command? valid-plain-message?) + (let [on-press (if command? + command/send-command + plain-message/send)] + [send-button {:on-press on-press + :accessibility-label :send-message}]))]]) diff --git a/src/status_im/chat/views/plain_message.cljs b/src/status_im/chat/views/plain_message.cljs index b7ecef4e2c..7af02e7142 100644 --- a/src/status_im/chat/views/plain_message.cljs +++ b/src/status_im/chat/views/plain_message.cljs @@ -14,9 +14,7 @@ (defn set-input-message [message] (dispatch [:set-chat-input-text message])) -(defn send [dismiss-keyboard] - (when dismiss-keyboard - (dismiss-keyboard!)) +(defn send [] (dispatch [:send-chat-msg])) (defn message-valid? [staged-commands message] @@ -24,15 +22,6 @@ (not= "!" message)) (pos? (count staged-commands)))) -(defn try-send [staged-commands message dismiss-keyboard] - (when (message-valid? staged-commands message) - (send dismiss-keyboard))) - -(defn prepare-message-input [message-input] - (when message-input - (.clear message-input) - (.focus message-input))) - (defn button-animation-logic [{:keys [command? val]}] (fn [_] (let [to-scale (if @command? 0 1)]