2016-03-25 14:29:38 +03:00
|
|
|
(ns syng-im.components.chat
|
2016-03-25 13:36:16 +03:00
|
|
|
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.components.react :refer [android?
|
|
|
|
view
|
|
|
|
text
|
|
|
|
image
|
|
|
|
touchable-highlight
|
|
|
|
navigator
|
2016-03-28 19:00:07 +03:00
|
|
|
toolbar-android]]
|
|
|
|
[syng-im.components.realm :refer [list-view]]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.utils.logging :as log]
|
|
|
|
[syng-im.navigation :refer [nav-pop]]
|
|
|
|
[syng-im.resources :as res]
|
2016-03-28 19:00:07 +03:00
|
|
|
[syng-im.utils.listview :refer [to-realm-datasource]]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]]
|
|
|
|
[reagent.core :as r]
|
2016-03-31 16:44:16 +03:00
|
|
|
[syng-im.components.chat.chat-message :refer [chat-message]]
|
|
|
|
[syng-im.components.chat.chat-message-new :refer [chat-message-new]]))
|
2016-03-25 13:36:16 +03:00
|
|
|
|
2016-03-25 14:29:38 +03:00
|
|
|
|
2016-04-06 16:13:31 +03:00
|
|
|
(defn contacts-by-identity [contacts]
|
|
|
|
(->> contacts
|
|
|
|
(map (fn [{:keys [identity] :as contact}]
|
|
|
|
[identity contact]))
|
|
|
|
(into {})))
|
|
|
|
|
|
|
|
(defn add-msg-color [{:keys [from] :as msg} contact-by-identity]
|
|
|
|
(let [{:keys [text-color background-color]} (get contact-by-identity from)]
|
|
|
|
(assoc msg :text-color text-color
|
|
|
|
:background-color background-color)))
|
|
|
|
|
2016-03-25 14:29:38 +03:00
|
|
|
(defn chat [{:keys [navigator]}]
|
2016-04-03 19:00:40 +03:00
|
|
|
(let [messages (subscribe [:get-chat-messages])
|
|
|
|
chat (subscribe [:get-current-chat])]
|
2016-03-25 13:36:16 +03:00
|
|
|
(fn []
|
2016-04-06 16:13:31 +03:00
|
|
|
(let [msgs @messages
|
|
|
|
;_ (log/debug "messages=" msgs)
|
|
|
|
datasource (to-realm-datasource msgs)
|
|
|
|
contacts (:contacts @chat)
|
|
|
|
contact-by-identity (contacts-by-identity contacts)]
|
2016-03-25 17:22:13 +03:00
|
|
|
[view {:style {:flex 1
|
2016-04-01 11:10:38 +03:00
|
|
|
:backgroundColor "#eef2f5"}}
|
2016-03-25 17:22:13 +03:00
|
|
|
(when android?
|
|
|
|
;; TODO add IOS version
|
|
|
|
[toolbar-android {:logo res/logo-icon
|
2016-04-03 19:00:40 +03:00
|
|
|
:title (or (@chat :name)
|
|
|
|
"Chat name")
|
2016-03-25 17:22:13 +03:00
|
|
|
:titleColor "#4A5258"
|
|
|
|
:subtitle "Last seen just now"
|
|
|
|
:subtitleColor "#AAB2B2"
|
|
|
|
:navIcon res/nav-back-icon
|
|
|
|
:style {:backgroundColor "white"
|
|
|
|
:height 56
|
|
|
|
:elevation 2}
|
|
|
|
:onIconClicked (fn []
|
|
|
|
(nav-pop navigator))}])
|
|
|
|
[list-view {:dataSource datasource
|
|
|
|
:renderScrollComponent (fn [props]
|
|
|
|
(invertible-scroll-view nil))
|
|
|
|
:renderRow (fn [row section-id row-id]
|
2016-04-06 16:13:31 +03:00
|
|
|
(let [msg (-> (js->clj row :keywordize-keys true)
|
|
|
|
(add-msg-color contact-by-identity))]
|
|
|
|
(r/as-element [chat-message msg])))
|
2016-03-29 23:45:31 +03:00
|
|
|
:style {:backgroundColor "white"}}]
|
2016-03-25 17:22:13 +03:00
|
|
|
[chat-message-new]]))))
|