commands canceling

This commit is contained in:
Roman Volosovskyi 2016-06-29 11:24:46 +03:00
parent 026b678e20
commit 471f637e0d
9 changed files with 110 additions and 63 deletions

View File

@ -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))

View File

@ -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

View File

@ -4,7 +4,7 @@
[status-im.chat.constants :refer [input-height]]))
(def message-input-container
{:flex 1
{:flex 1
:marginRight 0})
(def input-container

View File

@ -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)})

View File

@ -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)))))

View File

@ -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}

View File

@ -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])]])

View File

@ -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]

View File

@ -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?])