Add status message on open empty chat. Add last chat message to db. Fix 'typing' view. Fix chat background.

This commit is contained in:
virvar 2016-04-29 15:43:36 +03:00
parent 84cfddb3e8
commit 71cb0fde6c
7 changed files with 87 additions and 57 deletions

View File

@ -19,7 +19,6 @@
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
[syng-im.navigation :refer [nav-pop]] [syng-im.navigation :refer [nav-pop]]
[syng-im.resources :as res] [syng-im.resources :as res]
[syng-im.constants :refer [content-type-status]]
[syng-im.utils.listview :refer [to-realm-datasource]] [syng-im.utils.listview :refer [to-realm-datasource]]
[syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]] [syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]]
[reagent.core :as r] [reagent.core :as r]
@ -78,8 +77,7 @@
(defn typing [member] (defn typing [member]
[view {:style {:width 260 [view {:style {:width 260
:paddingTop 2 :marginTop 10
:paddingBottom 8
:paddingLeft 8 :paddingLeft 8
:paddingRight 8 :paddingRight 8
:alignItems "flex-start" :alignItems "flex-start"
@ -95,7 +93,7 @@
(str member " is typing")]]]) (str member " is typing")]]])
(defn typing-all [] (defn typing-all []
[view {:style {:marginBottom 12}} [view {:style {:marginBottom 20}}
(for [member ["Geoff" "Justas"]] (for [member ["Geoff" "Justas"]]
^{:key member} [typing member])]) ^{:key member} [typing member])])
@ -144,24 +142,13 @@
(defn chat [{:keys [navigator]}] (defn chat [{:keys [navigator]}]
(let [messages (subscribe [:get-chat-messages]) (let [messages (subscribe [:get-chat-messages])
chat (subscribe [:get-current-chat])] chat (subscribe [:get-current-chat])
last-message-atom (subscribe [:get-chat-last-message])]
(fn [] (fn []
(let [msgs @messages (let [msgs @messages
;_ (log/debug "messages=" msgs) ;_ (log/debug "messages=" msgs)
;; temp to show first status ;; temp
msgs-clj (as-> (js->clj msgs) ms msgs-clj (as-> (js->clj msgs) ms
(assoc ms "-1"
{:msg-id "-1"
:content (str "The brash businessmans 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]] (reduce (fn [items [n m]]
(assoc items (.parseInt js/window n) m (assoc items (.parseInt js/window n) m
;; (assoc m "from" (if (< 0.5 (rand)) ;; (assoc m "from" (if (< 0.5 (rand))
@ -179,14 +166,14 @@
(assoc :same-direction (assoc :same-direction
(if next (if next
(= (get current "outgoing") (get next "outgoing")) (= (get current "outgoing") (get next "outgoing"))
true)) true)))
(assoc :last-msg (= 0 n)))
current]) current])
ms (conj (vec (rest ms)) nil)) ms (conj (vec (rest ms)) nil))
(reduce (fn [items [n m]] (reduce (fn [items [n m]]
(assoc items n m)) (assoc items n m))
{} ms)) {} ms))
msgs (clj->js msgs-clj) msgs (clj->js msgs-clj)
typing (:group-chat @chat)
;; end temp ;; end temp
datasource (to-realm-datasource msgs) datasource (to-realm-datasource msgs)
contacts (:contacts @chat) contacts (:contacts @chat)
@ -218,15 +205,16 @@
:onIconClicked (fn [] :onIconClicked (fn []
(nav-pop navigator))} (nav-pop navigator))}
[toolbar-content-chat @chat]]) [toolbar-content-chat @chat]])
(let [last-message @last-message-atom]
[list-view {:dataSource datasource [list-view {:dataSource datasource
:renderScrollComponent (fn [props] :renderScrollComponent (fn [props]
(invertible-scroll-view (js->clj props))) (invertible-scroll-view (js->clj props)))
:renderRow (fn [row section-id row-id] :renderRow (fn [row section-id row-id]
(let [msg (-> (js->clj row :keywordize-keys true) (let [msg (-> (js->clj row :keywordize-keys true)
(add-msg-color contact-by-identity) (add-msg-color contact-by-identity)
(assoc :group-chat (:group-chat @chat)))] (assoc :group-chat (:group-chat @chat))
(r/as-element [chat-message msg]))) (assoc :typing typing))]
:style {:backgroundColor "white"}}] (r/as-element [chat-message msg last-message])))}])
(when (:group-chat @chat) (when (:group-chat @chat)
[typing-all]) [typing-all])
(when (:is-active @chat) (when (:is-active @chat)

View File

@ -287,7 +287,7 @@
(defn incoming-group-message-body [{:keys [msg-id from content content-type outgoing (defn incoming-group-message-body [{:keys [msg-id from content content-type outgoing
delivery-status selected new-day same-author delivery-status selected new-day same-author
same-direction last-msg]}] same-direction last-msg typing]}]
(let [delivery-status :seen-by-everyone] (let [delivery-status :seen-by-everyone]
[view {:style {:flexDirection "column"}} [view {:style {:flexDirection "column"}}
(when selected (when selected
@ -306,7 +306,7 @@
:else 10) :else 10)
:paddingRight 8 :paddingRight 8
:paddingLeft 8} :paddingLeft 8}
(when last-msg (when (and last-msg (not typing))
{:paddingBottom 20}))} {:paddingBottom 20}))}
[view {:style {:width 24}} [view {:style {:width 24}}
(when (not same-author) (when (not same-author)
@ -327,7 +327,7 @@
[message-delivery-status {:delivery-status delivery-status}])]]])) [message-delivery-status {:delivery-status delivery-status}])]]]))
(defn message-body [{:keys [msg-id content content-type outgoing 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] (let [delivery-status :seen]
[view {:style (merge {:flexDirection "column" [view {:style (merge {:flexDirection "column"
:width 260 :width 260
@ -343,7 +343,7 @@
:alignItems "flex-end"} :alignItems "flex-end"}
{:alignItems "flex-start" {:alignItems "flex-start"
:alignSelf "flex-start"}) :alignSelf "flex-start"})
(when last-msg (when (and last-msg (not typing))
{:paddingBottom 20}))} {:paddingBottom 20}))}
[message-content {:msg-id msg-id [message-content {:msg-id msg-id
:content-type content-type :content-type content-type
@ -353,7 +353,10 @@
(when (and outgoing delivery-status) (when (and outgoing delivery-status)
[message-delivery-status {:delivery-status 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 {} [view {}
(when new-day (when new-day
[message-date {:date date}]) [message-date {:date date}])
@ -368,7 +371,8 @@
:new-day new-day :new-day new-day
:same-author same-author :same-author same-author
:same-direction same-direction :same-direction same-direction
:last-msg last-msg}] :last-msg (= (:msg-id last-message) msg-id)
:typing typing}]
[view {} [view {}
(when (= content-type content-type-status) (when (= content-type content-type-status)
[message-content-status from content]) [message-content-status from content])

View File

@ -9,7 +9,8 @@
: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"
:command nil} :command nil
:last-message nil}
:chats {} :chats {}
:chats-updated-signal 0 :chats-updated-signal 0
:new-group #{} :new-group #{}
@ -37,5 +38,7 @@
[:chats chat-id :command-requests]) [:chats chat-id :command-requests])
(defn chat-command-request-path [chat-id msg-id] (defn chat-command-request-path [chat-id msg-id]
[:chats chat-id :command-requests 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-group-path [:new-group])
(def new-participants-path [:new-participants]) (def new-participants-path [:new-participants])

View File

@ -1,17 +1,25 @@
(ns syng-im.models.chat (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] (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] (defn current-chat-id [db]
(get-in db db/current-chat-id-path)) (get-in db db/current-chat-id-path))
(defn signal-chat-updated [db chat-id] (defn signal-chat-updated [db chat-id]
(update-in db (db/updated-chat-signal-path chat-id) (fn [current] (-> db
(on-chat-update chat-id)
(update-in (db/updated-chat-signal-path chat-id) (fn [current]
(if current (if current
(inc current) (inc current)
0)))) 0)))))
(defn chat-updated? [db chat-id] (defn chat-updated? [db chat-id]
(get-in db (db/updated-chat-signal-path chat-id))) (get-in db (db/updated-chat-signal-path chat-id)))

View File

@ -1,10 +1,12 @@
(ns syng-im.models.chats (ns syng-im.models.chats
(:require [syng-im.persistence.realm :as r] (: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?]] [clojure.string :refer [join blank?]]
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.utils.logging :as log] [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]])) [syng-im.persistence.realm-queries :refer [include-query]]))
(defn signal-chats-updated [db] (defn signal-chats-updated [db]
@ -31,6 +33,18 @@
(or (chat-name-from-contacts identities) (or (chat-name-from-contacts identities)
chat-id)) 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 businessmans braggadocio "
"and public exchange with candidates "
"in the US presidential election")
:content-type content-type-status
:outgoing false}))
(defn create-chat (defn create-chat
([db chat-id identities group-chat?] ([db chat-id identities group-chat?]
(create-chat db chat-id identities group-chat? nil)) (create-chat db chat-id identities group-chat? nil))
@ -52,6 +66,7 @@
:group-chat group-chat? :group-chat group-chat?
:timestamp (timestamp) :timestamp (timestamp)
:contacts contacts})))) :contacts contacts}))))
(add-status-message chat-id)
(signal-chats-updated db))))) (signal-chats-updated db)))))
(defn chats-list [] (defn chats-list []

View File

@ -27,6 +27,13 @@
(defn message-by-id [msg-id] (defn message-by-id [msg-id]
(r/single-cljs (r/get-by-field :msgs :msg-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}] (defn update-message! [{:keys [msg-id] :as msg}]
(log/debug "update-message!" msg) (log/debug "update-message!" msg)
(r/write (r/write

View File

@ -7,7 +7,8 @@
[syng-im.models.chats :refer [chats-list [syng-im.models.chats :refer [chats-list
chats-updated? chats-updated?
chat-by-id]] 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 [syng-im.models.contacts :refer [contacts-list
contacts-list-exclude contacts-list-exclude
contacts-list-include]] contacts-list-include]]
@ -65,6 +66,10 @@
(fn [db _] (fn [db _]
(reaction (get-chat-command-request @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 -------------------------------------------------------------- ;; -- Chats list --------------------------------------------------------------
(register-sub :get-chats (register-sub :get-chats