2016-05-09 18:17:40 +03:00
|
|
|
(ns syng-im.chat.screen
|
2016-04-18 13:38:38 +03:00
|
|
|
(:require [clojure.string :as s]
|
2016-05-10 13:58:37 +03:00
|
|
|
[re-frame.core :refer [subscribe dispatch]]
|
2016-05-04 14:35:35 +03:00
|
|
|
[syng-im.components.react :refer [view
|
2016-03-25 17:22:13 +03:00
|
|
|
text
|
|
|
|
image
|
2016-05-07 16:39:03 +03:00
|
|
|
icon
|
2016-05-06 14:06:58 +03:00
|
|
|
touchable-highlight
|
|
|
|
list-view
|
2016-05-10 13:58:37 +03:00
|
|
|
list-item]]
|
2016-05-09 18:17:40 +03:00
|
|
|
[syng-im.chat.styles.chat :as st]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.resources :as res]
|
2016-05-10 13:58:37 +03:00
|
|
|
[syng-im.utils.listview :refer [to-datasource2]]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]]
|
2016-05-09 18:17:40 +03:00
|
|
|
[syng-im.chat.views.message :refer [chat-message]]
|
|
|
|
[syng-im.chat.views.new-message :refer [chat-message-new]]))
|
2016-03-25 13:36:16 +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]
|
2016-04-07 14:01:41 +03:00
|
|
|
(if (= "system" from)
|
2016-05-10 13:58:37 +03:00
|
|
|
(assoc msg :text-color :#4A5258
|
|
|
|
:background-color :#D3EEEF)
|
2016-04-07 14:01:41 +03:00
|
|
|
(let [{:keys [text-color background-color]} (get contact-by-identity from)]
|
|
|
|
(assoc msg :text-color text-color
|
|
|
|
:background-color background-color))))
|
2016-04-06 16:13:31 +03:00
|
|
|
|
2016-04-18 13:38:38 +03:00
|
|
|
(defn chat-photo [{:keys [photo-path]}]
|
2016-05-02 16:56:42 +03:00
|
|
|
[view {:margin 10
|
|
|
|
:borderRadius 50}
|
2016-04-18 13:38:38 +03:00
|
|
|
[image {:source (if (s/blank? photo-path)
|
|
|
|
res/user-no-photo
|
|
|
|
{:uri photo-path})
|
2016-05-08 23:13:24 +03:00
|
|
|
:style st/chat-photo}]])
|
2016-04-18 13:38:38 +03:00
|
|
|
|
|
|
|
(defn contact-online [{:keys [online]}]
|
|
|
|
(when online
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/online-view
|
|
|
|
[view st/online-dot-left]
|
|
|
|
[view st/online-dot-right]]))
|
2016-04-18 13:38:38 +03:00
|
|
|
|
2016-04-19 15:38:16 +03:00
|
|
|
(defn typing [member]
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/typing-view
|
|
|
|
[view st/typing-background
|
|
|
|
[text {:style st/typing-text}
|
2016-04-19 15:38:16 +03:00
|
|
|
(str member " is typing")]]])
|
|
|
|
|
|
|
|
(defn typing-all []
|
2016-05-08 23:13:24 +03:00
|
|
|
[view st/typing-all
|
2016-04-19 15:38:16 +03:00
|
|
|
(for [member ["Geoff" "Justas"]]
|
|
|
|
^{:key member} [typing member])])
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn message-row [contact-by-identity group-chat]
|
2016-05-08 23:06:38 +03:00
|
|
|
(fn [row _ idx]
|
2016-05-06 14:06:58 +03:00
|
|
|
(let [msg (-> row
|
|
|
|
(add-msg-color contact-by-identity)
|
2016-05-08 23:06:38 +03:00
|
|
|
(assoc :group-chat group-chat)
|
|
|
|
(assoc :last-msg (zero? (js/parseInt idx))))]
|
2016-05-06 14:06:58 +03:00
|
|
|
(list-item [chat-message msg]))))
|
|
|
|
|
|
|
|
(defn on-action-selected [position]
|
|
|
|
(case position
|
|
|
|
0 (dispatch [:show-add-participants #_navigator])
|
|
|
|
1 (dispatch [:show-remove-participants #_navigator])
|
|
|
|
2 (dispatch [:leave-group-chat #_navigator])))
|
|
|
|
|
|
|
|
(defn overlay [{:keys [on-click-outside]} items]
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/actions-overlay
|
|
|
|
[touchable-highlight {:on-press on-click-outside
|
|
|
|
:style st/overlay-highlight}
|
2016-05-06 14:06:58 +03:00
|
|
|
[view nil]]
|
|
|
|
items])
|
|
|
|
|
2016-05-07 16:39:03 +03:00
|
|
|
(defn action-view [{:keys [icon-style handler title subtitle]
|
|
|
|
icon-name :icon}]
|
2016-05-03 16:39:08 +03:00
|
|
|
[touchable-highlight {:on-press (fn []
|
|
|
|
(dispatch [:set-show-actions false])
|
2016-05-07 16:39:03 +03:00
|
|
|
(handler))}
|
|
|
|
[view st/action-icon-row
|
|
|
|
[view st/action-icon-view
|
|
|
|
[icon icon-name icon-style]]
|
2016-05-08 23:13:24 +03:00
|
|
|
[view st/action-view
|
2016-05-07 16:39:03 +03:00
|
|
|
[text {:style st/action-title} title]
|
|
|
|
(when-let [subtitle subtitle]
|
|
|
|
[text {:style st/action-subtitle}
|
2016-05-02 16:56:42 +03:00
|
|
|
subtitle])]]])
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn actions-list-view []
|
|
|
|
(let [{:keys [group-chat active]}
|
|
|
|
(subscribe [:chat-properties [:group-chat :name :contacts :active]])]
|
|
|
|
(when-let [actions (when (and @group-chat @active)
|
|
|
|
[{:title "Add Contact to chat"
|
2016-05-07 16:39:03 +03:00
|
|
|
:icon :menu_group
|
2016-05-06 14:06:58 +03:00
|
|
|
:icon-style {:width 25
|
|
|
|
:height 19}
|
|
|
|
:handler nil #_#(dispatch [:show-add-participants
|
|
|
|
navigator])}
|
|
|
|
{:title "Remove Contact from chat"
|
|
|
|
:subtitle "Alex, John"
|
2016-05-07 16:39:03 +03:00
|
|
|
:icon :search_gray_copy
|
2016-05-06 14:06:58 +03:00
|
|
|
:icon-style {:width 17
|
|
|
|
:height 17}
|
|
|
|
:handler nil #_#(dispatch
|
|
|
|
[:show-remove-participants navigator])}
|
|
|
|
{:title "Leave Chat"
|
2016-05-07 16:39:03 +03:00
|
|
|
:icon :muted
|
2016-05-06 14:06:58 +03:00
|
|
|
:icon-style {:width 18
|
|
|
|
:height 21}
|
|
|
|
:handler nil #_#(dispatch [:leave-group-chat
|
|
|
|
navigator])}
|
|
|
|
{:title "Settings"
|
|
|
|
:subtitle "Not implemented"
|
2016-05-07 16:39:03 +03:00
|
|
|
:icon :settings
|
2016-05-06 14:06:58 +03:00
|
|
|
:icon-style {:width 20
|
|
|
|
:height 13}
|
|
|
|
:handler (fn [])}])]
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/actions-wrapper
|
|
|
|
[view st/actions-separator]
|
2016-05-08 23:13:24 +03:00
|
|
|
[view st/actions-view
|
2016-05-06 14:06:58 +03:00
|
|
|
(for [action actions]
|
|
|
|
^{:key action} [action-view action])]])))
|
2016-05-02 16:56:42 +03:00
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn actions-view []
|
|
|
|
[overlay {:on-click-outside #(dispatch [:set-show-actions false])}
|
|
|
|
[actions-list-view]])
|
2016-05-03 16:39:08 +03:00
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn toolbar []
|
|
|
|
(let [{:keys [group-chat name contacts]}
|
|
|
|
(subscribe [:chat-properties [:group-chat :name :contacts]])
|
|
|
|
show-actions (subscribe [:show-actions])]
|
|
|
|
(fn []
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/toolbar-view
|
2016-05-06 14:06:58 +03:00
|
|
|
(when (not @show-actions)
|
2016-05-07 16:39:03 +03:00
|
|
|
[touchable-highlight {:on-press #(dispatch [:navigate-back])}
|
|
|
|
[view st/icon-view
|
|
|
|
[icon :back st/back-icon]]])
|
|
|
|
[view (st/chat-name-view @show-actions)
|
|
|
|
[text {:style st/chat-name-text}
|
2016-05-06 14:06:58 +03:00
|
|
|
(or @name "Chat name")]
|
|
|
|
(if @group-chat
|
2016-05-07 16:39:03 +03:00
|
|
|
[view {:flexDirection :row}
|
|
|
|
[icon :group st/group-icon]
|
|
|
|
[text {:style st/members}
|
2016-05-06 14:06:58 +03:00
|
|
|
(let [cnt (count @contacts)]
|
|
|
|
(str cnt
|
|
|
|
(if (< 1 cnt)
|
|
|
|
" members"
|
|
|
|
" member")
|
|
|
|
", " cnt " active"))]]
|
2016-05-07 16:39:03 +03:00
|
|
|
[text {:style st/last-activity} "Active a minute ago"])]
|
2016-05-06 14:06:58 +03:00
|
|
|
(if @show-actions
|
|
|
|
[touchable-highlight
|
2016-05-07 16:39:03 +03:00
|
|
|
{:on-press #(dispatch [:set-show-actions false])}
|
|
|
|
[view st/icon-view
|
|
|
|
[icon :up st/up-icon]]]
|
2016-05-06 14:06:58 +03:00
|
|
|
[touchable-highlight
|
2016-05-07 16:39:03 +03:00
|
|
|
{:on-press #(dispatch [:set-show-actions true])}
|
|
|
|
[view st/icon-view
|
2016-05-06 14:06:58 +03:00
|
|
|
[chat-photo {}]
|
|
|
|
[contact-online {:online true}]]])])))
|
2016-05-03 16:39:08 +03:00
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn messages-view [group-chat]
|
|
|
|
(let [messages (subscribe [:chat :messages])
|
|
|
|
contacts (subscribe [:chat :contacts])]
|
|
|
|
(fn [group-chat]
|
|
|
|
(let [contacts' (contacts-by-identity @contacts)]
|
|
|
|
[list-view {:renderRow (message-row contacts' group-chat)
|
|
|
|
:renderScrollComponent #(invertible-scroll-view (js->clj %))
|
|
|
|
:onEndReached #(dispatch [:load-more-messages])
|
2016-05-09 16:40:45 +03:00
|
|
|
:enableEmptySections true
|
2016-05-06 14:06:58 +03:00
|
|
|
:dataSource (to-datasource2 @messages)}]))))
|
2016-04-19 15:38:16 +03:00
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn chat []
|
|
|
|
(let [is-active (subscribe [:chat :is-active])
|
|
|
|
group-chat (subscribe [:chat :group-chat])
|
2016-05-03 16:39:08 +03:00
|
|
|
show-actions-atom (subscribe [:show-actions])]
|
2016-03-25 13:36:16 +03:00
|
|
|
(fn []
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/chat-view
|
2016-05-06 14:06:58 +03:00
|
|
|
[toolbar]
|
|
|
|
[messages-view @group-chat]
|
|
|
|
(when @group-chat [typing-all])
|
|
|
|
(when is-active [chat-message-new])
|
|
|
|
(when @show-actions-atom [actions-view])])))
|