From 2493000dfb8fb729928f9497125a8a5f4bbcb4f4 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sun, 26 Jun 2016 12:11:23 +0300 Subject: [PATCH] autoselect command Former-commit-id: 7f156a69cf6b28e43bf8e5de28cc0a8f28cfce1a --- src/status_im/chat/handlers.cljs | 13 +++++-- src/status_im/chat/handlers/animation.cljs | 45 +++++++++++----------- src/status_im/chat/subs.cljs | 10 +---- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index b7ff184891..cca8759284 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -134,12 +134,18 @@ (defn check-suggestions [{:keys [current-chat-id] :as db} [_ text]] - (assoc-in db - [:command-suggestions current-chat-id] - (suggestions/get-suggestions db text))) + (let [suggestions (suggestions/get-suggestions db text)] + (assoc-in db [:command-suggestions current-chat-id] suggestions))) + +(defn select-suggestion! + [{:keys [current-chat-id] :as db} [_ text]] + (let [suggestions (get-in db [:command-suggestions current-chat-id])] + (when (= 1 (count suggestions)) + (dispatch [:set-chat-command (ffirst suggestions)])))) (register-handler :set-chat-input-text [(enrich update-command) + (after select-suggestion!) (enrich check-suggestions) (after #(dispatch [:animate-command-suggestions]))] update-text) @@ -497,6 +503,7 @@ (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 diff --git a/src/status_im/chat/handlers/animation.cljs b/src/status_im/chat/handlers/animation.cljs index b3faa364c4..4dd4af37dc 100644 --- a/src/status_im/chat/handlers/animation.cljs +++ b/src/status_im/chat/handlers/animation.cljs @@ -16,11 +16,11 @@ (register-handler name [(path :animations) middleware] handler))) (animation-handler :animate-cancel-command - (after #(dispatch [:text-edit-mode])) - (fn [db _] - (assoc db - :to-response-height input-height - :messages-offset? false))) + (after #(dispatch [:text-edit-mode])) + (fn [db _] + (assoc db + :to-response-height input-height + :messages-offset? false))) (def response-height (+ input-height response-height-normal)) @@ -30,8 +30,8 @@ (register-handler :animate-command-suggestions (fn [{:keys [current-chat-id] :as db} _] (let [suggestions? (seq (get-in db [:command-suggestions current-chat-id])) - current (get-in db [:animations :command-suggestions-height]) - height (if suggestions? middle-height 0.1)] + current (get-in db [:animations :command-suggestions-height]) + height (if suggestions? middle-height 0.1)] (-> db (update :animations assoc :messages-offset? suggestions? @@ -53,27 +53,26 @@ (defn fix-height [height-key height-signal-key suggestions-key minimum] (fn [{:keys [current-chat-id] :as db} [_ vy current]] - (let [max-height (get-in db [:layout-height]) - moving-down? (pos? vy) - moving-up? (not moving-down?) + (let [max-height (get-in db [:layout-height]) + moving-down? (pos? vy) + moving-up? (not moving-down?) under-middle-position? (<= current middle-height) - over-middle-position? (not under-middle-position?) - suggestions (get-in db [suggestions-key current-chat-id]) - new-fixed (cond (not suggestions) - minimum + over-middle-position? (not under-middle-position?) + suggestions (get-in db [suggestions-key current-chat-id]) + new-fixed (cond (not suggestions) + minimum - (and under-middle-position? moving-up?) - middle-height + (and under-middle-position? moving-up?) + middle-height - (and over-middle-position? moving-down?) - middle-height + (and over-middle-position? moving-down?) + middle-height - (and over-middle-position? moving-up?) - max-height + (and over-middle-position? moving-up?) + max-height - (and under-middle-position? - moving-down?) - minimum)] + (and under-middle-position? moving-down?) + minimum)] (-> db (assoc-in [:animations height-key] new-fixed) (update-in [:animations height-signal-key] inc))))) diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 4d197b1aad..0c878b8057 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -1,15 +1,11 @@ (ns status-im.chat.subs (:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [register-sub dispatch subscribe path]] - [status-im.db :as db] - [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.chat.views.plain-message :as plain-message] - [status-im.chat.views.command :as command] - [status-im.chat.constants :as c])) + [status-im.chat.views.command :as command])) (register-sub :chat-properties (fn [db [_ properties]] @@ -111,10 +107,6 @@ (fn [db [_ chat-id]] (reaction (get-in @db [:chats chat-id])))) -(register-sub :typing-command? - (fn [db _] - (reaction (typing-command? @db)))) - (register-sub :get-content-suggestions (fn [db _] (reaction (get-in @db [:suggestions (:current-chat-id @db)]))))