From 88238ed2c8a9cc412f831df44ff43dbef9ceafd9 Mon Sep 17 00:00:00 2001 From: virvar Date: Fri, 29 Apr 2016 15:43:36 +0300 Subject: [PATCH] Add status message on open empty chat. Add last chat message to db. Fix 'typing' view. Fix chat background. Former-commit-id: 71cb0fde6cb749f12d2dad0b13002868970d62e9 --- src/syng_im/components/chat.cljs | 48 +++++++------------ src/syng_im/components/chat/chat_message.cljs | 38 ++++++++------- src/syng_im/db.cljs | 5 +- src/syng_im/models/chat.cljs | 20 +++++--- src/syng_im/models/chats.cljs | 19 +++++++- src/syng_im/models/messages.cljs | 7 +++ src/syng_im/subs.cljs | 7 ++- 7 files changed, 87 insertions(+), 57 deletions(-) diff --git a/src/syng_im/components/chat.cljs b/src/syng_im/components/chat.cljs index de44012e86..c5aaa7e351 100644 --- a/src/syng_im/components/chat.cljs +++ b/src/syng_im/components/chat.cljs @@ -19,7 +19,6 @@ [syng-im.utils.logging :as log] [syng-im.navigation :refer [nav-pop]] [syng-im.resources :as res] - [syng-im.constants :refer [content-type-status]] [syng-im.utils.listview :refer [to-realm-datasource]] [syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]] [reagent.core :as r] @@ -78,8 +77,7 @@ (defn typing [member] [view {:style {:width 260 - :paddingTop 2 - :paddingBottom 8 + :marginTop 10 :paddingLeft 8 :paddingRight 8 :alignItems "flex-start" @@ -95,7 +93,7 @@ (str member " is typing")]]]) (defn typing-all [] - [view {:style {:marginBottom 12}} + [view {:style {:marginBottom 20}} (for [member ["Geoff" "Justas"]] ^{:key member} [typing member])]) @@ -143,25 +141,14 @@ [contact-online {:online true}]])])) (defn chat [{:keys [navigator]}] - (let [messages (subscribe [:get-chat-messages]) - chat (subscribe [:get-current-chat])] + (let [messages (subscribe [:get-chat-messages]) + chat (subscribe [:get-current-chat]) + last-message-atom (subscribe [:get-chat-last-message])] (fn [] (let [msgs @messages ;_ (log/debug "messages=" msgs) - ;; temp to show first status + ;; temp msgs-clj (as-> (js->clj msgs) ms - (assoc ms "-1" - {:msg-id "-1" - :content (str "The brash businessman’s braggadocio " - "and public exchange with candidates " - "in the US presidential election") - :delivery-status "seen" - :from "Status" - :chat-id "-" - :content-type content-type-status - :timestamp 1 - "outgoing" false - :to nil}) (reduce (fn [items [n m]] (assoc items (.parseInt js/window n) m ;; (assoc m "from" (if (< 0.5 (rand)) @@ -179,14 +166,14 @@ (assoc :same-direction (if next (= (get current "outgoing") (get next "outgoing")) - true)) - (assoc :last-msg (= 0 n))) + true))) current]) ms (conj (vec (rest ms)) nil)) (reduce (fn [items [n m]] (assoc items n m)) {} ms)) msgs (clj->js msgs-clj) + typing (:group-chat @chat) ;; end temp datasource (to-realm-datasource msgs) contacts (:contacts @chat) @@ -218,15 +205,16 @@ :onIconClicked (fn [] (nav-pop navigator))} [toolbar-content-chat @chat]]) - [list-view {:dataSource datasource - :renderScrollComponent (fn [props] - (invertible-scroll-view (js->clj props))) - :renderRow (fn [row section-id row-id] - (let [msg (-> (js->clj row :keywordize-keys true) - (add-msg-color contact-by-identity) - (assoc :group-chat (:group-chat @chat)))] - (r/as-element [chat-message msg]))) - :style {:backgroundColor "white"}}] + (let [last-message @last-message-atom] + [list-view {:dataSource datasource + :renderScrollComponent (fn [props] + (invertible-scroll-view (js->clj props))) + :renderRow (fn [row section-id row-id] + (let [msg (-> (js->clj row :keywordize-keys true) + (add-msg-color contact-by-identity) + (assoc :group-chat (:group-chat @chat)) + (assoc :typing typing))] + (r/as-element [chat-message msg last-message])))}]) (when (:group-chat @chat) [typing-all]) (when (:is-active @chat) diff --git a/src/syng_im/components/chat/chat_message.cljs b/src/syng_im/components/chat/chat_message.cljs index c20930d19d..d51e9dfd25 100644 --- a/src/syng_im/components/chat/chat_message.cljs +++ b/src/syng_im/components/chat/chat_message.cljs @@ -287,7 +287,7 @@ (defn incoming-group-message-body [{:keys [msg-id from content content-type outgoing delivery-status selected new-day same-author - same-direction last-msg]}] + same-direction last-msg typing]}] (let [delivery-status :seen-by-everyone] [view {:style {:flexDirection "column"}} (when selected @@ -306,7 +306,7 @@ :else 10) :paddingRight 8 :paddingLeft 8} - (when last-msg + (when (and last-msg (not typing)) {:paddingBottom 20}))} [view {:style {:width 24}} (when (not same-author) @@ -327,7 +327,7 @@ [message-delivery-status {:delivery-status delivery-status}])]]])) (defn message-body [{:keys [msg-id content content-type outgoing delivery-status - group-chat new-day same-author same-direction last-msg]}] + group-chat new-day same-author same-direction last-msg typing]}] (let [delivery-status :seen] [view {:style (merge {:flexDirection "column" :width 260 @@ -343,7 +343,7 @@ :alignItems "flex-end"} {:alignItems "flex-start" :alignSelf "flex-start"}) - (when last-msg + (when (and last-msg (not typing)) {:paddingBottom 20}))} [message-content {:msg-id msg-id :content-type content-type @@ -353,22 +353,26 @@ (when (and outgoing delivery-status) [message-delivery-status {:delivery-status delivery-status}])])) -(defn chat-message [{:keys [msg-id from content content-type outgoing delivery-status date new-day group-chat selected same-author same-direction last-msg] :as msg}] +(defn chat-message [{:keys [msg-id from content content-type outgoing delivery-status + date new-day group-chat selected same-author same-direction + last-msg typing] :as msg} + last-message] [view {} (when new-day [message-date {:date date}]) - (let [msg-data {:msg-id msg-id - :from from - :content content - :content-type content-type - :outgoing outgoing - :delivery-status (keyword delivery-status) - :group-chat group-chat - :selected selected - :new-day new-day - :same-author same-author - :same-direction same-direction - :last-msg last-msg}] + (let [msg-data {:msg-id msg-id + :from from + :content content + :content-type content-type + :outgoing outgoing + :delivery-status (keyword delivery-status) + :group-chat group-chat + :selected selected + :new-day new-day + :same-author same-author + :same-direction same-direction + :last-msg (= (:msg-id last-message) msg-id) + :typing typing}] [view {} (when (= content-type content-type-status) [message-content-status from content]) diff --git a/src/syng_im/db.cljs b/src/syng_im/db.cljs index c7bf8b10d1..307e279203 100644 --- a/src/syng_im/db.cljs +++ b/src/syng_im/db.cljs @@ -9,7 +9,8 @@ :identity-password "replace-me-with-user-entered-password" :contacts [] :chat {:current-chat-id "0x0479a5ed1f38cadfad1db6cd56c4b659b0ebe052bbe9efa950f6660058519fa4ca6be2dda66afa80de96ab00eb97a2605d5267a1e8f4c2a166ab551f6826608cdd" - :command nil} + :command nil + :last-message nil} :chats {} :chats-updated-signal 0 :new-group #{} @@ -37,5 +38,7 @@ [:chats chat-id :command-requests]) (defn chat-command-request-path [chat-id msg-id] [:chats chat-id :command-requests msg-id]) +(defn chat-last-message-path [chat-id] + [:chats chat-id :last-message]) (def new-group-path [:new-group]) (def new-participants-path [:new-participants]) diff --git a/src/syng_im/models/chat.cljs b/src/syng_im/models/chat.cljs index 920442174b..c859c439a5 100644 --- a/src/syng_im/models/chat.cljs +++ b/src/syng_im/models/chat.cljs @@ -1,17 +1,25 @@ (ns syng-im.models.chat - (:require [syng-im.db :as db])) + (:require [syng-im.db :as db] + [syng-im.models.messages :refer [update-chat-last-message]])) + +(defn on-chat-update [db chat-id] + (update-chat-last-message db chat-id)) (defn set-current-chat-id [db chat-id] - (assoc-in db db/current-chat-id-path chat-id)) + (-> db + (assoc-in db/current-chat-id-path chat-id) + (on-chat-update chat-id))) (defn current-chat-id [db] (get-in db db/current-chat-id-path)) (defn signal-chat-updated [db chat-id] - (update-in db (db/updated-chat-signal-path chat-id) (fn [current] - (if current - (inc current) - 0)))) + (-> db + (on-chat-update chat-id) + (update-in (db/updated-chat-signal-path chat-id) (fn [current] + (if current + (inc current) + 0))))) (defn chat-updated? [db chat-id] (get-in db (db/updated-chat-signal-path chat-id))) diff --git a/src/syng_im/models/chats.cljs b/src/syng_im/models/chats.cljs index d777dc4329..5295a45cc0 100644 --- a/src/syng_im/models/chats.cljs +++ b/src/syng_im/models/chats.cljs @@ -1,10 +1,12 @@ (ns syng-im.models.chats (:require [syng-im.persistence.realm :as r] - [syng-im.utils.random :refer [timestamp]] + [syng-im.utils.random :as random :refer [timestamp]] [clojure.string :refer [join blank?]] [syng-im.db :as db] [syng-im.utils.logging :as log] - [syng-im.constants :refer [group-chat-colors]] + [syng-im.constants :refer [group-chat-colors + content-type-status]] + [syng-im.models.messages :refer [save-message]] [syng-im.persistence.realm-queries :refer [include-query]])) (defn signal-chats-updated [db] @@ -31,6 +33,18 @@ (or (chat-name-from-contacts identities) chat-id)) +(defn add-status-message [chat-id] + ;; TODO Get real status + (save-message chat-id + {:from "Status" + :to nil + :msg-id (random/id) + :content (str "The brash businessman’s braggadocio " + "and public exchange with candidates " + "in the US presidential election") + :content-type content-type-status + :outgoing false})) + (defn create-chat ([db chat-id identities group-chat?] (create-chat db chat-id identities group-chat? nil)) @@ -52,6 +66,7 @@ :group-chat group-chat? :timestamp (timestamp) :contacts contacts})))) + (add-status-message chat-id) (signal-chats-updated db))))) (defn chats-list [] diff --git a/src/syng_im/models/messages.cljs b/src/syng_im/models/messages.cljs index c40826b9c8..3b34b447a1 100644 --- a/src/syng_im/models/messages.cljs +++ b/src/syng_im/models/messages.cljs @@ -27,6 +27,13 @@ (defn message-by-id [msg-id] (r/single-cljs (r/get-by-field :msgs :msg-id msg-id))) +(defn update-chat-last-message [db chat-id] + (let [last-message (r/single-cljs (get-messages chat-id))] + (assoc-in db (db/chat-last-message-path chat-id) last-message))) + +(defn get-chat-last-message [db chat-id] + (get-in db (db/chat-last-message-path chat-id))) + (defn update-message! [{:keys [msg-id] :as msg}] (log/debug "update-message!" msg) (r/write diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index fd71a3fcfb..8c4f277e34 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -7,7 +7,8 @@ [syng-im.models.chats :refer [chats-list chats-updated? chat-by-id]] - [syng-im.models.messages :refer [get-messages]] + [syng-im.models.messages :refer [get-messages + get-chat-last-message]] [syng-im.models.contacts :refer [contacts-list contacts-list-exclude contacts-list-include]] @@ -65,6 +66,10 @@ (fn [db _] (reaction (get-chat-command-request @db)))) +(register-sub :get-chat-last-message + (fn [db _] + (reaction (get-chat-last-message @db (current-chat-id @db))))) + ;; -- Chats list -------------------------------------------------------------- (register-sub :get-chats