2016-05-09 18:17:40 +03:00
|
|
|
(ns syng-im.chat.screen
|
2016-05-16 10:45:59 +03:00
|
|
|
(:require-macros [syng-im.utils.views :refer [defview]])
|
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-16 10:45:59 +03:00
|
|
|
[syng-im.chat.styles.screen :as st]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.resources :as res]
|
2016-05-13 19:03:14 +03:00
|
|
|
[syng-im.utils.listview :refer [to-datasource]]
|
2016-03-25 17:22:13 +03:00
|
|
|
[syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]]
|
2016-05-11 13:53:38 +03:00
|
|
|
[syng-im.components.toolbar :refer [toolbar]]
|
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
|
2016-05-12 12:56:47 +03:00
|
|
|
0 (dispatch [:show-add-participants])
|
|
|
|
1 (dispatch [:show-remove-participants])
|
|
|
|
2 (dispatch [:leave-group-chat])))
|
2016-05-06 14:06:58 +03:00
|
|
|
|
|
|
|
(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-11 14:37:31 +03:00
|
|
|
(defn action-view [{:keys [icon-style custom-icon handler title subtitle]
|
2016-05-07 16:39:03 +03:00
|
|
|
icon-name :icon}]
|
2016-05-03 16:39:08 +03:00
|
|
|
[touchable-highlight {:on-press (fn []
|
|
|
|
(dispatch [:set-show-actions false])
|
2016-05-11 14:37:31 +03:00
|
|
|
(when handler
|
|
|
|
(handler)))}
|
2016-05-07 16:39:03 +03:00
|
|
|
[view st/action-icon-row
|
|
|
|
[view st/action-icon-view
|
2016-05-11 14:37:31 +03:00
|
|
|
(or custom-icon
|
|
|
|
[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-11 14:37:31 +03:00
|
|
|
(defn menu-item-contact-photo [{:keys [photo-path]}]
|
|
|
|
[image {:source (if (s/blank? photo-path)
|
|
|
|
res/user-no-photo
|
|
|
|
{:uri photo-path})
|
|
|
|
:style st/menu-item-profile-contact-photo}])
|
|
|
|
|
|
|
|
(defn menu-item-contact-online [{:keys [online]}]
|
|
|
|
(when online
|
|
|
|
[view st/menu-item-profile-online-view
|
|
|
|
[view st/menu-item-profile-online-dot-left]
|
|
|
|
[view st/menu-item-profile-online-dot-right]]))
|
|
|
|
|
|
|
|
(defn menu-item-icon-profile []
|
|
|
|
[view st/icon-view
|
|
|
|
[menu-item-contact-photo {}]
|
|
|
|
[menu-item-contact-online {:online true}]])
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn actions-list-view []
|
2016-05-09 17:33:41 +03:00
|
|
|
(let [{:keys [group-chat chat-id]}
|
|
|
|
(subscribe [:chat-properties [:group-chat :chat-id]])]
|
|
|
|
(when-let [actions (if @group-chat
|
2016-05-06 14:06:58 +03:00
|
|
|
[{: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}
|
2016-05-12 12:13:09 +03:00
|
|
|
:handler #(dispatch [:show-add-participants])}
|
2016-05-06 14:06:58 +03:00
|
|
|
{: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}
|
2016-05-12 12:13:09 +03:00
|
|
|
:handler #(dispatch [:show-remove-participants])}
|
2016-05-06 14:06:58 +03:00
|
|
|
{: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}
|
2016-05-12 12:13:09 +03:00
|
|
|
:handler #(dispatch [:leave-group-chat])}
|
2016-05-06 14:06:58 +03:00
|
|
|
{:title "Settings"
|
2016-05-07 16:39:03 +03:00
|
|
|
:icon :settings
|
2016-05-06 14:06:58 +03:00
|
|
|
:icon-style {:width 20
|
|
|
|
:height 13}
|
2016-05-12 12:13:09 +03:00
|
|
|
:handler #(dispatch [:show-group-settings])}]
|
2016-05-09 17:33:41 +03:00
|
|
|
[{:title "Profile"
|
2016-05-11 14:37:31 +03:00
|
|
|
:custom-icon [menu-item-icon-profile]
|
2016-05-10 10:59:38 +03:00
|
|
|
:icon :menu_group
|
2016-05-09 17:33:41 +03:00
|
|
|
:icon-style {:width 25
|
|
|
|
:height 19}
|
2016-05-11 14:37:31 +03:00
|
|
|
:handler #(dispatch [:show-profile @chat-id])}
|
|
|
|
{:title "Search chat"
|
|
|
|
:subtitle "!not implemented"
|
|
|
|
:icon :search_gray_copy
|
|
|
|
:icon-style {:width 17
|
|
|
|
:height 17}
|
2016-05-12 12:13:09 +03:00
|
|
|
:handler nil}
|
2016-05-11 14:37:31 +03:00
|
|
|
{:title "Notifications and sounds"
|
|
|
|
:subtitle "!not implemented"
|
|
|
|
:icon :muted
|
|
|
|
:icon-style {:width 18
|
|
|
|
:height 21}
|
2016-05-12 12:13:09 +03:00
|
|
|
:handler nil}
|
2016-05-11 14:37:31 +03:00
|
|
|
{:title "Settings"
|
|
|
|
:subtitle "!not implemented"
|
|
|
|
:icon :settings
|
|
|
|
: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-11 13:53:38 +03:00
|
|
|
(defn toolbar-content []
|
2016-05-06 14:06:58 +03:00
|
|
|
(let [{:keys [group-chat name contacts]}
|
|
|
|
(subscribe [:chat-properties [:group-chat :name :contacts]])
|
|
|
|
show-actions (subscribe [:show-actions])]
|
|
|
|
(fn []
|
2016-05-11 13:53:38 +03:00
|
|
|
[view (st/chat-name-view @show-actions)
|
|
|
|
[text {:style st/chat-name-text}
|
|
|
|
(or @name "Chat name")]
|
|
|
|
(if @group-chat
|
|
|
|
[view {:flexDirection :row}
|
|
|
|
[icon :group st/group-icon]
|
|
|
|
[text {:style st/members}
|
|
|
|
(let [cnt (count @contacts)]
|
|
|
|
(str cnt
|
|
|
|
(if (< 1 cnt)
|
|
|
|
" members"
|
|
|
|
" member")
|
|
|
|
", " cnt " active"))]]
|
|
|
|
[text {:style st/last-activity} "Active a minute ago"])])))
|
|
|
|
|
|
|
|
(defn toolbar-action []
|
|
|
|
(let [show-actions (subscribe [:show-actions])]
|
|
|
|
(fn []
|
|
|
|
(if @show-actions
|
|
|
|
[touchable-highlight
|
|
|
|
{:on-press #(dispatch [:set-show-actions false])}
|
|
|
|
[view st/icon-view
|
|
|
|
[icon :up st/up-icon]]]
|
|
|
|
[touchable-highlight
|
|
|
|
{:on-press #(dispatch [:set-show-actions true])}
|
|
|
|
[view st/icon-view
|
|
|
|
[chat-photo {}]
|
|
|
|
[contact-online {:online true}]]]))))
|
|
|
|
|
|
|
|
(defn chat-toolbar []
|
|
|
|
(let [{:keys [group-chat name contacts]}
|
|
|
|
(subscribe [:chat-properties [:group-chat :name :contacts]])
|
|
|
|
show-actions (subscribe [:show-actions])]
|
|
|
|
(fn []
|
|
|
|
[toolbar {:hide-nav? @show-actions
|
|
|
|
:custom-content [toolbar-content]
|
|
|
|
:custom-action [toolbar-action]}])))
|
2016-05-03 16:39:08 +03:00
|
|
|
|
2016-05-16 10:45:59 +03:00
|
|
|
(defview messages-view [group-chat]
|
|
|
|
[messages [:chat :messages]
|
|
|
|
contacts [:chat :contacts]]
|
|
|
|
(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])
|
|
|
|
:enableEmptySections true
|
|
|
|
:dataSource (to-datasource messages)}]))
|
2016-04-19 15:38:16 +03:00
|
|
|
|
2016-05-16 10:45:59 +03:00
|
|
|
(defview chat []
|
|
|
|
[is-active [:chat :is-active]
|
|
|
|
group-chat [:chat :group-chat]
|
|
|
|
show-actions-atom [:show-actions]]
|
|
|
|
[view st/chat-view
|
2016-05-16 12:31:08 +03:00
|
|
|
[chat-toolbar]
|
2016-05-16 10:45:59 +03:00
|
|
|
[messages-view group-chat]
|
|
|
|
(when group-chat [typing-all])
|
|
|
|
(when is-active [chat-message-new])
|
|
|
|
(when show-actions-atom [actions-view])])
|