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 [:load-user-phone-number])
(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)))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,5 +5,12 @@
[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 (get-command command)))
(defn set-chat-command-content [db content]
(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
:from "console"
:to "me"}])
(dispatch [:set-input-command :keypair-password])
(dispatch [:set-chat-command :keypair-password])
db)
(defn send-console-msg [text]

View File

@ -1,6 +1,7 @@
(ns syng-im.handlers.suggestions
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.db :as db]
[syng-im.models.chat :refer [current-chat-id]]
[syng-im.models.commands :refer [commands suggestions]]
[syng-im.utils.utils :refer [log on-error http-post]]
[syng-im.utils.logging :as log]))
@ -10,6 +11,3 @@
;; TODO change 'commands' to 'suggestions'
(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]
(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
(swap! re-frame.db/app-db (fn [db]
(signal-chat-updated db "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd")))
(current-chat-id @re-frame.db/app-db)
)
)

View File

@ -8,7 +8,8 @@
chats-updated?
chat-by-id]]
[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 --------------------------------------------------------------
@ -29,11 +30,20 @@
(register-sub :get-suggestions
(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 _]
(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 --------------------------------------------------------------
@ -89,4 +99,3 @@
(fn [db _]
(reaction
(contacts-list))))