Group settings refactoring

This commit is contained in:
virvar 2016-05-13 15:15:40 +03:00
parent b2737d42f7
commit 6b54767b98
7 changed files with 36 additions and 64 deletions

View File

@ -35,6 +35,8 @@
(def updated-chats-signal-path [:chats-updated-signal]) (def updated-chats-signal-path [:chats-updated-signal])
(defn updated-chat-signal-path [chat-id] (defn updated-chat-signal-path [chat-id]
[:chats chat-id :chat-updated-signal]) [:chats chat-id :chat-updated-signal])
(defn chat-name-path [chat-id]
[:chats chat-id :name])
(defn chat-input-text-path [chat-id] (defn chat-input-text-path [chat-id]
[:chats chat-id :input-text]) [:chats chat-id :input-text])
(defn chat-staged-commands-path [chat-id] (defn chat-staged-commands-path [chat-id]
@ -50,8 +52,6 @@
(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])
(def show-actions-path [:show-actions]) (def show-actions-path [:show-actions])
(def group-settings-path [:group-settings])
(def group-settings-name-path [:group-settings :name])
(def group-settings-selected-member-path [:selected-member]) (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])

View File

@ -36,29 +36,26 @@
[text {:style st/modal-remove-text} [text {:style st/modal-remove-text}
"Remove"]]]]]) "Remove"]]]]])
(defn set-group-settings-name [chat-name] (defn set-group-chat-name [chat-name]
(dispatch [:set-group-settings-name chat-name])) (dispatch [:set-group-chat-name chat-name]))
(defn save-group-chat []
(dispatch [:save-group-chat]))
(defn chat-members [members] (defn chat-members [members]
[view st/chat-members-container [view st/chat-members-container
(for [member members] (for [member members]
^{:key member} [contact-inner-view member])]) ^{:key member} [contact-inner-view member])])
(defn action-save [] (defn chat-icon []
[touchable-highlight (let [chat-name (subscribe [:get-current-chat-name])]
{:on-press save-group-chat} (fn []
[view st/save-btn [view st/chat-icon
[text {:style st/save-btn-text} "S"]]]) [text {:style st/chat-icon-text} (nth @chat-name 0)]])))
(defn new-group-toolbar [] (defn new-group-toolbar []
[toolbar {:title "Chat settings" [toolbar {:title "Chat settings"
:custom-action [action-save]}]) :custom-action [chat-icon]}])
(defn group-settings [] (defn group-settings []
(let [chat-name (subscribe [:group-settings-name]) (let [chat-name (subscribe [:get-current-chat-name])
members (subscribe [:current-chat-contacts]) members (subscribe [:current-chat-contacts])
selected-member (subscribe [:selected-group-chat-member])] selected-member (subscribe [:selected-group-chat-member])]
(fn [] (fn []
@ -70,7 +67,7 @@
:underlineColorAndroid color-purple :underlineColorAndroid color-purple
:autoFocus true :autoFocus true
:placeholderTextColor text2-color :placeholderTextColor text2-color
:onChangeText set-group-settings-name} :onChangeText set-group-chat-name}
@chat-name] @chat-name]
[text {:style st/members-text} [text {:style st/members-text}
"Members"] "Members"]

View File

@ -38,14 +38,14 @@
(def chat-members-container (def chat-members-container
{:marginBottom 10}) {:marginBottom 10})
(def save-btn (def chat-icon
{:margin 10 {:margin 10
:width 36 :width 36
:height 36 :height 36
:borderRadius 50 :borderRadius 50
:backgroundColor color-purple}) :backgroundColor color-purple})
(def save-btn-text (def chat-icon-text
{:marginTop 7 {:marginTop 7
:marginLeft 13 :marginLeft 13
:color color-white :color color-white

View File

@ -35,7 +35,7 @@
[syng-im.components.discovery.handlers :as discovery] [syng-im.components.discovery.handlers :as discovery]
[syng-im.models.chats :refer [chat-exists? [syng-im.models.chats :refer [chat-exists?
create-chat create-chat
save-chat set-group-chat-name
chat-remove-member chat-remove-member
chat-add-participants chat-add-participants
chat-remove-participants chat-remove-participants
@ -45,7 +45,6 @@
[syng-im.models.chat :refer [signal-chat-updated [syng-im.models.chat :refer [signal-chat-updated
set-current-chat-id set-current-chat-id
current-chat-id current-chat-id
set-group-settings
update-new-group-selection update-new-group-selection
update-new-participants-selection update-new-participants-selection
clear-new-group clear-new-group
@ -602,14 +601,13 @@
(register-handler :show-group-settings (register-handler :show-group-settings
(fn [db [action]] (fn [db [action]]
(log/debug action) (log/debug action)
(let [db (set-group-settings db)] (dispatch [:navigate-to :group-settings])
(dispatch [:navigate-to :group-settings]) db))
db)))
(register-handler :set-group-settings-name (register-handler :set-group-chat-name
(fn [db [action chat-name]] (fn [db [action chat-name]]
(log/debug action) (log/debug action)
(assoc-in db db/group-settings-name-path chat-name))) (set-group-chat-name db chat-name)))
(register-handler :select-group-chat-member (register-handler :select-group-chat-member
(fn [db [action identity]] (fn [db [action identity]]
@ -622,18 +620,11 @@
(let [chat-id (current-chat-id db) (let [chat-id (current-chat-id db)
db (chat-remove-member db identity)] db (chat-remove-member db identity)]
(dispatch [:select-group-chat-member nil]) (dispatch [:select-group-chat-member nil])
;; TODO uncomment ;; TODO fix and uncomment
;; (api/group-remove-participant chat-id identity) ;; (api/group-remove-participant chat-id identity)
;; (removed-participant-msg chat-id identity) ;; (removed-participant-msg chat-id identity)
(signal-chat-updated db chat-id)))) (signal-chat-updated db chat-id))))
(register-handler :save-group-chat
(fn [db [action]]
(log/debug action)
(let [db (save-chat db)]
(dispatch [:navigate-back])
db)))
(register-handler :group-chat-invite-received (register-handler :group-chat-invite-received
(fn [db [action from group-id identities group-name]] (fn [db [action from group-id identities group-name]]
(log/debug action from group-id identities) (log/debug action from group-id identities)

View File

@ -19,14 +19,6 @@
(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)))
(defn get-group-settings [db]
(get-in db db/group-settings-path))
(defn set-group-settings [db]
(let [group-chat (current-chat db)]
(assoc-in db db/group-settings-path
(select-keys group-chat [:chat-id :name :contacts]))))
(defn update-new-group-selection [db identity add?] (defn update-new-group-selection [db identity add?]
(update-in db db/new-group-path (fn [new-group] (update-in db db/new-group-path (fn [new-group]
(if add? (if add?

View File

@ -9,9 +9,9 @@
[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 [current-chat [syng-im.models.chat :refer [current-chat-id
signal-chat-updated current-chat
get-group-settings]])) signal-chat-updated]]))
(defn signal-chats-updated [db] (defn signal-chats-updated [db]
(update-in db db/updated-chats-signal-path (fn [current] (update-in db db/updated-chats-signal-path (fn [current]
@ -78,19 +78,13 @@
(add-status-message chat-id) (add-status-message chat-id)
(signal-chats-updated db))))) (signal-chats-updated db)))))
(defn save-chat [db] (defn set-group-chat-name [db name]
(let [chat-settings (get-group-settings db) (let [chat-id (current-chat-id db)]
chat-id (:chat-id chat-settings)] (r/write (fn []
(r/write (-> (r/get-by-field :chats :chat-id chat-id)
(fn [] (r/single)
;; TODO UNDONE contacts (aset "name" name))))
(r/create :chats (assoc-in db (db/chat-name-path chat-id) name)))
(select-keys chat-settings [:chat-id :name]) true)))
;; TODO update chat in db atom
(dispatch [:initialize-chats])
(-> db
(signal-chats-updated)
(signal-chat-updated chat-id))))
(defn chat-contacts [chat-id] (defn chat-contacts [chat-id]
(-> (r/get-by-field :chats :chat-id chat-id) (-> (r/get-by-field :chats :chat-id chat-id)
@ -160,7 +154,7 @@
(aset object "is-in-chat" false)))))))) (aset object "is-in-chat" false))))))))
(defn chat-remove-member [db identity] (defn chat-remove-member [db identity]
(let [chat (current-chat db)] (let [chat (current-chat db)]
(r/write (r/write
(fn [] (fn []
(r/create :chats (r/create :chats

View File

@ -5,7 +5,6 @@
[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 current-chat
get-group-settings
chat-updated?]] chat-updated?]]
[syng-im.models.chats :refer [chats-list [syng-im.models.chats :refer [chats-list
chats-updated? chats-updated?
@ -92,6 +91,10 @@
(let [current-chat-id (current-chat-id @db)] (let [current-chat-id (current-chat-id @db)]
(reaction (get-in @db [:chats current-chat-id]))))) (reaction (get-in @db [:chats current-chat-id])))))
(register-sub :get-current-chat-name
(fn [db _]
(let [current-chat-id (current-chat-id @db)]
(reaction (get-in @db (db/chat-name-path current-chat-id :name))))))
;; -- User data -------------------------------------------------------------- ;; -- User data --------------------------------------------------------------
@ -177,13 +180,8 @@
(map :identity))] (map :identity))]
(contacts-list-include current-participants))))))) (contacts-list-include current-participants)))))))
;; TODO for new group only?
(register-sub :group-settings-name
(fn [db [_]]
(reaction (get-in @db db/group-settings-name-path))))
(register-sub :selected-group-chat-member (register-sub :selected-group-chat-member
(fn [db [_]] (fn [db [_]]
(reaction (reaction
(let [identity (get-in @db db/group-settings-selected-member-path)] (let [identity (get-in @db db/group-settings-selected-member-path)]
(contact-by-identity identity))))) (contact-by-identity identity)))))