From fddf80b126662576c953e70caa1446121fb9286f Mon Sep 17 00:00:00 2001 From: virvar Date: Wed, 30 Mar 2016 16:40:08 +0300 Subject: [PATCH] Start special (command) input Former-commit-id: 21178a625dccd0f771c794243d6ce12a56e20e85 --- .../syng_im/components/chat/suggestions.cljs | 63 +++++++++++-------- .../syng_im/components/chat_message_new.cljs | 33 +++++++++- syng-im/src/syng_im/db.cljs | 4 +- syng-im/src/syng_im/handlers.cljs | 5 ++ syng-im/src/syng_im/handlers/commands.cljs | 8 +++ syng-im/src/syng_im/handlers/suggestions.cljs | 21 +++++-- syng-im/src/syng_im/subs.cljs | 4 ++ 7 files changed, 104 insertions(+), 34 deletions(-) create mode 100644 syng-im/src/syng_im/handlers/commands.cljs diff --git a/syng-im/src/syng_im/components/chat/suggestions.cljs b/syng-im/src/syng_im/components/chat/suggestions.cljs index b68ed2b9cb..494772249e 100644 --- a/syng-im/src/syng_im/components/chat/suggestions.cljs +++ b/syng-im/src/syng_im/components/chat/suggestions.cljs @@ -7,41 +7,54 @@ [syng-im.components.react :refer [view image text + touchable-highlight list-view list-item]] [syng-im.utils.listview :refer [to-datasource]] [syng-im.utils.utils :refer [log toast http-post]] [syng-im.utils.logging :as log])) +(defn set-command-input [command] + (dispatch [:set-input-command command])) + (defn suggestion-list-item [suggestion] - [view {:style {:flexDirection "row" - :marginVertical 5 - :marginHorizontal 10 - :height 20 - ;; :backgroundColor "white" - }} - [text {:underlineColorAndroid "#9CBFC0" - :style {:flex 1 - :marginLeft 18 - :lineHeight 18 - :fontSize 14 - :fontFamily "Avenir-Roman" - :color "#9CBFC0"}} - (:text suggestion)]]) + [touchable-highlight {:onPress (fn [] + (set-command-input (keyword (:command suggestion))))} + [view {:style {:flexDirection "row" + :marginVertical 5 + :marginHorizontal 10 + :height 20 + ;; :backgroundColor "white" + }} + [text {:style {:flex 1 + :marginLeft 18 + :lineHeight 18 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "#9CBFC0"}} + (:text suggestion)] + [text {:style {:flex 1 + :marginLeft 18 + :lineHeight 18 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "#9CBFC0"}} + (:description suggestion)]]]) (defn render-row [row section-id row-id] (list-item [suggestion-list-item (js->clj row :keywordize-keys true)])) (defn suggestions-view [] - (let [suggestions (subscribe [:get-suggestions])] + (let [suggestions-atom (subscribe [:get-suggestions])] (fn [] - (when @suggestions - [view {:style {:flexDirection "row" - :marginVertical 5 - :marginHorizontal 10 - :height 120 - :backgroundColor "#E5F5F6" - :borderRadius 5}} - [list-view {:dataSource (to-datasource @suggestions) - :renderRow render-row - :style {}}]])))) + (let [suggestions @suggestions-atom] + (when (not (empty? suggestions)) + [view {:style {:flexDirection "row" + :marginVertical 5 + :marginHorizontal 10 + :height 120 + :backgroundColor "#E5F5F6" + :borderRadius 5}} + [list-view {:dataSource (to-datasource suggestions) + :renderRow render-row + :style {}}]]))))) diff --git a/syng-im/src/syng_im/components/chat_message_new.cljs b/syng-im/src/syng_im/components/chat_message_new.cljs index c4c7741c44..996458066a 100644 --- a/syng-im/src/syng_im/components/chat_message_new.cljs +++ b/syng-im/src/syng_im/components/chat_message_new.cljs @@ -5,12 +5,12 @@ image text-input]] [syng-im.components.chat.suggestions :refer [suggestions-view]] + [syng-im.utils.utils :refer [log toast http-post]] [syng-im.utils.logging :as log] [syng-im.resources :as res] [reagent.core :as r])) - -(defn chat-message-new [] +(defn message-input [] (let [text (r/atom nil) chat-id (subscribe [:get-current-chat-id])] (fn [] @@ -53,3 +53,32 @@ :width 17 :height 14}}]] [suggestions-view]]))) + +(defn special-input [command] + (case command + :phone [text-input {:underlineColorAndroid "#9CBFC0" + :style {:flex 1 + :marginLeft 18 + :lineHeight 42 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "#9CBFC0"} + :autoFocus true + :placeholder "Phone input"}] + [text-input {:underlineColorAndroid "#9CBFC0" + :style {:flex 1 + :marginLeft 18 + :lineHeight 42 + :fontSize 14 + :fontFamily "Avenir-Roman" + :color "#9CBFC0"} + :autoFocus true + :placeholder "Command input"}])) + +(defn chat-message-new [] + (let [input-command-atom (subscribe [:get-input-command])] + (fn [] + (let [input-command @input-command-atom] + (if input-command + [special-input input-command] + [message-input]))))) diff --git a/syng-im/src/syng_im/db.cljs b/syng-im/src/syng_im/db.cljs index a2e39531b0..90b768fe80 100644 --- a/syng-im/src/syng_im/db.cljs +++ b/syng-im/src/syng_im/db.cljs @@ -9,7 +9,8 @@ :identity-password "replace-me-with-user-entered-password" :contacts [] :chat {:current-chat-id "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd" - :suggestions []} + :suggestions [] + :command nil} :chats {} :chats-updated-signal 0}) @@ -18,6 +19,7 @@ (def identity-password-path [:identity-password]) (def current-chat-id-path [:chat :current-chat-id]) (def input-suggestions-path [:chat :suggestions]) +(def input-command-path [:chat :command]) (def updated-chats-signal-path [:chats-updated-signal]) (defn updated-chat-signal-path [chat-id] [:chats chat-id :chat-updated-signal]) diff --git a/syng-im/src/syng_im/handlers.cljs b/syng-im/src/syng_im/handlers.cljs index 4d99195b88..8c2f38f1a5 100644 --- a/syng-im/src/syng_im/handlers.cljs +++ b/syng-im/src/syng_im/handlers.cljs @@ -15,6 +15,7 @@ [syng-im.handlers.server :as server] [syng-im.handlers.contacts :as contacts-service] [syng-im.handlers.suggestions :as suggestions-service] + [syng-im.handlers.commands :as commands-service] [syng-im.models.chats :refer [create-chat]] [syng-im.models.chat :refer [signal-chat-updated @@ -150,3 +151,7 @@ (register-handler :generate-suggestions (fn [db [_ text]] (suggestions-service/generate-suggestions db text))) + +(register-handler :set-input-command + (fn [db [_ command]] + (commands-service/set-input-command db command))) diff --git a/syng-im/src/syng_im/handlers/commands.cljs b/syng-im/src/syng_im/handlers/commands.cljs new file mode 100644 index 0000000000..83b4f3b8a9 --- /dev/null +++ b/syng-im/src/syng_im/handlers/commands.cljs @@ -0,0 +1,8 @@ +(ns syng-im.handlers.commands + (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] + [syng-im.db :as db] + [syng-im.utils.utils :refer [log on-error http-post]] + [syng-im.utils.logging :as log])) + +(defn set-input-command [db command] + (assoc-in db db/input-command-path command)) diff --git a/syng-im/src/syng_im/handlers/suggestions.cljs b/syng-im/src/syng_im/handlers/suggestions.cljs index 85e9cc3879..19ec9c1256 100644 --- a/syng-im/src/syng_im/handlers/suggestions.cljs +++ b/syng-im/src/syng_im/handlers/suggestions.cljs @@ -4,14 +4,23 @@ [syng-im.utils.utils :refer [log on-error http-post]] [syng-im.utils.logging :as log])) -(def commands [{:text "!phone"} - {:text "!send"} - {:text "!request"} - {:text "!help"}]) +(def commands [{:command :phone + :text "!phone" + :description "Send phone number"} + {:command :send + :text "!send" + :description "Send location"} + {:command :request + :text "!request" + :description "Send request"} + {:command :help + :text "!help" + :description "Help"}]) (defn get-suggestions [text] - (when (= (get text 0) "!") - (filterv #(.startsWith (:text %) text) commands))) + (if (= (get text 0) "!") + (filterv #(.startsWith (:text %) text) commands) + [])) (defn generate-suggestions [db text] (assoc-in db db/input-suggestions-path (get-suggestions text))) diff --git a/syng-im/src/syng_im/subs.cljs b/syng-im/src/syng_im/subs.cljs index dcf6fd4f74..0d548c485b 100644 --- a/syng-im/src/syng_im/subs.cljs +++ b/syng-im/src/syng_im/subs.cljs @@ -29,6 +29,10 @@ (fn [db _] (reaction (get-in @db db/input-suggestions-path)))) +(register-sub :get-input-command + (fn [db _] + (reaction (get-in @db db/input-command-path)))) + ;; -- Chats list -------------------------------------------------------------- (register-sub :get-chats