From 38161d9e10dffc1a5bb58f495457873fede418fc Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 10 May 2016 15:25:36 +0300 Subject: [PATCH] more navigation handlers --- src/syng_im/chat/views/message.cljs | 3 +- .../components/chats/chat_list_item.cljs | 2 +- src/syng_im/components/chats/chats_list.cljs | 3 +- src/syng_im/handlers.cljs | 45 +-------------- src/syng_im/navigation/handlers.cljs | 56 +++++++++++++++++-- src/syng_im/navigation/subs.cljs | 4 ++ src/syng_im/subs.cljs | 4 -- 7 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/syng_im/chat/views/message.cljs b/src/syng_im/chat/views/message.cljs index a65cd07825..b7fe165406 100644 --- a/src/syng_im/chat/views/message.cljs +++ b/src/syng_im/chat/views/message.cljs @@ -112,8 +112,7 @@ (defn text-message [{:keys [content] :as message}] [message-view message - [text {:style (st/text-message message)} - content]]) + [text {:style (st/text-message message)} content]]) (defmethod message-content text-content-type [wrapper message] diff --git a/src/syng_im/components/chats/chat_list_item.cljs b/src/syng_im/components/chats/chat_list_item.cljs index a6cc0dc5a1..574af6fc87 100644 --- a/src/syng_im/components/chats/chat_list_item.cljs +++ b/src/syng_im/components/chats/chat_list_item.cljs @@ -11,7 +11,7 @@ (defn chat-list-item [chat-obj navigator] [touchable-highlight - {:on-press #(dispatch [:show-chat (aget chat-obj "chat-id") navigator :push])} + {:on-press #(dispatch [:show-chat (aget chat-obj "chat-id") :push])} ;; TODO add [photo-path delivery-status new-messages-count online] values to chat-obj ;; TODO should chat-obj be clj-map? [view {} [chat-list-item-inner-view (merge (js->clj chat-obj :keywordize-keys true) diff --git a/src/syng_im/components/chats/chats_list.cljs b/src/syng_im/components/chats/chats_list.cljs index f102e7be6b..9083be800d 100644 --- a/src/syng_im/components/chats/chats_list.cljs +++ b/src/syng_im/components/chats/chats_list.cljs @@ -61,8 +61,7 @@ :color "white"}}]] [action-button-item {:title "New Group Chat" :buttonColor "#1abc9c" - :onPress (fn [] - (dispatch [:show-group-new navigator]))} + :onPress #(dispatch [:show-group-new])} [icon {:name "person-stalker" :style {:fontSize 20 :height 22 diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index ab81a94276..a40a84dfa6 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -33,7 +33,6 @@ current-chat-id update-new-group-selection update-new-participants-selection - clear-new-group clear-new-participants new-group-selection set-chat-input-text @@ -117,18 +116,6 @@ (update-identity identity) (set-initialized true)))) -(defn gen-messages [n] - (mapv (fn [_] - (let [id (random-uuid)] - {:msg-id id - :content (str id - "ooops sdfg dsfg" - "s dfg\ndsfg dfg\ndsfgdsfgdsfg") - :content-type text-content-type - :outgoing false - :from "console" - :to "me"})) (range n))) - (defn system-message [msg-id content] {:from "system" :msg-id msg-id @@ -267,29 +254,11 @@ ;; -- Chats -------------------------------------------------------------- -(register-handler :show-chat - (fn [db [action chat-id navigator nav-type]] - (log/debug action "chat-id" chat-id) - (let [db (set-current-chat-id db chat-id)] - (dispatch [:navigate-to navigator {:view-id :chat} nav-type]) - db))) - -(register-handler :show-contacts - (fn [db [_ navigator]] - (nav-push navigator {:view-id :contact-list}) - db)) - (register-handler :select-new-participant (fn [db [action identity add?]] (log/debug action identity add?) (update-new-participants-selection db identity add?))) -(register-handler :show-remove-participants - (fn [db [action navigator]] - (log/debug action) - (nav-push navigator {:view-id :remove-participants}) - (clear-new-participants db))) - (register-handler :remove-selected-participants (fn [db [action navigator]] (log/debug action) @@ -302,12 +271,6 @@ (removed-participant-msg chat-id ident)) (signal-chat-updated db chat-id)))) -(register-handler :show-add-participants - (fn [db [action navigator]] - (log/debug action) - (nav-push navigator {:view-id :add-participants}) - (clear-new-participants db))) - (register-handler :add-new-participants (fn [db [action navigator]] (log/debug action) @@ -319,12 +282,6 @@ (api/group-add-participant chat-id ident)) db))) -(register-handler :show-group-new - (fn [db [action navigator]] - (log/debug action) - (nav-push navigator {:view-id :new-group}) - (clear-new-group db))) - (register-handler :select-for-new-group (fn [db [action identity add?]] (log/debug action identity add?) @@ -336,7 +293,7 @@ (let [identities (vec (new-group-selection db)) group-id (api/start-group-chat identities group-name) db (create-chat db group-id identities true group-name)] - (dispatch [:show-chat group-id navigator :replace]) + (dispatch [:show-chat group-id :replace]) db))) (register-handler :group-chat-invite-received diff --git a/src/syng_im/navigation/handlers.cljs b/src/syng_im/navigation/handlers.cljs index e39d0fc175..281b044318 100644 --- a/src/syng_im/navigation/handlers.cljs +++ b/src/syng_im/navigation/handlers.cljs @@ -1,11 +1,30 @@ (ns syng-im.navigation.handlers - (:require [re-frame.core :refer [register-handler]])) + (:require [re-frame.core :refer [register-handler dispatch]] + [syng-im.models.chat :as chat])) + +(defn push-view [db view-id] + (-> db + (update :navigation-stack conj view-id) + (assoc :view-id view-id))) + +(defn replace-top-element [stack view-id] + (let [stack' (if (pos? (count stack)) + (pop stack) + stack)] + (conj stack' view-id))) + +(defn replace-view [db view-id] + (-> db + (update :navigation-stack replace-top-element view-id) + (assoc :view-id view-id))) (register-handler :navigate-to (fn [db [_ view-id]] - (-> db - (assoc :view-id view-id) - (update :navigation-stack conj view-id)))) + (push-view db view-id))) + +(register-handler :navigation-replace + (fn [db [_ view-id]] + (replace-view db view-id))) (register-handler :navigate-back (fn [{:keys [navigation-stack] :as db} _] @@ -15,3 +34,32 @@ (-> db (assoc :view-id view-id) (assoc :navigation-stack navigation-stack')))))) + +(register-handler :show-group-new + (fn [db _] + (-> db + (push-view :new-group) + chat/clear-new-group))) + +(register-handler :show-chat + (fn [db [_ chat-id nav-type]] + (let [update-view-id-fn (if (= :replace nav-type) replace-view push-view)] + (-> db + (update-view-id-fn :chat) + (chat/set-current-chat-id chat-id))))) + +(register-handler :show-contacts + (fn [db _] + (push-view db :contact-list))) + +(register-handler :show-remove-participants + (fn [db _] + (-> db + (push-view :remove-participants) + chat/clear-new-participants))) + +(register-handler :show-add-participants + (fn [db _] + (-> db + (push-view :add-participants) + chat/clear-new-participants))) diff --git a/src/syng_im/navigation/subs.cljs b/src/syng_im/navigation/subs.cljs index deda90b09e..d7b2dc4a0a 100644 --- a/src/syng_im/navigation/subs.cljs +++ b/src/syng_im/navigation/subs.cljs @@ -2,6 +2,10 @@ (:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [register-sub]])) +(register-sub :view-id + (fn [db _] + (reaction (@db :view-id)))) + (register-sub :navigation-stack (fn [db _] (reaction (:navigation-stack @db)))) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 826e3d0ee4..360064daf4 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -78,10 +78,6 @@ (map :identity))] (contacts-list-include current-participants))))))) -(register-sub :view-id - (fn [db _] - (reaction (@db :view-id)))) - (register-sub :db (fn [db _] (reaction @db)))