From 471f637e0d420666dce3c807f011b25c371ab63b Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 29 Jun 2016 11:24:46 +0300 Subject: [PATCH] commands canceling --- src/status_im/chat/handlers.cljs | 85 ++++++++++++-------- src/status_im/chat/styles/input.cljs | 7 +- src/status_im/chat/styles/message_input.cljs | 2 +- src/status_im/chat/styles/response.cljs | 5 +- src/status_im/chat/subs.cljs | 31 +++++-- src/status_im/chat/views/command.cljs | 12 ++- src/status_im/chat/views/message_input.cljs | 23 +++--- src/status_im/chat/views/new_message.cljs | 5 +- src/status_im/chat/views/plain_message.cljs | 3 +- 9 files changed, 110 insertions(+), 63 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index db9f710d26..a5af15c8a1 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -45,33 +45,44 @@ (update-in [:chats current-chat-id :input-text] safe-trim)))) (defn invoke-suggestions-handler! - [{:keys [current-chat-id] :as db} _] - (let [{:keys [command content]} (get-in db [:chats current-chat-id :command-input]) - {:keys [name type]} command - path [(if (= :command type) :commands :responses) - name - :params - 0 - :suggestions] - params {:value content}] - (j/call current-chat-id - path - params - #(dispatch [:suggestions-handler {:command command - :content content - :chat-id current-chat-id} %])))) + [{:keys [current-chat-id canceled-command] :as db} _] + (when-not canceled-command + (let [{:keys [command content]} (get-in db [:chats current-chat-id :command-input]) + {:keys [name type]} command + path [(if (= :command type) :commands :responses) + name + :params + 0 + :suggestions] + params {:value content}] + (j/call current-chat-id + path + params + #(dispatch [:suggestions-handler {:command command + :content content + :chat-id current-chat-id} %]))))) (register-handler :start-cancel-command (u/side-effect! (fn [db _] (dispatch [:animate-cancel-command])))) +(def command-prefix "c ") + +(defn cancel-command! + [{:keys [canceled-command]}] + (when canceled-command + (dispatch [:start-cancel-command]))) + (register-handler :set-chat-command-content - [(after invoke-suggestions-handler!)] + [(after invoke-suggestions-handler!) + (after cancel-command!)] (fn [{:keys [current-chat-id] :as db} [_ content]] - (as-> db db - (commands/set-chat-command-content db content) - (assoc-in db [:chats current-chat-id :input-text] nil)))) + (let [starts-as-command? (str/starts-with? content command-prefix)] + (as-> db db + (commands/set-chat-command-content db content) + (assoc-in db [:chats current-chat-id :input-text] nil) + (assoc db :canceled-command (not starts-as-command?)))))) (defn update-input-text [{:keys [current-chat-id] :as db} text] @@ -81,10 +92,10 @@ [{:keys [current-chat-id staged-command]} _] (let [{:keys [command content]} staged-command {:keys [name type]} command - path [(if (= :command type) :commands :responses) - name - :preview] - params {:value content}] + path [(if (= :command type) :commands :responses) + name + :preview] + params {:value content}] (j/call current-chat-id path params @@ -93,7 +104,7 @@ (register-handler :stage-command (after invoke-command-preview!) (fn [{:keys [current-chat-id] :as db} _] - (let [db (update-input-text db nil) + (let [db (update-input-text db nil) {:keys [command content]} (get-in db [:chats current-chat-id :command-input]) command-info {:command command @@ -120,8 +131,12 @@ (commands/set-response-chat-command db to-msg-id command-key))) (defn update-text - [db [_ text]] - (update-input-text db text)) + [{:keys [current-chat-id] :as db} [_ text]] + (let [suggestions (get-in db [:command-suggestions current-chat-id])] + (println :hui (count suggestions) text :bla) + (if-not (= 1 (count suggestions)) + (update-input-text db text) + db))) (defn update-command [db [_ text]] (if-not (commands/get-chat-command db) @@ -145,9 +160,8 @@ (register-handler :set-chat-input-text [(enrich update-command) (after select-suggestion!) - (enrich check-suggestions) (after #(dispatch [:animate-command-suggestions]))] - update-text) + ((enrich update-text) check-suggestions)) (defn console? [s] (= "console" s)) @@ -187,7 +201,7 @@ (defn prepare-message [{:keys [identity current-chat-id] :as db} _] - (let [text (get-in db [:chats current-chat-id :input-text]) + (let [text (get-in db [:chats current-chat-id :input-text]) [command] (suggestions/check-suggestion db (str text " ")) message (check-author-direction db current-chat-id @@ -312,8 +326,11 @@ (register-handler :set-chat-command [(after invoke-suggestions-handler!) (after #(dispatch [:command-edit-mode]))] - (fn [db [_ command-key]] - (commands/set-chat-command db command-key))) + (fn [{:keys [current-chat-id] :as db} [_ command-key]] + (-> db + (commands/set-chat-command command-key) + (assoc-in [:chats current-chat-id :command-input :content] "c ") + (assoc :disable-input true)))) (register-handler :init-console-chat (fn [db [_]] @@ -406,9 +423,9 @@ (defmethod nav/preload-data! :chat [{:keys [current-chat-id] :as db} [_ _ id]] - (let [chat-id (or id current-chat-id) + (let [chat-id (or id current-chat-id) messages (get-in db [:chats chat-id :messages]) - db' (assoc db :current-chat-id chat-id)] + db' (assoc db :current-chat-id chat-id)] (if (seq messages) db' (-> db' @@ -499,8 +516,8 @@ (assoc-in db [:edit-mode current-chat-id] mode))) (register-handler :command-edit-mode - [(after #(dispatch [:set-chat-input-text ""]))] (edit-mode-handler :command)) (register-handler :text-edit-mode + (after #(dispatch [:set-chat-input-text ""])) (edit-mode-handler :text)) diff --git a/src/status_im/chat/styles/input.cljs b/src/status_im/chat/styles/input.cljs index 50f6fbfba7..6dd49b23be 100644 --- a/src/status_im/chat/styles/input.cljs +++ b/src/status_im/chat/styles/input.cljs @@ -16,12 +16,17 @@ :backgroundColor color-white :elevation 4}) +(def command-container + {:left 0 + :backgroundColor :white + :position :absolute}) + (defn command-text-container [{:keys [color]}] {:flexDirection :column :marginTop 16 :marginBottom 16 - :marginLeft 0 + :marginLeft 16 :marginRight 8 :backgroundColor color :height 24 diff --git a/src/status_im/chat/styles/message_input.cljs b/src/status_im/chat/styles/message_input.cljs index e999f749a9..c27a359646 100644 --- a/src/status_im/chat/styles/message_input.cljs +++ b/src/status_im/chat/styles/message_input.cljs @@ -4,7 +4,7 @@ [status-im.chat.constants :refer [input-height]])) (def message-input-container - {:flex 1 + {:flex 1 :marginRight 0}) (def input-container diff --git a/src/status_im/chat/styles/response.cljs b/src/status_im/chat/styles/response.cljs index 8963f8a1fe..a0a5c08cd5 100644 --- a/src/status_im/chat/styles/response.cljs +++ b/src/status_im/chat/styles/response.cljs @@ -83,11 +83,12 @@ :width 12 :height 12}) -(def command-input +(defn command-input [ml disbale?] {:flex 1 :marginRight 16 + :margin-left (- ml 5) :marginTop -2 :padding 0 :fontSize 14 :fontFamily font - :color text1-color}) + :color (if disbale? color-white text1-color)}) diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index d8c5b545cb..eee09009cf 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -127,16 +127,33 @@ (= :command) (reaction)))) +(register-sub :command-type + (fn [] + (let [command (subscribe [:get-chat-command])] + (reaction (:type @command))))) + (register-sub :messages-offset (fn [] (let [command? (subscribe [:command?]) - command (subscribe [:get-chat-command]) + type (subscribe [:command-type]) command-suggestions (subscribe [:get-content-suggestions]) suggestions (subscribe [:get-suggestions])] (reaction - (let [type (:type @command)] - (cond (and @command? (= type :response)) c/request-info-height - (and @command? (= type :command) (seq @command-suggestions)) - c/suggestions-header-height - (seq @suggestions) c/suggestions-header-height - :else 0)))))) + (cond (and @command? (= @type :response)) + c/request-info-height + + (and @command? (= @type :command) (seq @command-suggestions)) + c/suggestions-header-height + + (and (not @command?) (seq @suggestions)) + c/suggestions-header-height + + :else 0))))) + +(register-sub :command-icon-width + (fn [] + (let [width (subscribe [:get :command-icon-width]) + type (subscribe [:command-type])] + (reaction (if (= :command @type) + @width + 0))))) diff --git a/src/status_im/chat/views/command.cljs b/src/status_im/chat/views/command.cljs index c5f65f0130..49f5135b7e 100644 --- a/src/status_im/chat/views/command.cljs +++ b/src/status_im/chat/views/command.cljs @@ -27,9 +27,15 @@ (when (valid? message validator) (send-command))) -(defn command-icon [command] - [view (st/command-text-container command) - [text {:style st/command-text} (str "!" (:name command))]]) +(defview command-icon [command] + [icon-width [:get :command-icon-width]] + [view st/command-container + [view {:style (st/command-text-container command) + :onLayout (fn [event] + (let [width (.. event -nativeEvent -layout -width)] + (when (not= icon-width width) + (dispatch [:set :command-icon-width width]))))} + [text {:style st/command-text} (str "!" (:name command))]]]) (defn cancel-button [] [touchable-highlight {:on-press cancel-command-input} diff --git a/src/status_im/chat/views/message_input.cljs b/src/status_im/chat/views/message_input.cljs index fcf0ce6492..f9c5676ee5 100644 --- a/src/status_im/chat/views/message_input.cljs +++ b/src/status_im/chat/views/message_input.cljs @@ -21,24 +21,27 @@ (defn message-input-container [input] [view st/message-input-container input]) -(def plain-input-options +(defn plain-input-options [disbale?] {:style st-message/message-input - :onChangeText plain-message/set-input-message + :onChangeText (when-not disbale? plain-message/set-input-message) + :editable (not disbale?) :onSubmitEditing plain-message/send}) -(def command-input-options - {:style st-response/command-input +(defn command-input-options [icon-width disbale?] + {:style (st-response/command-input icon-width disbale?) :onChangeText command/set-input-message :onSubmitEditing command/send-command}) (defview message-input [input-options] [command? [:command?] input-message [:get-chat-input-text] - input-command [:get-chat-command-content]] + input-command [:get-chat-command-content] + icon-width [:command-icon-width] + disbale? [:get :disable-input]] [text-input (merge (if command? - command-input-options - plain-input-options) + (command-input-options icon-width disbale?) + (plain-input-options disbale?)) {:autoFocus false :blurOnSubmit false :accessibility-label :input} @@ -54,8 +57,6 @@ [view st/input-container [view st/input-view [plain-message/commands-button] - (when (and command? (= :command type)) - [command/command-icon command]) [message-input-container [message-input input-options validator]] ;; TODO emoticons: not implemented @@ -65,4 +66,6 @@ command/send-command plain-message/send)] [send-button {:on-press on-press - :accessibility-label :send-message}]))]]) + :accessibility-label :send-message}])) + (when (and command? (= :command type)) + [command/command-icon command])]]) diff --git a/src/status_im/chat/views/new_message.cljs b/src/status_im/chat/views/new_message.cljs index 2a1e2cd892..737a3f1344 100644 --- a/src/status_im/chat/views/new_message.cljs +++ b/src/status_im/chat/views/new_message.cljs @@ -24,10 +24,7 @@ :number {:input-options {:keyboardType :numeric}} ;; todo maybe nil is fine for now :) nil #_(throw (js/Error. "Uknown command type")))] - (println :plc placeholder) - (if placeholder - (assoc-in options [:input-options :placeholder] placeholder) - options))) + (assoc-in options [:input-options :placeholder] ""))) (defview show-input [] [parameter [:get-command-parameter] diff --git a/src/status_im/chat/views/plain_message.cljs b/src/status_im/chat/views/plain_message.cljs index 12ccadd664..86f06a7e6a 100644 --- a/src/status_im/chat/views/plain_message.cljs +++ b/src/status_im/chat/views/plain_message.cljs @@ -33,7 +33,8 @@ delay (if @command? 100 0)] (anim/start (anim/timing width {:toValue n-width :duration response-input-hiding-duration - :delay delay}))))) + :delay delay}) + #(dispatch [:set :disable-input false]))))) (defn commands-button [] (let [command? (subscribe [:command?])