From 95817924d60e7b0b0086079f768dd26f911ed879 Mon Sep 17 00:00:00 2001 From: michaelr Date: Wed, 23 Mar 2016 12:46:41 +0200 Subject: [PATCH] changed navigation method, send new message Former-commit-id: 77cae2cc42dfc62627de16ed540514f926d6218d --- .re-natal | 2 +- package.json | 2 +- src/messenger/android/core.cljs | 17 +++-- src/messenger/android/login.cljs | 12 +-- src/messenger/android/sign_up_confirm.cljs | 12 +-- src/messenger/comm/intercom.cljs | 15 +++- src/messenger/components/chat/chat.cljs | 75 +++++++++++-------- src/messenger/components/chat/message.cljs | 21 +++--- .../components/chat/new_message.cljs | 30 ++++++-- .../components/contact_list/contact_list.cljs | 45 ++++++----- src/messenger/constants.cljs | 2 + src/messenger/models/messages.cljs | 38 +++++++--- src/messenger/models/navigation.cljs | 8 ++ src/messenger/omnext.cljs | 53 ++++++++++++- src/messenger/persistence/realm.cljs | 18 +++-- src/messenger/protocol/protocol_handler.cljs | 4 +- src/messenger/services/navigate_to.cljs | 27 +++++-- src/messenger/services/protocol.cljs | 39 +++++++++- src/messenger/utils/state.cljs | 7 ++ 19 files changed, 307 insertions(+), 120 deletions(-) create mode 100644 src/messenger/models/navigation.cljs create mode 100644 src/messenger/utils/state.cljs diff --git a/.re-natal b/.re-natal index 0292c842d3..bcb176d3d7 100644 --- a/.re-natal +++ b/.re-natal @@ -1,7 +1,7 @@ { "name": "Messenger", "interface": "om-next", - "androidHost": "localhost", + "androidHost": "10.0.3.2", "modules": [ "react-native-contacts", "react-native-invertible-scroll-view", diff --git a/package.json b/package.json index 5aafac5feb..5d383198ef 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ }, "dependencies": { "awesome-phonenumber": "^1.0.12", - "react-native": "^0.21.0", + "react-native": "^0.22.0", "react-native-contacts": "^0.2.1", "react-native-i18n": "0.0.8", "react-native-invertible-scroll-view": "^0.2.0", diff --git a/src/messenger/android/core.cljs b/src/messenger/android/core.cljs index 8c35c5ea0f..36aecbaf64 100644 --- a/src/messenger/android/core.cljs +++ b/src/messenger/android/core.cljs @@ -11,8 +11,7 @@ [messenger.omnext :as omnext] [messenger.state :as state] [messenger.utils.utils :refer [log toast]] - [messenger.android.login :refer [login]] - [messenger.components.contact-list.contact-list :refer [contact-list]] + [messenger.android.login :refer [login Login]] [messenger.comm.pubsub :as pubsub] [messenger.comm.intercom :as intercom :refer [load-user-phone-number]] [messenger.protocol.protocol-handler :refer [make-handler]] @@ -20,12 +19,14 @@ [messenger.components.chat.chat :as chat] [syng-im.protocol.api :refer [init-protocol]] [syng-im.utils.logging :as log] - [messenger.components.iname :as in])) + [messenger.components.iname :as in] + [messenger.models.navigation :as n] + [messenger.components.contact-list.contact-list :refer [ContactList contact-list]])) (def app-registry (.-AppRegistry js/React)) -(def back-button-handler-atom (atom {:nav nil +(def back-button-handler-atom (atom {:nav nil :handler nil})) (defn init-back-button-handler! [nav] @@ -37,19 +38,20 @@ (when (< 1 (.-length (.getCurrentRoutes nav))) (.pop nav) true)))] - (reset! back-button-handler-atom {:nav nav + (reset! back-button-handler-atom {:nav nav :handler new-listener}) (add-event-listener "hardwareBackPress" new-listener))))) (defui AppRoot static om/IQuery (query [this] - [:loading :contacts-ds :user-phone-number :user-identity :confirmation-code]) + (let [component (n/current-screen-class)] + [{(in/get-name component) (om/get-query component)}])) Object (render [this] (log/debug "APPROOT Rendering") (navigator - {:initialRoute {:component login} + {:initialRoute {:component contact-list} :renderScene (fn [route nav] (log/debug "RENDER SCENE") (when state/*nav-render* @@ -68,6 +70,7 @@ (defonce app-root (om/factory RootNode)) (defn init [] + (n/set-current-screen-class ContactList) (init-simple-store) (pubsub/setup-pub-sub) (init-protocol (make-handler)) diff --git a/src/messenger/android/login.cljs b/src/messenger/android/login.cljs index 9795b41e26..a01d89538c 100644 --- a/src/messenger/android/login.cljs +++ b/src/messenger/android/login.cljs @@ -12,15 +12,14 @@ [messenger.utils.resources :as res] [messenger.components.spinner :refer [spinner]] [messenger.android.sign-up-confirm :refer [sign-up-confirm]] - [messenger.constants :refer [ethereum-rpc-url]])) + [messenger.constants :refer [ethereum-rpc-url]] + [messenger.components.iname :as in])) (def nav-atom (atom nil)) (defn show-confirm-view [] (swap! state/app-state assoc :loading false) - (binding [state/*nav-render* false] - (.replace @nav-atom (clj->js {:component sign-up-confirm - :name "sign-up-confirm"})))) + (intercom/show-signup-confirm @nav-atom)) (defn sign-up [] (swap! state/app-state assoc :loading true) @@ -34,12 +33,15 @@ (set-user-phone-number formatted))) (defui Login + static in/IName + (get-name [this] + :login/login) static om/IQuery (query [this] '[:user-phone-number :user-identity :loading]) Object (render [this] - (let [{:keys [user-phone-number user-identity loading]} (om/props this) + (let [{{:keys [user-phone-number user-identity loading]} :login/login} (om/props this) {:keys [nav]} (om/get-computed this)] (reset! nav-atom nav) (view diff --git a/src/messenger/android/sign_up_confirm.cljs b/src/messenger/android/sign_up_confirm.cljs index 13f2d7b296..b5d3e7f5b3 100644 --- a/src/messenger/android/sign_up_confirm.cljs +++ b/src/messenger/android/sign_up_confirm.cljs @@ -12,15 +12,14 @@ [messenger.utils.resources :as res] [messenger.components.spinner :refer [spinner]] [messenger.components.contact-list.contact-list :refer [contact-list]] - [messenger.comm.intercom :as intercom :refer [set-confirmation-code]])) + [messenger.comm.intercom :as intercom :refer [set-confirmation-code]] + [messenger.components.iname :as in])) (def nav-atom (atom nil)) (defn show-home-view [] (swap! state/app-state assoc :loading false) - (binding [state/*nav-render* false] - (.replace @nav-atom (clj->js {:component contact-list - :name "contact-list"})))) + (intercom/show-contacts @nav-atom)) (defn sync-contacts [] (intercom/sync-contacts show-home-view)) @@ -47,13 +46,16 @@ (set-confirmation-code formatted))) (defui SignUpConfirm + static in/IName + (get-name [this] + :signup/confirm) static om/IQuery (query [this] '[:confirmation-code :loading]) Object (render [this] - (let [{:keys [confirmation-code loading]} (om/props this) + (let [{{:keys [confirmation-code loading]} :signup/confirm} (om/props this) {:keys [nav]} (om/get-computed this)] (reset! nav-atom nav) (view diff --git a/src/messenger/comm/intercom.cljs b/src/messenger/comm/intercom.cljs index e055a109d8..4982716565 100644 --- a/src/messenger/comm/intercom.cljs +++ b/src/messenger/comm/intercom.cljs @@ -54,9 +54,12 @@ (defn protocol-initialized [identity] (publish! :service [:protocol :protocol/initialized {:identity identity}])) -(defn save-new-msg [from payload] - (publish! :service [:protocol :protocol/save-new-msg {:from from - :payload payload}])) +(defn save-new-msg [msg] + (publish! :service [:protocol :protocol/save-new-msg {:msg msg}])) + +(defn send-msg [chat-id text] + (publish! :service [:protocol :protocol/send-msg {:chat-id chat-id + :text text}])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; navigate-to @@ -64,3 +67,9 @@ (defn show-chat [navigator chat-id] (publish! :service [:navigate-to :scene/chat {:chat-id chat-id :navigator navigator}])) + +(defn show-signup-confirm [navigator] + (publish! :service [:navigate-to :scene/signup-confirm {:navigator navigator}])) + +(defn show-contacts [navigator] + (publish! :service [:navigate-to :scene/contacts {:navigator navigator}])) diff --git a/src/messenger/components/chat/chat.cljs b/src/messenger/components/chat/chat.cljs index e2371d06bc..f1a4cd2e50 100644 --- a/src/messenger/components/chat/chat.cljs +++ b/src/messenger/components/chat/chat.cljs @@ -6,31 +6,38 @@ [messenger.utils.resources :as res] [messenger.components.invertible-scroll-view :refer [invertible-scroll-view]] [messenger.components.chat.message :refer [message]] - [messenger.components.chat.new-message :refer [new-message]] + [messenger.components.chat.new-message :refer [new-message NewMessage]] [messenger.state :as state] [syng-im.utils.logging :as log] - [messenger.components.iname :as in])) + [messenger.components.iname :as in] + [messenger.omnext :as omnext])) -(defn generate-message [n] - {:id n - :type (if (= (rem n 4) 3) - :audio - :text) - :body (if (= (rem n 3) 0) - (apply str n "." (repeat 5 " This is a text.")) - (str n ". This is a text.")) - :outgoing (< (rand) 0.5) - :delivery-status (if (< (rand) 0.5) :delivered :seen) - :date "TODAY" - :new-day (= (rem n 3) 0)}) +;(defn generate-message [n] +; {:id n +; :type (if (= (rem n 4) 3) +; :audio +; :text) +; :body (if (= (rem n 3) 0) +; (apply str n "." (repeat 5 " This is a text.")) +; (str n ". This is a text.")) +; :outgoing (< (rand) 0.5) +; :delivery-status (if (< (rand) 0.5) :delivered :seen) +; :date "TODAY" +; :new-day (= (rem n 3) 0)}) +; +;(defn generate-messages [n] +; (map generate-message (range 1 (inc n)))) -(defn generate-messages [n] - (map generate-message (range 1 (inc n)))) +;(defn load-messages [] +; (clone-with-rows (data-source {:rowHasChanged (fn [row1 row2] +; (not= row1 row2))}) +; (vec (generate-messages 100)))) +; -(defn load-messages [] - (clone-with-rows (data-source {:rowHasChanged (fn [row1 row2] - (not= row1 row2))}) - (vec (generate-messages 100)))) +(defn to-datasource [msgs] + (-> (data-source {:rowHasChanged (fn [row1 row2] + (not= row1 row2))}) + (clone-with-rows msgs))) (defn nav-pop [nav] (binding [state/*nav-render* false] @@ -42,17 +49,21 @@ (defui Chat static in/IName (get-name [this] - :chat) + :chat/chat) static om/IQuery (query [this] - '[:chat/messages]) + `[:chat/messages ~@(om/get-query NewMessage)]) Object + (componentDidMount [this] + (om.next.protocols/reindex! omnext/reconciler)) (render [this] (let [{:keys [nav]} (om/get-computed this) - {{messages :chat/messages} :chat} (om/props this) + {{messages :chat/messages + chat-id :chat/chat-id} :chat/chat} (om/props this) _ (log/debug "messages=" messages) - messages-ds (load-messages)] + messages-ds (when messages + (to-datasource messages))] (view {:style {:flex 1 :backgroundColor "white"}} (toolbar-android {:logo res/logo-icon @@ -66,13 +77,15 @@ :elevation 2} :onIconClicked (fn [] (nav-pop nav))}) - (list-view {:dataSource messages-ds - :renderScrollComponent - (fn [props] - (invertible-scroll-view nil)) - :renderRow render-row - :style {:backgroundColor "white"}}) - (new-message))))) + (when messages-ds + (list-view {:dataSource messages-ds + :renderScrollComponent + (fn [props] + (invertible-scroll-view nil)) + :renderRow render-row + :style {:backgroundColor "white"}})) + + (new-message {:chat-id chat-id}))))) (def chat (om/factory Chat)) diff --git a/src/messenger/components/chat/message.cljs b/src/messenger/components/chat/message.cljs index 92bb537368..30d5595486 100644 --- a/src/messenger/components/chat/message.cljs +++ b/src/messenger/components/chat/message.cljs @@ -2,21 +2,20 @@ (:require-macros [natal-shell.components :refer [view text image]]) (:require [om.next :as om :refer-macros [defui]] - [messenger.utils.resources :as res])) + [messenger.utils.resources :as res] + [messenger.constants :refer [text-content-type]])) (defui Message static om/Ident - (ident [this {:keys [id]}] - [:message/by-id id]) + (ident [this {:keys [msg-id]}] + [:message/by-id msg-id]) static om/IQuery (query [this] - '[:id :type :body :outgoing :delivery-status :date :new-day]) + '[:msg-id :content :content-type :outgoing :delivery-status :date :new-day]) Object (render [this] - (let [{:keys [id body outgoing delivery-status date new-day] :as props} - (om/props this) - type (keyword (:type props))] + (let [{:keys [msg-id content content-type outgoing delivery-status date new-day] :as props} (om/props this)] (view {:paddingHorizontal 15} ;;; date (when new-day @@ -39,7 +38,7 @@ {:alignSelf "flex-start" :alignItems "flex-start"}))} (view {:style (merge {:borderRadius 6} - (if (= type :text) + (if (= content-type text-content-type) {:paddingVertical 12 :paddingHorizontal 16} {:paddingVertical 14 @@ -47,11 +46,11 @@ (if outgoing {:backgroundColor "#D3EEEF"} {:backgroundColor "#FBF6E3"}))} - (if (= type :text) + (if (= content-type text-content-type) (text {:style {:fontSize 14 :fontFamily "Avenir-Roman" :color "#4A5258"}} - body) + content) ;;; audio (view {:style {:flexDirection "row" :alignItems "center"}} @@ -105,4 +104,4 @@ "Seen" "Delivered"))))))))) -(def message (om/factory Message {:keyfn :id})) \ No newline at end of file +(def message (om/factory Message {:keyfn :msg-id})) \ No newline at end of file diff --git a/src/messenger/components/chat/new_message.cljs b/src/messenger/components/chat/new_message.cljs index 22d6280f04..5dd56a685d 100644 --- a/src/messenger/components/chat/new_message.cljs +++ b/src/messenger/components/chat/new_message.cljs @@ -2,12 +2,19 @@ (:require-macros [natal-shell.components :refer [view image text-input]]) (:require [om.next :as om :refer-macros [defui]] - [messenger.utils.resources :as res])) + [messenger.utils.resources :as res] + [syng-im.utils.logging :as log] + [messenger.utils.state :refer [from-state]] + [messenger.comm.intercom :refer [send-msg]])) + +(def local-state (atom {})) (defui NewMessage + static om/IQuery + (query [this] + '[:chat/chat-id]) Object - (render - [this] + (render [this] (view {:style {:flexDirection "row" :margin 10 :height 40 @@ -24,8 +31,21 @@ :lineHeight 42 :fontSize 14 :fontFamily "Avenir-Roman" - :color "#9CBFC0"}} - "Your message") + :color "#9CBFC0"} + :autoFocus true + :placeholder "Enter your message here" + :value (from-state this :text) + :onChangeText (fn [text] + ;(log/debug (with-out-str (pr (js->clj (om/props this)))) (-> (om/props this) :chat-id)) + ;(om/set-state! this (clj->js {:text text})) + (swap! local-state assoc :text text) + ) + :onSubmitEditing (fn [e] + (let [chat-id (-> (om/props this) :chat-id) + ;text (from-state this :text) + text (get @local-state :text)] + ;(om/set-state! this (clj->js {:text nil})) + (send-msg chat-id text)))}) (image {:source res/smile :style {:marginTop 11 :marginRight 12 diff --git a/src/messenger/components/contact_list/contact_list.cljs b/src/messenger/components/contact_list/contact_list.cljs index d939338917..fa07257529 100644 --- a/src/messenger/components/contact_list/contact_list.cljs +++ b/src/messenger/components/contact_list/contact_list.cljs @@ -1,14 +1,15 @@ (ns messenger.components.contact-list.contact-list (:require-macros - [natal-shell.components :refer [view text image touchable-highlight list-view - toolbar-android]] - [natal-shell.core :refer [with-error-view]]) + [natal-shell.components :refer [view text image touchable-highlight list-view + toolbar-android]] + [natal-shell.core :refer [with-error-view]]) (:require [om.next :as om :refer-macros [defui]] - [messenger.state :as state] [messenger.utils.utils :refer [log toast http-post]] [messenger.utils.resources :as res] [messenger.comm.intercom :as intercom] - [messenger.components.contact-list.contact :refer [contact]])) + [messenger.components.contact-list.contact :refer [contact]] + [messenger.components.iname :as in] + [syng-im.utils.logging :as log])) (defn render-row [nav row section-id row-id] (contact (om/computed (js->clj row :keywordize-keys true) @@ -18,25 +19,29 @@ (intercom/load-syng-contacts)) (defui ContactList + static in/IName + (get-name [this] + :contacts/contacts) static om/IQuery (query [this] - '[:contacts-ds]) + '[:contacts-ds]) Object (componentDidMount [this] - (load-contacts)) + (load-contacts)) (render [this] - (let [{:keys [contacts-ds]} (om/props this) - {:keys [nav]} (om/get-computed this)] - (view {:style {:flex 1 - :backgroundColor "white"}} - (toolbar-android {:logo res/logo-icon - :title "Chats" - :titleColor "#4A5258" - :style {:backgroundColor "white" - :height 56 - :elevation 2}}) - (list-view {:dataSource contacts-ds - :renderRow (partial render-row nav) - :style {:backgroundColor "white"}}))))) + (let [{{contacts-ds :contacts-ds} :contacts/contacts} (om/props this) + {:keys [nav]} (om/get-computed this)] + (view {:style {:flex 1 + :backgroundColor "white"}} + (toolbar-android {:logo res/logo-icon + :title "Chats" + :titleColor "#4A5258" + :style {:backgroundColor "white" + :height 56 + :elevation 2}}) + (when contacts-ds + (list-view {:dataSource contacts-ds + :renderRow (partial render-row nav) + :style {:backgroundColor "white"}})))))) (def contact-list (om/factory ContactList)) diff --git a/src/messenger/constants.cljs b/src/messenger/constants.cljs index 5a37187434..b838bafd6a 100644 --- a/src/messenger/constants.cljs +++ b/src/messenger/constants.cljs @@ -1,3 +1,5 @@ (ns messenger.constants) (def ethereum-rpc-url "http://localhost:8545") + +(def text-content-type "text/plain") \ No newline at end of file diff --git a/src/messenger/models/messages.cljs b/src/messenger/models/messages.cljs index fbe946cdc3..75bd0e9900 100644 --- a/src/messenger/models/messages.cljs +++ b/src/messenger/models/messages.cljs @@ -1,26 +1,42 @@ (ns messenger.models.messages (:require [messenger.persistence.realm :as r] + [cljs.reader :refer [read-string]] [syng-im.utils.random :refer [timestamp]])) -(defn save-message [from {:keys [msg-id] :as msg}] +(defn save-message [chat-id {:keys [from to msg-id content content-type outgoing] :or {outgoing false} :as msg}] (when-not (r/exists? :msgs :msg-id msg-id) (r/write (fn [] - (r/create :msgs {:msg-id msg-id - :chat-id from - :timestamp (timestamp) - :msg (with-out-str (pr msg))} true))))) + (r/create :msgs {:chat-id chat-id + :msg-id msg-id + :from from + :to to + :content content + :content-type content-type + :outgoing outgoing + :timestamp (timestamp)} true))))) + +(defn get-messages* [chat-id] + (-> (r/get-by-field :msgs :chat-id chat-id) + (r/sorted :timestamp :desc) + (r/page 0 10))) (defn get-messages [chat-id] - (-> (r/get-by-field :msgs :chat-id chat-id) - (r/sorted :timestamp) - (r/page 0 10))) + (-> (get-messages* chat-id) + (js->clj :keywordize-keys true))) (comment (save-message "0x040028c500ff086ecf1cfbb3c1a7240179cde5b86f9802e6799b9bbe9cdd7ad1b05ae8807fa1f9ed19cc8ce930fc2e878738c59f030a6a2f94b3522dc1378ff154" - {:msg-id "23456" - :content "hi!"}) - (get-messages "0x040028c500ff086ecf1cfbb3c1a7240179cde5b86f9802e6799b9bbe9cdd7ad1b05ae8807fa1f9ed19cc8ce930fc2e878738c59f030a6a2f94b3522dc1378ff154") + {:msg-id "153" + :content "hello!" + :content-type "text/plain"}) + + (get-messages* "0x040028c500ff086ecf1cfbb3c1a7240179cde5b86f9802e6799b9bbe9cdd7ad1b05ae8807fa1f9ed19cc8ce930fc2e878738c59f030a6a2f94b3522dc1378ff154") + + (get-messages "0x043df89d36f6e3d8ade18e55ac3e2e39406ebde152f76f2f82d674681d59319ffd9880eebfb4f5f8d5c222ec485b44d6e30ba3a03c96b1c946144fdeba1caccd43") + + (doseq [msg (get-messages* "0x043df89d36f6e3d8ade18e55ac3e2e39406ebde152f76f2f82d674681d59319ffd9880eebfb4f5f8d5c222ec485b44d6e30ba3a03c96b1c946144fdeba1caccd43")] + (r/delete msg)) ) \ No newline at end of file diff --git a/src/messenger/models/navigation.cljs b/src/messenger/models/navigation.cljs new file mode 100644 index 0000000000..7906c1c272 --- /dev/null +++ b/src/messenger/models/navigation.cljs @@ -0,0 +1,8 @@ +(ns messenger.models.navigation + (:require [messenger.state :as state])) + +(defn set-current-screen-class [class] + (swap! state/app-state assoc-in [:current-screen-class] class)) + +(defn current-screen-class [] + (get-in @state/app-state [:current-screen-class])) diff --git a/src/messenger/omnext.cljs b/src/messenger/omnext.cljs index be1b1e4683..e7a5cec606 100644 --- a/src/messenger/omnext.cljs +++ b/src/messenger/omnext.cljs @@ -4,7 +4,8 @@ [re-natal.support :as sup] [messenger.models.messages :as msgs] [messenger.models.chat :as chat] - [messenger.state :as state])) + [messenger.state :as state] + [messenger.components.iname :as in])) (defmulti read om/dispatch) @@ -15,15 +16,59 @@ {:value v} {:value :not-found}))) -(defmethod read :chat [env key param] +(defmethod read :chat/chat [env key param] (log/debug "reading" "key=" key "param=" param) (let [chat-id (chat/current-chat-id) - val {:value {:chat/messages (msgs/get-messages chat-id)}}] + val {:value {:chat/messages (msgs/get-messages chat-id) + :chat/chat-id chat-id}} + _ (log/debug "returning" val)] val)) +(defmethod read :chat/messages [env key param] + (log/debug "reading" "key=" key "param=" param) + (let [chat-id (chat/current-chat-id) + val {:value {:chat/messages (msgs/get-messages chat-id) + :chat/chat-id chat-id}} + _ (log/debug "returning" val)] + val)) + +(defmethod read :contacts/contacts [env key param] + (log/debug "reading" "key=" key "param=" param) + (let [val {:value {:contacts-ds (get-in @state/app-state [:contacts-ds])}} + _ (log/debug "returning" val)] + val)) + +(defmethod read :login/login [env key param] + (log/debug "reading" "key=" key "param=" param) + (let [val {:value (select-keys @state/app-state [:user-phone-number :user-identity :loading])} + _ (log/debug "returning" val)] + val)) + +(defmethod read :signup/confirm [env key param] + (log/debug "reading" "key=" key "param=" param) + (let [val {:value (select-keys @state/app-state [:confirmation-code :loading])} + _ (log/debug "returning" val)] + val)) + +(defmulti mutate om/dispatch) + +(defmethod mutate 'chat/add-msg-to-chat [{:keys [state] :as env} key {:keys [chat-id msg] :as param}] + (log/debug "writing" "key=" key "param=" param) + {:action #(do + (log/debug "Writing msg to db") + (msgs/save-message chat-id msg) + (swap! state/app-state assoc-in [:chat :messages] msg))}) + (defonce reconciler (om/reconciler {:state state/app-state - :parser (om/parser {:read read}) + :parser (om/parser {:read read + :mutate mutate}) :root-render sup/root-render :root-unmount sup/root-unmount})) + + +(defn set-root-query [component] + (let [app-root (om/class->any reconciler (om/app-root reconciler))] + (om/set-query! app-root {:query [{(in/get-name component) (om/get-query component)}]}) + (om.next.protocols/reindex! reconciler))) \ No newline at end of file diff --git a/src/messenger/persistence/realm.cljs b/src/messenger/persistence/realm.cljs index 3904fbfc01..b25ee6788e 100644 --- a/src/messenger/persistence/realm.cljs +++ b/src/messenger/persistence/realm.cljs @@ -17,10 +17,14 @@ :value "string"}} {:name :msgs :primaryKey :msg-id - :properties {:msg-id "string" - :timestamp "int" - :chat-id "string" - :msg "string"}}]}) + :properties {:msg-id "string" + :from "string" + :to "string" + :content "string" ;; TODO make it ArrayBuffer + :content-type "string" + :timestamp "int" + :chat-id "string" + :outgoing "bool"}}]}) (def realm (js/Realm. (clj->js opts))) @@ -57,8 +61,10 @@ (-> (.objects realm (name schema-name)) (.filtered (to-query schema-name :eq field value)))) -(defn sorted [results field-name] - (.sorted results (to-string field-name))) +(defn sorted [results field-name order] + (.sorted results (to-string field-name) (if (= order :asc) + false + true))) (defn page [results from to] (js/Array.prototype.slice.call results from to)) diff --git a/src/messenger/protocol/protocol_handler.cljs b/src/messenger/protocol/protocol_handler.cljs index 543597a8f2..caeecd0d42 100644 --- a/src/messenger/protocol/protocol_handler.cljs +++ b/src/messenger/protocol/protocol_handler.cljs @@ -16,8 +16,8 @@ (case event-type :initialized (let [{:keys [identity]} event] (protocol-initialized identity)) - :new-msg (let [{:keys [from payload]} event] - (save-new-msg from payload)) + :new-msg (let [{:keys [from to payload]} event] + (save-new-msg (assoc payload :from from :to to))) ;:msg-acked (let [{:keys [msg-id]} event] ; (add-to-chat "chat" ":" (str "Message " msg-id " was acked"))) ;:delivery-failed (let [{:keys [msg-id]} event] diff --git a/src/messenger/services/navigate_to.cljs b/src/messenger/services/navigate_to.cljs index e51ca0877d..5c7ea0c310 100644 --- a/src/messenger/services/navigate_to.cljs +++ b/src/messenger/services/navigate_to.cljs @@ -2,18 +2,21 @@ (:require [syng-im.utils.logging :as log] [messenger.state :as state] [messenger.components.chat.chat :as chat] + [messenger.android.sign-up-confirm :as sc] + [messenger.components.contact-list.contact-list :as cl] [messenger.models.chat :refer [set-current-chat-id]] [om.next :as om] [messenger.omnext :as omnext] - [messenger.components.iname :as in])) + [messenger.components.iname :as in] + [messenger.models.navigation :as n])) (defn nav-push [nav route] (binding [state/*nav-render* false] (.push nav (clj->js route)))) -(defn set-root-query [component] - (let [app-root (om/class->any omnext/reconciler (om/app-root omnext/reconciler))] - (om/set-query! app-root {:query [{(in/get-name component) (om/get-query component)}]}))) +(defn nav-replace [nav route] + (binding [state/*nav-render* false] + (.replace nav (clj->js route)))) (defmulti navigate-to (fn [state id args] id)) @@ -21,10 +24,24 @@ (defmethod navigate-to :scene/chat [state id {:keys [navigator chat-id] :as args}] (log/debug "handling " id "args = " (dissoc args :navigator)) + (n/set-current-screen-class chat/Chat) (set-current-chat-id chat-id) - (set-root-query chat/Chat) (nav-push navigator {:component chat/chat})) +(defmethod navigate-to :scene/signup-confirm + [state id {:keys [navigator] :as args}] + (log/debug "handling " id "args = " (dissoc args :navigator)) + (n/set-current-screen-class sc/SignUpConfirm) + (nav-replace navigator {:component sc/sign-up-confirm + :name "sign-up-confirm"})) + +(defmethod navigate-to :scene/contacts + [state id {:keys [navigator] :as args}] + (log/debug "handling " id "args = " (dissoc args :navigator)) + (n/set-current-screen-class cl/ContactList) + (nav-replace navigator {:component cl/contact-list + :name "contact-list"})) + (defn navigate-to-handler [state [id args]] (log/debug "navigate-to-handler: " (dissoc args :navigator)) (navigate-to state id args)) diff --git a/src/messenger/services/protocol.cljs b/src/messenger/services/protocol.cljs index 036ca1fe9b..184290dd97 100644 --- a/src/messenger/services/protocol.cljs +++ b/src/messenger/services/protocol.cljs @@ -3,7 +3,11 @@ update-identity]] [messenger.models.messages :refer [save-message]] [messenger.models.user-data :refer [set-identity]] - [syng-im.utils.logging :as log])) + [syng-im.utils.logging :as log] + [syng-im.protocol.api :as api] + [messenger.omnext :as omnext] + [om.next :as om] + [messenger.constants :refer [text-content-type]])) (defmulti protocol (fn [state id args] id)) @@ -16,10 +20,39 @@ (set-initialized true)) (defmethod protocol :protocol/save-new-msg - [state id {:keys [from payload] :as args}] + [state id {{from :from :as msg} :msg :as args}] (log/debug "handling " id "args = " args) - (save-message from payload)) + (let [chat-id from] + (om/transact! omnext/reconciler `[(chat/add-msg-to-chat {:msg ~msg + :chat-id ~chat-id}) [:chat/messages :chat/chat-id :chat/chat]]))) + +(defmethod protocol :protocol/send-msg + [state id {:keys [chat-id text] :as args}] + (log/debug "handling " id "args = " args) + (let [{msg-id :msg-id + {from :from + to :to} :msg} (api/send-user-msg {:to chat-id + :content text}) + msg {:msg-id msg-id + :from from + :to to + :content text + :content-type text-content-type + :outgoing true}] + (om/transact! omnext/reconciler ;;(om/class->any omnext/reconciler messenger.components.chat.chat/Chat) + `[(chat/add-msg-to-chat {:msg ~msg + :chat-id ~chat-id}) [:chat/messages :chat/chat-id :chat/chat]]))) (defn protocol-handler [state [id args]] (log/debug "protocol-handler: " args) (protocol state id args)) + + +(comment + + (om/transact! omnext/reconciler `[(chat/add-msg-to-chat {:msg {:msg-id "1458670960090-ed5f995a-b686-5cbe-bf96-8a60ada8f6c3"} + :chat-id "1"}) [:chat/chat]]) + + (om/get-indexer omnext/reconciler) +(om.next.protocols/reindex! omnext/reconciler) + ) \ No newline at end of file diff --git a/src/messenger/utils/state.cljs b/src/messenger/utils/state.cljs new file mode 100644 index 0000000000..dda0369152 --- /dev/null +++ b/src/messenger/utils/state.cljs @@ -0,0 +1,7 @@ +(ns messenger.utils.state + (:require [om.next :as om])) + +(defn from-state [component key] + (-> (om/get-state component) + (js->clj :keywordize-keys true) + key))