Add/remove group chat members

This commit is contained in:
virvar 2016-05-13 14:17:03 +03:00
parent 5283256804
commit b2737d42f7
9 changed files with 121 additions and 33 deletions

View File

@ -28,6 +28,7 @@
text]) text])
(def drawer-layout-android (r/adapt-react-class (.-DrawerLayoutAndroid js/React))) (def drawer-layout-android (r/adapt-react-class (.-DrawerLayoutAndroid js/React)))
(def touchable-opacity (r/adapt-react-class (.-TouchableOpacity js/React))) (def touchable-opacity (r/adapt-react-class (.-TouchableOpacity js/React)))
(def modal (r/adapt-react-class (.-Modal js/React)))
(defn icon [n style] (defn icon [n style]

View File

@ -52,7 +52,7 @@
(def show-actions-path [:show-actions]) (def show-actions-path [:show-actions])
(def group-settings-path [:group-settings]) (def group-settings-path [:group-settings])
(def group-settings-name-path [:group-settings :name]) (def group-settings-name-path [:group-settings :name])
(def group-settings-members-path [:group-settings :contacts]) (def group-settings-selected-member-path [:selected-member])
(def new-group-path [:new-group]) (def new-group-path [:new-group])
(def new-participants-path [:new-participants]) (def new-participants-path [:new-participants])
(def updated-discoveries-signal-path [:discovery-updated-signal]) (def updated-discoveries-signal-path [:discovery-updated-signal])

View File

@ -6,6 +6,7 @@
text text
image image
icon icon
modal
touchable-highlight]] touchable-highlight]]
[syng-im.components.toolbar :refer [toolbar]] [syng-im.components.toolbar :refer [toolbar]]
[syng-im.components.realm :refer [list-view]] [syng-im.components.realm :refer [list-view]]
@ -16,6 +17,25 @@
[syng-im.group-settings.views.member :refer [contact-inner-view]] [syng-im.group-settings.views.member :refer [contact-inner-view]]
[reagent.core :as r])) [reagent.core :as r]))
(defn remove-member [{:keys [whisper-identity]}]
(dispatch [:chat-remove-member whisper-identity]))
(defn close-member-menu []
(dispatch [:select-group-chat-member nil]))
(defn member-menu [member]
[modal {:animated false
:transparent false
:onRequestClose close-member-menu}
[touchable-highlight {:style st/modal-container
:on-press close-member-menu}
[view st/modal-inner-container
[text {:style st/modal-member-name}
(:name member)]
[touchable-highlight {:on-press #(remove-member member)}
[text {:style st/modal-remove-text}
"Remove"]]]]])
(defn set-group-settings-name [chat-name] (defn set-group-settings-name [chat-name]
(dispatch [:set-group-settings-name chat-name])) (dispatch [:set-group-settings-name chat-name]))
@ -38,8 +58,9 @@
:custom-action [action-save]}]) :custom-action [action-save]}])
(defn group-settings [] (defn group-settings []
(let [chat-name (subscribe [:group-settings-name]) (let [chat-name (subscribe [:group-settings-name])
members (subscribe [:group-settings-members])] members (subscribe [:current-chat-contacts])
selected-member (subscribe [:selected-group-chat-member])]
(fn [] (fn []
[view st/group-settings [view st/group-settings
[new-group-toolbar] [new-group-toolbar]
@ -54,12 +75,13 @@
[text {:style st/members-text} [text {:style st/members-text}
"Members"] "Members"]
[touchable-highlight {:on-press (fn [] [touchable-highlight {:on-press (fn []
;; TODO not implemented (dispatch [:show-add-participants]))}
)}
[view st/add-members-container [view st/add-members-container
[icon :add-gray st/add-members-icon] [icon :add-gray st/add-members-icon]
[text {:style st/add-members-text} [text {:style st/add-members-text}
"Add members"]]] "Add members"]]]
[chat-members (vals (js->clj @members :keywordize-keys true))] [chat-members (vals (js->clj @members :keywordize-keys true))]
[text {:style st/settings-text} [text {:style st/settings-text}
"Settings"]]))) "Settings"]
(when @selected-member
[member-menu @selected-member])])))

View File

@ -11,6 +11,30 @@
text2-color text2-color
toolbar-background1]])) toolbar-background1]]))
(def modal-container
{:flex 1
:justifyContent :center
:padding 20})
(def modal-inner-container
{:borderRadius 10
:alignItems :center
:padding 5
:backgroundColor color-white})
(def modal-member-name
{:color text2-color
:fontFamily font
:fontSize 14
:lineHeight 20})
(def modal-remove-text
{:margin 10
:color text1-color
:fontFamily font
:fontSize 14
:lineHeight 20})
(def chat-members-container (def chat-members-container
{:marginBottom 10}) {:marginBottom 10})

View File

@ -1,6 +1,11 @@
(ns syng-im.group-settings.views.member (ns syng-im.group-settings.views.member
(:require [clojure.string :as s] (:require [clojure.string :as s]
[syng-im.components.react :refer [view image text icon touchable-highlight]] [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.components.react :refer [view
image
text
icon
touchable-highlight]]
[syng-im.resources :as res] [syng-im.resources :as res]
[syng-im.group-settings.styles.member :as st])) [syng-im.group-settings.styles.member :as st]))
@ -17,7 +22,7 @@
[view st/online-dot-left] [view st/online-dot-left]
[view st/online-dot-right]])) [view st/online-dot-right]]))
(defn contact-inner-view [{:keys [name photo-path online role]}] (defn contact-inner-view [{:keys [whisper-identity name photo-path online role]}]
[view st/contact-container [view st/contact-container
[view st/photo-container [view st/photo-container
[contact-photo {:photo-path photo-path}] [contact-photo {:photo-path photo-path}]
@ -32,8 +37,7 @@
(when role (when role
[text {:style st/role-text} [text {:style st/role-text}
role])] role])]
[touchable-highlight {:on-press (fn [] [touchable-highlight
;; TODO not implemented {:on-press #(dispatch [:select-group-chat-member whisper-identity])}
)}
[view st/more-btn [view st/more-btn
[icon :more-vertical st/more-btn-icon]]]]) [icon :more-vertical st/more-btn-icon]]]])

View File

@ -36,6 +36,7 @@
[syng-im.models.chats :refer [chat-exists? [syng-im.models.chats :refer [chat-exists?
create-chat create-chat
save-chat save-chat
chat-remove-member
chat-add-participants chat-add-participants
chat-remove-participants chat-remove-participants
set-chat-active set-chat-active
@ -555,7 +556,7 @@
(let [identities (vec (new-participants-selection db)) (let [identities (vec (new-participants-selection db))
chat-id (current-chat-id db)] chat-id (current-chat-id db)]
(chat-remove-participants chat-id identities) (chat-remove-participants chat-id identities)
(nav-pop navigator) (dispatch [:navigate-back])
(doseq [ident identities] (doseq [ident identities]
(api/group-remove-participant chat-id ident) (api/group-remove-participant chat-id ident)
(removed-participant-msg chat-id ident)) (removed-participant-msg chat-id ident))
@ -573,7 +574,7 @@
(let [identities (vec (new-participants-selection db)) (let [identities (vec (new-participants-selection db))
chat-id (current-chat-id db)] chat-id (current-chat-id db)]
(chat-add-participants chat-id identities) (chat-add-participants chat-id identities)
(nav-pop navigator) (dispatch [:navigate-back])
(doseq [ident identities] (doseq [ident identities]
(api/group-add-participant chat-id ident)) (api/group-add-participant chat-id ident))
db))) db)))
@ -610,6 +611,22 @@
(log/debug action) (log/debug action)
(assoc-in db db/group-settings-name-path chat-name))) (assoc-in db db/group-settings-name-path chat-name)))
(register-handler :select-group-chat-member
(fn [db [action identity]]
(log/debug action)
(assoc-in db db/group-settings-selected-member-path identity)))
(register-handler :chat-remove-member
(fn [db [action identity]]
(log/debug action)
(let [chat-id (current-chat-id db)
db (chat-remove-member db identity)]
(dispatch [:select-group-chat-member nil])
;; TODO uncomment
;; (api/group-remove-participant chat-id identity)
;; (removed-participant-msg chat-id identity)
(signal-chat-updated db chat-id))))
(register-handler :save-group-chat (register-handler :save-group-chat
(fn [db [action]] (fn [db [action]]
(log/debug action) (log/debug action)

View File

@ -9,7 +9,8 @@
[syng-im.constants :refer [content-type-status]] [syng-im.constants :refer [content-type-status]]
[syng-im.models.messages :refer [save-message]] [syng-im.models.messages :refer [save-message]]
[syng-im.persistence.realm-queries :refer [include-query]] [syng-im.persistence.realm-queries :refer [include-query]]
[syng-im.models.chat :refer [signal-chat-updated [syng-im.models.chat :refer [current-chat
signal-chat-updated
get-group-settings]])) get-group-settings]]))
(defn signal-chats-updated [db] (defn signal-chats-updated [db]
@ -79,12 +80,12 @@
(defn save-chat [db] (defn save-chat [db]
(let [chat-settings (get-group-settings db) (let [chat-settings (get-group-settings db)
chat-id (:chat-id chat-settings)] chat-id (:chat-id chat-settings)]
(r/write (r/write
(fn [] (fn []
;; TODO UNDONE contacts ;; TODO UNDONE contacts
(r/create :chats (r/create :chats
(select-keys chat-settings [:chat-id :name :contacts]) true))) (select-keys chat-settings [:chat-id :name]) true)))
;; TODO update chat in db atom ;; TODO update chat in db atom
(dispatch [:initialize-chats]) (dispatch [:initialize-chats])
(-> db (-> db
@ -143,8 +144,11 @@
(if-let [contact-exists (.find contacts (fn [object index collection] (if-let [contact-exists (.find contacts (fn [object index collection]
(= contact-identity (aget object "identity"))))] (= contact-identity (aget object "identity"))))]
(aset contact-exists "is-in-chat" true) (aset contact-exists "is-in-chat" true)
(.push contacts (clj->js {:identity contact-identity})))))))) (.push contacts (clj->js {:identity contact-identity})))))))
;; TODO temp. Update chat in db atom
(dispatch [:initialize-chats]))
;; TODO deprecated? (is there need to remove multiple member at once?)
(defn chat-remove-participants [chat-id identities] (defn chat-remove-participants [chat-id identities]
(r/write (r/write
(fn [] (fn []
@ -155,6 +159,19 @@
(.forEach (fn [object index collection] (.forEach (fn [object index collection]
(aset object "is-in-chat" false)))))))) (aset object "is-in-chat" false))))))))
(defn chat-remove-member [db identity]
(let [chat (current-chat db)]
(r/write
(fn []
(r/create :chats
(update chat :contacts
(fn [members]
(filter #(not= (:identity %) identity) members)))
true)))
;; TODO temp. Update chat in db atom
(dispatch [:initialize-chats])
db))
(defn active-group-chats [] (defn active-group-chats []
(let [results (r/filtered (r/get-all :chats) (let [results (r/filtered (r/get-all :chats)
"group-chat = true && is-active = true")] "group-chat = true && is-active = true")]

View File

@ -95,16 +95,20 @@
(r/sorted (r/get-all :contacts) :name :asc)) (r/sorted (r/get-all :contacts) :name :asc))
(defn contacts-list-exclude [exclude-idents] (defn contacts-list-exclude [exclude-idents]
(let [query (exclude-query :whisper-identity exclude-idents)] (if (empty? exclude-idents)
(-> (r/get-all :contacts) (contacts-list)
(r/filtered query) (let [query (exclude-query :whisper-identity exclude-idents)]
(r/sorted :name :asc)))) (-> (r/get-all :contacts)
(r/filtered query)
(r/sorted :name :asc)))))
(defn contacts-list-include [include-indents] (defn contacts-list-include [include-indents]
(let [query (include-query :whisper-identity include-indents)] (if (empty? include-indents)
(-> (r/get-all :contacts) ()
(r/filtered query) (let [query (include-query :whisper-identity include-indents)]
(r/sorted :name :asc)))) (-> (r/get-all :contacts)
(r/filtered query)
(r/sorted :name :asc)))))
(defn contact-by-identity [identity] (defn contact-by-identity [identity]
(if (= identity "console") (if (= identity "console")

View File

@ -4,6 +4,7 @@
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.components.discovery.subs :as discovery] [syng-im.components.discovery.subs :as discovery]
[syng-im.models.chat :refer [current-chat-id [syng-im.models.chat :refer [current-chat-id
current-chat
get-group-settings get-group-settings
chat-updated?]] chat-updated?]]
[syng-im.models.chats :refer [chats-list [syng-im.models.chats :refer [chats-list
@ -168,9 +169,7 @@
(register-sub :current-chat-contacts (register-sub :current-chat-contacts
(fn [db _] (fn [db _]
(let [current-chat-id (reaction (current-chat-id @db)) (let [chat (reaction (current-chat @db))]
chat (reaction (when-let [chat-id @current-chat-id]
(chat-by-id chat-id)))]
(reaction (reaction
(when @chat (when @chat
(let [current-participants (->> @chat (let [current-participants (->> @chat
@ -178,16 +177,16 @@
(map :identity))] (map :identity))]
(contacts-list-include current-participants))))))) (contacts-list-include current-participants)))))))
;; TODO for new group only?
(register-sub :group-settings-name (register-sub :group-settings-name
(fn [db [_]] (fn [db [_]]
(reaction (get-in @db db/group-settings-name-path)))) (reaction (get-in @db db/group-settings-name-path))))
(register-sub :group-settings-members (register-sub :selected-group-chat-member
(fn [db [_]] (fn [db [_]]
(let [members (reaction (get-in @db db/group-settings-members-path))] (reaction
(reaction (let [identity (get-in @db db/group-settings-selected-member-path)]
(let [current-participants (map :identity @members)] (contact-by-identity identity)))))
(contacts-list-include current-participants))))))
(register-sub :view-id (register-sub :view-id
(fn [db _] (fn [db _]