Send command

This commit is contained in:
virvar 2016-04-04 18:19:52 +03:00
parent 048be42aa8
commit 6f5deac5ee
7 changed files with 95 additions and 16 deletions

View File

@ -7,10 +7,12 @@
touchable-highlight
navigator
toolbar-android]]
[syng-im.models.commands :as commands]
[syng-im.utils.logging :as log]
[syng-im.navigation :refer [nav-pop]]
[syng-im.resources :as res]
[syng-im.constants :refer [text-content-type]]))
[syng-im.constants :refer [text-content-type
content-type-command]]))
(defn message-date [{:keys [date]}]
@ -60,6 +62,29 @@
:lineHeight 15}}
"03:39"]]])
(defn message-content-command [content]
(let [{:keys [command content]} (commands/parse-command-msg-content content)]
[view {:style {:flexDirection "column"}}
[view {:style {:margin 10
:backgroundColor (:color command)
:borderRadius 10}}
[text {:style {:marginTop -2
:marginHorizontal 10
:fontSize 14
:fontFamily "Avenir-Roman"
:color "white"}}
(:text command)]]
[text {:style {:marginTop -2
:marginHorizontal 10
:fontSize 14
:fontFamily "Avenir-Roman"
:color "black"}}
;; TODO isn't smart
(if (= (:command command) :keypair-password)
"******"
content)]]))
(defn message-content [{:keys [content-type content outgoing]}]
[view {:style (merge {:borderRadius 6}
(if (= content-type text-content-type)
@ -70,12 +95,15 @@
(if outgoing
{:backgroundColor "#D3EEEF"}
{:backgroundColor "#FBF6E3"}))}
(if (= content-type text-content-type)
(cond
(= content-type text-content-type)
[text {:style {:fontSize 14
:fontFamily "Avenir-Roman"
:color "#4A5258"}}
content]
[message-content-audio {:content content
(= content-type content-type-command)
[message-content-command content]
:else [message-content-audio {:content content
:content-type content-type}])])
(defn message-delivery-status [{:keys [delivery-status]}]

View File

@ -14,11 +14,16 @@
(defn cancel-command-input []
(dispatch [:set-input-command nil]))
(defn send-command [chat-id command text]
(dispatch [:send-chat-command chat-id (:command command) text])
(cancel-command-input))
(defn simple-command-input-view [command input-options]
(let [message-atom (r/atom nil)
chat-id (subscribe [:get-current-chat-id])]
chat-id-atom (subscribe [:get-current-chat-id])]
(fn []
(let [message @message-atom]
(let [message @message-atom
chat-id @chat-id-atom]
[view {:style {:flexDirection "row"}}
[view {:style {:flex 1
:flexDirection "column"
@ -60,9 +65,10 @@
:keyboardType "default"
:value message
:onChangeText (fn [new-text]
)
(reset! message-atom new-text))
:onSubmitEditing (fn [e]
)}
(send-command chat-id command message)
(reset! message-atom nil))}
input-options)]]]
[touchable-highlight {:style {:marginTop 14
:marginRight 16

View File

@ -6,3 +6,4 @@
;; (def server-address "http://10.0.3.2:3000/")
(def text-content-type "text/plain")
(def content-type-command "command")

View File

@ -103,6 +103,25 @@
(save-message chat-id msg)
(signal-chat-updated db chat-id))))
(register-handler :send-chat-command
(fn [db [action chat-id command content]]
(log/debug action "chat-id" chat-id "command" command "content" content)
(let [msg (if (= chat-id "console")
(sign-up-service/send-console-command command content)
;; TODO handle command, now sends as plain message
(let [{msg-id :msg-id
{from :from
to :to} :msg} (api/send-user-msg {:to chat-id
:content content})]
{:msg-id msg-id
:from from
:to to
:content content
:content-type text-content-type
:outgoing true}))]
(save-message chat-id msg)
(signal-chat-updated db chat-id))))
;; -- User data --------------------------------------------------------------
(register-handler :set-user-phone-number

View File

@ -1,11 +1,12 @@
(ns syng-im.handlers.sign-up
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.db :as db]
;; [syng-im.models.commands :refer [commands suggestions]]
[syng-im.models.commands :as commands]
[syng-im.utils.utils :refer [log on-error http-post toast]]
[syng-im.utils.logging :as log]
[syng-im.utils.random :as random]
[syng-im.constants :refer [text-content-type]]))
[syng-im.constants :refer [text-content-type
content-type-command]]))
(defn intro [db]
(dispatch [:received-msg {:msg-id "1"
@ -36,3 +37,12 @@
:content text
:content-type text-content-type
:outgoing true})
;; TODO store command key in a separate field
(defn send-console-command [command content]
{:msg-id (random/id)
:from "me"
:to "console"
:content (commands/format-command-msg-content command content)
:content-type content-type-command
:outgoing true})

View File

@ -1,5 +1,7 @@
(ns syng-im.models.commands
(:require [cljs.core.async :as async :refer [chan put! <! >!]]
(:require [clojure.string :refer [join split]]
[clojure.walk :refer [stringify-keys keywordize-keys]]
[cljs.core.async :as async :refer [chan put! <! >!]]
[re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.utils.utils :refer [log toast]]
[syng-im.persistence.realm :as realm]))
@ -44,3 +46,19 @@
(defn get-command [command-key]
(first (filter #(= command-key (:command %)) commands)))
(defn- map-to-str
[m]
(join ";" (map #(join "=" %) (stringify-keys m))))
(defn- str-to-map
[s]
(keywordize-keys (apply hash-map (split s #"[;=]"))))
;; TODO store command key in separate field
(defn format-command-msg-content [command content]
(map-to-str {:command (name command) :content content}))
;; TODO temp
(defn parse-command-msg-content [content]
(update (str-to-map content) :command #(get-command (keyword %))))

View File

@ -31,10 +31,7 @@
(register-sub :get-input-command
(fn [db _]
(reaction (get-in @db db/input-command-path))
;; (let [input-command-name ]
;; (reaction @input-command-name))
))
(reaction (get-in @db db/input-command-path))))
;; -- Chats list --------------------------------------------------------------