Move message and command input text to state

Former-commit-id: b27fdfe96c
This commit is contained in:
virvar 2016-04-05 17:00:49 +03:00
parent e143760b4a
commit 2d7ae7c478
12 changed files with 117 additions and 85 deletions

View File

@ -55,5 +55,6 @@
(dispatch [:initialize-protocol]) (dispatch [:initialize-protocol])
(dispatch [:load-user-phone-number]) (dispatch [:load-user-phone-number])
(dispatch [:load-syng-contacts]) (dispatch [:load-syng-contacts])
(dispatch [:set-sign-up-chat]) ;; TODO execute on first run only
;; (dispatch [:set-sign-up-chat])
(.registerComponent app-registry "SyngIm" #(r/reactify-component app-root))) (.registerComponent app-registry "SyngIm" #(r/reactify-component app-root)))

View File

@ -29,10 +29,10 @@
[default-command-input-view command])) [default-command-input-view command]))
(defn chat-message-new [] (defn chat-message-new []
(let [input-command-atom (subscribe [:get-input-command])] (let [command-atom (subscribe [:get-chat-command])]
(fn [] (fn []
(let [input-command @input-command-atom] (let [command @command-atom]
[view [view
(if input-command (if command
[special-input-view input-command] [special-input-view command]
[plain-message-input-view])])))) [plain-message-input-view])]))))

View File

@ -12,18 +12,21 @@
[reagent.core :as r])) [reagent.core :as r]))
(defn cancel-command-input [] (defn cancel-command-input []
(dispatch [:set-input-command nil])) (dispatch [:set-chat-command nil]))
(defn set-input-message [message]
(dispatch [:set-chat-command-content message]))
(defn send-command [chat-id command text] (defn send-command [chat-id command text]
(dispatch [:send-chat-command chat-id (:command command) text]) (dispatch [:send-chat-command chat-id (:command command) text])
(cancel-command-input)) (cancel-command-input))
(defn simple-command-input-view [command input-options] (defn simple-command-input-view [command input-options]
(let [message-atom (r/atom nil) (let [chat-id-atom (subscribe [:get-current-chat-id])
chat-id-atom (subscribe [:get-current-chat-id])] message-atom (subscribe [:get-chat-command-content])]
(fn [] (fn [command input-options]
(let [message @message-atom (let [chat-id @chat-id-atom
chat-id @chat-id-atom] message @message-atom]
[view {:style {:flexDirection "row"}} [view {:style {:flexDirection "row"}}
[view {:style {:flex 1 [view {:style {:flex 1
:flexDirection "column" :flexDirection "column"
@ -63,13 +66,13 @@
:underlineColorAndroid "transparent" :underlineColorAndroid "transparent"
:autoFocus true :autoFocus true
:keyboardType "default" :keyboardType "default"
:value message
:onChangeText (fn [new-text] :onChangeText (fn [new-text]
(reset! message-atom new-text)) (set-input-message new-text))
:onSubmitEditing (fn [e] :onSubmitEditing (fn [e]
(send-command chat-id command message) (send-command chat-id command message)
(reset! message-atom nil))} (set-input-message nil))}
input-options)]]] input-options)
message]]]
[touchable-highlight {:style {:marginTop 14 [touchable-highlight {:style {:marginTop 14
:marginRight 16 :marginRight 16
:position "absolute" :position "absolute"

View File

@ -10,50 +10,54 @@
[syng-im.resources :as res] [syng-im.resources :as res]
[reagent.core :as r])) [reagent.core :as r]))
(defn set-input-message [message]
(dispatch [:set-chat-input-text message]))
(defn plain-message-input-view [] (defn plain-message-input-view []
(let [text (r/atom "") (let [chat (subscribe [:get-current-chat])
chat (subscribe [:get-current-chat])] input-message-atom (subscribe [:get-chat-input-text])]
(dispatch [:generate-suggestions @text])
(fn [] (fn []
[view {:style {:flexDirection "column"}} (let [input-message @input-message-atom]
[suggestions-view] [view {:style {:flexDirection "column"}}
[view {:style {:flexDirection "row" [suggestions-view]
:margin 1 [view {:style {:flexDirection "row"
:height 40 :margin 1
:backgroundColor "white" :height 40
:borderRadius 5}} :backgroundColor "white"
[image {:source res/mic :borderRadius 5}}
:style {:marginTop 11 [image {:source res/mic
:marginLeft 14 :style {:marginTop 11
:width 13 :marginLeft 14
:height 20}}] :width 13
[text-input {:underlineColorAndroid "transparent" :height 20}}]
:style {:flex 1 [text-input {:underlineColorAndroid "transparent"
:marginLeft 18 :style {:flex 1
:lineHeight 42 :marginLeft 18
:fontSize 14 :lineHeight 42
:fontFamily "Avenir-Roman" :fontSize 14
:color "#9CBFC0"} :fontFamily "Avenir-Roman"
:autoFocus true :color "#9CBFC0"}
:placeholder "Type your message here" :autoFocus true
:value @text :placeholder "Type your message here"
:onChangeText (fn [new-text] :onChangeText (fn [new-text]
(dispatch [:generate-suggestions new-text]) (set-input-message new-text))
(reset! text new-text) :onSubmitEditing (fn [e]
(r/flush)) (let [{:keys [group-chat chat-id]} @chat]
:onSubmitEditing (fn [e] ;; TODO get text from state?
(let [{:keys [group-chat chat-id]} @chat] (if group-chat
(if group-chat (dispatch [:send-group-chat-msg chat-id
(dispatch [:send-group-chat-msg chat-id @text]) input-message])
(dispatch [:send-chat-msg chat-id @text]))) (dispatch [:send-chat-msg chat-id
(reset! text nil))}] input-message])))
[image {:source res/smile (set-input-message nil))}
:style {:marginTop 11 input-message]
:marginRight 12 [image {:source res/smile
:width 18 :style {:marginTop 11
:height 18}}] :marginRight 12
[image {:source res/att :width 18
:style {:marginTop 14 :height 18}}]
:marginRight 16 [image {:source res/att
:width 17 :style {:marginTop 14
:height 14}}]]]))) :marginRight 16
:width 17
:height 14}}]]]))))

View File

@ -15,7 +15,7 @@
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
(defn set-command-input [command] (defn set-command-input [command]
(dispatch [:set-input-command command])) (dispatch [:set-chat-command command]))
(defn suggestion-list-item [suggestion] (defn suggestion-list-item [suggestion]
[touchable-highlight {:onPress (fn [] [touchable-highlight {:onPress (fn []

View File

@ -9,7 +9,6 @@
:identity-password "replace-me-with-user-entered-password" :identity-password "replace-me-with-user-entered-password"
:contacts [] :contacts []
:chat {:current-chat-id "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd" :chat {:current-chat-id "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd"
:suggestions []
:command nil} :command nil}
:chats {} :chats {}
:chats-updated-signal 0 :chats-updated-signal 0
@ -19,10 +18,13 @@
(def protocol-initialized-path [:protocol-initialized]) (def protocol-initialized-path [:protocol-initialized])
(def identity-password-path [:identity-password]) (def identity-password-path [:identity-password])
(def current-chat-id-path [:chat :current-chat-id]) (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]) (def updated-chats-signal-path [:chats-updated-signal])
(defn updated-chat-signal-path [chat-id] (defn updated-chat-signal-path [chat-id]
[:chats chat-id :chat-updated-signal]) [:chats chat-id :chat-updated-signal])
(defn chat-input-text-path [chat-id]
[:chats chat-id :input-text])
(defn chat-command-path [chat-id]
[:chats chat-id :command-input :command])
(defn chat-command-content-path [chat-id]
[:chats chat-id :command-input :content])
(def new-group-path [:new-group]) (def new-group-path [:new-group])

View File

@ -14,8 +14,8 @@
message-by-id]] message-by-id]]
[syng-im.handlers.server :as server] [syng-im.handlers.server :as server]
[syng-im.handlers.contacts :as contacts-service] [syng-im.handlers.contacts :as contacts-service]
[syng-im.handlers.suggestions :as suggestions-service] [syng-im.handlers.commands :refer [set-chat-command
[syng-im.handlers.commands :as commands-service] set-chat-command-content]]
[syng-im.handlers.sign-up :as sign-up-service] [syng-im.handlers.sign-up :as sign-up-service]
[syng-im.models.chats :refer [create-chat]] [syng-im.models.chats :refer [create-chat]]
@ -23,7 +23,8 @@
set-current-chat-id set-current-chat-id
update-new-group-selection update-new-group-selection
clear-new-group clear-new-group
new-group-selection]] new-group-selection
set-chat-input-text]]
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
[syng-im.protocol.api :as api] [syng-im.protocol.api :as api]
[syng-im.constants :refer [text-content-type]] [syng-im.constants :refer [text-content-type]]
@ -229,13 +230,17 @@
;; -- Chat -------------------------------------------------------------- ;; -- Chat --------------------------------------------------------------
(register-handler :generate-suggestions (register-handler :set-chat-input-text
(fn [db [_ text]] (fn [db [_ text]]
(suggestions-service/generate-suggestions db text))) (set-chat-input-text db text)))
(register-handler :set-input-command (register-handler :set-chat-command
(fn [db [_ command]] (fn [db [_ command-key]]
(commands-service/set-input-command db command))) (set-chat-command db command-key)))
(register-handler :set-chat-command-content
(fn [db [_ content]]
(set-chat-command-content db content)))
(register-handler :show-contacts (register-handler :show-contacts
(fn [db [action navigator]] (fn [db [action navigator]]

View File

@ -5,5 +5,12 @@
[syng-im.utils.utils :refer [log on-error http-post]] [syng-im.utils.utils :refer [log on-error http-post]]
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
(defn set-input-command [db command] (defn set-chat-command-content [db content]
(assoc-in db db/input-command-path (get-command command))) (assoc-in db (db/chat-command-content-path (get-in db db/current-chat-id-path))
content))
(defn set-chat-command [db command-key]
(-> db
(set-chat-command-content nil)
(assoc-in (db/chat-command-path (get-in db db/current-chat-id-path))
(get-command command-key))))

View File

@ -27,7 +27,7 @@
:outgoing false :outgoing false
:from "console" :from "console"
:to "me"}]) :to "me"}])
(dispatch [:set-input-command :keypair-password]) (dispatch [:set-chat-command :keypair-password])
db) db)
(defn send-console-msg [text] (defn send-console-msg [text]

View File

@ -1,6 +1,7 @@
(ns syng-im.handlers.suggestions (ns syng-im.handlers.suggestions
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.models.chat :refer [current-chat-id]]
[syng-im.models.commands :refer [commands suggestions]] [syng-im.models.commands :refer [commands suggestions]]
[syng-im.utils.utils :refer [log on-error http-post]] [syng-im.utils.utils :refer [log on-error http-post]]
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
@ -10,6 +11,3 @@
;; TODO change 'commands' to 'suggestions' ;; TODO change 'commands' to 'suggestions'
(filterv #(.startsWith (:text %) text) commands) (filterv #(.startsWith (:text %) text) commands)
[])) []))
(defn generate-suggestions [db text]
(assoc-in db db/input-suggestions-path (get-suggestions text)))

View File

@ -29,10 +29,13 @@
(defn clear-new-group [db] (defn clear-new-group [db]
(assoc-in db db/new-group-path #{})) (assoc-in db db/new-group-path #{}))
(defn set-chat-input-text [db text]
(assoc-in db (db/chat-input-text-path (current-chat-id db)) text))
(comment (comment
(swap! re-frame.db/app-db (fn [db] (swap! re-frame.db/app-db (fn [db]
(signal-chat-updated db "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd"))) (signal-chat-updated db "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd")))
(current-chat-id @re-frame.db/app-db) (current-chat-id @re-frame.db/app-db)
) )

View File

@ -8,7 +8,8 @@
chats-updated? chats-updated?
chat-by-id]] chat-by-id]]
[syng-im.models.messages :refer [get-messages]] [syng-im.models.messages :refer [get-messages]]
[syng-im.models.contacts :refer [contacts-list]])) [syng-im.models.contacts :refer [contacts-list]]
[syng-im.handlers.suggestions :refer [get-suggestions]]))
;; -- Chat -------------------------------------------------------------- ;; -- Chat --------------------------------------------------------------
@ -29,11 +30,20 @@
(register-sub :get-suggestions (register-sub :get-suggestions
(fn [db _] (fn [db _]
(reaction (get-in @db db/input-suggestions-path)))) (let [input-text (reaction (get-in @db (db/chat-input-text-path (current-chat-id @db))))]
(reaction (get-suggestions @input-text)))))
(register-sub :get-input-command (register-sub :get-chat-input-text
(fn [db _] (fn [db _]
(reaction (get-in @db db/input-command-path)))) (reaction (get-in @db (db/chat-input-text-path (current-chat-id @db))))))
(register-sub :get-chat-command
(fn [db _]
(reaction (get-in @db (db/chat-command-path (current-chat-id @db))))))
(register-sub :get-chat-command-content
(fn [db _]
(reaction (get-in @db (db/chat-command-content-path (current-chat-id @db))))))
;; -- Chats list -------------------------------------------------------------- ;; -- Chats list --------------------------------------------------------------
@ -89,4 +99,3 @@
(fn [db _] (fn [db _]
(reaction (reaction
(contacts-list)))) (contacts-list))))